nguyenp99 commited on
Commit
d19b3e3
1 Parent(s): 6ad7442

Create model.py

Browse files
Files changed (1) hide show
  1. model.py +22 -0
model.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import torch
3
+ import plasma.functional as f
4
+ from ultralytics import YOLO
5
+
6
+
7
+ class YOLORunner(f.Pipe):
8
+
9
+ def __init__(self, model: YOLO, image_size, conf_thrs, height_ratio, device, verbose):
10
+ super().__init__(image_size=image_size, conf_thrs=conf_thrs, device=device, height_ratio=height_ratio, verbose=verbose)
11
+
12
+ self.model = model
13
+
14
+ def run(self, image):
15
+
16
+ results = self.model.predict(source=image, imgsz=self.image_size, conf=self.conf_thrs, verbose=self.verbose)
17
+ results = results[0].boxes.xyxy.cpu().numpy().astype(int)
18
+ # expand the height of the boxes
19
+ height = results[:, 3] - results[:, 1]
20
+ results[:, 1] = (results[:, 1] - height * self.height_ratio).clip(0)
21
+ results[:, 3] = (results[:, 3] + height * self.height_ratio).clip(0, image.shape[0])
22
+ return results