예제 #1
0
  Supported resolutions:
  720:  1280 x 720 @ 30 FPS  binned depth
  1080: 1920 x 1080 @ 30 FPS binned depth
  1440: 2560 x 1440 @ 30 FPS binned depth
  1535: 2048 x 1536 @ 30 FPS binned depth
  2160: 3840 x 2160 @ 30 FPS binned depth
  3072: 4096 x 3072 @ 15 FPS binned depth
"""
import numpy as np
import cv2
import kinz

# Create Kinect object and initialize
kin = kinz.Kinect(resolution=720,
                  wfov=False,
                  binned=True,
                  framerate=30,
                  imu_sensors=False,
                  body_tracking=True)

# initialize fps counter
t = cv2.getTickCount()
fps_count = 0
fps = 0

try:
    while True:
        if fps_count == 0:
            t = cv2.getTickCount()

        # read kinect frames. If frames available return 1
        if kin.get_frames(get_color=True,
예제 #2
0
  Kinect for Azure Color, Depth and Infrared streaming in Python

  Supported resolutions:
  720:  1280 x 720 @ 30 FPS  binned depth
  1080: 1920 x 1080 @ 30 FPS binned depth
  1440: 2560 x 1440 @ 30 FPS binned depth
  1535: 2048 x 1536 @ 30 FPS binned depth
  2160: 3840 x 2160 @ 30 FPS binned depth
  3072: 4096 x 3072 @ 15 FPS binned depth
"""
import numpy as np 
import cv2
import kinz

# Create Kinect object and initialize
kin = kinz.Kinect(resolution=720, wfov=True, binned=True, framerate=30, imu_sensors=True)

# Get depth aligned with color?
align_frames = False
image_scale = 0.5    # visualized image scale

# initialize fps counter
t = cv2.getTickCount()
fps_count = 0
fps = 0

while True:
    if fps_count==0:
      t = cv2.getTickCount()

    # read kinect frames. If frames available return 1
def main():
    global depth_points
    # Create Kinect object and initialize
    kin = kinz.Kinect(resolution=720, wfov=True, binned=True)

    depth_window_name = 'Click on the Depth image'
    color_window_name = 'Mapped coordinates in Color'
    cv2.namedWindow(color_window_name, cv2.WINDOW_AUTOSIZE)
    cv2.namedWindow(depth_window_name, cv2.WINDOW_AUTOSIZE)
    cv2.setMouseCallback(depth_window_name, mouse_event)

    points_3d = []
    prev_points_3d = []

    while True:
        if kin.get_frames(get_color=True,
                          get_depth=True,
                          get_ir=False,
                          align_depth=True):
            color_data = kin.get_color_data()
            depth_data = kin.get_depth_data()

            depth_image = np.array(depth_data.buffer, copy=True)
            color_image = np.array(color_data.buffer, copy=True)
            color_image = cv2.cvtColor(color_image, cv2.COLOR_BGRA2BGR)

            # Project depth image points to color image points
            # depth_points is a list of value pairs [[xd1,yd1], [xd2,yd2], ...]
            # color_points have the same format as depth_points
            # map_coords_depth_to_color return [-1, -1] if there is no depth information
            # at the desire location, or the transformation failed.
            color_points = kin.map_coords_depth_to_color(depth_points)

            # Backproject depth image points to 3D depth camera space
            # points_3d is a list of triplets of X, Y, Z points in
            # the depth camera reference
            # if depth_coords is [[x1,y1], [x2,y2], ...]
            # points_3d is [[X1, Y1, Z1], [X2, Y2, Z2], ...]
            # points_3d contains [0, 0, 0] if for desired depth coordinates
            # there is not depth available.
            # E.g. if at [x2, y2] the depth is 0, points_3d = [[X1, Y1, Z1], [0, 0, 0]]
            # we select 0 because depth = 0 means no depth data.
            points_3d = kin.map_coords_depth_to_3D(depth_points)

            if points_3d != prev_points_3d:
                print("3D point:", points_3d)

            # Draw mapped points in color image
            for p in color_points:
                cv2.circle(color_image, (p[0], p[1]), 8, (0, 0, 255), -1)

            # Apply colormap on depth image for visualization
            depth_colormap = cv2.applyColorMap(
                cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)

            # Draw depth points
            for p in depth_points:
                cv2.circle(depth_colormap, (p[0], p[1]), 5, (0, 0, 255), -1)

            # Resize color image for visualization purposes
            color_small = cv2.resize(color_image, None, fx=0.5, fy=0.5)

            cv2.imshow(depth_window_name, depth_colormap)
            cv2.imshow(color_window_name, color_small)
        prev_points_3d = points_3d

        k = cv2.waitKey(1) & 0xFF
        if k == ord('c'):
            depth_points = []
        if k == 27:
            break

    kin.close()  # close Kinect
    cv2.destroyAllWindows()
