etiennedub / pyk4a

Python 3 wrapper for Azure-Kinect-Sensor-SDK
MIT License
287 stars 81 forks source link

how can i change camera settings? #214

Open ImJaewooChoi opened 1 year ago

ImJaewooChoi commented 1 year ago

import pyk4a from pyk4a import Config, PyK4A, Calibration, CalibrationType import cv2 from camera.helpers import convert_to_bgra_if_required import numpy as np class Azure_Camera: def init(self, config):

    self.cam_config = config
    #print(self.config)
    if self.cam_config["camera_setting"]["color_resoultion"] == "1536":
        self.color_resolution = pyk4a.ColorResolution.RES_1536P
        self.color_resolution_ = (1536, 2048)
    if self.cam_config["camera_setting"]["color_format"] == "BGRA32":
        self.color_format = pyk4a.ImageFormat.COLOR_BGRA32
    if self.cam_config["camera_setting"]["depth_mode"] == "OFF":
        self.depth_mode = pyk4a.DepthMode.OFF
    self.config = Config(\
                            color_resolution=self.color_resolution, color_format=self.color_format\
                          )
    self.k4a = PyK4A(self.config)
    self.calibration = Calibration(
        self.k4a._device_handle, self.depth_mode, self.color_resolution
    )
    self.k4a.start()
    self.get_color_intrinsic_mat()
    self.get_color_distortion_mat()
    self.get_color_rectifi_intrinsic_mat()
    self.set_color_setting()
    #self.k4a._start_cameras()
    while True:
        image = self.capture_rectif_color()
        cv2.imshow("image", image)
        key=cv2.waitKey(100)
        if key == ord('q'):
            cv2.destroyAllWindows()
            break
def get_color_resolution(self):

    return self.color_resolution_
def set_color_setting(self):

    #self.k4a.exposure_mode_auto = (1, self.cam_config["color_setting"]["exposure"])
    self.k4a.exposure = self.cam_config["color_setting"]["exposure"]
    self.k4a.brightness = self.cam_config["color_setting"]["brightness"]
    self.k4a.contrast = self.cam_config["color_setting"]["contrast"]
    self.k4a.saturation = self.cam_config["color_setting"]["saturation"]
    self.k4a.sharpness = self.cam_config["color_setting"]["sharpness"]
    self.k4a.gain = self.cam_config["color_setting"]["gain"]
    print("Color Camera Brightness :  ", self.k4a.brightness)
    print("Color Camera Contrast :  ", self.k4a.contrast)
    print("Color Camera Saturation :  ", self.k4a.saturation)
    print("Color Camera Sharpness :  ", self.k4a.sharpness)
    print("Color Camera Gain :  ", self.k4a.gain)
    print("Color Camera Exposure :  ", self.k4a.exposure)
def get_color_intrinsic_mat(self):

    self.camera_info = self.k4a.calibration.get_camera_matrix(CalibrationType.COLOR)
def get_color_distortion_mat(self):

    self.distortion = self.k4a.calibration.get_distortion_coefficients(
        CalibrationType.COLOR
    )
    self.distortion = np.squeeze(self.distortion)
def get_color_rectifi_intrinsic_mat(self):

    self.new_camera_info, _ = cv2.getOptimalNewCameraMatrix(
        self.camera_info,
        self.distortion,
        self.color_resolution_,
        1,
        self.color_resolution_,
    )
def capture_color(self):

    capture = None
    while True:
        capture = self.k4a.get_capture()
        if np.any(capture.color):
            return convert_to_bgra_if_required(
               self.config.color_format, capture.color
            )
            return capture.color[:, :, :3]
def capture_rectif_color(self):

    img = self.capture_color()
    rectifi_img = cv2.undistort(
        img, self.camera_info, self.distortion, None, self.new_camera_info\
    )
    return rectifi_img

The code above is what I wrote. When I run the camera through this code, the changed camera value is not applied. Is there anything I need to change or add?