Ubuntu下python调用kinect v2

Ubuntu下python调用kinect v2

工作环境:
Ubuntu16.04
python2.7.2
本教程在不使用ros的情况下,可以通过python-opencv直接读取kinect的图像数据。

python调用kinect,三种方法:

方法一:

首先安装libfreenect2驱动
通过pyfreenect接口,此方法已经在Ubuntu上实现可行(windows可行,未尝试)
或者通过openni接口,实现在Ubuntu上,可行(windows可行,未尝试)

方法二:

通过ros包,可以实现kinect使用,并且运行节点将图像节点发布出去。

方法三:

通过pykinect方式,python在直接使用kinect时,直接导入
from pykinect2 import PyKinectV2
from pykinect2 import PyKinectRuntime即可,(迄今已知在Windows平台下,Ubuntu下未找到使用方法,此方法应用也简单一些,理想使用方法)

安装pyfreenect接口参考链接:
Ubuntu16.04 配置kinect2 python接口

注意其中几个步骤是安装opencv等一些包,安装过opencv的可以不用重新安装。

安装openni接口参考链接:
Ubuntu16.04安装OpenNI2(最简单)
unbuntu18.04安装openni2过程记录

**本文通过openni的python接口:

并且定义函数,通过调用函数就可以完成kinect图像的获取,类似于opencv的capture功能。**

#!/usr/bin/python2
# coding=utf-8

"""
# --------------------------------------------------------
# @Project: kinect huoqu tuxiang
# @Author : qiuzhixiaocainiao
# @E-mail : 1964696833@qq.com
# @Date   : 2021-04-04 15:26:01
# --------------------------------------------------------
"""

from openni import openni2
import numpy as np
import cv2

openni2.initialize()     # can also accept the path of the OpenNI redistribution

dev = openni2.Device.open_any()
print(dev.get_device_info())


depth_stream = dev.create_depth_stream()
color_stream = dev.create_color_stream()
depth_stream.start()
color_stream.start()


# 获取深度图, 默认尺寸 424x512
def get_last_depth():
    #depth_stream = dev.create_depth_stream()
    #depth_stream.start()
    # 显示深度图
    frame = depth_stream.read_frame()
    dframe_data = np.array(frame.get_buffer_as_triplet()).reshape([480, 640, 2])
    dpt1 = np.asarray(dframe_data[:, :, 0], dtype='float32')
    dpt2 = np.asarray(dframe_data[:, :, 1], dtype='float32')
    dpt2 *= 255
    dpt = dpt1 + dpt2
    #cv2.imshow('dpt', dpt)
    return dpt
    depth_stream.stop()

#获取rgb图, 1080x1920x4
def get_last_rgb():
    #color_stream = dev.create_color_stream()
    #color_stream.start()
    # 显示RGB图像
    cframe = color_stream.read_frame()
    cframe_data = np.array(cframe.get_buffer_as_triplet()).reshape([1080, 1920, 3])
    R = cframe_data[:, :, 0]
    G = cframe_data[:, :, 1]
    B = cframe_data[:, :, 2]
    cframe_data = np.transpose(np.array([B, G, R]), [1, 2, 0])
    # print(cframe_data.shape)
    #cv2.imshow('color', cframe_data)
    #print(cframe_data)
    return cframe_data    
    color_stream.stop()


if __name__ == "__main__":

    for i in range(1000):
        rgb = get_last_rgb()
        cv2.imshow('rgb',rgb)
        depth = get_last_depth()
        print("depth:",depth[100][100])
        cv2.imshow('depth',depth)
        cv2.waitKey(1)
        print(i)
    # 人走带门,关闭设备
    dev.close()

其他参考链接:

Ubuntu18.04配置Kinect2-python接口
另外还有一种方式pykinec,没有尝试,
参考链接:
python调用Kinect-V2 rgb与深度图
https://github.com/Kinect/PyKinect2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

求知小菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值