supun9's picture
Update app.py
53e8b7f
raw
history blame contribute delete
No virus
3.47 kB
import face_recognition
import numpy as np
import gradio as gr
def read_files(files):
images = []
encodings = []
no_face = []
more_face = []
for idx, image in enumerate(files):
image = face_recognition.load_image_file(image.name) #.split("/")[-1]
encoding = face_recognition.face_encodings(image)
if len(encoding)==1:
images.append(image)
encodings.append(encoding[0])
elif len(encoding)==0:
no_face.append(image)
else:
more_face.append(image)
return images, encodings, no_face, more_face
def read(profile, files):
profile_encoding = face_recognition.face_encodings(profile)
if len(profile_encoding)==1:
images, encodings, no_face, more_face = read_files(files)
true_images = []
false_images = []
for index in range(len(images)):
results = face_recognition.compare_faces(encodings[index], profile_encoding)
if results[0] == True:
true_images.append(images[index])
else:
false_images.append(images[index])
score = len(true_images)/(len(images)+len(no_face)+len(more_face))
text = ""
if len(false_images)>0:
text += str(len(false_images)) + " of the images do not match with the profile picture.\n"
if len(no_face)>0:
text += "No faces were detected in " + str(len(no_face)) + " images.\n"
if len(more_face)>0:
text += "More than one face were detected in " + str(len(more_face)) + " images."
return {"Percentage of matched images":score}, text, true_images, false_images, no_face, more_face
else:
return {"Percentage of matched images":0}, "No faces or more than one faces are detected in the profile picture", [], [], [], []
def nothing():
return
with gr.Blocks() as demo:
gr.Markdown("""# Face Verification System""")
with gr.Row():
with gr.Column():
gr.Markdown("""### Upload the profile picture here""")
profile = gr.Image(label="Profile picture")
with gr.Column():
gr.Markdown("""### Upload the screenshots here""")
files = gr.File(file_count="directory", label="Screenshots")
btn = gr.Button(value="Verify").style(full_width=True) #show_progress=True
with gr.Row():
with gr.Column():
gr.Markdown("""### Report""")
text = gr.Textbox(show_label=False).style(container=False)
label = gr.Label(num_top_classes=1, show_label=False)
with gr.Tab("Matched images"):
gallery1 = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[5], height=3) #grid=[5]
with gr.Tab("Not matched"):
gallery2 = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[5], height=3)
with gr.Row():
btn1 = gr.Button(value="Accept")
btn2 = gr.Button(value="Escalate")
with gr.Tab("No faces detected"):
gallery3 = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[5], height=3)
with gr.Row():
btn3 = gr.Button(value="Accept")
btn4 = gr.Button(value="Escalate")
with gr.Tab("More than one face detected"):
gallery4 = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[5], height=3)
btn.click(read, [profile, files], [label, text, gallery1, gallery2, gallery3, gallery4], show_progress=True, scroll_to_output=True)
btn1.click(nothing, [], [])
btn2.click(nothing, [], [])
btn3.click(nothing, [], [])
btn4.click(nothing, [], [])
demo.launch()