【问题标题】:OpenCV 4.4.0 error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' , 14 other erros module cv2OpenCV 4.4.0 错误: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' , 14 other erros module cv2
【发布时间】:2020-11-21 20:20:45
【问题描述】:
cap = cv2.VideoCapture(0)

while True:
    # Grab a single frame of video
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    faces = face_classifier.detectMultiScale(gray,1.3,5)
    for (x,y,w,h) in faces:
        cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h,x:x+w]
        roi_gray = cv2.resize(roi_gray,(48,48),interpolation=cv2.INTER_AREA)
    # rect,face,image = face_detector(frame)
        if np.sum([roi_gray])!=0:
            roi = roi_gray.astype('float')/255.0
            roi = img_to_array(roi)
            roi = np.expand_dims(roi,axis=0)
        # make a prediction on the ROI, then lookup the class
            preds = classifier.predict(roi)[0]
            label=class_labels[preds.argmax()]
            label_position = (x,y)
            cv2.putText(frame,label,label_position,cv2.FONT_HERSHEY_SIMPLEX,2,(0,255,0),3)
        else:
            cv2.putText(frame,'No Face Found',(20,60),cv2.FONT_HERSHEY_SIMPLEX,2,(0,255,0),3)
    cv2.imshow('Emotion Detector',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
  1. 错误 灰色 = cv2.cvtColor(帧,cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-6lylwdcz\opencv\modules\imgproc\src\color.cpp:182: 错误:( -215: 断言失败) !_src.empty() 在函数 'cv::cvtColor' enter image description here enter image description here

【问题讨论】:

  • 很可能,您无法从cap.read() 获取frame,请检查ret 是否返回True
  • @Ahx 提到 ret 必须为 True,cap.isOpened() 也是如此。如果两者都是 True,则尝试 cap = cv2.VideoCapture(-1)

标签: python-3.x opencv keras


【解决方案1】:
while True:
    # Grab a single frame of video
    ret, frame = cap.read()
    if ret==False:
        continue
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    faces = face_classifier.detectMultiScale(gray,1.3,5)

在您的代码中添加此 if 条件。 如果 "ret" 总是返回 "False" 则将 "cap = cv2.VideoCapture(0)" 更改为 "cap = cv2.VideoCapture(1)" 并重试

【讨论】:

    【解决方案2】:

    你只需要改变这一行

    cap = cv2.VideoCapture(1)
    

    cap = cv2.VideoCapture(0)
    

    【讨论】:

      【解决方案3】:

      您可以尝试将USB2.0设备插入电脑的某个端口,然后编译程序。

      【讨论】:

        猜你喜欢
        • 2019-06-26
        • 2019-08-30
        • 2021-07-30
        • 2019-11-12
        • 2021-04-19
        • 2019-05-24
        • 2023-04-01
        • 2021-02-08
        • 2021-04-14
        相关资源
        最近更新 更多