yahyaahmed commited on
Commit
4c1ef64
·
verified ·
1 Parent(s): 7fabab5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -3
app.py CHANGED
@@ -1,8 +1,42 @@
1
  import gradio as gr
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text", live=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  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
6
+ import numpy as np
7
+ import cv2
8
 
9
+ # Download and load the model
10
+ model_path = hf_hub_download(repo_id="arnabdhar/YOLOv8-Face-Detection", filename="model.pt")
11
+ model = YOLO(model_path)
12
 
13
+ # Define prediction function
14
+ def detect_faces(image):
15
+ # Run inference
16
+ output = model(image)
17
+ detections = Detections.from_ultralytics(output[0])
18
+
19
+ # Convert PIL image to OpenCV format
20
+ image_np = np.array(image)
21
+ image_cv = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
22
+
23
+ # Draw bounding boxes
24
+ for box in detections.xyxy:
25
+ x1, y1, x2, y2 = map(int, box)
26
+ cv2.rectangle(image_cv, (x1, y1), (x2, y2), (0, 255, 0), 2)
27
+
28
+ # Convert back to PIL image
29
+ result_image = Image.fromarray(cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB))
30
+ return result_image
31
+
32
+ # Gradio Interface
33
+ demo = gr.Interface(
34
+ fn=detect_faces,
35
+ inputs=gr.Image(type="pil", label="Upload Image"),
36
+ outputs=gr.Image(type="pil", label="Detected Faces"),
37
+ title="Face Detection with YOLOv8",
38
+ description="Drag and drop an image or click to upload. The model will detect faces using YOLOv8.",
39
+ live=False
40
+ )
41
 
42
  demo.launch()