diff options
author | Saumit Dinesan <justsaumit@protonmail.com> | 2023-05-12 04:51:34 +0530 |
---|---|---|
committer | Saumit Dinesan <justsaumit@protonmail.com> | 2023-05-12 04:51:34 +0530 |
commit | a8734e6c94047e38c83acbac6f1405ff0c7b7b3f (patch) | |
tree | 59c5065bfd4e4a2f0c98a4ca6a1273c44b91abc2 /face-detection | |
parent | 4ec9fa23b3c37f4ddf50e77a8f41ecdd4ddf1399 (diff) |
webui: Cleaning up dump+ Livestream IMPLEMENTATION
Diffstat (limited to 'face-detection')
-rw-r--r-- | face-detection/03_face_recogition.py | 88 |
1 files changed, 48 insertions, 40 deletions
diff --git a/face-detection/03_face_recogition.py b/face-detection/03_face_recogition.py index 185d3c2..51741a5 100644 --- a/face-detection/03_face_recogition.py +++ b/face-detection/03_face_recogition.py @@ -2,6 +2,9 @@ import cv2 import os import numpy as np from picamera2 import Picamera2 +from flask import Flask, render_template, Response + +app = Flask(__name__) #Parameters id = 0 @@ -28,53 +31,58 @@ cam.preview_configuration.align() cam.configure("preview") cam.start() -while True: - # Capture a frame from the camera - frame=cam.capture_array() +def generate_frames(): + while True: + # Capture a frame from the camera + frame=cam.capture_array() - #Convert fram from BGR to grayscale - frameGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) - #Create a DS faces- array with 4 elements- x,y coordinates top-left corner), width and height - faces = face_detector.detectMultiScale( - frameGray, # The grayscale frame to detect - scaleFactor=1.1,# how much the image size is reduced at each image scale-10% reduction - minNeighbors=5, # how many neighbors each candidate rectangle should have to retain it - minSize=(150, 150)# Minimum possible object size. Objects smaller than this size are ignored. - ) - for(x,y,w,h) in faces: - namepos=(x+5,y-5) #shift right and up/outside the bounding box from top - confpos=(x+5,y+h-5) #shift right and up/intside the bounding box from bottom - #create a bounding box across the detected face - cv2.rectangle(frame, (x,y), (x+w,y+h), boxColor, 3) #5 parameters - frame, topleftcoords,bottomrightcooords,boxcolor,thickness + #Convert fram from BGR to grayscale + frameGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + #Create a DS faces- array with 4 elements- x,y coordinates top-left corner), width and height + faces = face_detector.detectMultiScale( + frameGray, # The grayscale frame to detect + scaleFactor=1.1,# how much the image size is reduced at each image scale-10% reduction + minNeighbors=5, # how many neighbors each candidate rectangle should have to retain it + minSize=(150, 150)# Minimum possible object size. Objects smaller than this size are ignored. + ) + for(x,y,w,h) in faces: + namepos=(x+5,y-5) #shift right and up/outside the bounding box from top + confpos=(x+5,y+h-5) #shift right and up/intside the bounding box from bottom + #create a bounding box across the detected face + cv2.rectangle(frame, (x,y), (x+w,y+h), boxColor, 3) #5 parameters - frame, topleftcoords,bottomrightcooords,boxcolor,thickness - #recognizer.predict() method takes the ROI as input and - #returns the predicted label (id) and confidence score for the given face region. - id, confidence = recognizer.predict(frameGray[y:y+h,x:x+w]) + #recognizer.predict() method takes the ROI as input and + #returns the predicted label (id) and confidence score for the given face region. + id, confidence = recognizer.predict(frameGray[y:y+h,x:x+w]) - # If confidence is less than 100, it is considered a perfect match - if confidence < 100: - id = names[id] - confidence = f"{100 - confidence:.0f}%" - else: - id = "unknown" - confidence = f"{100 - confidence:.0f}%" + # If confidence is less than 100, it is considered a perfect match + if confidence < 100: + id = names[id] + confidence = f"{100 - confidence:.0f}%" + else: + id = "unknown" + confidence = f"{100 - confidence:.0f}%" #Display name and confidence of person who's face is recognized cv2.putText(frame, str(id), namepos, font, height, nameColor, 2) cv2.putText(frame, str(confidence), confpos, font, height, confColor, 1) - # Display realtime capture output to the user - cv2.imshow('Raspi Face Recognizer',frame) + # Display output Flask web application: + # Convert the frame to JPEG format + ret, buffer = cv2.imencode('.jpg', frame) + frame = buffer.tobytes() + + # Yield the frame in the HTTP response + yield (b'--frame\r\n' + b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') + +@app.route('/') +def index(): + return render_template('index.html') - # Wait for 30 milliseconds for a key event (extract sigfigs) and exit if 'ESC' or 'q' is pressed - key = cv2.waitKey(100) & 0xff - #Checking keycode - if key == 27: # ESCAPE key - break - elif key == 113: # q key - break +@app.route('/video_feed') +def video_feed(): + return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame') -# Release the camera and close all windows -print("\n [INFO] Exiting Program and cleaning up stuff") -cam.stop() -cv2.destroyAllWindows() +if __name__ == '__main__': + app.run(debug=True) |