(-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
Hi,
The error encountered is :
Traceback (most recent call last):
File ".\faces-test.py", line 26, in <module> gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) cv2.error: OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
The code is below:
import numpy as numpy
import cv2 as cv
import pickle
from PIL import Image
face_cascade = cv.CascadeClassifier('cascades/data/haarcascade_frontalface_alt2.xml')
eye_cascade = cv.CascadeClassifier('cascades/data/haarcascade_eye.xml')
recognizer = cv.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 = cv.VideoCapture('mygeneratedvideo.avi')
if(cap.isOpened()):
print("Camera opened")
while(cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
if(ret == True):
print("It's working")
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5) # gray was there instead of frame
for (x, y, w, h) in faces:
# print(x, y, w, h)
roi_gray = gray[y:y + h, x:x + w] # roi(region of interest) # gray was there instead of frame
roi_color = frame[y:y + h, x:x + w]
id_, conf = recognizer.predict(roi_gray)
if conf >= 45 and conf <= 85:
# print(id_)
# print(labels[id_])
font = cv.FONT_HERSHEY_SIMPLEX
name = labels[id_]
color = (255, 255, 255)
stroke = 2
cv.putText(frame, name, (x, y), font, 1, color, stroke, cv.LINE_AA)
print(name)
img_item = "my-image.png"
cv.imwrite(img_item, roi_gray)
color = (255, 0, 0)
stroke = 2
end_cord_x = x + w
end_cord_y = y + h
cv.rectangle(frame, (x, y), (end_cord_x, end_cord_y), color, stroke)
eyes = eye_cascade.detectMultiScale(roi_gray)
for(ex, ey, ew, eh) in eyes:
cv.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
# Display the resulting frame
cv.imshow('myframe', frame)
if cv.waitKey(20) & 0xFF == ord('q'):
break
# When everything is done, release the capture
cap.release()
cv.destroyAllWindows()