Opencv capturing image
Any reference link for this "manually set exposure"
I have changed parameters in code. But, it still taking 5 sec to stable image. I need more guidance.
Problem statement:
I will run code. It will capture image & it will save it.
As soon as I run code, it will stable camera & capture image.
I am using below code.
It still taking 4 to 5 second to stable image.
I am not understanding what parameters to set to get good quality image instantly.
In depth guide needed. Because, I have to attach 6 cameras to our machine
import depthai as dai
import cv2
import time
import os
# ---------------------------
# STEP 1: CREATE THE PIPELINE
# ---------------------------
pipeline = dai.Pipeline()
# --------------------------------------------------
# STEP 2: CREATE & CONFIGURE THE COLOR CAMERA NODE
# --------------------------------------------------
camRgb = pipeline.createColorCamera()
camRgb.setPreviewSize(640, 480) # Increase preview size (1920, 1080) (640, 480)
camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_12_MP)
camRgb.setInterleaved(False)
camRgb.setBoardSocket(dai.CameraBoardSocket.CAM_A)
# ----------------------------------------------------------------
# STEP 3: CONFIGURE INITIAL CAMERA SETTINGS (EXPOSURE, FOCUS, ISO)
# ----------------------------------------------------------------
camRgb.initialControl.setAutoFocusMode(dai.CameraControl.AutoFocusMode.CONTINUOUS_VIDEO)
camRgb.initialControl.setManualExposure(50000, 300)
# ---------------------------------------------------
# STEP 4: CREATE AN OUTPUT (XLinkOut) TO STREAM FRAMES
# ---------------------------------------------------
xoutPreview = pipeline.createXLinkOut()
xoutPreview.setStreamName("preview")
camRgb.preview.link(xoutPreview.input)
# -------------------------------------
# STEP 5: START THE PIPELINE ON DEVICE
# -------------------------------------
with dai.Device(pipeline) as device:
previewQueue = device.getOutputQueue(name="preview", maxSize=4, blocking=False)
print("Press 's' to save an image larger than 5MB, 'q' to quit the program.")
while True:
inPreview = previewQueue.tryGet()
if inPreview is not None:
frame = inPreview.getCvFrame()
cv2.imshow("preview", frame)
cv2.imwrite("output_images1\\preview.png", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('s'):
if 'frame' in locals():
timestamp = time.strftime("%Y%m%d_%H%M%S")
filename = f"D:\\oak_kit\\example\\output_imgaes1\\6_feb_25\\capture{timestamp}.bmp" #filename = f"D:\\oak_kit\\example\\output_images1\\6_feb_25\\capture{timestamp}.bmp"
cv2.imwrite(filename, frame)
# Check the file size
file_size = os.path.getsize(filename)
if file_size > 5 * 1024 * 1024: # 5MB in bytes
print(f"Image saved: {filename} (Size: {file_size / (1024 * 1024)} MB)")
else:
os.remove(filename)
print(f"Image discarded: {filename} (Size: {file_size / (1024 * 1024)} MB)")
else:
print("No frame to save yet.")
elif key == ord('q'):
print("Quitting...")
break
cv2.destroyAllWindows()
We are getting good result for small frame (500*500).
But, for big frames it is taking time to stable camera. Around 6 sec.
I need to capture big frames (1500*800) as soon as I run codes
Below is code we use for big frames.
cv2.imshow("preview", frame)
cv2.imwrite("output_imgaes1\\preview.png", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('s'):
if 'frame' in locals():
timestamp = time.strftime("%Y%m%d_%H%M%S")
filename = f"D:\\oak_kit\\example\\output_imgaes1\\8_feb_25\\capture_{timestamp}.bmp"
cv2.imwrite(filename, frame)
print(f"Image saved: {filename}")
else:
print("No frame to save yet.")
elif key == ord('q'):
print("Quitting...")
break
cv2.destroyAllWindows()
we have done latency test. As given in link here, OAK bandwidth test - Should be around 800/200 Mbps.
But I am getting as per attached in screenshot.
Does screen value is correct or do we need to update hardware (POE switch / cable)
poe_test.py gives all OK results.
Finally we have use below code by changing CAT 6 good quality ethernet cable.
Now problem is, for below code after running code, we are getting below warning on console & it is taking 8 sec to show video frame.
[2025-02-10 16:48:16.272] [depthai] [warning] [18443010F15F9D0F00] [192.168.31.197] Flashed bootloader version 0.0.22, less than 0.0.28 is susceptible to bootup/restart failure. Upgrading is advised, flashing main/factory (not user) bootloader. Available: 0.0.28
Press 's' to save an image, 'q' to quit the program.
import depthai as dai
import cv2
import time
# ---------------------------
# STEP 1: CREATE THE PIPELINE
# ---------------------------
pipeline = dai.Pipeline()
# --------------------------------------------------
# STEP 2: CREATE & CONFIGURE THE COLOR CAMERA NODE
# --------------------------------------------------
camRgb = pipeline.createColorCamera()
camRgb.setPreviewSize(640, 480) # camRgb.setPreviewSize(400, 300) # (500, 500) (1080, 480) camRgb.setPreviewSize(1920, 1080) camRgb.setPreviewSize(640, 480)
camRgb.setInterleaved(False)
# Sets the sensor to 4K mode (maximum sensor resolution around 3840x2160 or slightly more)
camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_12_MP)
# WARNING DEPRECATION: 'RGB' is deprecated; use 'CAM_A' (or whichever is your color socket)
camRgb.setBoardSocket(dai.CameraBoardSocket.CAM_A)
# ----------------------------------------------------------------
# STEP 3: CONFIGURE INITIAL CAMERA SETTINGS (EXPOSURE, FOCUS, ISO)
# ----------------------------------------------------------------
# Instead of creating a separate CameraControl and setting a commandList,
# we directly set the camera's initial control properties here:
camRgb.initialControl.setAutoFocusMode(dai.CameraControl.AutoFocusMode.CONTINUOUS_PICTURE) # CONTINUOUS_PICTURE
# Manual exposure: 20,000 µs = 20 ms, ISO = 200
camRgb.initialControl.setManualExposure(200, 400) # (100000, 500) (200000, 300)
camRgb.initialControl.setManualFocus(130)
camRgb.initialControl.setManualWhiteBalance(5834)
# ---------------------------------------------------
# STEP 4: CREATE AN OUTPUT (XLinkOut) TO STREAM FRAMES
# ---------------------------------------------------
xoutPreview = pipeline.createXLinkOut()
xoutPreview.setStreamName("preview")
camRgb.preview.link(xoutPreview.input)
# -------------------------------------
# STEP 5: START THE PIPELINE ON DEVICE
# -------------------------------------
with dai.Device(pipeline) as device:
previewQueue = device.getOutputQueue(name="preview", maxSize=4, blocking=False)
print("Press 's' to save an image, 'q' to quit the program.")
while True:
inPreview = previewQueue.tryGet()
if inPreview is not None:
frame = inPreview.getCvFrame()
cv2.imshow("preview", frame)
cv2.imwrite("output_imgaes1\\\\preview.png", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('s'):
if 'frame' in locals():
timestamp = time.strftime("%Y%m%d_%H%M%S")
filename = f"D:\\\\oak_kit\\\\example\\\\output_imgaes1\\\\10_feb_25\\\\capture_{timestamp}.bmp"
cv2.imwrite(filename, frame)
print(f"Image saved: {filename}")
else:
print("No frame to save yet.")
elif key == ord('q'):
print("Quitting...")
break
cv2.destroyAllWindows()
spatil
That is expected and is the time taken by watchdog to boot the device. No getting around it but to keep the device booted. Also I'd recommend updating your bootloader since it is currently on 0.22 which is quite old. https://docs.luxonis.com/software/depthai-components/device/
Thanks,
Jaka
Bootloader is updated & now bootloader warning is not coming.
But, I am not able to understand below things. As per documentation,
Customizing the Watchdog Timeout
Set the environment variablesDEPTHAI_WATCHDOG_INITIAL_DELAY
andDEPTHAI_BOOTUP_TIMEOUT
to your desired timeout values (in milliseconds) as follows:
Alternatively, you can set the timeout directly in your code:
Python
$$
1pipeline = depthai.Pipeline()
2
3# Create a BoardConfig object
4config = depthai.BoardConfig()
5
6# Set the parameters
7config.watchdogInitialDelayMs = <my_value>
8config.watchdogTimeoutMs = <my_value>
9
10pipeline.setBoardConfig(config)
$$
By adjusting these settings, you can tailor the watchdog functionality to better suit your specific requirements.
I have put 1 sec (1000), but its not reducing any time.
Any link to understand this in depth. I have to capture images in microseconds. Instead of POE, do I need to use power supply for this.
spatil
But also
The watchdog process is necessary to make the camera available for reconnection and typically takes about 10 seconds, which means the fastest possible reconnection time is 10 seconds.
Some of it refers to watchdog bootup time. if you put too little, the device will timeout.
spatil I have to capture images in microseconds.
I'm afraid that is not possible since exposure time alone takes up .5 to 15ms unless you want an underexposed image.
Not sure what you mean by
spatil Instead of POE, do I need to use power supply for this.
Thanks,
Jaka
Since, for device reconnection time is 10 seconds(whether it is POE or we give supply from power cable), do I need to follow below steps.
1: Once our machine is ON, python code will be called & camera video streaming will be playing.
2: Once our software get external trigger, it will capture image & keep video streaming running till it get next trigger.
Every time it get trigger, it will capture images.
In this, way, camera will be remain ON 24 hours in video mode & whenever we want we can capture images.
is this understanding is true?
What we need?
1: Machine will be ON. Once our software got external trigger, it will run python code. But everytime it call python code,
reconnection will take 10 seconds. we have to reduce this 10 seconds.
Hi @spatil
- This is what is fully possible, yes. That way the trigger time to snapshot time should only be dependent on the message latency.
Here is an example that does that:
https://docs.luxonis.com/software/depthai/examples/script_camera_control
Thanks,
Jaka