Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- gradio_app.py +26 -0
- gradio_app/.gitkeep +0 -0
- gradio_app/__init__.py +0 -0
- gradio_app/inference.py +57 -0
gradio_app.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gradio_app.inference import run_inference
|
3 |
+
|
4 |
+
def create_gradio_interface():
|
5 |
+
return gr.Interface(
|
6 |
+
fn=run_inference,
|
7 |
+
inputs=[
|
8 |
+
gr.Image(type="pil", label="Upload Image"),
|
9 |
+
gr.File(label="Reference Dict JSON File"),
|
10 |
+
gr.File(label="Index to Class Mapping JSON File"),
|
11 |
+
gr.File(label="Classifier Model (.pth) File"),
|
12 |
+
gr.Textbox(label="EdgeFace Model Name", value="edgeface_base"),
|
13 |
+
gr.Textbox(label="EdgeFace Model Directory", value="ckpts/idiap"),
|
14 |
+
gr.Dropdown(choices=["yolo", "mtcnn"], label="Face Detection Algorithm", value="yolo"),
|
15 |
+
gr.Dropdown(choices=["auto", "cpu", "gpu"], label="Accelerator", value="auto"),
|
16 |
+
gr.Slider(minimum=112, maximum=448, step=1, value=224, label="Resolution"),
|
17 |
+
gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.6, label="Similarity Threshold")
|
18 |
+
],
|
19 |
+
outputs="text",
|
20 |
+
title="Face Classification with EdgeFace Validation",
|
21 |
+
description="Upload an image and required files to perform face classification with EdgeFace embedding validation."
|
22 |
+
)
|
23 |
+
|
24 |
+
if __name__ == "__main__":
|
25 |
+
iface = create_gradio_interface()
|
26 |
+
iface.launch()
|
gradio_app/.gitkeep
ADDED
File without changes
|
gradio_app/__init__.py
ADDED
File without changes
|
gradio_app/inference.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# Append the path to the inference script's directory
|
6 |
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'src', 'slimface', 'inference')))
|
7 |
+
from end2end_inference import cinference_and_confirm
|
8 |
+
|
9 |
+
def run_inference(image, reference_dict_path, index_to_class_mapping_path, model_path,
|
10 |
+
edgeface_model_name="edgeface_base", edgeface_model_dir="ckpts/idiap",
|
11 |
+
algorithm="yolo", accelerator="auto", resolution=224, similarity_threshold=0.6):
|
12 |
+
# Save uploaded image temporarily in apps/gradio_app/
|
13 |
+
temp_image_path = os.path.join(os.path.dirname(__file__), "temp_image.jpg")
|
14 |
+
image.save(temp_image_path)
|
15 |
+
|
16 |
+
# Create args object to mimic command-line arguments
|
17 |
+
class Args:
|
18 |
+
def __init__(self):
|
19 |
+
self.unknown_image_path = temp_image_path
|
20 |
+
self.reference_dict_path = reference_dict_path.name if reference_dict_path else None
|
21 |
+
self.index_to_class_mapping_path = index_to_class_mapping_path.name if index_to_class_mapping_path else None
|
22 |
+
self.model_path = model_path.name if model_path else None
|
23 |
+
self.edgeface_model_name = edgeface_model_name
|
24 |
+
self.edgeface_model_dir = edgeface_model_dir
|
25 |
+
self.algorithm = algorithm
|
26 |
+
self.accelerator = accelerator
|
27 |
+
self.resolution = resolution
|
28 |
+
self.similarity_threshold = similarity_threshold
|
29 |
+
|
30 |
+
args = Args()
|
31 |
+
|
32 |
+
# Validate inputs
|
33 |
+
if not all([args.reference_dict_path, args.index_to_class_mapping_path, args.model_path]):
|
34 |
+
return "Error: Please provide all required files (reference dict, index-to-class mapping, and model)."
|
35 |
+
|
36 |
+
try:
|
37 |
+
# Call the inference function from end2end_inference.py
|
38 |
+
results = cinference_and_confirm(args)
|
39 |
+
|
40 |
+
# Format output
|
41 |
+
output = ""
|
42 |
+
for result in results:
|
43 |
+
output += f"Image: {result['image_path']}\n"
|
44 |
+
output += f"Predicted Class: {result['predicted_class']}\n"
|
45 |
+
output += f"Confidence: {result['confidence']:.4f}\n"
|
46 |
+
output += f"Similarity: {result.get('similarity', 'N/A'):.4f}\n"
|
47 |
+
output += f"Confirmed: {result.get('confirmed', 'N/A')}\n\n"
|
48 |
+
|
49 |
+
return output
|
50 |
+
|
51 |
+
except Exception as e:
|
52 |
+
return f"Error: {str(e)}"
|
53 |
+
|
54 |
+
finally:
|
55 |
+
# Clean up temporary image
|
56 |
+
if os.path.exists(temp_image_path):
|
57 |
+
os.remove(temp_image_path)
|