예제 #4
0
  Kinect for Azure Pointcloud demo

    This demo uses the Open3D library to visualize the pointcloud.
    It saves a Pointcloud in a PLY file.
"""
from datetime import datetime
import numpy as np
import os
import kinz
import open3d as o3d

# Output directory to save the pointcloud
POINTCLOUD_OUTPUT_DIR = "."

# Create Kinect object and initialize
kin = kinz.Kinect(resolution=1080, wfov=True, binned=True, framerate=30)

try:
    count = 0
    geometry_added = False
    vis = o3d.visualization.Visualizer()
    vis.create_window("PointCloud")
    pcd = o3d.geometry.PointCloud()

    while True:
        dt0 = datetime.now()

        # read kinect frames. If frames available return 1
        if kin.get_frames(get_color=True,
                          get_depth=True,
                          get_ir=False,
예제 #5
0
import numpy as np
import cv2
import kinz

# Create Kinect object and initialize
kin = kinz.Kinect(resolution=720, wfov=True, binned=False, framerate=30)

# initialize fps counter
t = cv2.getTickCount()
fps_count = 0
fps = 0

try:
    count = 0
    while True:
        if fps_count == 0:
            t = cv2.getTickCount()

        # read kinect frames. If frames available return 1
        if kin.get_frames(get_color=True, get_depth=True, get_ir=False):
            count += 1

            # Get the pointcloud data and convert to Numpy (512 x 512 x 3, int16)
            pointcloud_data = kin.get_pointcloud()
            pointcloud_np = np.array(pointcloud_data, copy=True)

            # Get the pointcloud color and conver to Numpy (512 x 512 x 3, uint8)
            pointcloudcolor_data = kin.get_pointcloud_color()
            pointcloudcolor_np = np.array(pointcloudcolor_data, copy=True)

        # increment frame counter and calculate FPS
예제 #6
0
def main():
    global color_coords
    # Create Kinect object and initialize
    kin = kinz.Kinect(resolution=720, wfov=True, binned=True)

    depth_window_name = 'Mapped coordinates to Depth image'
    color_window_name = 'Click on the Color image'
    cv2.namedWindow(color_window_name, cv2.WINDOW_AUTOSIZE)
    cv2.namedWindow(depth_window_name, cv2.WINDOW_AUTOSIZE)
    cv2.setMouseCallback(color_window_name, mouse_event)

    points_3d = []
    prev_points_3d = []

    while True:
        if kin.get_frames(get_color=True,
                          get_depth=True,
                          get_ir=False,
                          align_depth=True):
            # get buffer data from Kinect
            color_data = kin.get_color_data()
            depth_data = kin.get_depth_data()
            ir_data = kin.get_ir_data()
            sensor_data = kin.get_sensor_data()

            # convert buffers to numpy arrays
            depth_image = np.array(depth_data.buffer, copy=True)
            color_image = np.array(color_data.buffer, copy=True)
            color_image = cv2.cvtColor(color_image, cv2.COLOR_BGRA2BGR)

            # Apply colormap on depth image
            depth_colormap = cv2.applyColorMap(
                cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)

            print("Color coords:", color_coords)
            # Project color image points to depth image
            # color_coords is a list of value pairs [[xc1,yc1], [xc2,yc2], ...]
            # depth_coords have the same format as color_coords
            # depth_coords return -1 if there is no depth information
            # at the desire location,
            # e.g. if at [x2, y2] the depth is 0 depth_coords = [[xd1, yd1], [-1, -1]]
            # we select -1 because there can not be negative pixel coordinates
            depth_coords = kin.map_coords_color_to_depth(color_coords)
            print("Depth coords:", depth_coords)

            # Backproject color image points to 3D depth camera space
            # points_3d is a list of triplets of X, Y, Z points in
            # the depth camera reference or color camera reference
            # if color_coords is [[xc1,yc1], [xc2,yc2], ...]
            # points_3d is [[X1, Y1, Z1], [X2, Y2, Z2], ...]
            # points_3d contains [0, 0, 0] if for desired color coordinates
            # there is not depth available.
            # E.g. if at [x2, y2] the depth is 0, points_3d = [[X1, Y1, Z1], [0, 0, 0]]
            # we select 0 because depth = 0 means no depth data.
            points_3d = kin.map_coords_color_to_3D(color_coords,
                                                   depth_reference=False)

            #if points_3d != prev_points_3d:
            print("3D points:", points_3d)

            # Project 3D points image points to depth image and color image
            # points_3d is a list of value triplets [[X1, Y1, Z1], [X2, Y2, Z2], ...]
            # depth_coords2 and color_coords2 have the same format: [[x1, y1], [x2, y2], ...]
            # depth_coords return -1 if there is no depth information
            # at the desire location,
            # e.g. if at [x2, y2] the depth is 0 depth_coords = [[xd1, yd1], [-1, -1]]
            # we select -1 because there can not be negative pixel coordinates
            depth_coords2 = kin.map_coords_3d_to_depth(points_3d,
                                                       depth_reference=False)
            color_coords2 = kin.map_coords_3d_to_color(points_3d,
                                                       depth_reference=False)
            print("Color coords2:", color_coords2)
            print("Depth coords2:", depth_coords2)

            ## Visualization
            # Draw color image points
            for p in color_coords:
                cv2.circle(color_image, (p[0], p[1]), 8, (0, 0, 255), -1)

            # Draw depth points
            for p in depth_coords:
                cv2.circle(depth_colormap, (p[0], p[1]), 5, (0, 0, 255), -1)

            cv2.imshow(depth_window_name, depth_colormap)
            cv2.imshow(color_window_name, color_image)

        prev_points_3d = points_3d

        k = cv2.waitKey(10) & 0xFF
        if k == ord('c'):
            color_coords = []
        if k == 27:
            break

    kin.close()  # close Kinect
    cv2.destroyAllWindows()
예제 #7
0
"""
    Calibration demo

    Shows how to extract the instrinsic and extrinsic parameters.
