|
import gradio as gr |
|
import face_recognition |
|
import cv2 |
|
import numpy as np |
|
import pickle |
|
|
|
|
|
with open('EncodeFile.p', 'rb') as file: |
|
encodeListKnownWithIds = pickle.load(file) |
|
encodeListKnown, studentsIds = encodeListKnownWithIds |
|
|
|
def recognize_face(input_image): |
|
|
|
img = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB) |
|
|
|
|
|
facesCurFrame = face_recognition.face_locations(img) |
|
encodesCurFrame = face_recognition.face_encodings(img, facesCurFrame) |
|
|
|
names = [] |
|
for encodeFace in encodesCurFrame: |
|
|
|
matches = face_recognition.compare_faces(encodeListKnown, encodeFace) |
|
faceDist = face_recognition.face_distance(encodeListKnown, encodeFace) |
|
|
|
matchIndex = np.argmin(faceDist) |
|
if matches[matchIndex]: |
|
name = studentsIds[matchIndex] |
|
names.append(name) |
|
else: |
|
names.append("Unknown") |
|
|
|
|
|
for (top, right, bottom, left), name in zip(facesCurFrame, names): |
|
cv2.rectangle(input_image, (left, top), (right, bottom), (0, 0, 255), 2) |
|
cv2.putText(input_image, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_DUPLEX, 1.0, (255, 255, 255), 1) |
|
|
|
return input_image |
|
|
|
|
|
iface = gr.Interface(fn=recognize_face, |
|
inputs=gr.inputs.Image(shape=(480, 360)), |
|
outputs="image", |
|
title="Face Recognition Attendance System", |
|
description="Upload an image to identify registered students.") |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|