Tensorflow object detection video recording

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

before asking a human, do some research (googling) on your own. most of the things already exist and you can find them, if you look. this forum does not replace you having to google stuff. I hope you understand.

ok so you say you know of VideoWriter, but your code doesn’t contain it, even though you say you’re already using it, but then you ask how to use it again, just on different data?

what you call video in your code is pointless. cv.imshow always returns nothing (None). it has nothing to return.

and what’s with that catch-all try-except block? you’ll never know what exceptions you’re just catching and not inspecting at all.

and there’s no error checking on your uses of VideoCapture either. it doesn’t throw any exceptions unless that is explicitly enabled (here it is not). you have to check cap.isOpened() once after creation and if not ret: break after every read()