DepthAI v2 has been superseded by DepthAI v3. You are viewing legacy documentation.
DepthAI Tutorials
DepthAI API References

ON THIS PAGE

  • Demo
  • Setup
  • Source code
  • Pipeline

Mono Full Resolution Saver

This example does its best to save 1280x720 .png files as fast at it can from the Mono sensor. It serves as an example of recording mono pictures to disk.Be careful, this example saves pictures to your host storage. So if you leave it running, you could fill up your storage on your host.

Similar samples:

Demo

Setup

Please run the install script to download all required dependencies. Please note that this script must be ran from git context, so you have to download the depthai-python repository first and then run the script
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
For additional information, please follow the installation guide.

Source code

Python

Python
GitHub
1#!/usr/bin/env python3
2
3from pathlib import Path
4import cv2
5import depthai as dai
6import time
7
8# Create pipeline
9pipeline = dai.Pipeline()
10
11# Define source and output
12monoRight = pipeline.create(dai.node.MonoCamera)
13xoutRight = pipeline.create(dai.node.XLinkOut)
14
15xoutRight.setStreamName("right")
16
17# Properties
18monoRight.setCamera("right")
19monoRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P)
20
21# Linking
22monoRight.out.link(xoutRight.input)
23
24# Connect to device and start pipeline
25with dai.Device(pipeline) as device:
26
27    # Output queue will be used to get the grayscale frames from the output defined above
28    qRight = device.getOutputQueue(name="right", maxSize=4, blocking=False)
29
30    dirName = "mono_data"
31    Path(dirName).mkdir(parents=True, exist_ok=True)
32
33    while True:
34        inRight = qRight.get()  # Blocking call, will wait until a new data has arrived
35        # Data is originally represented as a flat 1D array, it needs to be converted into HxW form
36        # Frame is transformed and ready to be shown
37        cv2.imshow("right", inRight.getCvFrame())
38
39        # After showing the frame, it's being stored inside a target directory as a PNG image
40        cv2.imwrite(f"{dirName}/{int(time.time() * 1000)}.png", inRight.getFrame())
41
42        if cv2.waitKey(1) == ord('q'):
43            break

C++

1#include <chrono>
2#include <iostream>
3
4// Includes common necessary includes for development using depthai library
5#include "depthai/depthai.hpp"
6#include "utility.hpp"
7
8int main() {
9    using namespace std::chrono;
10    // Create pipeline
11    dai::Pipeline pipeline;
12
13    // Define source and output
14    auto monoRight = pipeline.create<dai::node::MonoCamera>();
15    auto xoutRight = pipeline.create<dai::node::XLinkOut>();
16
17    xoutRight->setStreamName("right");
18
19    // Properties
20    monoRight->setCamera("right");
21    monoRight->setResolution(dai::MonoCameraProperties::SensorResolution::THE_720_P);
22
23    // Linking
24    monoRight->out.link(xoutRight->input);
25
26    // Connect to device and start pipeline
27    dai::Device device(pipeline);
28
29    // Output queue will be used to get the grayscale frames from the output defined above
30    auto qRight = device.getOutputQueue("right", 4, false);
31
32    std::string dirName = "mono_data";
33    createDirectory(dirName);
34
35    while(true) {
36        auto inRight = qRight->get<dai::ImgFrame>();
37        // Data is originally represented as a flat 1D array, it needs to be converted into HxW form
38        // Frame is transformed and ready to be shown
39        cv::imshow("right", inRight->getCvFrame());
40
41        uint64_t time = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
42        std::stringstream videoStr;
43        videoStr << dirName << "/" << time << ".png";
44        // After showing the frame, it's being stored inside a target directory as a PNG image
45        cv::imwrite(videoStr.str(), inRight->getCvFrame());
46
47        int key = cv::waitKey(1);
48        if(key == 'q' || key == 'Q') {
49            return 0;
50        }
51    }
52    return 0;
53}

Pipeline

Need assistance?

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