进入视觉编程并遇到问题。这是我的代码:
import numpy as np
import cv2
# multiple cascades: https://github.com/Itseez/opencv/tree/master/data/haarcascades
#https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_eye.xml
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
roi_gray = gray[y:y + h, x:x + w]
roi_color = img[y:y + h, x:x + w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
cv2.imshow('img', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
我在cv :: ipp_cvtColor中遇到错误" OpenCV错误:断言失败(scn == 3 || scn == 4),文件C:\ projects \ opencv-python \ opencv \ modules \ imgproc \ src \ color.cpp,第7456行 Traceback(最近一次调用最后一次): File" C:/Users/Peter/PycharmProjects/untitled/FaceDetection.py" ;,第15行,在 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) cv2.error:C:\ projects \ opencv-python \ opencv \ modules \ imgproc \ src \ color.cpp:7456:错误:(-215)scn == 3 || scn == 4 in function cv :: ipp_cvtColor"
我可以在使用静止图像而非视频输入时找到解决此问题的示例。我检查了img.shape,它是3维,所以这不是我的问题。有什么想法吗?
由于
答案 0 :(得分:0)
更改:
while 1:
为:
while ret == True:
希望这有帮助!
答案 1 :(得分:0)
可能'haarcascade_frontalface_default.xml'不在我们脚本的文件夹中。