Spaces:
Runtime error
Runtime error
Commit
·
20be593
1
Parent(s):
595bb0b
Upload 3 files
Browse files- app.py +95 -0
- requirements.txt +1 -0
- siamesemodelv2.h5 +3 -0
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import os
|
3 |
+
import random
|
4 |
+
import numpy as np
|
5 |
+
from matplotlib import pyplot as plt
|
6 |
+
|
7 |
+
from tensorflow.keras.layers import Layer
|
8 |
+
import tensorflow as tf
|
9 |
+
|
10 |
+
import gradio as gr
|
11 |
+
|
12 |
+
import uuid
|
13 |
+
|
14 |
+
|
15 |
+
def preprocess(file_path):
|
16 |
+
# Read in image from file path
|
17 |
+
byte_img = tf.io.read_file(file_path)
|
18 |
+
# Load in the image
|
19 |
+
img = tf.io.decode_jpeg(byte_img)
|
20 |
+
# Preprocessing steps - resizing the image to be 100x100x3
|
21 |
+
img = tf.image.resize(img, (100, 100))
|
22 |
+
# Scale image to be between 0 and 1
|
23 |
+
img = img / 255.0
|
24 |
+
# Return image
|
25 |
+
return img
|
26 |
+
|
27 |
+
# Siamese L1 Distance class
|
28 |
+
class L1Dist(Layer):
|
29 |
+
# Init method - inheritance
|
30 |
+
def __init__(self, **kwargs):
|
31 |
+
super().__init__()
|
32 |
+
# Magic happens here - similarity calculation
|
33 |
+
def call(self, input_embedding, validation_embedding):
|
34 |
+
return tf.math.abs(input_embedding - validation_embedding)
|
35 |
+
|
36 |
+
# Reload model
|
37 |
+
siamese_model = tf.keras.models.load_model('siamesemodelv2.h5',
|
38 |
+
custom_objects={'L1Dist': L1Dist, 'BinaryCrossentropy': tf.losses.BinaryCrossentropy})
|
39 |
+
|
40 |
+
def verify(model, detection_threshold, verification_threshold):
|
41 |
+
# Build results array
|
42 |
+
results = []
|
43 |
+
ver_images= []
|
44 |
+
for file in os.listdir(os.path.join('application_data', 'verification_images')):
|
45 |
+
if file[len(file)-4:] == '.jpg':
|
46 |
+
ver_images.append(file)
|
47 |
+
ver_images_len= len(ver_images)
|
48 |
+
for image in ver_images:
|
49 |
+
input_img = preprocess(os.path.join('application_data', 'input_image', 'input_image.jpg'))
|
50 |
+
validation_img = preprocess(os.path.join('application_data', 'verification_images', image))
|
51 |
+
# Make Predictions
|
52 |
+
result = model.predict(list(np.expand_dims([input_img, validation_img], axis=1)))
|
53 |
+
results.append(result)
|
54 |
+
|
55 |
+
# Detection Threshold: Metric above which a prediciton is considered positive
|
56 |
+
detection = np.sum(np.array(results) > detection_threshold)
|
57 |
+
print('Detection -- ',detection)
|
58 |
+
# Verification Threshold: Proportion of positive predictions / total positive samples
|
59 |
+
verification = detection / len(ver_images)
|
60 |
+
print('Verification -- ',verification)
|
61 |
+
verified = verification > verification_threshold
|
62 |
+
|
63 |
+
return detection,verification,verified,ver_images_len
|
64 |
+
|
65 |
+
def take_ver_images (image):
|
66 |
+
count =0
|
67 |
+
cv2.imwrite(os.path.join('application_data', 'verification_images', '{}.jpg'.format(uuid.uuid1())), image)
|
68 |
+
count+=1
|
69 |
+
return '{} Image Collected!'.format(count)
|
70 |
+
def verify_identity (image):
|
71 |
+
cv2.imwrite(os.path.join('application_data', 'input_image', 'input_image.jpg'), image)
|
72 |
+
detection,verification,verified,ver_images_len = verify(siamese_model, 0.8, 0.7)
|
73 |
+
# return {'Detections': int(detection),'Confidence': float(verification),'Verified': bool(verified)}
|
74 |
+
return 'Detected {} out of {} samples'.format(detection,ver_images_len),float(verification),'Verified' if verified else 'Not Verified'
|
75 |
+
|
76 |
+
with gr.Blocks() as demo:
|
77 |
+
with gr.Tab("Setup Identity"):
|
78 |
+
with gr.Row():
|
79 |
+
input_img = gr.inputs.Image(shape=(100,100))
|
80 |
+
output_text = gr.outputs.Label()
|
81 |
+
with gr.Row():
|
82 |
+
submit_btn = gr.Button("Add Image")
|
83 |
+
submit_btn.click(take_ver_images, inputs=input_img,outputs=output_text)
|
84 |
+
with gr.Tab("Identify"):
|
85 |
+
with gr.Row():
|
86 |
+
input_img1 = gr.inputs.Image(shape=(100,100))
|
87 |
+
output_text3 = gr.outputs.Label(label='Verification Result')
|
88 |
+
with gr.Row():
|
89 |
+
output_text1 = gr.outputs.Label(label='Detections')
|
90 |
+
output_text2 = gr.outputs.Label(label='Confidence')
|
91 |
+
|
92 |
+
with gr.Row():
|
93 |
+
submit_btn1 = gr.Button("Verify")
|
94 |
+
submit_btn1.click(verify_identity, inputs=input_img1,outputs=[output_text1,output_text2,output_text3])
|
95 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
gradio
|
siamesemodelv2.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9b167d0780c82879e76b0c3eebb42ae15d18b20bbca27108a3a6d249d478466e
|
3 |
+
size 155886704
|