遇到的错误:函数'cvtColor'中的OpenCV!_src.empty()错误

时间:2020-10-06 17:39:07

标签: opencv-python

Traceback (most recent call last):
  File ".\faces-test.py", line 20, in <module>
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-10658tfl\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

我无法解决以上错误。以下代码用于从保存到本地的视频中捕获帧,并用作VideoCapture()函数的参数。 但是当我运行下面的代码时,遇到了上面的错误

import numpy as numpy
import cv2
import pickle

face_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_alt2.xml')
eye_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_eye.xml')
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read("trainner.yml")

labels = {"person_name": 1}
with open("labels.pickle", 'rb') as f:
    og_labels = pickle.load(f)
    labels = {v: k for k, v in og_labels.items()}

cap = cv2.VideoCapture('C:\\My_Studies\\Python\\testing\\images\\mygeneratedvideo.avi')

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

1 个答案:

答案 0 :(得分:0)

删除所有其他代码,然后尝试阅读和显示视频。完成这项工作后,请担心其余的代码。确保文件在指定的文件夹中,并且名称匹配。然后试试这个:

{'f0': 'cmex10', 'f1': 'cmmi12', 'f2': 'cmr12'}