Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,71 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import
|
| 4 |
import numpy as np
|
| 5 |
from utils import load_model, preprocess, verify_identity
|
| 6 |
|
|
|
|
| 7 |
model = load_model("siamese_model.h5")
|
| 8 |
registration_dir = "registered_users"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def register_user(name, image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
user_path = os.path.join(registration_dir, name)
|
| 12 |
os.makedirs(user_path, exist_ok=True)
|
| 13 |
image.save(os.path.join(user_path, f"{name}_1.jpg"))
|
| 14 |
-
return f"User {name} registered successfully."
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
with gr.Blocks() as demo:
|
| 21 |
-
gr.Markdown("## 🧠 Siamese Face Verification")
|
| 22 |
-
|
|
|
|
| 23 |
name = gr.Textbox(label="Enter Your Name")
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
with gr.Tab("Verify"):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
from utils import load_model, preprocess, verify_identity
|
| 6 |
|
| 7 |
+
# Load the Siamese model
|
| 8 |
model = load_model("siamese_model.h5")
|
| 9 |
registration_dir = "registered_users"
|
| 10 |
+
os.makedirs(registration_dir, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
# Registration function
|
| 13 |
+
from PIL import Image
|
| 14 |
|
| 15 |
def register_user(name, image):
|
| 16 |
+
if image is None or not name.strip():
|
| 17 |
+
return "❌ Please provide a name and photo."
|
| 18 |
+
|
| 19 |
+
# Convert NumPy array to PIL Image
|
| 20 |
+
image = Image.fromarray(image.astype("uint8"))
|
| 21 |
+
|
| 22 |
user_path = os.path.join(registration_dir, name)
|
| 23 |
os.makedirs(user_path, exist_ok=True)
|
| 24 |
image.save(os.path.join(user_path, f"{name}_1.jpg"))
|
| 25 |
+
return f"✅ User '{name}' registered successfully."
|
| 26 |
|
| 27 |
+
# Real-time processing function for live webcam stream
|
| 28 |
+
def process_image(frame):
|
| 29 |
+
if frame is None:
|
| 30 |
+
return None, "No frame captured."
|
| 31 |
|
| 32 |
+
result = verify_identity(model, frame, registration_dir)
|
| 33 |
+
return frame, result
|
| 34 |
+
|
| 35 |
+
# Gradio UI
|
| 36 |
with gr.Blocks() as demo:
|
| 37 |
+
gr.Markdown("## 🧠 Real-Time Siamese Face Verification")
|
| 38 |
+
|
| 39 |
+
with gr.Tab("👤 Register"):
|
| 40 |
name = gr.Textbox(label="Enter Your Name")
|
| 41 |
+
image = gr.Image(label="Capture Image")
|
| 42 |
+
register_btn = gr.Button("Register")
|
| 43 |
+
register_out = gr.Textbox(label="Status")
|
| 44 |
+
register_btn.click(fn=register_user, inputs=[name, image], outputs=register_out)
|
| 45 |
+
|
| 46 |
+
with gr.Tab("🔍 Live Verify"):
|
| 47 |
+
with gr.Row():
|
| 48 |
+
input_img = gr.Image(label="📸 Webcam", streaming=True, sources=["webcam"])
|
| 49 |
+
output_img = gr.Image(label="🧠 Output Frame")
|
| 50 |
+
result_text = gr.Textbox(label="Prediction", interactive=False)
|
| 51 |
+
|
| 52 |
+
input_img.stream(fn=process_image, inputs=input_img, outputs=[output_img, result_text])
|
| 53 |
+
|
| 54 |
+
import threading
|
| 55 |
+
import time
|
| 56 |
+
import shutil
|
| 57 |
+
|
| 58 |
+
def auto_cleanup(interval=600): # 600 seconds = 10 minutes
|
| 59 |
+
while True:
|
| 60 |
+
if os.path.exists(registration_dir):
|
| 61 |
+
shutil.rmtree(registration_dir)
|
| 62 |
+
os.makedirs(registration_dir)
|
| 63 |
+
print("🧹 Cleaned up registered_users/")
|
| 64 |
+
time.sleep(interval)
|
| 65 |
+
|
| 66 |
+
# Start cleanup thread
|
| 67 |
+
threading.Thread(target=auto_cleanup, daemon=True).start()
|
| 68 |
|
| 69 |
+
# Launch app
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
demo.launch()
|