supun9 commited on
Commit
59e91a3
1 Parent(s): b45be94

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
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
+ face_distances = []
27
+ true_images = []
28
+ false_images = []
29
+ for index in range(len(images)):
30
+ results = face_recognition.compare_faces(encodings[index], profile_encoding)
31
+ if results[0] == True:
32
+ face_distance = face_recognition.face_distance(profile_encoding, encodings[index])
33
+ face_distances.append(face_distance)
34
+ true_images.append(images[index])
35
+ else:
36
+ false_images.append(images[index])
37
+ score = len(face_distances)/(len(images)+len(no_face)+len(more_face))
38
+ text = ""
39
+ vals, counts = np.unique(face_distances, return_counts=True)
40
+
41
+ if (np.std(face_distances)<0.01) or max(counts)>((len(images)+len(no_face)+len(more_face))/2):
42
+ text += "Most of the images look similar.\n\n"
43
+ if len(false_images)>0:
44
+ text += str(len(false_images)) + " of the images do not match with the profile picture.\n"
45
+ if len(no_face)>0:
46
+ text += "No faces were detected in " + str(len(no_face)) + " images.\n"
47
+ if len(more_face)>0:
48
+ text += "More than one face were detected in " + str(len(more_face)) + " images."
49
+ return {"Percentage of matched images":score}, text, true_images, false_images, no_face, more_face
50
+ else:
51
+ return {"Percentage of matched images":0}, "No faces or more than one faces are detected in the profile picture", [], [], [], []
52
+
53
+ with gr.Blocks() as demo:
54
+ gr.Markdown("""# Face Verification System""")
55
+ with gr.Row():
56
+ with gr.Column():
57
+ gr.Markdown("""### Upload the profile picture here""")
58
+ profile = gr.Image(label="Profile picture")
59
+ with gr.Column():
60
+ gr.Markdown("""### Upload the screenshots here""")
61
+ files = gr.File(file_count="directory", label="Screenshots")
62
+ btn = gr.Button(label="Verify").style(full_width=True) #show_progress=True
63
+ with gr.Row():
64
+ with gr.Column():
65
+ gr.Markdown("""### Report""")
66
+ text = gr.Textbox(show_label=False).style(container=False)
67
+ label = gr.Label(num_top_classes=1, show_label=False)
68
+ with gr.Tab("Matched images"):
69
+ gallery1 = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(columns=[5], height=3)
70
+ with gr.Tab("Not matched"):
71
+ gallery2 = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(columns=[5], height=3)
72
+ with gr.Tab("No faces detected"):
73
+ gallery3 = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(columns=[5], height=3)
74
+ with gr.Tab("More than one face detected"):
75
+ gallery4 = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(columns=[5], height=3)
76
+
77
+ btn.click(read, [profile, files], [label, text, gallery1, gallery2, gallery3, gallery4], show_progress=True, scroll_to_output=True)
78
+
79
+ demo.launch()