Warlord-K commited on
Commit
7c37b2e
1 Parent(s): 3e29639

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ from face_recognition_system import FaceRecognitionSystem
5
+ import os
6
+ import tempfile
7
+
8
+ # Initialize the face recognition system
9
+ face_system = FaceRecognitionSystem()
10
+
11
+ def process_image(image, confidence_threshold=0.5, similarity_threshold=2.0):
12
+ """Process a single image and return the annotated result"""
13
+ # Convert from RGB (Gradio) to BGR (OpenCV)
14
+ image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
15
+
16
+ # Update thresholds
17
+ face_system.confidence_threshold = confidence_threshold
18
+ face_system.similarity_threshold = similarity_threshold
19
+
20
+ # Process the frame
21
+ processed_frame = face_system.process_frame(image_bgr)
22
+
23
+ # Convert back to RGB for display
24
+ return cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB)
25
+
26
+ def add_face(image, name):
27
+ """Add a new face to the database"""
28
+ if not name.strip():
29
+ return "Error: Please enter a name"
30
+
31
+ # Convert from RGB (Gradio) to BGR (OpenCV)
32
+ image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
33
+
34
+ if face_system.add_face_to_database(name, image_bgr):
35
+ return f"Successfully added {name} to database"
36
+ return "Failed to add face to database"
37
+
38
+ # Create the Gradio interface
39
+ with gr.Blocks(title="Face Recognition System") as demo:
40
+ gr.Markdown("# Face Recognition System")
41
+ gr.Markdown("Upload an image to detect and recognize faces, or add new faces to the database.")
42
+
43
+ with gr.Tab("Recognize Faces"):
44
+ with gr.Row():
45
+ with gr.Column():
46
+ input_image = gr.Image(label="Input Image", type="numpy")
47
+ confidence_slider = gr.Slider(
48
+ minimum=0.1,
49
+ maximum=1.0,
50
+ value=0.5,
51
+ step=0.1,
52
+ label="Confidence Threshold"
53
+ )
54
+ similarity_slider = gr.Slider(
55
+ minimum=0.5,
56
+ maximum=5.0,
57
+ value=2.0,
58
+ step=0.1,
59
+ label="Similarity Threshold"
60
+ )
61
+ detect_btn = gr.Button("Detect Faces")
62
+
63
+ with gr.Column():
64
+ output_image = gr.Image(label="Output Image")
65
+
66
+ with gr.Tab("Add New Face"):
67
+ with gr.Row():
68
+ with gr.Column():
69
+ new_face_image = gr.Image(label="Face Image", type="numpy")
70
+ name_input = gr.Textbox(label="Name")
71
+ add_btn = gr.Button("Add Face to Database")
72
+
73
+ with gr.Column():
74
+ result_text = gr.Textbox(label="Result")
75
+
76
+ # Set up event handlers
77
+ detect_btn.click(
78
+ fn=process_image,
79
+ inputs=[input_image, confidence_slider, similarity_slider],
80
+ outputs=output_image
81
+ )
82
+
83
+ add_btn.click(
84
+ fn=add_face,
85
+ inputs=[new_face_image, name_input],
86
+ outputs=result_text
87
+ )
88
+
89
+ # Launch the app
90
+ if __name__ == "__main__":
91
+ demo.launch()