"""
import numpy as np
import cv2
import kinz

# Create Kinect object and initialize
kin = kinz.Kinect(resolution=1080, wfov=True, binned=True)

# get calibration objects
depth_calib = kin.get_depth_calibration()
color_calib = kin.get_color_calibration()

# extract calibration parameters
depth_size = depth_calib.get_size()  # image size
color_size = color_calib.get_size()

# Intrinsics is a 3x3 if extended=False or 4x4 if extended=True
depth_intrinsics = depth_calib.get_intrinsics_matrix(extended=False)
color_intrinsics = color_calib.get_intrinsics_matrix(extended=False)

# Distortion is a 8x1 numpy vector: k1,k2,p1,p2,k3,k4,k5,k6
depth_dist = depth_calib.get_distortion_params()
color_dist = color_calib.get_distortion_params()

# Rotation is a 3x3 Rotation matrix wrt depth camera
depth_rot = depth_calib.get_rotation_matrix()
color_rot = color_calib.get_rotation_matrix()
예제 #8
0
  Supported resolutions:
  720:  1280 x 720 @ 30 FPS  binned depth
  1080: 1920 x 1080 @ 30 FPS binned depth
  1440: 2560 x 1440 @ 30 FPS binned depth
  1535: 2048 x 1536 @ 30 FPS binned depth
  2160: 3840 x 2160 @ 30 FPS binned depth
  3072: 4096 x 3072 @ 15 FPS binned depth
"""
import numpy as np
import cv2
import kinz

