Spaces:
Runtime error
Runtime error
| import cv2 | |
| from ultralytics import YOLO | |
| class ObjectDetectionModel(): | |
| def __init__(self): | |
| self.model = YOLO("models/yolov8n_openvino_model", task = "detect") | |
| def process(self, img): | |
| result = self.model(img) | |
| img_plot = result[0].plot() | |
| return img_plot | |
| def play_video(self, video_path): | |
| camera = cv2.VideoCapture(video_path) | |
| frame_width = int(camera.get(cv2.CAP_PROP_FRAME_WIDTH)) | |
| frame_height = int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |
| fps = camera.get(cv2.CAP_PROP_FPS) | |
| fourcc = cv2.VideoWriter_fourcc(*'mp4v') | |
| output_video_path = 'output_video.mp4' | |
| out = cv2.VideoWriter(output_video_path,fourcc,fps, (frame_width,frame_height)) | |
| processed_frames = [] | |
| while(True): | |
| ret, frame = camera.read() | |
| if not ret: | |
| break | |
| result = self.model(frame, verbose=False) | |
| img_plot = result[0].plot() | |
| processed_frames.append(img_plot) | |
| camera.release() | |
| for frame in processed_frames: | |
| out.write(frame) | |
| out.release() | |
| return output_video_path | |