File size: 1,664 Bytes
bbf40dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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()