coutant commited on
Commit
0316c1c
1 Parent(s): 83502d7

simple person detection with Yolov8.

Browse files
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .idea
2
+ *.pt
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import gradio as gr
3
+ from ultralytics import YOLO
4
+
5
+ def inference(path:str, threshold:float=0.6):
6
+ print("trying inference with path", path)
7
+ if path is None:
8
+ return None,0
9
+ model = YOLO('yolov8m.pt')
10
+
11
+ model.classes = [0] # only considering class 'person' and not the 79 other classes...
12
+ image = cv2.imread(path)
13
+ outputs = model.predict(source=path, return_outputs=True)
14
+ for output in outputs: # mono item batch
15
+ detections = output['det']
16
+ counter=0
17
+ for detection in detections:
18
+ if detection[4]<threshold:
19
+ break
20
+ cv2.rectangle(
21
+ image,
22
+ (int(detection[0]), int(detection[1])),
23
+ (int(detection[2]), int(detection[3])),
24
+ color=(0, 0, 255),
25
+ thickness=2,
26
+ )
27
+ counter+=1
28
+ return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), counter
29
+
30
+ gr.Interface(
31
+ fn = inference,
32
+ inputs = [ gr.components.Image(type="filepath", label="Input"), gr.Slider(minimum=0.5, maximum=0.9, step=0.05, value=0.7, label="Confidence threshold") ],
33
+ outputs = [ gr.components.Image(type="numpy", label="Output"), gr.Label(label="nb of persons detected for given confidence threshold") ],
34
+ title="Person detection with YOLO v8",
35
+ description="Person detection, you can tweak the corresponding confidence threshold. Good results even when face not visible.",
36
+ examples=[ ['data/businessmen-612.jpg'], ['data/businessmen-back.jpg']],
37
+ allow_flagging="never"
38
+ ).launch(debug=True, enable_queue=True)
data/businessmen-612.jpg ADDED
data/businessmen-back.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch
2
+ torchvision
3
+ ultralytics
4
+ gradio
5
+ opencv-python
6
+ numpy