Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
import torch.nn.functional as F
|
5 |
+
import onnxruntime
|
6 |
+
import cv2
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
# Declare ONNX session as a global variable
|
10 |
+
MODEL_PATH = "weights/Glint360K_R200_TopoFR_9784.onnx"
|
11 |
+
session = onnxruntime.InferenceSession(MODEL_PATH)
|
12 |
+
|
13 |
+
def pil_to_cv2(pil_image):
|
14 |
+
# Convert PIL Image to CV2 format
|
15 |
+
numpy_image = np.array(pil_image)
|
16 |
+
# Convert RGB to BGR
|
17 |
+
cv2_image = cv2.cvtColor(numpy_image, cv2.COLOR_RGB2BGR)
|
18 |
+
return cv2_image
|
19 |
+
|
20 |
+
def process_image(pil_img):
|
21 |
+
if pil_img is None:
|
22 |
+
img = np.random.randint(0, 255, size=(112, 112, 3), dtype=np.uint8)
|
23 |
+
else:
|
24 |
+
# Convert PIL image to CV2
|
25 |
+
img = pil_to_cv2(pil_img)
|
26 |
+
img = cv2.resize(img, (112, 112))
|
27 |
+
|
28 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
29 |
+
img = np.transpose(img, (2, 0, 1))
|
30 |
+
img = torch.from_numpy(img).unsqueeze(0).float()
|
31 |
+
img.div_(255).sub_(0.5).div_(0.5)
|
32 |
+
return img
|
33 |
+
|
34 |
+
def calculate_similarity(img1, img2):
|
35 |
+
# Image preprocessing
|
36 |
+
img1_tensor = process_image(img1)
|
37 |
+
img2_tensor = process_image(img2)
|
38 |
+
|
39 |
+
# Extract features using ONNX model
|
40 |
+
def get_features(img_tensor):
|
41 |
+
input_name = session.get_inputs()[0].name
|
42 |
+
features = session.run(None, {input_name: img_tensor.numpy()})[0]
|
43 |
+
return torch.from_numpy(features)
|
44 |
+
|
45 |
+
# Extract features for each image
|
46 |
+
feat1 = get_features(img1_tensor)
|
47 |
+
feat2 = get_features(img2_tensor)
|
48 |
+
|
49 |
+
# Normalize features
|
50 |
+
feat1 = F.normalize(feat1, p=2, dim=1)
|
51 |
+
feat2 = F.normalize(feat2, p=2, dim=1)
|
52 |
+
|
53 |
+
# Calculate cosine similarity
|
54 |
+
cosine_similarity = torch.sum(feat1 * feat2, dim=1).item()
|
55 |
+
return f"Cosine Similarity: {cosine_similarity:.4f}"
|
56 |
+
|
57 |
+
# Create Gradio interface with custom layout
|
58 |
+
with gr.Blocks() as iface:
|
59 |
+
gr.Markdown("# Face Recognition with TopoFR")
|
60 |
+
gr.Markdown("Compare two faces to calculate their cosine similarity.")
|
61 |
+
|
62 |
+
with gr.Row():
|
63 |
+
img1_input = gr.Image(label="Reference Face", type="pil")
|
64 |
+
img2_input = gr.Image(label="Comparison Face", type="pil")
|
65 |
+
|
66 |
+
with gr.Row():
|
67 |
+
similarity_output = gr.Text(label="Results")
|
68 |
+
|
69 |
+
btn = gr.Button("Compare Faces")
|
70 |
+
btn.click(
|
71 |
+
fn=calculate_similarity,
|
72 |
+
inputs=[img1_input, img2_input],
|
73 |
+
outputs=similarity_output
|
74 |
+
)
|
75 |
+
|
76 |
+
# Launch the interface
|
77 |
+
iface.launch()
|