Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import face_recognition
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
import pickle
|
6 |
+
|
7 |
+
# Load the known face encodings and their IDs
|
8 |
+
with open('EncodeFile.p', 'rb') as file:
|
9 |
+
encodeListKnownWithIds = pickle.load(file)
|
10 |
+
encodeListKnown, studentsIds = encodeListKnownWithIds
|
11 |
+
|
12 |
+
def recognize_face(input_image):
|
13 |
+
# Convert the image to RGB
|
14 |
+
img = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
|
15 |
+
|
16 |
+
|
17 |
+
facesCurFrame = face_recognition.face_locations(img)
|
18 |
+
encodesCurFrame = face_recognition.face_encodings(img, facesCurFrame)
|
19 |
+
|
20 |
+
names = []
|
21 |
+
for encodeFace in encodesCurFrame:
|
22 |
+
# Compare faces and get the distance
|
23 |
+
matches = face_recognition.compare_faces(encodeListKnown, encodeFace)
|
24 |
+
faceDist = face_recognition.face_distance(encodeListKnown, encodeFace)
|
25 |
+
|
26 |
+
matchIndex = np.argmin(faceDist)
|
27 |
+
if matches[matchIndex]:
|
28 |
+
name = studentsIds[matchIndex]
|
29 |
+
names.append(name)
|
30 |
+
else:
|
31 |
+
names.append("Unknown")
|
32 |
+
|
33 |
+
|
34 |
+
for (top, right, bottom, left), name in zip(facesCurFrame, names):
|
35 |
+
cv2.rectangle(input_image, (left, top), (right, bottom), (0, 0, 255), 2)
|
36 |
+
cv2.putText(input_image, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_DUPLEX, 1.0, (255, 255, 255), 1)
|
37 |
+
|
38 |
+
return input_image
|
39 |
+
|
40 |
+
# Create a Gradio interface
|
41 |
+
iface = gr.Interface(fn=recognize_face,
|
42 |
+
inputs=gr.inputs.Image(shape=(480, 360)),
|
43 |
+
outputs="image",
|
44 |
+
title="Face Recognition Attendance System",
|
45 |
+
description="Upload an image to identify registered students.")
|
46 |
+
|
47 |
+
if __name__ == "__main__":
|
48 |
+
iface.launch()
|