supun9 commited on
Commit
d7dd8d3
1 Parent(s): ccdf4e6

Create app.py

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