Kinect2.0-Python调用-PyKinect2

Kinect2.0-Python调用-PyKinect2

一.环境

1.win10

2.python3.8.5

3.Kinect2.0SDK

4.opencv4.5.1

5.vscode

二.调试步骤

1.安装Kinect2.0SDK

​ 正常安装即可

2.安装python版本为3.8.5的anaconda

3.安装OpenCV

​ 下载opencv_python-4.5.1.48-cp38-cp38-win_amd64.whl和opencv_contrib_python-4.5.1.48-cp38-cp38-win_amd64.whl到anaconda的base路径下

​ 在命令行中执行pip install opencv_python-4.5.1.48-cp38-cp38-win_amd64.whl和pip install opencv_contrib_python-4.5.1.48-cp38-cp38-win_amd64.whl。

4.在vscode中执行代码

# -*- coding:utf-8 -*-
"""
time:2019/5/1 19:34
author:Lance
organization: HIT
contact: QQ:261983626 , wechat:yuan261983626
——————————————————————————————
description:
$ 自己基于Pykinect2 写的一个Kinect的类。
主要包括:
彩色图像、深度图像、红外图像的获取
彩色图像和深度图像的坐标空间互换
——————————————————————————————
note:
$
"""
from pykinect2 import PyKinectV2
from pykinect2.PyKinectV2 import *
from pykinect2 import PyKinectRuntime
import numpy as np
import ctypes
import math
import cv2 as cv
import time
import copy

