File size: 4,316 Bytes
d7dd8d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
!pip install face-recognition
!pip install gradio
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)
    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)
    face_distances = []
    true_images = []
    false_images = []
    for index in range(len(images)):
      results = face_recognition.compare_faces(encodings[index], profile_encoding)
      if results[0] == True:
        face_distance = face_recognition.face_distance(profile_encoding, encodings[index])
        face_distances.append(face_distance)
        true_images.append(images[index])
      else:
        false_images.append(images[index])
    score = len(face_distances)/(len(images)+len(no_face)+len(more_face))
    text = ""
    vals, counts = np.unique(face_distances, return_counts=True)

    if (np.std(face_distances)<0.01) or max(counts)>((len(images)+len(no_face)+len(more_face))/2):
      text += "Most of the images look similar.\n\n"
    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 "none"

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(label="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(columns=[5], height=3)
    with gr.Tab("Not matched"):
      gallery2 = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(columns=[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(columns=[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(columns=[5], height=3)
      with gr.Row():
        btn5 = gr.Button(value="Accept")
        btn6 = gr.Button(value="Escalate")

  btn.click(read, [profile, files], [label, text, gallery1, gallery2, gallery3, gallery4], show_progress=True, scroll_to_output=True)
  btn1.click(nothing, [], [], show_progress=True, scroll_to_output=True)
  btn2.click(nothing, [], [], show_progress=True, scroll_to_output=True)
  btn3.click(nothing, [], [], show_progress=True, scroll_to_output=True)
  btn4.click(nothing, [], [], show_progress=True, scroll_to_output=True)
  btn5.click(nothing, [], [], show_progress=True, scroll_to_output=True)
  btn6.click(nothing, [], [], show_progress=True, scroll_to_output=True)

demo.launch()