nikshep01 commited on
Commit
bb99b14
1 Parent(s): d6d1fc2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -37
app.py CHANGED
@@ -52,49 +52,46 @@ if file_name is not None:
52
 
53
  imgS = cv2.resize(image, (0, 0), None, 0.25, 0.25)
54
  imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
55
- facesCurFrame = face_recognition.face_locations(imgS)
56
-
 
57
  # List to store recognized names for all faces in the image
58
  recognized_names = []
59
 
60
- # Iterate over each face location
61
- for faceLoc in facesCurFrame:
62
- # Extract the coordinates of the face location
63
- top, right, bottom, left = faceLoc
64
-
65
- # Crop the face region from the image
66
- face_img = imgS[top:bottom, left:right]
67
-
68
- # Encode the face
69
- face_encoding = face_recognition.face_encodings(face_img)[0]
70
-
71
- # Compare the face encoding with known face encodings
72
- matches = face_recognition.compare_faces(encodeListknown, face_encoding)
73
-
74
- # Initialize name as Unknown
75
- name = "Unknown"
76
-
77
- # Check if there's a match with known faces
78
- if True in matches:
79
- matchIndex = matches.index(True)
80
- name = classnames[matchIndex].upper()
81
-
82
- # Append recognized name to the list
83
- recognized_names.append(name)
84
 
85
- # Draw rectangle around the face
86
- cv2.rectangle(image, (left*4, top*4), (right*4, bottom*4), (0, 255, 0), 2)
87
- cv2.putText(image, name, (left*4 + 6, bottom*4 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
 
 
 
 
88
 
89
- # Store attendance in SQLite database
90
- add_attendance(name)
 
 
 
 
 
 
 
91
 
92
- # Display the image with recognized faces
93
- st.image(image, use_column_width=True, output_format="PNG")
94
 
95
- # Display recognized names
96
- st.write("Recognized Names:")
97
- for i, name in enumerate(recognized_names):
98
- st.write(f"Face {i+1}: {name}")
99
  else:
100
  st.warning("No faces detected in the image. Face recognition failed.")
 
52
 
53
  imgS = cv2.resize(image, (0, 0), None, 0.25, 0.25)
54
  imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
55
+ facesCurFrame = face_recognition.face_locations(imgS)
56
+ encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
57
+
58
  # List to store recognized names for all faces in the image
59
  recognized_names = []
60
 
61
+ # Checking if faces are detected
62
+ if len(encodesCurFrame) > 0:
63
+ for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
64
+ # Assuming that encodeListknown is defined and populated in your code
65
+ matches = face_recognition.compare_faces(encodeListknown, encodeFace)
66
+ faceDis = face_recognition.face_distance(encodeListknown, encodeFace)
67
+
68
+ # Initialize name as Unknown
69
+ name = "Unknown"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
+ # Check if there's a match with known faces
72
+ if True in matches:
73
+ matchIndex = np.argmin(faceDis)
74
+ name = classnames[matchIndex].upper()
75
+
76
+ # Append recognized name to the list
77
+ recognized_names.append(name)
78
 
79
+ # Draw rectangle around the face
80
+ y1, x2, y2, x1 = faceLoc
81
+ y1, x2, y2, x1 = (y1 * 4), (x2 * 4), (y2 * 4) ,(x1 * 4)
82
+ image = image.copy()
83
+ cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
84
+ cv2.putText(image, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
85
+
86
+ # Store attendance in SQLite database
87
+ add_attendance(name)
88
 
89
+ # Display the image with recognized faces
90
+ st.image(image, use_column_width=True, output_format="PNG")
91
 
92
+ # Display recognized names
93
+ st.write("Recognized Names:")
94
+ for i, name in enumerate(recognized_names):
95
+ st.write(f"Face {i+1}: {name}")
96
  else:
97
  st.warning("No faces detected in the image. Face recognition failed.")