face_app / app.py
ntam0001's picture
Create app.py
bbf40dd
raw
history blame
1.66 kB
import gradio as gr
import face_recognition
import cv2
import numpy as np
import pickle
# Load the known face encodings and their IDs
with open('EncodeFile.p', 'rb') as file:
encodeListKnownWithIds = pickle.load(file)
encodeListKnown, studentsIds = encodeListKnownWithIds
def recognize_face(input_image):
# Convert the image to RGB
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:
# Compare faces and get the distance
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
# Create a Gradio interface
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()