Update utils/object_detection_brainai.py
Browse files
utils/object_detection_brainai.py
CHANGED
|
@@ -1,11 +1,9 @@
|
|
| 1 |
-
from ultralytics import YOLO
|
| 2 |
import cv2
|
|
|
|
| 3 |
|
| 4 |
-
class ObjectDetectionModel():
|
| 5 |
-
|
| 6 |
def __init__(self):
|
| 7 |
self.model = YOLO("models/yolov8n_openvino_model", task = "detect")
|
| 8 |
-
self.class_names = self.model.names
|
| 9 |
|
| 10 |
def process(self, img):
|
| 11 |
result = self.model(img)
|
|
@@ -13,33 +11,29 @@ class ObjectDetectionModel():
|
|
| 13 |
|
| 14 |
return img_plot
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
if not ret:
|
| 27 |
break
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 36 |
-
out_path = 'output_video.mp4'
|
| 37 |
-
out = cv2.VideoWriter(out_path, fourcc, fps, (frame_width, frame_height))
|
| 38 |
-
|
| 39 |
-
for frame in frames:
|
| 40 |
-
out.write(frame)
|
| 41 |
-
|
| 42 |
out.release()
|
| 43 |
-
return out_path
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
|
| 4 |
+
class ObjectDetectionModel():
|
|
|
|
| 5 |
def __init__(self):
|
| 6 |
self.model = YOLO("models/yolov8n_openvino_model", task = "detect")
|
|
|
|
| 7 |
|
| 8 |
def process(self, img):
|
| 9 |
result = self.model(img)
|
|
|
|
| 11 |
|
| 12 |
return img_plot
|
| 13 |
|
| 14 |
+
def play_video(self, video_path):
|
| 15 |
+
camera = cv2.VideoCapture(video_path)
|
| 16 |
+
frame_width = int(camera.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 17 |
+
frame_height = int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 18 |
+
fps = camera.get(cv2.CAP_PROP_FPS)
|
| 19 |
+
|
| 20 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 21 |
+
output_video_path = 'output_video.mp4'
|
| 22 |
+
out = cv2.VideoWriter(output_video_path,fourcc,fps,
|
| 23 |
+
(frame_width,frame_height))
|
| 24 |
+
processed_frames = []
|
| 25 |
+
|
| 26 |
+
while(True):
|
| 27 |
+
ret, frame = camera.read()
|
| 28 |
if not ret:
|
| 29 |
break
|
| 30 |
|
| 31 |
+
result = self.model(frame, verbose=False)
|
| 32 |
+
img_plot = result[0].plot()
|
| 33 |
+
processed_frames.append(img_plot)
|
| 34 |
+
camera.release()
|
| 35 |
+
for frame in processed_frames:
|
| 36 |
+
out.write(frame)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
out.release()
|
|
|
|
| 38 |
|
| 39 |
+
return output_video_path
|