如何修复函数'cv :: cvtColor'[WARN:0]中的!_src.empty()终止异步回调

时间:2019-05-22 09:50:54

标签: opencv python-3.7

我正在尝试通过使用以下代码来检测人脸,但是每次遇到错误时,我都无法找到相同的解决方案。请帮忙。

我尝试搜索显示erorr的路径,但是事实证明没有文件显示错误 C:\ projects \ opencv-python \ opencv \ modules \ imgproc \ src \ color.cpp:

import numpy as np
import cv2
detector= cv2.CascadeClassifier('C:\\python\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
while(True):
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = detector.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)

    cv2.imshow('frame',img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

我收到此错误。

C:\Users\admin\OneDrive\Documents\python_projects\face_detection>python recognition.py
[ WARN:1] videoio(MSMF): OnReadSample() is called with error status: -1072873822
[ WARN:1] videoio(MSMF): async ReadSample() call is failed with error status: -1072873822
[ WARN:0] videoio(MSMF): can't grab frame. Error: -1072873822
Traceback (most recent call last):
  File "recognition.py", line 9, in <module>
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
[ WARN:0] terminating async callback

0 个答案:

没有答案