I am making a project to recognize the person by his/her face using OpenCV library in Python. My code is :-
# Import OpenCV and sys libraries
import cv2
import sys
import os
import numpy as np
subjects = ["", "Daksh", "Ishika", "Dashya", "Shivang"]
#Create the haar cascade
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
def draw_rectangle(img, rect):
(x, y, w, h) = rect
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
def draw_text(img, text, x, y):
cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 255, 0), 2)
def imgdetect(gray,image):
#Detect faces in the image
faces = faceCascade.detectMultiScale(gray, 1.3, 5)
#Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = image[y:y+h, x:x+w]
return image
#for video capturing
vidCapture = cv2.VideoCapture(0)
while True:
# To capture video frame by frame
_, image = vidCapture.read()
#monochrome image capturing (color to gray)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
subjects = ["Daksh", "Ishika", "Shivang", "Dashya"]
canvas = imgdetect(gray, image)
gray, rect = detect_face(image)
label = face_recognizer.predict(gray)
label_text = subjects[label]
#draw a rectangle around face detected
draw_rectangle(canvas, rect)
#draw name of predicted person
draw_text(canvas, label_text, rect[0], rect[1]-5)
cv2.imshow("Attendence....", canvas)
#To break control by pressing 'q'
if cv2.waitKey(1) & 0xff == ord('q'):
break
#Release after processing is done
vidCapture.release()
cv2.destroyAllWindows()
But, I am getting the following error while proceeding with the code
---------------------------------------------------------------------------
error Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13428/371309763.py in <module>
40 canvas = imgdetect(gray, image)
41 img, rect = detect_face(image)
---> 42 label = face_recognizer.predict(img)
43 label_text = subjects[label]
44
error: OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\core\src\matrix.cpp:250: error: (-215:Assertion failed) s >= 0 in function 'cv::setSize'
I am unable to understand the error message. Why am I getting this error and what can be the possible fixes for the error?