This is code for detecting a face.
I want that whenever the face is recognized, a rectangle is shown on the face image, and that's fine, but whenever the face is not recognized, no rectangle is shown on the face image, and some message is printed.
import cv2
face_cascade = cv2.CascadeClassifier('C:/Users/320052863/PycharmProjects/trial/venv/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('C:/Users/320052863/PycharmProjects/trial/venv/Lib/site-packages/cv2/data/haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
re = cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2)
if 're' in locals():
print('abhishek')
else:
print('gupta')
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
cv2.imshow('Abhishek',img)
# Wait for Esc key to stop
k = cv2.waitKey(30)
if k == 13:
break
cap.release()
cv2.destroyAllWindows()