sakshi116 commited on
Commit
ad58fa6
·
verified ·
1 Parent(s): d9762ad

Update app.py

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