因此,如果我继续进去,我会不断收到“断言失败”错误,

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

所以,自然地,我在这里找出原因。完整的“荣耀”存在错误:



这是我的代码的副本:
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()

最佳答案

imwrite()中有三个参数:文件名和两个图像(vidgray[y:y+h, x:x+w])。当然,您只能传递一张图像。但是imwrite()实际上可以接受三个参数,第三个是质量/压缩级别的参数标志。从the imwrite() docs:

关于python - CV2(cv2.imwrite)-Python不断抛出 “Assertion failed,”错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46108333/

10-12 19:30