刚刚进入视觉编程并遇到问题。这是我的代码:
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,文件 C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp 中收到错误“OpenCV 错误:断言失败 (scn == 3 || scn == 4),第 7456 行 Traceback(最后一次调用):文件“C:/Users/Peter/PycharmProjects/untitled/FaceDetection.py”,第 15 行,灰色 = 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 维的,所以这不是我的问题。有任何想法吗?
谢谢