coutant commited on
Commit
17994b0
1 Parent(s): 0316c1c

Adapting to Ultralyrics 8.0.43+ changed API

Browse files
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -6,21 +6,24 @@ 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
  )
@@ -32,7 +35,7 @@ gr.Interface(
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)
 
6
  print("trying inference with path", path)
7
  if path is None:
8
  return None,0
9
+ model = YOLO('yolov8m.pt') # Caution new API since Ultralytics 8.0.43
10
+ # [0] # only considering class 'person' and not the 79 other classes...
11
+ outputs = model.predict(source=path, classes= [0], show=False, conf=threshold) # new API with Ultralytics 8.0.43. Accepts 'show', 'classes', 'stream', 'conf' (default is 0.25)
12
  image = cv2.imread(path)
13
+ counter = 0
14
  for output in outputs: # mono item batch
15
+ conf = output.boxes.conf # the tensor of detection confidences
16
+ xyxy = output.boxes.xyxy
17
+ cls = output.boxes.cls # 0 is 'person' and 5 is 'bus' 16 is dog
18
+ nb=cls.size(dim=0)
19
+ for i in range(nb):
20
+ box = xyxy[i]
21
+ if conf[i]<threshold:
22
  break
23
  cv2.rectangle(
24
  image,
25
+ (int(box[0]), int(box[1])),
26
+ (int(box[2]), int(box[3])),
27
  color=(0, 0, 255),
28
  thickness=2,
29
  )
 
35
  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") ],
36
  outputs = [ gr.components.Image(type="numpy", label="Output"), gr.Label(label="nb of persons detected for given confidence threshold") ],
37
  title="Person detection with YOLO v8",
38
+ description="Person detection, you can tweak the corresponding confidence threshold. Good results even when face not visible. New API since Ultralytics 8.0.43.",
39
  examples=[ ['data/businessmen-612.jpg'], ['data/businessmen-back.jpg']],
40
  allow_flagging="never"
41
  ).launch(debug=True, enable_queue=True)