darpanaswal's picture
Update run.py
63ffffa verified
import os
import face_recognition
import numpy as np
from PIL import Image, ImageDraw
import pandas as pd
def predict_and_show(image):
X = np.load("X.npy")
Y = np.load("Y.npy")
attendance = []
image = face_recognition.load_image_file(image)
face_bounding_boxes = face_recognition.face_locations(image)
if len(face_bounding_boxes) == 0:
print("No face found in the image")
return
X_test = face_recognition.face_encodings(image, known_face_locations=face_bounding_boxes)
for i in range(len(X_test)):
distances = np.linalg.norm(np.array(X) - np.array(X_test[i]), axis=1)
min_distance = np.min(distances)
if min_distance > 0.55:
print("Unknown")
else:
print(Y[np.argmin(distances)], min_distance)
attendance.append(Y[np.argmin(distances)])
top, right, bottom, left = face_bounding_boxes[i]
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
draw = ImageDraw.Draw(pil_image)
draw.text((0, 0), Y[np.argmin(distances)], (255, 255, 255))
# pil_image.show()
return attendance
# predict_and_show()
# #ask for input string from user
# print("Enter the name of the image file")
# image_path = input()
# predict_and_show(image_path)
# #save the attendance list to a txt file
# with open("attendance.txt", "w") as file:
# for i in attendance:
# file.write(i + "\n")