Hi guys. I have this system where I do detection using yolov8 and classification using efficientnetB3. I test them on a video and I want to modify and save this video with all the bounding box indìformation, but I cannot visualize the video I save. What do I do wrong??
import torch
from tensorflow.keras.models import load_model
import cv2
from ultralytics import YOLO
import numpy as np
detector = YOLO('/myPath/best.pt')
classifier = load_model('/myPath/efficientnet_model_unfreeze_128.h5')
video_path = '/myPath/video_test.mp4'
cap = cv2.VideoCapture(video_path)
output_path = '/myPath/output_video_a.mp4'
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
results = detector(frame)
detections = results[0].boxes.xyxy.cpu().numpy()
for detection in results:
for box in detection.boxes:
x1, y1, x2, y2, conf, cls = box.data.tolist()[0]
roi = frame[int(y1):int(y2), int(x1):int(x2)]
roi_resized = cv2.resize(roi, (300, 300))
roi_resized = roi_resized / 255.0
roi_resized = roi_resized.reshape(1, 300, 300, 3)
pred = classifier.predict(roi_resized)
class_id = pred.argmax(axis=1)[0]
label = f'Class: {class_id}, Conf: {conf:.2f}'
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
cv2.putText(frame, label, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
out.write(frame)
cap.release()
out.release()
cv2.destroyAllWindows()