python入门(2):调用kinect相机 安装与配置(含完整代码)

软件需要
win7
python
opencv
kinect官方驱动(kinect studio v1.8.0和Developer Toolkit Browser v1.8.0
openni2
vs2010(应该要,不是百分百确定)

下载资源
基本在CSDN里都找得到,除了vs,其他都是在官网下载的,风险也较小。

openni2 建议安装到默认文件夹。

参考贴
安装的驱动类型和我不一样,主要参考了里面的深度图和彩色图代码)
https://blog.csdn.net/Peng154/article/details/79127630
https://blog.csdn.net/chenxin_130/article/details/6693390
还有这位台湾大神heresy的系列介绍文章
http://viml.nchc.org.tw/blog/paper_info.php?CLASS_ID=1&SUB_ID=1&PAPER_ID=215

机器学习小白,捣鼓了几天,终于可以用了,不容易。

samples simpleviewer运行如图
python入门(2):调用kinect相机 安装与配置(含完整代码)_第1张图片

#!/usr/bin/python
# -*- coding: utf-8 -*-

from openni import openni2
import numpy as np
import cv2
import sys
import time

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()


while True:
    # 显示深度图
    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)

    # 显示RGB图像
    cframe = color_stream.read_frame()
    cframe_data = np.array(cframe.get_buffer_as_triplet()).reshape([480, 640, 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)
    
    i=0
#while(1):
#    ret,frame = cframe_data.read()
    if cv2.waitKey(1) == ord('s'):
        cv2.imwrite('E:/cvsave/%d.jpg'%i,cframe_data)  #%d 十进制数
        i += 1
    #waitKey()函数在一个给定的时间内(单位ms)等待用户按键触发;如果用户没有按下 键,则接续等待(循环)
    
    # 按下q键退出循环
   
# 关闭设备
depth_stream.stop()
color_stream.stop()
dev.close()

代码转自:CSDN帖:Win10安装OpenNI2并通过python接口调用Kinect
如有侵权,请联系

你可能感兴趣的:(python入门(2):调用kinect相机 安装与配置(含完整代码))