0
#### This function will run when we click on Take Attendance Button
@app.route('/start',methods=['GET'])
def start():
    if 'face_recognition_model.pkl' not in os.listdir('static'):
        return render_template('home.html',totalreg=totalreg(),datetoday2=datetoday2,mess='There is no trained model in the static folder. Please add a new face to continue.') 

    cap = cv2.VideoCapture(0)
    ret = True
    while ret:
        ret,frame = cap.read()
        if extract_faces(frame)!=():
            (x,y,w,h) = extract_faces(frame)[0]
            cv2.rectangle(frame,(x, y), (x+w, y+h), (255, 0, 20), 2)
            face = cv2.resize(frame[y:y+h,x:x+w], (50, 50))
            identified_person = identify_face(face.reshape(1,-1))[0]
            add_attendance(identified_person)
            cv2.putText(frame,f'{identified_person}',(30,30),cv2.FONT_HERSHEY_SIMPLEX,1,(255, 0, 20),2,cv2.LINE_AA)
        cv2.imshow('Attendance',frame)
        if cv2.waitKey(1)==27:
            break
    cap.release()
    cv2.destroyAllWindows()
    names,rolls,times,l = extract_attendance()    
    return render_template('home.html',names=names,rolls=rolls,times=times,l=l,totalreg=totalreg(),datetoday2=datetoday2) 

I am pressing q to close the webcam window but it is not closing until and unless i am terminating the entire program

1 Answers1

0

Change the waitKey line to:

if cv2.waitKey(1) & 0xFF == ord("q"):
    break
Toyo
  • 667
  • 1
  • 5
  • 22
  • The `& 0xFF` is not necessary. – beaker Apr 25 '23 at 02:54
  • I disagree. Source: https://stackoverflow.com/questions/35372700/whats-0xff-for-in-cv2-waitkey1 – Toyo Apr 25 '23 at 02:56
  • If you look at the [source code](https://github.com/opencv/opencv/blob/6dd8a9b6add2d44aebaac570525ecbbe37e39840/modules/highgui/src/window.cpp#L655) for OpenCV version 4.x, you'll see on line 668 that there is already an explicit `code & 0xff` before the code is returned. To return the full code with flags intact, you'd need to call [`cv::waitKeyEx`](https://github.com/opencv/opencv/blob/6dd8a9b6add2d44aebaac570525ecbbe37e39840/modules/highgui/src/window.cpp#L639) instead. – beaker May 01 '23 at 18:48