DepthAI
Software Stack

ON THIS PAGE

  • Source code

Video Replay

Supported on:RVC2RVC4
This example demonstrates how to replay a previously recorded video and showcases how to use the ReplayVideo node for feeding frames back into a DepthAI pipeline.This example requires the DepthAI v3 API, see installation instructions.

Source code

Python

Python
GitHub
1import depthai as dai
2import argparse
3import time
4from pathlib import Path
5import cv2
6
7parser = argparse.ArgumentParser()
8parser.add_argument("-v", "--inputVideo", default="test_video.mp4", help="Input video name")
9parser.add_argument("-m", "--inputMetadata", default="test_video.mcap", help="Input metadata name")
10
11args = parser.parse_args()
12
13# Check if the input video file exists
14if not Path(args.inputVideo).exists():
15    print("First record a video using the record_video.py script")
16    raise FileNotFoundError(f'Input video file not found: {args.inputVideo}')
17
18with dai.Pipeline() as pipeline:
19    replay = pipeline.create(dai.node.ReplayVideo)
20    replay.setReplayVideoFile(Path(args.inputVideo))
21    if Path(args.inputMetadata).exists():
22        replay.setReplayMetadataFile(Path(args.inputMetadata))
23    replay.setOutFrameType(dai.ImgFrame.Type.NV12)
24    replay.setLoop(False)
25
26    videoOut = replay.out.createOutputQueue()
27
28    pipeline.start()
29    while pipeline.isRunning() and replay.isRunning():
30        try:
31            outFrame : dai.ImgFrame = videoOut.get()
32        except dai.MessageQueue.QueueException:
33            # Replay stopped the pipeline
34            break
35        outFrameCv = outFrame.getCvFrame()
36        cv2.imshow("video", outFrameCv)
37        if cv2.waitKey(1) == ord('q'):
38            print("Stopping pipeline")
39            pipeline.stop()
40            break

C++

1#include "depthai/depthai.hpp"
2#include "depthai/pipeline/node/host/Display.hpp"
3#include "depthai/pipeline/node/host/Replay.hpp"
4
5int main(int argc, char** argv) {
6    std::string vidName = "test_video.avi";
7    if(argc > 1) vidName = argv[1];
8    dai::Pipeline pipeline(false);
9
10    auto replay = pipeline.create<dai::node::ReplayVideo>();
11    auto display = pipeline.create<dai::node::Display>();
12
13    replay->setReplayVideoFile(vidName);
14    replay->setFps(30);
15
16    replay->out.link(display->input);
17
18    pipeline.run();  // Let the display node stop the pipeline
19}

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.