resnow

  • a day ago
  • Joined 6 days ago
  • 0 best answers
  • Hello,

    I'm trying to capture images at 12MP, but the preview is cropped to the the 4K FOV. Using a different ISP scale lets me see the whole FOV, but then I can't capture a still at the full resolution. How can I mitigate this? Just using a lower resolution overall is not an option. Any help is appreciated.

    Here's the code I'm currently using:

    import cv2
    import depthai as dai
    import numpy as np
    import os
    import datetime
    
    # Create pipeline
    pipeline = dai.Pipeline()
    
    # Define ColorCamera Node
    camRgb = pipeline.create(dai.node.ColorCamera)
    camRgb.setBoardSocket(dai.CameraBoardSocket.CAM_A)
    camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_12_MP) 
    camRgb.setIspScale(12, 17) # I want to use (1,1) to save the still image
    camRgb.setInterleaved(False)
    camRgb.setPreviewSize(541,406)
    camRgb.setPreviewKeepAspectRatio(True)
    camRgb.setColorOrder(dai.ColorCameraProperties.ColorOrder.BGR)
    
    # Create preview Output Stream
    xoutVideo = pipeline.create(dai.node.XLinkOut)
    xoutVideo.setStreamName("video")
    camRgb.preview.link(xoutVideo.input)
    
    # Create Capture Output Stream
    capture = pipeline.create(dai.node.XLinkOut)
    capture.setStreamName('still')
    camRgb.still.link(capture.input)
    
    # Create Control Stream
    controlIn = pipeline.create(dai.node.XLinkIn)
    controlIn.setStreamName('control')
    controlIn.out.link(camRgb.inputControl)
    
    # Directory for snapshots
    snapshot_dir = "snapshots"
    if not os.path.exists(snapshot_dir):
        os.makedirs(snapshot_dir)
    
    # Function to take a snapshot
    def take_snapshot(frame):
        # Save the snapshot
        timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
        snapshot_path = os.path.join(snapshot_dir, f"snapshot_{timestamp}.png")
        cv2.imwrite(snapshot_path, frame)
        print(f"Snapshot saved as {snapshot_path}")
    
    def toggle_stillcapture(controlQueue, value):
        ctrl = dai.CameraControl()
        ctrl.setCaptureStill(value)
        controlQueue.send(ctrl)
    
    # Connect to device and start pipeline
    with dai.Device(pipeline) as device:
        qVideo = device.getOutputQueue(name="video", maxSize=4, blocking=False)
        qCapture = device.getOutputQueue(name="still", maxSize=30, blocking=True)
        controlQueue = device.getInputQueue("control")
    
        capture = False
        while True:
            if capture:
                inRgb = (
                    qCapture.tryGet()
                )  # Non-blocking call, will return a new data that has arrived or None otherwise
    
                if inRgb is not None:
                    take_snapshot(inRgb.getCvFrame())
                    capture = False
                    toggle_stillcapture(controlQueue, capture)
    
            else:
                inVideo = qVideo.get() # wait until new data has arrived
                img = inVideo.getCvFrame()
               
                cv2.imshow("Capture View", img)
                # Set the callback function for mouse events
                cv2.setMouseCallback('Capture View', draw_rectangle)
    
            # Check for keyboard input
            key = cv2.waitKey(1)
            if key == ord('s'):  # 's' key to save the snapshot
                capture = True
                camRgb.setIspScale(1,1)
                toggle_stillcapture(controlQueue, capture)
            elif key == ord('q'):  # 'q' key to quit
                break
    
    cv2.destroyAllWindows()