supun9 commited on
Commit
671d008
1 Parent(s): 6028e28

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import face_recognition
2
+ import numpy as np
3
+ import gradio as gr
4
+
5
+ def read_files(files):
6
+ images = []
7
+ encodings = []
8
+ no_face = []
9
+ more_face = []
10
+ for idx, image in enumerate(files):
11
+ image = face_recognition.load_image_file(image.name) #.split("/")[-1]
12
+ encoding = face_recognition.face_encodings(image)
13
+ if len(encoding)==1:
14
+ images.append(image)
15
+ encodings.append(encoding[0])
16
+ elif len(encoding)==0:
17
+ no_face.append(image)
18
+ else:
19
+ more_face.append(image)
20
+ return images, encodings, no_face, more_face
21
+
22
+ def read(profile, files):
23
+ profile_encoding = face_recognition.face_encodings(profile)
24
+ if len(profile_encoding)==1:
25
+ images, encodings, no_face, more_face = read_files(files)
26
+ true_images = []
27
+ false_images = []
28
+ for index in range(len(images)):
29
+ results = face_recognition.compare_faces(encodings[index], profile_encoding)
30
+ if results[0] == True:
31
+ true_images.append(images[index])
32
+ else:
33
+ false_images.append(images[index])
34
+ score = len(true_images)/(len(images)+len(no_face)+len(more_face))
35
+ text = ""
36
+ if len(false_images)>0:
37
+ text += str(len(false_images)) + " of the images do not match with the profile picture.\n"
38
+ if len(no_face)>0:
39
+ text += "No faces were detected in " + str(len(no_face)) + " images.\n"
40
+ if len(more_face)>0:
41
+ text += "More than one face were detected in " + str(len(more_face)) + " images."
42
+
43
+ return {"Percentage of matched images":score}, text, true_images, false_images, no_face, more_face
44
+ else:
45
+ return {"Percentage of matched images":0}, "No faces or more than one faces are detected in the profile picture", [], [], [], []
46
+
47
+ def nothing():
48
+ return "none"
49
+
50
+ with gr.Blocks() as demo:
51
+ gr.Markdown("""# Face Verification System""")
52
+ with gr.Row():
53
+ with gr.Column():
54
+ gr.Markdown("""### Upload the profile picture here""")
55
+ profile = gr.Image(label="Profile picture")
56
+ with gr.Column():
57
+ gr.Markdown("""### Upload the screenshots here""")
58
+ files = gr.File(file_count="directory", label="Screenshots")
59
+ btn = gr.Button(value="Verify").style(full_width=True) #show_progress=True
60
+ with gr.Row():
61
+ with gr.Column():
62
+ gr.Markdown("""### Report""")
63
+ text = gr.Textbox(show_label=False).style(container=False)
64
+ label = gr.Label(num_top_classes=1, show_label=False)
65
+ with gr.Tab("Matched images"):
66
+ gallery1 = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(columns=[2], height=3) #grid=[5]
67
+ with gr.Tab("Not matched"):
68
+ gallery2 = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(columns=[2], height=3)
69
+ with gr.Row():
70
+ btn1 = gr.Button(value="Accept")
71
+ btn2 = gr.Button(value="Escalate")
72
+ with gr.Tab("No faces detected"):
73
+ gallery3 = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(columns=[2], height=3)
74
+ with gr.Row():
75
+ btn3 = gr.Button(value="Accept")
76
+ btn4 = gr.Button(value="Escalate")
77
+ with gr.Tab("More than one face detected"):
78
+ gallery4 = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(columns=[2], height=3)
79
+
80
+ btn.click(read, [profile, files], [label, text, gallery1, gallery2, gallery3, gallery4], show_progress=True, scroll_to_output=True)
81
+ btn1.click(nothing, [], [], show_progress=True, scroll_to_output=True)
82
+ btn2.click(nothing, [], [], show_progress=True, scroll_to_output=True)
83
+ btn3.click(nothing, [], [], show_progress=True, scroll_to_output=True)
84
+ btn4.click(nothing, [], [], show_progress=True, scroll_to_output=True)
85
+
86
+ demo.launch()