1

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()
David Jones
  • 4,766
  • 3
  • 32
  • 45
Abhishek Gupta
  • 107
  • 1
  • 10
  • Can you elaborate: What do you mean by recognised? recognised as a face or recognised asa specific person? – Lucas Apr 10 '19 at 07:30

1 Answers1

0

It appears that if, in your script, after your line:

faces = face_cascade.detectMultiScale(gray, 1.3, 5)

you insert the following line, it will warn if no faces have been detected:

if len(faces) == 0:
    print "No faces found"
Dlamini
  • 285
  • 1
  • 9
  • I want to display that message in webpage. how can i do using flask? – Abhishek Gupta Apr 10 '19 at 08:48
  • @AbhishekGupta Firstly, please give feedback on whether the submitted solution works for you. – Dlamini Apr 11 '19 at 00:28
  • But i wanna do that message show on webpage as a pop-up message. – Abhishek Gupta Apr 11 '19 at 06:05
  • @AbhishekGupta Ok, great, please accept the answer and/or vote it as useful, and then I can give you further tips. – Dlamini Apr 11 '19 at 07:22
  • Actually,I am new user. I do not more than 15 reputation.That's why i can not give the vote. – Abhishek Gupta Apr 11 '19 at 11:56
  • @AbhishekGupta Thanks, understood. One possible approach is instead of just printing as above, you write the result to a small text file that can be read by the program that does the popup display in Flask. Please look at this stackexchange question, which should help: https://stackoverflow.com/questions/15721679/update-and-render-a-value-from-flask-periodically – Dlamini Apr 12 '19 at 07:57