Skip to content

Commit 145b877

Browse files
committed
rgb_uvc.py: add UVC boardConfig, --load-and-exit script option.
Flashing is possible as well on certain devices
1 parent 9920bad commit 145b877

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

examples/ColorCamera/rgb_uvc.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
import time
55
import argparse
66

7-
enable_4k = True # Will downscale 4K -> 1080p
8-
97
parser = argparse.ArgumentParser()
108
parser.add_argument('-fb', '--flash-bootloader', default=False, action="store_true")
119
parser.add_argument('-f', '--flash-app', default=False, action="store_true")
10+
parser.add_argument('-l', '--load-and-exit', default=False, action="store_true")
1211
args = parser.parse_args()
1312

1413
def getPipeline():
15-
# Start defining a pipeline
14+
enable_4k = True # Will downscale 4K -> 1080p
15+
1616
pipeline = dai.Pipeline()
1717

1818
# Define a source - color camera
1919
cam_rgb = pipeline.createColorCamera()
20-
cam_rgb.setBoardSocket(dai.CameraBoardSocket.RGB)
20+
cam_rgb.setBoardSocket(dai.CameraBoardSocket.CAM_A)
2121
cam_rgb.setInterleaved(False)
2222
#cam_rgb.initialControl.setManualFocus(130)
2323

@@ -27,25 +27,29 @@ def getPipeline():
2727
else:
2828
cam_rgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
2929

30-
# Create an UVC (USB Video Class) output node. It needs 1920x1080, NV12 input
30+
# Create an UVC (USB Video Class) output node
3131
uvc = pipeline.createUVC()
3232
cam_rgb.video.link(uvc.input)
3333

34-
return pipeline
34+
# Note: if the pipeline is sent later to device (using startPipeline()),
35+
# it is important to pass the device config separately when creating the device
36+
config = dai.Device.Config()
37+
# config.board.uvc = dai.BoardConfig.UVC() # enable default 1920x1080 NV12
38+
config.board.uvc = dai.BoardConfig.UVC(1920, 1080)
39+
config.board.uvc.frameType = dai.ImgFrame.Type.NV12
40+
# config.board.uvc.cameraName = "My Custom Cam"
41+
pipeline.setBoardConfig(config.board)
3542

36-
# Workaround for a bug with the timeout-enabled bootloader
37-
progressCalled = False
38-
# TODO move this under flash(), will need to handle `progressCalled` differently
39-
def progress(p):
40-
global progressCalled
41-
progressCalled = True
42-
print(f'Flashing progress: {p*100:.1f}%')
43+
return pipeline
4344

4445
# Will flash the bootloader if no pipeline is provided as argument
4546
def flash(pipeline=None):
4647
(f, bl) = dai.DeviceBootloader.getFirstAvailableDevice()
4748
bootloader = dai.DeviceBootloader(bl, True)
4849

50+
# Create a progress callback lambda
51+
progress = lambda p : print(f'Flashing progress: {p*100:.1f}%')
52+
4953
startTime = time.monotonic()
5054
if pipeline is None:
5155
print("Flashing bootloader...")
@@ -54,8 +58,6 @@ def flash(pipeline=None):
5458
print("Flashing application pipeline...")
5559
bootloader.flash(progress, pipeline)
5660

57-
if not progressCalled:
58-
raise RuntimeError('Flashing failed, please try again')
5961
elapsedTime = round(time.monotonic() - startTime, 2)
6062
print("Done in", elapsedTime, "seconds")
6163

@@ -65,11 +67,23 @@ def flash(pipeline=None):
6567
print("Flashing successful. Please power-cycle the device")
6668
quit()
6769

68-
# Pipeline defined, now the device is connected to
70+
if args.load_and_exit:
71+
import os
72+
# Disabling device watchdog, so it doesn't need the host to ping periodically
73+
os.environ["DEPTHAI_WATCHDOG"] = "0"
74+
device = dai.Device(getPipeline())
75+
print("\nDevice started, open a UVC viewer to check the camera stream.")
76+
print("Attempting to force-quit this process...")
77+
print("To reconnect with depthai, a device power-cycle may be required")
78+
# We do not want the device to be closed, so kill the process.
79+
# (TODO add depthai API to be able to cleanly exit without closing device)
80+
import signal
81+
os.kill(os.getpid(),signal.SIGKILL)
82+
83+
# Standard UVC load with depthai
6984
with dai.Device(getPipeline()) as device:
7085
print("\nDevice started, please keep this process running")
71-
print("and open an UVC viewer. Example on Linux:")
72-
print(" guvcview -d /dev/video0")
86+
print("and open an UVC viewer to check the camera stream.")
7387
print("\nTo close: Ctrl+C")
7488

7589
# Doing nothing here, just keeping the host feeding the watchdog

0 commit comments

Comments
 (0)