Spaces:
Sleeping
Sleeping
import os | |
import warnings | |
os.environ['TF_ENABLE_ONEDNN_OPTS']=str(0) | |
warnings.filterwarnings('ignore') | |
#import pandas as pd | |
import numpy as np | |
from deepface import DeepFace | |
from db import create_db, get_student_row | |
from helper_fns import binary_to_pil, extract_faces | |
def verify_student(db_path, last_name, matric_no, input_image): | |
data = get_student_row(db_path, last_name, matric_no) | |
if data is not None: | |
binary_image = data['image'] | |
else: | |
raise ValueError('No student having last name {last_name} and matric no: {matric_no} exists in this database.') | |
actual_image =np.array(binary_to_pil(binary_image)) | |
webcam_image = input_image | |
if webcam_image is None: | |
raise ValueError(f'No image') | |
else: | |
print('Received image') | |
print(type(webcam_image)) | |
results = DeepFace.verify(webcam_image, | |
actual_image, | |
model_name='Facenet', | |
detector_backend="retinaface", | |
distance_metric="cosine", | |
enforce_detection=True, | |
anti_spoofing=True, | |
align=True, | |
normalization="Facenet") | |
result = results['verified'] | |
if results['verified'] == True: | |
result = f"Verification check complete! Successfully verified student: {data['last_name']} {data['first_name']} with matriculation number {data['matric_no']}" | |
else: | |
result = f"Verification check complete! You are not student {data['last_name']} {data['first_name']} with matriculation number {data['matric_no']}" | |
cropped_input_image, cropped_returned_image = extract_faces(webcam_image, actual_image, results) | |
return result, cropped_input_image, cropped_returned_image, actual_image | |
def main(last_name, matric_no, input_image): | |
#df = pd.read_csv('students_df.csv') | |
db_path = 'students_database.db' | |
#create_db(db_path, df) | |
result, cropped_input_image, cropped_returned_image, actual_image = verify_student(db_path, | |
last_name, | |
matric_no, | |
input_image) | |
#os.remove('students_database.db') | |
return result, cropped_input_image, cropped_returned_image, actual_image | |
if __name__ == '__main__': | |
main() |