# Create Kinect object and initialize
kin = kinz.Kinect(resolution=4096,
                  wfov=True,
                  binned=False,
                  framerate=30,
                  imu_sensors=False)

# Get depth aligned with color?
align_frames = False

# initialize fps counter
t = cv2.getTickCount()
fps_count = 0
fps = 0

while True:
    if fps_count == 0:
        t = cv2.getTickCount()
예제 #9
0
def main():
    # Create Kinect object and initialize
    kin = kinz.Kinect(resolution=1080,
                      wfov=True,
                      binned=True,
                      framerate=30,
                      imu_sensors=False,
                      body_tracking=True)

    # Get depth aligned with color?
    align_frames = False
    image_scale = 0.5  # visualized image scale

    # initialize fps counter
    t = cv2.getTickCount()
    fps_count = 0
    fps = 0

    while True:
        if fps_count == 0:
            t = cv2.getTickCount()

        # read kinect frames. If frames available return 1
        if kin.get_frames(get_color=True,
                          get_depth=True,
                          get_ir=False,
                          get_sensors=False,
                          get_body=True,
                          get_body_index=True,
                          align_depth=align_frames):

            color_data = kin.get_color_data()
            depth_data = kin.get_depth_data()
            bodies = kin.get_bodies()
            body_index_data = kin.get_body_index_map(returnId=True,
                                                     inColor=False)

            print("bodies:", bodies)

            # extract frames to np arrays
            depth_image = np.array(depth_data.buffer, copy=True)
            color_image = np.array(color_data.buffer,
                                   copy=True)  # image is BGRA
            color_image = cv2.cvtColor(color_image,
                                       cv2.COLOR_BGRA2BGR)  # to BGR
            body_index_image = np.array(body_index_data.buffer, copy=True)
            print(body_index_image.shape)

            # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
            depth_colormap = cv2.applyColorMap(
                cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)

            # Apply colormap on body index image
            body_index_image = cv2.applyColorMap(body_index_image * 10,
                                                 cmapy.cmap('tab20'))

            # Draw bodies on the RGB image
            draw_keypoints(color_image, bodies, img_type='rgb')

            # Draw bodies on the depth image
            draw_keypoints(depth_colormap, bodies, img_type='depth')

            # Resize images
            if align_frames:
                depth_colormap = cv2.resize(depth_colormap,
                                            None,
                                            fx=image_scale,
                                            fy=image_scale)
                body_index_image = cv2.resize(body_index_image,
                                              None,
                                              fx=image_scale,
                                              fy=image_scale)

            color_small = cv2.resize(color_image,
                                     None,
                                     fx=image_scale,
                                     fy=image_scale)
            size = color_small.shape[0:2]
            cv2.putText(color_small, "{0:.2f}-FPS".format(fps),
                        (20, size[0] - 20), cv2.FONT_HERSHEY_COMPLEX, 0.8,
                        (0, 0, 255), 2)

            cv2.imshow('Depth', depth_colormap)
            cv2.imshow('Color', color_small)
            cv2.imshow('Body index', body_index_image)

        k = cv2.waitKey(1) & 0xFF
        if k == 27:
            break
        elif k == ord('s'):
            cv2.imwrite("color.jpg", color_image)
            print("Image saved")

        # increment frame counter and calculate FPS
        fps_count = fps_count + 1
        if (fps_count == 30):
            t = (cv2.getTickCount() - t) / cv2.getTickFrequency()
            fps = 30.0 / t
            fps_count = 0

    kin.close()  # close Kinect
    cv2.destroyAllWindows()