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Dict, Any
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
from utils import draw_detections, process_image, load_detection_models
|
| 10 |
+
from models import detect_faces, detect_objects
|
| 11 |
+
|
| 12 |
+
# Load models at startup
|
| 13 |
+
face_cascade, object_net, object_classes = load_detection_models()
|
| 14 |
+
|
| 15 |
+
def recognize_face_and_objects(
|
| 16 |
+
image: np.ndarray,
|
| 17 |
+
enable_face_detection: bool,
|
| 18 |
+
enable_object_detection: bool,
|
| 19 |
+
face_confidence: float,
|
| 20 |
+
object_confidence: float,
|
| 21 |
+
draw_boxes: bool,
|
| 22 |
+
show_labels: bool,
|
| 23 |
+
box_color: str
|
| 24 |
+
) -> Tuple[np.ndarray, str, str]:
|
| 25 |
+
"""
|
| 26 |
+
Perform face and object detection on the input image.
|
| 27 |
+
|
| 28 |
+
Args:
|
| 29 |
+
image: Input image as numpy array
|
| 30 |
+
enable_face_detection: Whether to detect faces
|
| 31 |
+
enable_object_detection: Whether to detect objects
|
| 32 |
+
face_confidence: Confidence threshold for face detection
|
| 33 |
+
object_confidence: Confidence threshold for object detection
|
| 34 |
+
draw_boxes: Whether to draw bounding boxes
|
| 35 |
+
show_labels: Whether to show labels on detections
|
| 36 |
+
box_color: Color for bounding boxes
|
| 37 |
+
|
| 38 |
+
Returns:
|
| 39 |
+
Tuple of (processed_image, face_results_json, object_results_json)
|
| 40 |
+
"""
|
| 41 |
+
if image is None:
|
| 42 |
+
return None, "No image provided", "No image provided"
|
| 43 |
+
|
| 44 |
+
# Convert PIL to numpy if needed
|
| 45 |
+
if isinstance(image, Image.Image):
|
| 46 |
+
image = np.array(image)
|
| 47 |
+
|
| 48 |
+
# Process image
|
| 49 |
+
processed_image, face_results, object_results = process_image(
|
| 50 |
+
image,
|
| 51 |
+
face_cascade,
|
| 52 |
+
object_net,
|
| 53 |
+
object_classes,
|
| 54 |
+
enable_face_detection,
|
| 55 |
+
enable_object_detection,
|
| 56 |
+
face_confidence,
|
| 57 |
+
object_confidence
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Draw detections if requested
|
| 61 |
+
if draw_boxes:
|
| 62 |
+
processed_image = draw_detections(
|
| 63 |
+
processed_image.copy(),
|
| 64 |
+
face_results,
|
| 65 |
+
object_results,
|
| 66 |
+
show_labels,
|
| 67 |
+
box_color
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# Convert results to JSON
|
| 71 |
+
face_json = json.dumps(face_results, indent=2) if face_results else "No faces detected"
|
| 72 |
+
object_json = json.dumps(object_results, indent=2) if object_results else "No objects detected"
|
| 73 |
+
|
| 74 |
+
return processed_image, face_json, object_json
|
| 75 |
+
|
| 76 |
+
def webcam_recognition(
|
| 77 |
+
image: np.ndarray,
|
| 78 |
+
enable_face_detection: bool,
|
| 79 |
+
enable_object_detection: bool,
|
| 80 |
+
face_confidence: float,
|
| 81 |
+
object_confidence: float,
|
| 82 |
+
draw_boxes: bool,
|
| 83 |
+
show_labels: bool,
|
| 84 |
+
box_color: str
|
| 85 |
+
) -> np.ndarray:
|
| 86 |
+
"""Real-time webcam recognition."""
|
| 87 |
+
if image is None:
|
| 88 |
+
return None
|
| 89 |
+
|
| 90 |
+
processed_image, _, _ = recognize_face_and_objects(
|
| 91 |
+
image,
|
| 92 |
+
enable_face_detection,
|
| 93 |
+
enable_object_detection,
|
| 94 |
+
face_confidence,
|
| 95 |
+
object_confidence,
|
| 96 |
+
draw_boxes,
|
| 97 |
+
show_labels,
|
| 98 |
+
box_color
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
return processed_image
|
| 102 |
+
|
| 103 |
+
def get_detection_statistics() -> str:
|
| 104 |
+
"""Get information about available detection models."""
|
| 105 |
+
stats = {
|
| 106 |
+
"face_detection": {
|
| 107 |
+
"model": "Haar Cascade",
|
| 108 |
+
"features": ["Face detection", "Eye detection", "Smile detection"],
|
| 109 |
+
"speed": "Fast",
|
| 110 |
+
"accuracy": "Medium"
|
| 111 |
+
},
|
| 112 |
+
"object_detection": {
|
| 113 |
+
"model": "OpenCV DNN with MobileNet-SSD",
|
| 114 |
+
"classes": len(object_classes) if object_classes else 0,
|
| 115 |
+
"input_size": "300x300",
|
| 116 |
+
"speed": "Real-time capable",
|
| 117 |
+
"accuracy": "High"
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
return json.dumps(stats, indent=2)
|
| 121 |
+
|
| 122 |
+
# Create custom CSS for better styling
|
| 123 |
+
custom_css = """
|
| 124 |
+
.main-container {
|
| 125 |
+
max-width: 1400px;
|
| 126 |
+
margin: 0 auto;
|
| 127 |
+
}
|
| 128 |
+
.settings-panel {
|
| 129 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 130 |
+
border-radius: 10px;
|
| 131 |
+
padding: 20px;
|
| 132 |
+
}
|
| 133 |
+
.result-panel {
|
| 134 |
+
border: 2px solid #e0e0e0;
|
| 135 |
+
border-radius: 10px;
|
| 136 |
+
padding: 15px;
|
| 137 |
+
}
|
| 138 |
+
.image-container {
|
| 139 |
+
border: 1px solid #ddd;
|
| 140 |
+
border-radius: 8px;
|
| 141 |
+
overflow: hidden;
|
| 142 |
+
}
|
| 143 |
+
"""
|
| 144 |
+
|
| 145 |
+
with gr.Blocks(css=custom_css, title="Face & Object Recognition Platform") as demo:
|
| 146 |
+
gr.Markdown("""
|
| 147 |
+
# π Face & Object Recognition Platform
|
| 148 |
+
Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
|
| 149 |
+
|
| 150 |
+
Advanced computer vision platform for real-time face and object detection with customizable settings.
|
| 151 |
+
""")
|
| 152 |
+
|
| 153 |
+
with gr.Row():
|
| 154 |
+
with gr.Column(scale=2):
|
| 155 |
+
gr.Markdown("### π€ Input Source")
|
| 156 |
+
with gr.Tabs():
|
| 157 |
+
with gr.TabItem("Upload Image"):
|
| 158 |
+
input_image = gr.Image(
|
| 159 |
+
label="Upload an image for analysis",
|
| 160 |
+
type="numpy",
|
| 161 |
+
height=400
|
| 162 |
+
)
|
| 163 |
+
analyze_btn = gr.Button("π Analyze Image", variant="primary", size="lg")
|
| 164 |
+
|
| 165 |
+
with gr.TabItem("Webcam"):
|
| 166 |
+
webcam_image = gr.Image(
|
| 167 |
+
label="Webcam Feed",
|
| 168 |
+
sources="webcam",
|
| 169 |
+
type="numpy",
|
| 170 |
+
streaming=True,
|
| 171 |
+
height=400
|
| 172 |
+
)
|
| 173 |
+
gr.Markdown("*Webcam provides real-time detection (may have slight delay)*")
|
| 174 |
+
|
| 175 |
+
with gr.Column(scale=1):
|
| 176 |
+
gr.Markdown("### βοΈ Detection Settings")
|
| 177 |
+
with gr.Group(elem_classes=["settings-panel"]):
|
| 178 |
+
gr.Markdown("#### Detection Modes")
|
| 179 |
+
enable_face = gr.Checkbox(label="π€ Enable Face Detection", value=True)
|
| 180 |
+
enable_objects = gr.Checkbox(label="π¦ Enable Object Detection", value=True)
|
| 181 |
+
|
| 182 |
+
gr.Markdown("#### Confidence Thresholds")
|
| 183 |
+
face_conf = gr.Slider(
|
| 184 |
+
label="Face Detection Confidence",
|
| 185 |
+
minimum=0.1,
|
| 186 |
+
maximum=1.0,
|
| 187 |
+
value=0.7,
|
| 188 |
+
step=0.1,
|
| 189 |
+
info="Lower values detect more faces"
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
object_conf = gr.Slider(
|
| 193 |
+
label="Object Detection Confidence",
|
| 194 |
+
minimum=0.1,
|
| 195 |
+
maximum=1.0,
|
| 196 |
+
value=0.5,
|
| 197 |
+
step=0.1,
|
| 198 |
+
info="Lower values detect more objects"
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
gr.Markdown("#### Display Options")
|
| 202 |
+
draw_boxes = gr.Checkbox(label="π Draw Bounding Boxes", value=True)
|
| 203 |
+
show_labels = gr.Checkbox(label="π·οΈ Show Labels", value=True)
|
| 204 |
+
box_color = gr.Dropdown(
|
| 205 |
+
label="Box Color",
|
| 206 |
+
choices=["red", "green", "blue", "yellow", "purple", "orange"],
|
| 207 |
+
value="red"
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
with gr.Row():
|
| 211 |
+
with gr.Column():
|
| 212 |
+
gr.Markdown("### πΌοΈ Detection Results")
|
| 213 |
+
output_image = gr.Image(
|
| 214 |
+
label="Processed Image with Detections",
|
| 215 |
+
type="numpy",
|
| 216 |
+
height=400,
|
| 217 |
+
elem_classes=["image-container"]
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
with gr.Column():
|
| 221 |
+
with gr.Tabs():
|
| 222 |
+
with gr.TabItem("π€ Face Results"):
|
| 223 |
+
face_results = gr.JSON(
|
| 224 |
+
label="Face Detection Data",
|
| 225 |
+
elem_classes=["result-panel"]
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
with gr.TabItem("π¦ Object Results"):
|
| 229 |
+
object_results = gr.JSON(
|
| 230 |
+
label="Object Detection Data",
|
| 231 |
+
elem_classes=["result-panel"]
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
with gr.TabItem("βΉοΈ Model Info"):
|
| 235 |
+
model_info = gr.JSON(
|
| 236 |
+
label="Detection Models Information",
|
| 237 |
+
value=json.loads(get_detection_statistics()),
|
| 238 |
+
elem_classes=["result-panel"]
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
# Event handlers
|
| 242 |
+
analyze_btn.click(
|
| 243 |
+
fn=recognize_face_and_objects,
|
| 244 |
+
inputs=[
|
| 245 |
+
input_image,
|
| 246 |
+
enable_face,
|
| 247 |
+
enable_objects,
|
| 248 |
+
face_conf,
|
| 249 |
+
object_conf,
|
| 250 |
+
draw_boxes,
|
| 251 |
+
show_labels,
|
| 252 |
+
box_color
|
| 253 |
+
],
|
| 254 |
+
outputs=[output_image, face_results, object_results]
|
| 255 |
+
)
|
| 256 |
+
|
| 257 |
+
# Real-time webcam processing
|
| 258 |
+
webcam_image.stream(
|
| 259 |
+
fn=webcam_recognition,
|
| 260 |
+
inputs=[
|
| 261 |
+
webcam_image,
|
| 262 |
+
enable_face,
|
| 263 |
+
enable_objects,
|
| 264 |
+
face_conf,
|
| 265 |
+
object_conf,
|
| 266 |
+
draw_boxes,
|
| 267 |
+
show_labels,
|
| 268 |
+
box_color
|
| 269 |
+
],
|
| 270 |
+
outputs=[output_image],
|
| 271 |
+
time_limit=30,
|
| 272 |
+
stream_every=0.5
|
| 273 |
+
)
|
| 274 |
+
|
| 275 |
+
# Examples
|
| 276 |
+
gr.Examples(
|
| 277 |
+
examples=[
|
| 278 |
+
# These would need actual image files, for now using placeholder
|
| 279 |
+
["example1.jpg", True, True, 0.7, 0.5, True, True, "red"],
|
| 280 |
+
["example2.jpg", False, True, 0.8, 0.6, True, True, "blue"],
|
| 281 |
+
["example3.jpg", True, False, 0.6, 0.4, True, False, "green"],
|
| 282 |
+
],
|
| 283 |
+
inputs=[
|
| 284 |
+
input_image,
|
| 285 |
+
enable_face,
|
| 286 |
+
enable_objects,
|
| 287 |
+
face_conf,
|
| 288 |
+
object_conf,
|
| 289 |
+
draw_boxes,
|
| 290 |
+
show_labels,
|
| 291 |
+
box_color
|
| 292 |
+
],
|
| 293 |
+
outputs=[output_image, face_results, object_results],
|
| 294 |
+
cache_examples=False
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
gr.Markdown("""
|
| 298 |
+
---
|
| 299 |
+
### π Usage Instructions
|
| 300 |
+
1. **Upload Image**: Select an image from your device for analysis
|
| 301 |
+
2. **Webcam**: Use your webcam for real-time detection
|
| 302 |
+
3. **Adjust Settings**: Customize confidence thresholds and display options
|
| 303 |
+
4. **View Results**: See detections overlayed on the image with detailed JSON data
|
| 304 |
+
|
| 305 |
+
### π― Features
|
| 306 |
+
- **Face Detection**: Identifies faces in images using Haar Cascade classifiers
|
| 307 |
+
- **Object Detection**: Recognizes 80+ object classes using MobileNet-SSD
|
| 308 |
+
- **Real-time Processing**: Webcam support with live detection
|
| 309 |
+
- **Customizable**: Adjustable confidence thresholds and visual settings
|
| 310 |
+
- **Detailed Output**: JSON formatted results with coordinates and confidence scores
|
| 311 |
+
""")
|
| 312 |
+
|
| 313 |
+
if __name__ == "__main__":
|
| 314 |
+
demo.launch(share=True, debug=True)
|