I'm not sure how to set the brightness before running the pipeline. I used the device object to access getInputQueue("control"). This didn't fix the problem. The video still flickers.
import time
from fractions import Fraction
import depthai as dai
import av
from rgb_camera_control import ids, get_log_level_obj, camera_models
def write_stream(cam_id, settings):
pipeline = dai.Pipeline()
camRgb = pipeline.create(dai.node.ColorCamera)
camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_4_K)
# camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_4000X3000)
# camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_5312X6000)
width, height = camRgb.getVideoSize()
print(width, height)
camRgb.setFps(settings["fps"])
imageManip = pipeline.create(dai.node.ImageManip)
frame_size = width * height * 3
imageManip.setMaxOutputFrameSize(int(frame_size))
imageManip.initialConfig.setFrameType(dai.ImgFrame.Type.NV12)
camRgb.isp.link(imageManip.inputImage)
videoEnc = pipeline.create(dai.node.VideoEncoder)
videoEnc.setDefaultProfilePreset(settings["fps"], dai.VideoEncoderProperties.Profile.H264_MAIN)
imageManip.out.link(videoEnc.input)
xoutEnc = pipeline.create(dai.node.XLinkOut)
xoutEnc.setStreamName('video')
videoEnc.bitstream.link(xoutEnc.input)
controlIn = pipeline.create(dai.node.XLinkIn)
controlIn.setStreamName('control')
controlIn.out.link(camRgb.inputControl)
with dai.Device(pipeline, dai.DeviceInfo(ids[cam_id]), maxUsbSpeed=dai.UsbSpeed.HIGH) as device:
device.setLogLevel(get_log_level_obj(settings["log_level"]))
device.setLogOutputLevel(get_log_level_obj(settings["log_level"]))
controlQueue = device.getInputQueue('control') # For sending control commands
# apply_settings(controlQueue, settings)
ctrl = dai.CameraControl()
ctrl.setBrightness(-5)
ctrl.setSharpness(2)
controlQueue.send(ctrl)
videoQueue = device.getOutputQueue('video', maxSize=30, blocking=True)
output_container = av.open("video.mp4", 'w')
stream = output_container.add_stream("h264", rate=Fraction(camRgb.getFps()))
stream.time_base = Fraction(1, 1000 * 1000)
start = time.time()
print("Recording...ctrl+c to stop\n")
try:
while True:
data = videoQueue.get().getData()
ctrl.setBrightness(-5)
controlQueue.send(ctrl)
packet = av.Packet(data)
packet.pts = int((time.time() - start) * 1000 * 1000)
output_container.mux_one(packet)
except KeyboardInterrupt:
pass
output_container.close()
if __name__ == "__main__":
import json
devices = dai.Device.getAllAvailableDevices()
print(f"Found {len(devices)} devices")
for i, device_info in enumerate(devices):
ids[i+1] = device_info.getMxId()
for id in ids:
print(camera_models[ids[id]], ": ", ids[id])
with open('settings.json', 'r') as f:
settings = json.load(f)
write_stream(2, settings)