gpt4 book ai didi

python - CV2(cv2.imwrite)-Python不断抛出 “Assertion failed,”错误

转载 作者:行者123 更新时间:2023-12-02 17:40:08 29 4
gpt4 key购买 nike

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

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

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

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



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

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

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com