themehmi commited on
Commit
216c2d5
·
verified ·
1 Parent(s): 5c0f6d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -32
app.py CHANGED
@@ -81,39 +81,41 @@ def mark_attendance(name):
81
  def generate_frames():
82
  camera = cv2.VideoCapture(0)
83
 
84
- while True:
85
- success, frame = camera.read()
86
- if not success:
87
- break
88
- else:
89
- small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
90
- rgb_small_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)
91
-
92
- face_locations = face_recognition.face_locations(rgb_small_frame)
93
- face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
94
-
95
- for face_encoding, face_location in zip(face_encodings, face_locations):
96
- matches = face_recognition.compare_faces(known_encodings, face_encoding, tolerance=0.5)
97
- face_distances = face_recognition.face_distance(known_encodings, face_encoding)
98
 
99
- name = "Unknown"
100
- if len(face_distances) > 0:
101
- best_match_index = np.argmin(face_distances)
102
- if matches[best_match_index]:
103
- name = known_names[best_match_index]
104
- mark_attendance(name)
105
-
106
- top, right, bottom, left = face_location
107
- top, right, bottom, left = top*4, right*4, bottom*4, left*4
108
-
109
- cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
110
- cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 2)
111
-
112
- ret, buffer = cv2.imencode('.jpg', frame)
113
- frame = buffer.tobytes()
114
-
115
- yield (b'--frame\r\n'
116
- b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
 
 
 
 
 
 
117
 
118
 
119
  # ROUTES
 
81
  def generate_frames():
82
  camera = cv2.VideoCapture(0)
83
 
84
+ try:
85
+ while True:
86
+ success, frame = camera.read()
87
+ if not success:
88
+ break
89
+ else:
90
+ small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
91
+ rgb_small_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)
92
+ face_locations = face_recognition.face_locations(rgb_small_frame)
93
+ face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
 
 
 
 
94
 
95
+ for face_encoding, face_location in zip(face_encodings, face_locations):
96
+ matches = face_recognition.compare_faces(known_encodings, face_encoding, tolerance=0.5)
97
+ face_distances = face_recognition.face_distance(known_encodings, face_encoding)
98
+
99
+ name = "Unknown"
100
+ if len(face_distances) > 0:
101
+ best_match_index = np.argmin(face_distances)
102
+ if matches[best_match_index]:
103
+ name = known_names[best_match_index]
104
+ mark_attendance(name)
105
+
106
+ top, right, bottom, left = face_location
107
+ top, right, bottom, left = top*4, right*4, bottom*4, left*4
108
+ cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
109
+ cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 2)
110
+
111
+ ret, buffer = cv2.imencode('.jpg', frame)
112
+ frame = buffer.tobytes()
113
+ yield (b'--frame\r\n'
114
+ b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
115
+ finally:
116
+ # This executes when the frontend clears the image src,
117
+ # disconnecting the stream and releasing the webcam hardware.
118
+ camera.release()
119
 
120
 
121
  # ROUTES