Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
-
from utils import
|
| 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 |
-
#
|
| 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 webcam photo."
|
| 18 |
|
| 19 |
-
image = Image.fromarray(image.astype("uint8"))
|
| 20 |
user_path = os.path.join(registration_dir, name)
|
| 21 |
os.makedirs(user_path, exist_ok=True)
|
| 22 |
image.save(os.path.join(user_path, f"{name}_1.jpg"))
|
| 23 |
return f"✅ User '{name}' registered successfully."
|
| 24 |
|
|
|
|
| 25 |
def delete_all_users():
|
| 26 |
if os.path.exists(registration_dir):
|
| 27 |
shutil.rmtree(registration_dir)
|
|
@@ -29,17 +29,16 @@ def delete_all_users():
|
|
| 29 |
return "✅ All registered users deleted."
|
| 30 |
return "⚠️ No users to delete."
|
| 31 |
|
| 32 |
-
#
|
| 33 |
def process_image(frame):
|
| 34 |
if frame is None:
|
| 35 |
return None, "No frame captured."
|
| 36 |
-
|
| 37 |
-
result = verify_identity(model, frame, registration_dir)
|
| 38 |
return frame, result
|
| 39 |
|
| 40 |
# Gradio UI
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
-
gr.Markdown("## 🧠 Real-Time
|
| 43 |
|
| 44 |
with gr.Tab("👤 Register"):
|
| 45 |
name = gr.Textbox(label="Enter Your Name")
|
|
@@ -58,14 +57,9 @@ with gr.Blocks() as demo:
|
|
| 58 |
input_img = gr.Image(label="📸 Webcam", streaming=True, sources=["webcam"])
|
| 59 |
output_img = gr.Image(label="🧠 Output Frame")
|
| 60 |
result_text = gr.Textbox(label="Prediction", interactive=False)
|
| 61 |
-
|
| 62 |
input_img.stream(fn=process_image, inputs=input_img, outputs=[output_img, result_text])
|
| 63 |
|
| 64 |
-
|
| 65 |
-
import time
|
| 66 |
-
import shutil
|
| 67 |
-
|
| 68 |
-
def auto_cleanup(interval=600): # 600 seconds = 10 minutes
|
| 69 |
while True:
|
| 70 |
if os.path.exists(registration_dir):
|
| 71 |
shutil.rmtree(registration_dir)
|
|
@@ -73,9 +67,7 @@ def auto_cleanup(interval=600): # 600 seconds = 10 minutes
|
|
| 73 |
print("🧹 Cleaned up registered_users/")
|
| 74 |
time.sleep(interval)
|
| 75 |
|
| 76 |
-
# Start cleanup thread
|
| 77 |
threading.Thread(target=auto_cleanup, daemon=True).start()
|
| 78 |
|
| 79 |
-
# Launch app
|
| 80 |
if __name__ == "__main__":
|
| 81 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import shutil
|
| 4 |
+
import threading
|
| 5 |
+
import time
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
| 8 |
+
from utils import verify_identity
|
| 9 |
|
|
|
|
|
|
|
| 10 |
registration_dir = "registered_users"
|
| 11 |
os.makedirs(registration_dir, exist_ok=True)
|
| 12 |
|
| 13 |
+
# Register user
|
|
|
|
|
|
|
| 14 |
def register_user(name, image):
|
| 15 |
if image is None or not name.strip():
|
| 16 |
return "❌ Please provide a name and webcam photo."
|
| 17 |
|
| 18 |
+
image = Image.fromarray(image.astype("uint8"))
|
| 19 |
user_path = os.path.join(registration_dir, name)
|
| 20 |
os.makedirs(user_path, exist_ok=True)
|
| 21 |
image.save(os.path.join(user_path, f"{name}_1.jpg"))
|
| 22 |
return f"✅ User '{name}' registered successfully."
|
| 23 |
|
| 24 |
+
# Delete all
|
| 25 |
def delete_all_users():
|
| 26 |
if os.path.exists(registration_dir):
|
| 27 |
shutil.rmtree(registration_dir)
|
|
|
|
| 29 |
return "✅ All registered users deleted."
|
| 30 |
return "⚠️ No users to delete."
|
| 31 |
|
| 32 |
+
# Webcam verify
|
| 33 |
def process_image(frame):
|
| 34 |
if frame is None:
|
| 35 |
return None, "No frame captured."
|
| 36 |
+
result = verify_identity(None, frame, registration_dir)
|
|
|
|
| 37 |
return frame, result
|
| 38 |
|
| 39 |
# Gradio UI
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("## 🧠 Real-Time Face Verification (FaceNet)")
|
| 42 |
|
| 43 |
with gr.Tab("👤 Register"):
|
| 44 |
name = gr.Textbox(label="Enter Your Name")
|
|
|
|
| 57 |
input_img = gr.Image(label="📸 Webcam", streaming=True, sources=["webcam"])
|
| 58 |
output_img = gr.Image(label="🧠 Output Frame")
|
| 59 |
result_text = gr.Textbox(label="Prediction", interactive=False)
|
|
|
|
| 60 |
input_img.stream(fn=process_image, inputs=input_img, outputs=[output_img, result_text])
|
| 61 |
|
| 62 |
+
def auto_cleanup(interval=600):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
while True:
|
| 64 |
if os.path.exists(registration_dir):
|
| 65 |
shutil.rmtree(registration_dir)
|
|
|
|
| 67 |
print("🧹 Cleaned up registered_users/")
|
| 68 |
time.sleep(interval)
|
| 69 |
|
|
|
|
| 70 |
threading.Thread(target=auto_cleanup, daemon=True).start()
|
| 71 |
|
|
|
|
| 72 |
if __name__ == "__main__":
|
| 73 |
demo.launch()
|