BrainAI-1 commited on
Commit
65767be
·
verified ·
1 Parent(s): 5bbbbe3

Update utils/object_detection_brainai.py

Browse files
Files changed (1) hide show
  1. utils/object_detection_brainai.py +23 -29
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
- def detect_objects_in_video(self, video_path, conf_thres=0.25, iou_thres=0.45):
18
- cap = cv2.VideoCapture(video_path)
19
- frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
20
- frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
21
- fps = cap.get(cv2.CAP_PROP_FPS)
22
- frames = []
23
-
24
- while cap.isOpened():
25
- ret, frame = cap.read()
 
 
 
 
26
  if not ret:
27
  break
28
 
29
- img_plot = self.process(frame)
30
- frames.append(img_plot)
31
- cap.release()
32
- return frames, fps, frame_width, frame_height
33
-
34
- def create_video(self, frames, fps, frame_width, frame_height):
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