class Kinect(object):
    def __init__(self):
        self._kinect = PyKinectRuntime.PyKinectRuntime(PyKinectV2.FrameSourceTypes_Color | PyKinectV2.FrameSourceTypes_Depth | PyKinectV2.FrameSourceTypes_Infrared)
        self.depth_ori = None
        self.infrared_frame = None
        self.color_frame = None
        self.w_color = 1920
        self.h_color = 1080
        self.w_depth = 512
        self.h_depth = 424
        """————————————————(2019/5/10)——————————————————"""
        self.csp_type = _ColorSpacePoint * np.int(1920 * 1080)
        self.csp = ctypes.cast(self.csp_type(), ctypes.POINTER(_DepthSpacePoint))
        """————————————————(2019/9/4)——————————————————"""
        self.color = None
        self.depth = None
        self.depth_draw = None
        self.color_draw = None
        self.infrared = None
        self.first_time = True
    """————————————————(2019/5/1)——————————————————"""
    """获取最新的图像数据"""
    def get_the_last_color(self):
        """
        Time :2019/5/1
        FunC:获取最新的图像数据
        Input:无
        Return:无
        """
        if self._kinect.has_new_color_frame():
            # 获得的图像数据是二维的,需要转换为需要的格式
            frame = self._kinect.get_last_color_frame()
            # 返回的是4通道,还有一通道是没有注册的
            gbra = frame.reshape([self._kinect.color_frame_desc.Height, self._kinect.color_frame_desc.Width, 4])
            # 取出彩色图像数据
            self.color_frame = gbra[:, :, 0:3]
            return self.color_frame
    """————————————————(2019/5/1)——————————————————"""
    """获取最新的深度数据"""
    def get_the_last_depth(self):
        """
        Time :2019/5/1
        FunC:获取最新的图像数据
        Input:无
        Return:无
        """
        if self._kinect.has_new_depth_frame():
            # 获得深度图数据
            frame = self._kinect.get_last_depth_frame()
            # 转换为图像排列
            image_depth_all = frame.reshape([self._kinect.depth_frame_desc.Height,
                                             self._kinect.depth_frame_desc.Width])
            self.depth_ori = image_depth_all

            return self.depth_ori

    """————————————————(2019/5/1)——————————————————"""
    """获取最新的红外数据"""
    def get_the_last_infrared(self):
        """
        Time :2019/5/1
        FunC:获取最新的图像数据
        Input:无
        Return:无
        """
        if self._kinect.has_new_infrared_frame():
            # 获得深度图数据
            frame = self._kinect.get_last_infrared_frame()
            # 转换为图像排列
            image_infrared_all = frame.reshape([self._kinect.infrared_frame_desc.Height,
                                             self._kinect.infrared_frame_desc.Width])
            self.infrared_frame = image_infrared_all
            return self.infrared_frame

    """————————————————(2019/5/1)——————————————————"""
    """将深度像素点匹配到彩色图像中"""
    def map_depth_point_to_color_point(self, depth_point):
        """
        Time :2019/5/1
        FunC: 将深度图像坐标映射到彩色坐标中,
        Input: depth_points:深度像素点,列表、数组格式,图像坐标,且为人眼视角
        Return: color_points:对应的彩色像素点,列表格式
        均采用图像坐标的形式
        """
        depth_point_to_color  = copy.deepcopy(depth_point)
        n = 0
        while 1:
            self.get_the_last_depth()
            self.get_the_last_color()
            if self.depth_ori is None:
                continue
            color_point = self._kinect._mapper.MapDepthPointToColorSpace(
                _DepthSpacePoint(511-depth_point_to_color[1], depth_point_to_color[0]), self.depth_ori[depth_point_to_color[0], 511-depth_point_to_color[1]])
            """————————————————(2019/6/22)——————————————————"""
            # cobot 第二次培训更改
            # color_point = self._kinect._mapper.MapDepthPointToColorSpace(
            #     _DepthSpacePoint(depth_point[0], depth_point[1]), self.depth[depth_point[1], depth_point[0]])
            """————————————————(2019/6/22)——————————————————"""
            if math.isinf(float(color_point.y)):
                n += 1
                if n >= 50000:
                    print('深度映射彩色,无效点')
                    color_point = [0, 0]
                    break
            else:
                color_point = [np.int0(color_point.y), 1920-np.int0(color_point.x)]  # 图像坐标,人眼视角
                break
        return color_point
    """————————————————(2019/5/10)——————————————————"""
    """将彩色像素数组映射到深度图像中"""
    def map_color_points_to_depth_points(self, color_points):
        """
        Time :2019/5/1
        FunC: 将深度图像坐标映射到彩色坐标中,输入的为图像坐标
        Input: depth_points:彩色像素点
        Return: color_points:对应的深度像素点,图像坐标
        """
        self.get_the_last_depth()
        self.get_the_last_color()
        self._kinect._mapper.MapColorFrameToDepthSpace(
            ctypes.c_uint(512 * 424), self._kinect._depth_frame_data, ctypes.c_uint(1920 * 1080), self.csp)
        depth_points = [self.map_color_point_to_depth_point(x, True) for x in color_points]
        return depth_points
    """————————————————(2019/5/1)——————————————————"""
    """将彩色像素点映射到深度图像中"""
    def map_color_point_to_depth_point(self, color_point, if_call_flg=False):
        """
        Time :2019/5/1
        FunC: 将深度图像坐标映射到彩色坐标中,输入的为图像坐标
        Input: depth_points:彩色像素点
        Return: color_points:对应的深度像素点,图像坐标
        """
        n = 0
        color_point_to_depth = copy.deepcopy(color_point)
        color_point_to_depth[1] = 1920 - color_point_to_depth[1]
        while 1:
            self.get_the_last_depth()
            self.get_the_last_color()
            # self.depth = cv.medianBlur(image_depth_all, 5)
            if not if_call_flg:
                self._kinect._mapper.MapColorFrameToDepthSpace(
                    ctypes.c_uint(512 * 424), self._kinect._depth_frame_data, ctypes.c_uint(1920 * 1080), self.csp)
            if math.isinf(float(self.csp[color_point_to_depth[0]*1920+color_point_to_depth[1]-1].y)) or np.isnan(self.csp[color_point_to_depth[0]*1920+color_point_to_depth[1]-1].y):
                n += 1
                if n >= 50000:
                    print('彩色映射深度,无效的点')
                    depth_point = [0, 0]
                    break
            else:
                self.cor = self.csp[color_point_to_depth[0]*1920+color_point_to_depth[1]-1].y
                try:
                    depth_point = [np.int0(self.csp[color_point_to_depth[0]*1920+color_point_to_depth[1]-1].y),
                                   np.int0(self.csp[color_point_to_depth[0]*1920+color_point_to_depth[1]-1].x)]
                except OverflowError as e:
                    print('彩色映射深度,无效的点')
                    depth_point = [0, 0]
                break
        depth_point[1] = 512-depth_point[1]
        return depth_point

        # depth_points = [self._kinect._mapper.MapColorPointToDepthSpace(_ColorSpacePoint(color_point[0],color_point[1]),self.color_frame[depth_point]))
        #                 for depth_point in depth_points]
        # return color_points
    """————————————————(2019/4/26)——————————————————"""
    """获得最新的彩色和深度图像以及红外图像"""
    def get_the_data_of_color_depth_infrared_image(self, Infrared_threshold = 16000):
        """
        # ————————查看是否有新的一帧————————
        :return:
        """
        # 访问新的RGB帧
        time_s = time.time()
        if self.first_time:
            while 1:
                n = 0
                if self._kinect.has_new_color_frame():
                    #                 # 获得的图像数据是二维的,需要转换为需要的格式
                    frame = self._kinect.get_last_color_frame()
                    # 返回的是4通道,还有一通道是没有注册的
                    gbra = frame.reshape([self._kinect.color_frame_desc.Height, self._kinect.color_frame_desc.Width, 4])
                    # 取出彩色图像数据
                    # self.color = gbra[:, :, 0:3]
                    self.color = gbra[:, :, 0:3][:,::-1,:]
                    # 这是因为在python中直接复制该图像的效率不如直接再从C++中获取一帧来的快
                    frame = self._kinect.get_last_color_frame()
                    # 返回的是4通道,还有一通道是没有注册的
                    gbra = frame.reshape([self._kinect.color_frame_desc.Height, self._kinect.color_frame_desc.Width, 4])
                    # 取出彩色图像数据
                    # self.color_draw = gbra[:, :, 0:3][:,::-1,:]
                    self.color_draw = gbra[:, :, 0:3][:,::-1,:]
                    n += 1
                # 访问新的Depth帧
                if self._kinect.has_new_depth_frame():
                    # 获得深度图数据
                    frame = self._kinect.get_last_depth_frame()
                    # 转换为图像排列
                    image_depth_all = frame.reshape([self._kinect.depth_frame_desc.Height,
                                                     self._kinect.depth_frame_desc.Width])
                    # 转换为(n,m,1) 形式
                    image_depth_all = image_depth_all.reshape(
                        [self._kinect.depth_frame_desc.Height, self._kinect.depth_frame_desc.Width, 1])
                    self.depth_ori = np.squeeze(image_depth_all)
                    self.depth = np.squeeze(image_depth_all)[:,::-1]

                    """————————————————(2019/5/11)——————————————————"""
                    # 获得深度图数据
                    frame = self._kinect.get_last_depth_frame()
                    # 转换为图像排列
                    depth_all_draw = frame.reshape([self._kinect.depth_frame_desc.Height,
                                                     self._kinect.depth_frame_desc.Width])
                    # 转换为(n,m,1) 形式
                    depth_all_draw = depth_all_draw.reshape(
                        [self._kinect.depth_frame_desc.Height, self._kinect.depth_frame_desc.Width, 1])
                    depth_all_draw[depth_all_draw >= 1500] = 0
                    depth_all_draw[depth_all_draw <= 500] = 0
                    depth_all_draw = np.uint8(depth_all_draw / 1501 * 255)
                    self.depth_draw = depth_all_draw[:,::-1,:]
                    n += 1
                # 获取红外数据
                if self._kinect.has_new_infrared_frame():
                    # 获得深度图数据
                    frame = self._kinect.get_last_infrared_frame()
                    # 转换为图像排列
                    image_infrared_all = frame.reshape([self._kinect.depth_frame_desc.Height,
                                                     self._kinect.depth_frame_desc.Width])
                    # 转换为(n,m,1) 形式
                    image_infrared_all[image_infrared_all > Infrared_threshold] = 0
                    image_infrared_all = image_infrared_all / Infrared_threshold * 255
                    self.infrared = image_infrared_all[:,::-1]
                    n += 1
                t = time.time() - time_s
                if n == 3:
                    self.first_time = False
                    break
                elif t > 5:
                    print('未获取图像数据,请检查Kinect2连接是否正常')
                    break
        else:
            if self._kinect.has_new_color_frame():
                #                 # 获得的图像数据是二维的,需要转换为需要的格式
                frame = self._kinect.get_last_color_frame()
                # 返回的是4通道,还有一通道是没有注册的
                gbra = frame.reshape([self._kinect.color_frame_desc.Height, self._kinect.color_frame_desc.Width, 4])
                # 取出彩色图像数据
                # self.color = gbra[:, :, 0:3]
                self.color = gbra[:, :, 0:3][:, ::-1, :]
                # 这是因为在python中直接复制该图像的效率不如直接再从C++中获取一帧来的快
                frame = self._kinect.get_last_color_frame()
                # 返回的是4通道,还有一通道是没有注册的
                gbra = frame.reshape([self._kinect.color_frame_desc.Height, self._kinect.color_frame_desc.Width, 4])
                # 取出彩色图像数据
                # self.color_draw = gbra[:, :, 0:3][:,::-1,:]
                self.color_draw = gbra[:, :, 0:3][:, ::-1, :]

            # 访问新的Depth帧
            if self._kinect.has_new_depth_frame():
                # 获得深度图数据
                frame = self._kinect.get_last_depth_frame()
                # 转换为图像排列
                image_depth_all = frame.reshape([self._kinect.depth_frame_desc.Height,
                                                 self._kinect.depth_frame_desc.Width])
                # 转换为(n,m,1) 形式
                image_depth_all = image_depth_all.reshape(
                    [self._kinect.depth_frame_desc.Height, self._kinect.depth_frame_desc.Width, 1])
                self.depth_ori = np.squeeze(image_depth_all)
                self.depth = np.squeeze(image_depth_all)[:, ::-1]

                """————————————————(2019/5/11)——————————————————"""
                # 获得深度图数据
                frame = self._kinect.get_last_depth_frame()
                # 转换为图像排列
                depth_all_draw = frame.reshape([self._kinect.depth_frame_desc.Height,
                                                self._kinect.depth_frame_desc.Width])
                # 转换为(n,m,1) 形式
                depth_all_draw = depth_all_draw.reshape(
                    [self._kinect.depth_frame_desc.Height, self._kinect.depth_frame_desc.Width, 1])
                depth_all_draw[depth_all_draw >= 1500] = 0
                depth_all_draw[depth_all_draw <= 500] = 0
                depth_all_draw = np.uint8(depth_all_draw / 1501 * 255)
                self.depth_draw = depth_all_draw[:, ::-1, :]

            # 获取红外数据
            if self._kinect.has_new_infrared_frame():
                # 获得深度图数据
                frame = self._kinect.get_last_infrared_frame()
                # 转换为图像排列
                image_infrared_all = frame.reshape([self._kinect.depth_frame_desc.Height,
                                                    self._kinect.depth_frame_desc.Width])
                # 转换为(n,m,1) 形式
                image_infrared_all[image_infrared_all > Infrared_threshold] = 0
                image_infrared_all = image_infrared_all / Infrared_threshold * 255
                self.infrared = image_infrared_all[:, ::-1]





        return self.color, self.color_draw, self.depth, self.depth_draw, self.infrared
    """————————————————(2019/9/3)——————————————————"""
    """显示各种图像的视频流"""
    def Kinect_imshow(self,type_im='rgb'):
        """
        Time :2019/9/3
        FunC:
        Input: color_data
        Return: color_data
        """
        if type_im =='all':
            pass
        elif type_im =='rgb':
            pass
        elif type_im =='depth':
            pass
        elif type_im =='grared':
            pass


