OpenCV错误-cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

时间:2019-06-04 08:15:15

标签: python opencv

运行代码时出现此错误,该如何解决?:

2019-06-04 16:58:02.840 Python[1608:18081] +[AVCaptureDevice authorizationStatusForMediaType:]: unrecognized selector sent to class 0x7fff969286a0
[ERROR:0] VIDEOIO(AVFOUNDATION): raised unknown C++ exception!


Traceback (most recent call last):
  File "faces.py", line 11, in <module>
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.1.0) /tmp/opencv-20190505-15642-eu6z8v/opencv-4.1.0/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

我尝试搜索解决方案,但找不到任何解决方法。

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('HaarCascade/data/haarcascade_frontalface_alt2.xml')

cap = cv2.VideoCapture(0)

while(True): 
    #Capture frame by frame
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)
    for (x, y, w, h) in faces:
        print(x,y,w,h)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = frame[y:y+h, x:x+w]c
        img_item = "my-image.png"
        cv2.imwrite(img_item, roi_gray)

0 个答案:

没有答案