Shekarss commited on
Commit
a8cdc05
·
verified ·
1 Parent(s): e81c0ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -18
app.py CHANGED
@@ -1,35 +1,71 @@
1
  import gradio as gr
2
  import os
3
- import cv2
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
- def verify(image):
17
- result = verify_identity(model, image, registration_dir)
18
- return result
 
19
 
 
 
 
 
20
  with gr.Blocks() as demo:
21
- gr.Markdown("## 🧠 Siamese Face Verification")
22
- with gr.Tab("Register"):
 
23
  name = gr.Textbox(label="Enter Your Name")
24
- reg_img = gr.Camera(label="Click Your Photo", show_label=False)
25
- reg_btn = gr.Button("Register")
26
- reg_output = gr.Textbox()
27
- reg_btn.click(fn=register_user, inputs=[name, reg_img], outputs=reg_output)
28
-
29
- with gr.Tab("Verify"):
30
- ver_img = gr.Camera(label="Click Your Photo", show_label=False)
31
- ver_btn = gr.Button("Verify")
32
- ver_output = gr.Textbox()
33
- ver_btn.click(fn=verify, inputs=ver_img, outputs=ver_output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- demo.launch()
 
 
 
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()