if __name__ == '__main__':
    a = Kinect()
    while 1:
        t = time.time()
        color_data = a.get_the_data_of_color_depth_infrared_image()
        cv.imshow('a',color_data[0])
        cv.waitKey(1)


注意:代码执行后会报clock的错误。因为python3.8开始就不支持time了,所以要在PyKinectRuntime中将time.clock()改为time.perf_counter()。

(下载安装Bandzip打开PyKinectRuntime.py)

然后就可以顺利运行了!!!

  • 5
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
Kinect是微软开发的一款体感控制设备,Kinect2.0是该设备的第二代产品。Kinect2.0程序开发文档是微软提供给开发者的一份指南,旨在帮助他们了解如何使用和开发Kinect2.0的应用程序。 该开发文档包含了以下内容: 1. 硬件介绍:文档首先介绍了Kinect2.0的硬件组成,包括摄像头、红外传感器、麦克风等。开发者可以通过了解硬件的具体功能和特点,为后续的程序开发做好准备。 2. 开发环境搭建:文档详细介绍了如何搭建Kinect2.0的开发环境,包括操作系统的选择和安装、Kinect2.0 SDK的下载和配置等。开发者需要按照文档的步骤进行操作,确保开发环境的稳定和正确。 3. API和工具使用:文档中提供了Kinect2.0的应用程序编程接口(API)的使用指南,包括如何使用API读取Kinect2.0传感器的数据、如何控制设备的运动和触摸等。开发者可以通过学习和使用这些API,为自己的应用程序添加更多的交互和体验。 4. 示例代码和案例:文档中还提供了一些示例代码和案例,用于帮助开发者更好地理解和使用Kinect2.0。开发者可以通过研究这些示例,了解Kinect2.0的各种功能和应用场景,并从中获取灵感和思路。 总之,Kinect2.0程序开发文档是一份重要的参考资料,对于想要使用和开发Kinect2.0的应用程序的开发者来说,具有很大的帮助。文档详细介绍了硬件的组成、开发环境的搭建、API的使用和示例代码的分析,为开发者提供了一个系统而全面的学习和开发平台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值