NTAMBARA Etienne commited on
Commit
8feb066
·
1 Parent(s): 74690f9

Changes Made Keys p3

Browse files
Files changed (1) hide show
  1. app.py +17 -31
app.py CHANGED
@@ -31,18 +31,18 @@ def recognize_face(input_image):
31
  # Convert PIL Image to numpy array
32
  img = np.array(input_image)
33
  img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
 
34
  # Detect faces and encode
35
  face_locations = face_recognition.face_locations(img)
36
  face_encodings = face_recognition.face_encodings(img, face_locations)
37
- # Initialize the database reference
38
- ref = db.reference('Students')
39
-
40
  # Recognize faces and fetch data from the database
41
- results = []
42
- for face_encoding in face_encodings:
 
 
43
  matches = face_recognition.compare_faces(encodeListKnown, face_encoding)
44
  name = "Unknown"
45
- student_info = {}
46
 
47
  face_distances = face_recognition.face_distance(encodeListKnown, face_encoding)
48
  best_match_index = np.argmin(face_distances)
@@ -52,43 +52,29 @@ def recognize_face(input_image):
52
 
53
  if student_info:
54
  name = student_info['name']
55
- results.append(student_info)
56
  else:
57
- results.append({'name': 'Unknown'})
58
 
59
  # Draw rectangles around the faces
60
- for (top, right, bottom, left), name in zip(face_locations, [student_info.get('name', 'Unknown') for student_info in results]):
61
- cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)
62
- cv2.putText(img, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
63
 
64
  # Convert back to PIL Image
65
  pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
66
- return pil_img, results
67
-
68
- # Define a function to handle webcam images
69
- def process_webcam_image(image):
70
- # Convert the base64 image to a format that can be processed
71
- # Process the image through the face recognition function
72
- return recognize_face(image)
73
- # # Gradio interface
74
- # iface = gr.Interface(
75
- # fn=recognize_face,
76
- # inputs=gr.Image(type="pil"),
77
- # outputs=[gr.Image(type="pil"), gr.JSON(label="Student Information")],
78
- # title="Face Recognition Attendance System",
79
- # description="Upload an image to identify individuals."
80
- # )
81
 
82
- # if __name__ == "__main__":
83
- # iface.launch(debug=True,inline=False)
84
- # Create the Gradio interface
85
  iface = gr.Interface(
86
  fn=recognize_face,
87
  inputs=gr.components.Image(source="webcam", type="pil", tool="editor"),
88
- outputs=[gr.components.Image(type="pil"), gr.components.Label()],
 
 
 
89
  title="Real-time Face Recognition Attendance System",
90
  description="Activate your webcam and take a photo to check attendance."
91
  )
92
 
93
  if __name__ == "__main__":
94
- iface.launch(share=True,debug=True,inline=False)
 
31
  # Convert PIL Image to numpy array
32
  img = np.array(input_image)
33
  img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
34
+
35
  # Detect faces and encode
36
  face_locations = face_recognition.face_locations(img)
37
  face_encodings = face_recognition.face_encodings(img, face_locations)
38
+
 
 
39
  # Recognize faces and fetch data from the database
40
+ ref = db.reference('Students')
41
+ recognized_faces_info = []
42
+
43
+ for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
44
  matches = face_recognition.compare_faces(encodeListKnown, face_encoding)
45
  name = "Unknown"
 
46
 
47
  face_distances = face_recognition.face_distance(encodeListKnown, face_encoding)
48
  best_match_index = np.argmin(face_distances)
 
52
 
53
  if student_info:
54
  name = student_info['name']
55
+ recognized_faces_info.append(student_info)
56
  else:
57
+ recognized_faces_info.append({'name': 'Unknown'})
58
 
59
  # Draw rectangles around the faces
60
+ cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)
61
+ cv2.putText(img, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
 
62
 
63
  # Convert back to PIL Image
64
  pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
65
+ return pil_img, recognized_faces_info
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
+ # Gradio interface
 
 
68
  iface = gr.Interface(
69
  fn=recognize_face,
70
  inputs=gr.components.Image(source="webcam", type="pil", tool="editor"),
71
+ outputs=[
72
+ gr.components.Image(type="pil"),
73
+ gr.components.JSON(label="Student Information")
74
+ ],
75
  title="Real-time Face Recognition Attendance System",
76
  description="Activate your webcam and take a photo to check attendance."
77
  )
78
 
79
  if __name__ == "__main__":
80
+ iface.launch(share=True)