I think you should check whether you have captured enough faces with respect to the number of neighbors.
import cv2
haar_file = cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
face_cascade = cv2.CascadeClassifier(haar_file)
# Reduzindo resolução
webcam = cv2.VideoCapture(0)
webcam.set(cv2.CAP_PROP_FRAME_WIDTH, 320) # width
webcam.set(cv2.CAP_PROP_FRAME_HEIGHT, 240) # height
while True:
retorno, frame = webcam.read()
if not retorno:
print("No frame captured") # Add this line to check if the frame is captured or not
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
print("Grayscaled")
faces = face_cascade.detectMultiScale(gray, 1.3, 3, flags= cv2.CASCADE_SCALE_IMAGE)
print("Faces detected")
if len(faces) == 0:
print("No faces detected") # Add this line to check if any faces are detected for the number of neighbors
continue
for (x1, y1, x2, y2) in faces:
moldura_face = gray[y1:y1+y2, x1:x1+x2]
cv2.rectangle(gray, (x1, y1), (x2, y2), (0, 255, 255), 2)
moldura_face = cv2.resize(moldura_face, (48, 48))
# cv2.putText(im,prediction_label)
cv2.putText(frame, '% s' % ('prediction_label'), (x1//2, y1//2),
cv2.FONT_HERSHEY_COMPLEX_SMALL, 2, (0, 0, 255))
cv2.imshow("Output", frame)
if(cv2.waitKey(1) & 0xFF == ord("q")):
break
webcam.release()
cv2.destroyAllWindows()