Skip to content

Instantly share code, notes, and snippets.

@Erol444
Last active April 13, 2021 09:51
Show Gist options
  • Save Erol444/349dbc48ada07ddbd7599a6bbe6e1f2b to your computer and use it in GitHub Desktop.
Save Erol444/349dbc48ada07ddbd7599a6bbe6e1f2b to your computer and use it in GitHub Desktop.
DepthAI mono camera schedule
#!/usr/bin/env python3
import time
from pathlib import Path
import cv2
import depthai as dai
import schedule
import time
# Start defining a pipeline
pipeline = dai.Pipeline()
# Define a source - mono (grayscale) camera
camRight = pipeline.createMonoCamera()
camRight.setBoardSocket(dai.CameraBoardSocket.RIGHT)
camRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P)
# Create output
xoutRight = pipeline.createXLinkOut()
xoutRight.setStreamName("right")
camRight.out.link(xoutRight.input)
# Pipeline is defined, now we can connect to the device
with dai.Device(pipeline) as device:
# Start pipeline
device.startPipeline()
# Output queue will be used to get the grayscale frames from the output defined above
qRight = device.getOutputQueue(name="right", maxSize=1, blocking=False)
# Make sure the destination path is present before starting to store the examples
Path('07_data').mkdir(parents=True, exist_ok=True)
def save_img():
inRight = qRight.get() # Blocking call, will wait until a new data has arrived
# Data is originally represented as a flat 1D array, it needs to be converted into HxW form
frameRight = inRight.getCvFrame()
# Frame is transformed and ready to be shown
cv2.imshow("right", frameRight)
# After showing the frame, it's being stored inside a target directory as a PNG image
cv2.imwrite(f"07_data/{int(time.time() * 10000)}.png", frameRight)
# Schedule the save_img function
schedule.every(1).hour.do(save_img)
while True:
schedule.run_pending()
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment