cvdetectors commited on
Commit
f317b92
·
verified ·
1 Parent(s): 0a418af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -42
app.py CHANGED
@@ -1,55 +1,54 @@
1
- # app.py
2
-
3
  import gradio as gr
4
- import torch
5
  from ultralytics import YOLO
6
- from PIL import Image
 
7
 
8
- def load_model():
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
- model, device = load_model()
 
18
 
19
- def count_persons(image: Image.Image) -> str:
20
  """
21
- Run inference on the input image, apply TTA, filter for class 0 (person),
22
- and return the total count.
23
  """
24
- # Perform prediction with augmentation (TTA), limit detections, and only class 0
25
- results = model.predict(
26
- source=image,
27
- conf=0.6, # Confidence threshold
28
- imgsz=640, # Inference resolution
29
- augment=True, # Test Time Augmentation :contentReference[oaicite:4]{index=4}
30
- max_det=300, # Cap detections for crowded scenes
31
- classes=[0] # Only detect persons (class 0) :contentReference[oaicite:5]{index=5}
32
- )
33
-
34
- # Sum counts across all results (usually one per image)
35
- total = sum(len(r.boxes) for r in results)
36
- return f"Persons detected: {total}"
 
 
 
 
 
 
 
 
 
37
 
38
- # Build Gradio interface
39
  demo = gr.Interface(
40
- fn=count_persons,
41
  inputs=gr.Image(type="pil", label="Upload Image"),
42
- outputs=gr.Text(label="Person Count"),
43
- title="Advanced Person Counter with YOLOv8",
44
- description=(
45
- "Upload an image to count people using a state‑of‑the‑art "
46
- "YOLOv8 segmentation model with Test‑Time Augmentation."
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() # Launch locally; add `share=True` for a public link
 
 
 
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()