Ankit93 commited on
Commit
6b20b3a
·
verified ·
1 Parent(s): 12ba455

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -1
app.py CHANGED
@@ -57,6 +57,50 @@ def start_training():
57
  import src.faceRecognize.facerec.train_v2
58
  st.success("Training completed successfully.")
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  class FaceRecognitionProcessor(VideoProcessorBase):
62
  def __init__(self):
@@ -118,7 +162,8 @@ def main():
118
  key="face-detection",
119
  mode=WebRtcMode.SENDRECV,
120
  rtc_configuration=RTC_CONFIGURATION,
121
- video_frame_callback=FaceRecognitionProcessor().recv,
 
122
  media_stream_constraints={"video": True, "audio": False},async_processing=True
123
  )
124
  if webrtc_ctx.state.playing:
 
57
  import src.faceRecognize.facerec.train_v2
58
  st.success("Training completed successfully.")
59
 
60
+ def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
61
+ image = frame.to_ndarray(format="bgr24")
62
+ face_encoder = model_selector("Facenet")
63
+ encodings_path = './src/faceRecognize/facerec/encodings/encodings.pkl'
64
+ encoding_dict = load_pickle(self.encodings_path)
65
+ # Run inference
66
+ blob = cv2.dnn.blobFromImage(
67
+ cv2.resize(image, (300, 300)), 0.007843, (300, 300), 127.5
68
+ )
69
+ net.setInput(blob)
70
+ output = net.forward()
71
+
72
+ h, w = image.shape[:2]
73
+
74
+ # Convert the output array into a structured form.
75
+ output = output.squeeze() # (1, 1, N, 7) -> (N, 7)
76
+ output = output[output[:, 2] >= score_threshold]
77
+
78
+ detections = [
79
+ detect(output, face_detector, face_encoder, encoding_dict)
80
+ for detection in output
81
+ ]
82
+
83
+ # Render bounding boxes and captions
84
+ for detection in detections:
85
+ # image, pred = detect(frame_resized, face_detector, self.face_encoder, self.encoding_dict)
86
+ caption = f"{detection.label}: {round(detection.score * 100, 2)}%"
87
+ color = COLORS[detection.class_id]
88
+ xmin, ymin, xmax, ymax = detection.box.astype("int")
89
+
90
+ cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2)
91
+ cv2.putText(
92
+ image,
93
+ caption,
94
+ (xmin, ymin - 15 if ymin - 15 > 15 else ymin + 15),
95
+ cv2.FONT_HERSHEY_SIMPLEX,
96
+ 0.5,
97
+ color,
98
+ 2,
99
+ )
100
+
101
+ result_queue.put(detections)
102
+
103
+ return av.VideoFrame.from_ndarray(image, format="bgr24")
104
 
105
  class FaceRecognitionProcessor(VideoProcessorBase):
106
  def __init__(self):
 
162
  key="face-detection",
163
  mode=WebRtcMode.SENDRECV,
164
  rtc_configuration=RTC_CONFIGURATION,
165
+ #video_frame_callback=FaceRecognitionProcessor().recv,
166
+ video_frame_callback = video_frame_callback
167
  media_stream_constraints={"video": True, "audio": False},async_processing=True
168
  )
169
  if webrtc_ctx.state.playing: