Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,60 @@
|
|
| 1 |
-
|
| 2 |
-
from ultralytics import YOLO
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
try:
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
except ImportError:
|
| 4 |
+
import os
|
| 5 |
+
os.system("pip install ultralytics==8.3.15")
|
| 6 |
+
from ultralytics import YOLO
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
import gradio as gr
|
| 10 |
+
from ultralytics import YOLO
|
| 11 |
+
from PIL import Image
|
| 12 |
+
import numpy as np
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Load YOLOv8 face model (make sure it's in the same directory)
|
| 16 |
+
model = YOLO("yolov8n-face.pt")
|
| 17 |
+
|
| 18 |
+
# Image detection
|
| 19 |
+
def detect_faces(image, conf):
|
| 20 |
+
results = model.predict(source=image, conf=conf)
|
| 21 |
+
result_img = results[0].plot()
|
| 22 |
+
return Image.fromarray(result_img)
|
| 23 |
+
|
| 24 |
+
# Webcam detection
|
| 25 |
+
def detect_faces_live(frame, conf):
|
| 26 |
+
results = model.predict(source=frame, conf=conf)
|
| 27 |
+
result_frame = results[0].plot()
|
| 28 |
+
return Image.fromarray(result_frame)
|
| 29 |
+
|
| 30 |
+
# Gradio interface
|
| 31 |
+
title = "👤 Real-Time Face Detection with YOLOv8"
|
| 32 |
+
description = """
|
| 33 |
+
Upload an image or use your webcam to detect faces in real-time.
|
| 34 |
+
Powered by **Ultralytics YOLOv8** and **Gradio**.
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
with gr.Blocks(title=title) as app:
|
| 38 |
+
gr.Markdown(f"# {title}")
|
| 39 |
+
gr.Markdown(description)
|
| 40 |
+
|
| 41 |
+
conf_slider = gr.Slider(0.1, 1.0, value=0.5, label="Confidence Threshold")
|
| 42 |
+
|
| 43 |
+
with gr.Tab("📸 Image Upload"):
|
| 44 |
+
gr.Interface(
|
| 45 |
+
fn=detect_faces,
|
| 46 |
+
inputs=[gr.Image(type="pil", label="Upload Image"), conf_slider],
|
| 47 |
+
outputs=gr.Image(label="Detected Faces"),
|
| 48 |
+
live=False
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
with gr.Tab("🎥 Live Webcam"):
|
| 52 |
+
gr.Interface(
|
| 53 |
+
fn=detect_faces_live,
|
| 54 |
+
inputs=[gr.Image(type="numpy", streaming=True), conf_slider],
|
| 55 |
+
outputs=gr.Image(label="Detected Faces"),
|
| 56 |
+
live=True
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
app.launch()
|