Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,54 @@
|
|
1 |
-
# app.py
|
2 |
-
|
3 |
import gradio as gr
|
4 |
-
import
|
5 |
from ultralytics import YOLO
|
6 |
-
from
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
Load the YOLOv8 segmentation model onto GPU (if available)
|
11 |
-
with mixed‑precision enabled.
|
12 |
-
"""
|
13 |
-
device = "cuda" if torch.cuda.is_available() else "cpu" # GPU if available :contentReference[oaicite:2]{index=2}
|
14 |
-
model = YOLO('yolov8x-seg.pt').to(device) # Segmentation variant for finer masks :contentReference[oaicite:3]{index=3}
|
15 |
-
return model, device
|
16 |
|
17 |
-
|
|
|
18 |
|
19 |
-
def
|
20 |
"""
|
21 |
-
|
22 |
-
|
23 |
"""
|
24 |
-
#
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
#
|
39 |
demo = gr.Interface(
|
40 |
-
fn=
|
41 |
inputs=gr.Image(type="pil", label="Upload Image"),
|
42 |
-
outputs=
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
examples=[ # optional: add example images if you like
|
49 |
-
# ["examples/crowd1.jpg"],
|
50 |
-
# ["examples/street_scene.jpg"],
|
51 |
-
]
|
52 |
)
|
53 |
|
54 |
if __name__ == "__main__":
|
55 |
-
demo.launch()
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import hf_hub_download
|
3 |
from ultralytics import YOLO
|
4 |
+
from supervision import Detections
|
5 |
+
from PIL import Image, ImageDraw
|
6 |
|
7 |
+
# Download the YOLOv8 face detection model from Hugging Face Hub
|
8 |
+
model_path = hf_hub_download(repo_id="arnabdhar/YOLOv8-Face-Detection", filename="model.pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Load the YOLOv8 face detection model
|
11 |
+
model = YOLO(model_path)
|
12 |
|
13 |
+
def detect_faces(image: Image.Image):
|
14 |
"""
|
15 |
+
Detects faces in an input image using YOLOv8 face detection model and returns the annotated image
|
16 |
+
along with the number of faces detected.
|
17 |
"""
|
18 |
+
# Run inference on the input image
|
19 |
+
output = model(image)
|
20 |
+
|
21 |
+
# Convert YOLO output to Detections format using the supervision library
|
22 |
+
results = Detections.from_ultralytics(output[0])
|
23 |
+
|
24 |
+
# Extract bounding boxes; results.xyxy contains boxes in [x1, y1, x2, y2] format
|
25 |
+
boxes = results.xyxy # This is assumed to be a list-like structure of bounding boxes
|
26 |
+
|
27 |
+
# Create a copy of the input image for drawing
|
28 |
+
annotated_image = image.copy()
|
29 |
+
draw = ImageDraw.Draw(annotated_image)
|
30 |
+
|
31 |
+
# Draw a red bounding box for each detected face
|
32 |
+
for box in boxes:
|
33 |
+
x1, y1, x2, y2 = box # unpack the coordinates
|
34 |
+
draw.rectangle([x1, y1, x2, y2], outline="red", width=2)
|
35 |
+
|
36 |
+
# Count the number of faces detected
|
37 |
+
face_count = len(boxes)
|
38 |
+
|
39 |
+
return annotated_image, f"Number of faces detected: {face_count}"
|
40 |
|
41 |
+
# Create a Gradio interface for the face detection function
|
42 |
demo = gr.Interface(
|
43 |
+
fn=detect_faces,
|
44 |
inputs=gr.Image(type="pil", label="Upload Image"),
|
45 |
+
outputs=[
|
46 |
+
gr.Image(type="pil", label="Annotated Image"),
|
47 |
+
gr.Text(label="Face Count")
|
48 |
+
],
|
49 |
+
title="YOLOv8 Face Detector",
|
50 |
+
description="Upload an image to detect faces using a YOLOv8 face detection model. The detected faces will be highlighted with red bounding boxes."
|
|
|
|
|
|
|
|
|
51 |
)
|
52 |
|
53 |
if __name__ == "__main__":
|
54 |
+
demo.launch()
|