在 Opencv 中收到错误:(-215:断言失败) 函数 'cvtColor' 中的 !_src.empty()



我目前正在使用opencv来识别屏幕上的个人,我已经创建了一个模型,但我收到了这个错误:

error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

我搜索了很多论坛,试图找到一个解决方案,但都没有真正的帮助。下面是卡片的一部分。

#Creates the video capture
camera = cv2.VideoCapture(0, cv2.CAP_DSHOW)
camera.set(3, 650)  # Sets the camera width
camera.set(4, 480)  # Sets the camera height
# Creates the window size of for the face
minW = 0.1 * camera.get(3)
minH = 0.1 * camera.get(4)
face_module = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
eye_module = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.xml")
recogniser = cv2.face.LBPHFaceRecognizer_create()  # This will rea the LBPH method
recogniser.read("/content/trainner/trainner.yml")  # This is where the YML file will be read

def getStudent(ID):
connection = sqlite3.connect("FaceBase.db")
cmd = "SELECT * FROM Class WHERE ID=" + str(ID)
cursor = connection.execute(cmd)
student=None
for row in cursor:
student=row
connection.close()
return student

df = pd.read_csv("attendance.csv")
columnNames = ["ID", "Full Name", "Major", "Time"]
attendance = pd.DataFrame(columns=columnNames)
# This is where the detection logic will occur
while (True):
ret, frame = camera.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face = face_module.detectMultiScale(gray, 1.5, 5)

上面显示的代码中有两个问题,其中一个是:

recogniser.read("/content/trainner/trainner.yml")

这带来了一个关于trainner文件路径的错误,但要解决这个问题,你需要实现它的绝对路径。

recogniser.read(r"/Users/...../Documents/....../trainner/trainner.yml") 

要修复cv2Color问题,您需要在相机读取的正下方添加代码

if ret is True:
#print("frame.shape")

else:
continue

希望这能帮助到外面的人!

相关内容

最新更新