0

So, I keep getting an an, "assertion failed" error if I leave in,

        cv2.imwrite('FaceRecBaseTest/' + str(sum) + '.jpeg', vid, gray[y:y+h, x:x+w])

So, naturally, I'm here to find out why. Here is there error in its full "glory:"

OpenCV Error: Assertion failed (dims == 2 && (sizes[0] == 1 || sizes[1] == 1 || sizes[0]*sizes[1] == 0)) in create, file /tmp/opencv-20170224-1869-10nlf6f/opencv-2.4.13.2/modules/core/src/matrix.cpp, line 1466 libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /tmp/opencv-20170224-1869-10nlf6f/opencv-2.4.13.2/modules/core/src/matrix.cpp:1466: error: (-215) dims == 2 && (sizes[0] == 1 || sizes[1] == 1 || sizes[0]*sizes[1] == 0) in function create

Here is a copy of my code:

import cv2
import time
face_cascade = cv2. 
CascadeClassifier('haarcascade/haarcascade_frontalface_alt.xml')
video_capture = cv2.VideoCapture(0)
sum = 0
while True:
    # Captures video frame by frame
    ret, vid = video_capture.read()
    vid = cv2.resize(vid, (320, 220))
    gray = cv2.cvtColor(vid, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.1, 5)

    print str(len(faces))
    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        sum += 1
        cv2.rectangle(vid, (x, y), (x+w, y+h), (0, 255, 0), 2)
        cv2.imwrite('FaceRecBaseTest/' + str(sum) + '.jpeg', vid, gray[y:y+h, x:x+w])
        cv2.waitKey(1)
    cv2.putText(vid, "Video: " + str(time.ctime()), (0, 10), 
    cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 2, 8, bottomLeftOrigin = False)
    # Display the results
    if sum > 20:
        break
    cv2.imshow('Video', vid)
    cv2.waitKey(1)
video_capture.release()
cv2.destroyAllWindows()
3
  • 1
    You have three arguments in your imwrite function: the filename, vid and gray[y:y+h, x:x+w]. What exactly are you trying to do here? You can only write one image at a time of course, hence the single filename. The correct usage is imwrite(filename, image). Commented Sep 8, 2017 at 3:53
  • 1
    ...do you ever have one of those moments coding where you feel like an ass? Because I totally just did. Thank you. I was totally overlooking that! I had meant to write, cv2.imwrite("FaceRecBaseTest/" + str(sum) + ".jpeg", gray[y:y+h, x:x+w]) Thank you! I have it working now...! Commented Sep 8, 2017 at 4:22
  • Lol of course, we all do. I've added it as an answer. Commented Sep 8, 2017 at 4:32

1 Answer 1

1

You've got three arguments inside imwrite(): the filename and two images (vid and gray[y:y+h, x:x+w]). Of course, you can only pass in one image. However imwrite() can actually accept three parameters, the third being a parameters flag for quality/compression level. From the imwrite() docs:

Python: cv2.imwrite(filename, img[, params]) → retval
...
params – Format-specific save parameters encoded as pairs paramId_1, paramValue_1, paramId_2, paramValue_2, ... . The following parameters are currently supported:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.