Hello Forum,
I am using the the code below to detect objects in a video. A .mp4 video is opened, and the trained detector runs on the video successfully. I can successfully write a video that is opened through ‘cap’ using the cv2.VideoWriter() function (not shown in code), however, I would like to save the output video (see variable ‘video’ in below code) that has overlaid bounding boxes as an .avi formatted video.
import pathlib
from PIL import Image
from matplotlib import pyplot as plt
cap = cv2.VideoCapture('FILEPATH/')
while True:
try:
ret, image = cap.read()
image_np_expanded = np.expand_dims(image, axis=0)
input_tensor = tf.convert_to_tensor(np.expand_dims(image, 0), dtype=tf.float32)
detections, predictions_dict, shapes = detect_fn(input_tensor)
label_id_offset = 1
image_np_with_detections = image.copy()
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections,
detections['detection_boxes'][0].numpy(),
(detections['detection_classes'][0].numpy() + label_id_offset).astype(int),
detections['detection_scores'][0].numpy(),
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=50,
min_score_thresh=.50,
agnostic_mode=False)
video = cv2.imshow('objdet', cv2.resize(image_np_with_detections, (400, 300)))
if cv2.waitKey(1) & 0xFF == ord('q'):
cap.release()
cv2.destroyAllWindows()
break
except:
cap.release()
cap = cv2.VideoCapture('FILEPATH/')
continue
I look forward to any constructive feedback/answers and thank you in advance.
Best regards