Skip to content

Commit

Permalink
Version 1.2.0 (#115)
Browse files Browse the repository at this point in the history
- Use clang-format as CPP code formatter/linter (#105)
- Available devices count (#107)
- Recording support (#106)
- Capture color exposure and white balance properties (#110)
- Calibration: Add functions to access intrinsics (#113)
- Add 3d => 2d conversion function (#114)

* Run test with different python versions (#65)

* Add matrix to workflow

* Change python versions list

* Change python versions list

* Add k4a versions to matrix

* Typofix

* Drop k4a from matrix

* Add dataclasses requirement for python <3.7

* Fix python 3.6 test behavior

* Fix python 3.6 test behavior

* Restore fail-fast option

* fix conversion seconds to ns

* fix conversion seconds to ns

* fix timestamp ns to us

Co-authored-by: Louis-Philippe Asselin <[email protected]>

* fix install using pip --editable --user (#67)

* Codecov support (#64)

* Codecov support

* Add badge

* Order badges

* fix capture.transformed_depth_point_cloud (#73)

* version 1.0.1

* Added transformed_ir with transform_depth_image_to_color_camera_custom functionality (#76)

* Added transform_depth_image_to_color_camera_custom functionality

* keeping things c

* add interpolation option condition as a parameter

* returned the depth image

* unpack return value if not None so avoid error

* Image timestamp support (#88)

* Support for capture images timestamps

* Support for capture images timestamps

* Add more changes

* version 1.1.0

* fix lint

* readme fix wrong example version of SDK

* Use clang-format as CPP code formatter/linter (#105)

* Use clang-format as CPP code formatter/linter

* Add missed .clang-format

* Available devices count (#107)

* Add ability to querying devices count and read serial numbers

* Fix test

* Rename installed_count => connected_device_count

* Recording support (#106)

* Recording support

* Tests

* Small refactoring

* Add ability to querying devices count and read serial numbers

* Fix test

* Fix format

* Capture color exposure and white balance properties (#110)

* Add color_white_balance and exposure_usec properties to capture

* Add color_white_balance and exposure_usec properties to capture

Co-authored-by: Louis-Philippe Asselin <[email protected]>

* Calibration: Add functions to access intrinsics (#113)

Camera matrix and distortion coefficients in OpenCV-compatible format.
These values are already specific to the selected camera resolution (in contrast to those accessed through calibration_raw).

related to #35, #69

Co-authored-by: Johan von Forstner <[email protected]>

* version 1.2.0

Co-authored-by: Ilya Gruzinov <[email protected]>
Co-authored-by: Samuel Boulanger <[email protected]>
Co-authored-by: Johan von Forstner <[email protected]>
Co-authored-by: Johan von Forstner <[email protected]>
  • Loading branch information
5 people authored Apr 8, 2021
1 parent 8186ae9 commit e94b0fa
Show file tree
Hide file tree
Showing 17 changed files with 656 additions and 19 deletions.
13 changes: 13 additions & 0 deletions example/devices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pyk4a import PyK4A, connected_device_count


cnt = connected_device_count()
if not cnt:
print("No devices available")
exit()
print(f"Available devices: {cnt}")
for device_id in range(cnt):
device = PyK4A(device_id=device_id)
device.open()
print(f"{device_id}: {device.serial}")
device.close()
29 changes: 29 additions & 0 deletions example/record.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from argparse import ArgumentParser

from pyk4a import Config, ImageFormat, PyK4A, PyK4ARecord


parser = ArgumentParser(description="pyk4a recorder")
parser.add_argument("--device", type=int, help="Device ID", default=0)
parser.add_argument("FILE", type=str, help="Path to MKV file")
args = parser.parse_args()

print(f"Starting device #{args.device}")
config = Config(color_format=ImageFormat.COLOR_MJPG)
device = PyK4A(config=config, device_id=args.device)
device.start()

print(f"Open record file {args.FILE}")
record = PyK4ARecord(device=device, config=config, path=args.FILE)
record.create()
try:
print("Recording... Press CTRL-C to stop recording.")
while True:
capture = device.get_capture()
record.write_capture(capture)
except KeyboardInterrupt:
print("CTRL-C pressed. Exiting.")

record.flush()
record.close()
print(f"{record.captures_count} frames written.")
9 changes: 9 additions & 0 deletions pyk4a/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import k4a_module

from .calibration import Calibration, CalibrationType
from .capture import PyK4ACapture
from .config import (
Expand All @@ -13,6 +15,7 @@
from .errors import K4AException, K4ATimeoutException
from .playback import PyK4APlayback, SeekOrigin
from .pyk4a import ColorControlCapabilities, PyK4A
from .record import PyK4ARecord
from .transformation import (
color_image_to_depth_camera,
depth_image_to_color_camera,
Expand All @@ -21,6 +24,10 @@
)


def connected_device_count() -> int:
return k4a_module.device_get_installed_count()


__all__ = (
"Calibration",
"CalibrationType",
Expand All @@ -43,4 +50,6 @@
"depth_image_to_point_cloud",
"depth_image_to_color_camera",
"depth_image_to_color_camera_custom",
"PyK4ARecord",
"connected_device_count",
)
27 changes: 27 additions & 0 deletions pyk4a/calibration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from enum import IntEnum
from typing import Optional, Tuple

import numpy as np

import k4a_module

from .config import ColorResolution, DepthMode
Expand Down Expand Up @@ -157,3 +159,28 @@ def transformation_handle(self) -> object:
raise K4AException("Cannot create transformation handle")
self._transformation_handle = handle
return self._transformation_handle

def get_camera_matrix(self, camera: CalibrationType) -> np.ndarray:
"""
Get the camera matrix (in OpenCV compatible format) for the color or depth camera
"""
if camera not in [CalibrationType.COLOR, CalibrationType.DEPTH]:
raise ValueError("Camera matrix only available for color and depth cameras.")
params = k4a_module.calibration_get_intrinsics(self._calibration_handle, self.thread_safe, camera)
if len(params) != 14:
raise ValueError("Unknown camera calibration type")

cx, cy, fx, fy = params[:4]
return np.array([[fx, 0, cx], [0, fy, cy], [0, 0, 1]])

def get_distortion_coefficients(self, camera: CalibrationType) -> np.ndarray:
"""
Get the distortion coefficients (in OpenCV compatible format) for the color or depth camera
"""
if camera not in [CalibrationType.COLOR, CalibrationType.DEPTH]:
raise ValueError("Distortion coefficients only available for color and depth cameras.")
params = k4a_module.calibration_get_intrinsics(self._calibration_handle, self.thread_safe, camera)
if len(params) != 14:
raise ValueError("Unknown camera calibration type")

return np.array([params[4], params[5], params[13], params[12], *params[6:10]])
21 changes: 21 additions & 0 deletions pyk4a/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from .calibration import Calibration
from .config import ImageFormat
from .errors import K4AException
from .transformation import (
color_image_to_depth_camera,
depth_image_to_color_camera,
Expand All @@ -25,6 +26,8 @@ def __init__(

self._color: Optional[np.ndarray] = None
self._color_timestamp_usec: int = 0
self._color_exposure_usec: Optional[int] = None
self._color_white_balance: Optional[int] = None
self._depth: Optional[np.ndarray] = None
self._depth_timestamp_usec: int = 0
self._ir: Optional[np.ndarray] = None
Expand All @@ -50,6 +53,24 @@ def color_timestamp_usec(self) -> int:
self.color
return self._color_timestamp_usec

@property
def color_exposure_usec(self) -> int:
if self._color_exposure_usec is None:
value = k4a_module.color_image_get_exposure_usec(self._capture_handle)
if value == 0:
raise K4AException("Cannot read exposure from color image")
self._color_exposure_usec = value
return self._color_exposure_usec

@property
def color_white_balance(self) -> int:
if self._color_white_balance is None:
value = k4a_module.color_image_get_white_balance(self._capture_handle)
if value == 0:
raise K4AException("Cannot read white balance from color image")
self._color_white_balance = value
return self._color_white_balance

@property
def depth(self) -> Optional[np.ndarray]:
if self._depth is None:
Expand Down
Loading

0 comments on commit e94b0fa

Please sign in to comment.