pykinect2实验配置

miniconda python版本=3.8

pycharm随意,只是个IDE

以上安装好后,配置miniconda环境变量

直接设置到系统变量里,也可以设置到用户环境变量里

安装numpy

pip install numpy

安装cv2

pip install opencv-python

安装open3d

pip install open3d

安装pykinect2

pip install pykinect2

安装完后,运行下面的代码,即可采集数据

然后,需要修改两个文件

都在

C:\ProgramData\miniconda3\Lib\site-packages\pykinect2 路径下

1、PykinectV2

assert sizeof(tagSTATSTG) == 72, sizeof(tagSTATSTG)

修改为

assert sizeof(tagSTATSTG) == 80, sizeof(tagSTATSTG)

from comtypes import _check_version; _check_version('')

注释掉

2、PykinectRuntime

time. time()

修改为

time.perf_counter()

这样就完成了,下面是采集使用的代码

import os
import numpy as np
import warnings

warnings.filterwarnings('ignore')
import time

from pykinect2 import PyKinectV2
from pykinect2 import PyKinectRuntime
from pykinect2.PyKinectV2 import *
import cv2
from datetime import datetime
import open3d as o3d
import shutil

# # 获取深度图, 默认尺寸 424x512
def get_last_depth():
    frame0 = kinect.get_last_depth_frame()
    frame1 = frame0.astype(np.uint8)
    dep_frame0 = cv2.cvtColor(np.reshape(frame0, [424, 512]), cv2.COLOR_GRAY2RGB)
    dep_frame1 = cv2.cvtColor(np.reshape(frame1, [424, 512]), cv2.COLOR_GRAY2RGB)
    return dep_frame0,dep_frame1
    # return dep_frame

#获取rgb图, 1080x1920x4
def get_last_rbg():
    frame = kinect.get_last_color_frame()
    frame = np.reshape(frame, [1080, 1920, 4])[:, :, 0:3]

    frame = cv2.resize(frame, (512, 424))
    return frame

def get_file_name():
    return datetime.now().strftime("%Y_%m_%d_%H_%M_%S")


def get_data(name, i ):
    color_raw = get_last_rbg()
    depth_raw, depth_raw1 = get_last_depth()
    # 使用opencv显示图片
    # cv2.imshow('test', last_frame)
    # cv2.waitKey(1)
    # exit()
    cv2.imwrite(RGB_path + f'{name}_{i}.png', color_raw)  # %d 十进制数

    # depth_raw = map_color_point_to_depth_point(depth_raw, if_call_flg=False)
    cv2.imwrite(depth_path + f'{name}_{i}.png', depth_raw)  # %d 十进制数
    cv2.imwrite(depth_path + f'{name}_{i}_RGB.png', depth_raw1)  # %d 十进制数

    if i > 1:  # 因为前几张都是空白所以从,后面的几秒开始采集
        color_raw = o3d.io.read_image(RGB_path + f'{name}_{i}.png')
        depth_raw = o3d.io.read_image(depth_path + f'{name}_{i}.png')

        rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(color_raw, depth_raw,
                                                                        convert_rgb_to_intensity=True)

        inter = o3d.camera.PinholeCameraIntrinsic()

        inter.set_intrinsics(1280, 720, 599.795593, 599.633118, 645.792786, 372.238983)  # 720p的内参
        # inter.set_intrinsics(1920, 1920, 899.693420, 899.449646, 968.939209, 558.608459)  # 1080的内参
        pcd = o3d.geometry.PointCloud().create_from_rgbd_image(rgbd_image, inter)
        pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
        o3d.io.write_point_cloud(point_path + f"{name}_{i}.pcd", pcd)

def create_point_Data():
    time.sleep(1)
    i = 0
    while i < 500:  # 60* 秒, 60* 分钟
        name = get_file_name()
        print(name)
        time.sleep(1)
        if i == 0:
            print('111')
        else:
            get_data(name, i )

        i += 1

def get_the_last_depth():
    """
    FunC:获取最新的深度数据
    """
    if kinect.has_new_depth_frame():
        # 获得深度图数据
        frame = kinect.get_last_depth_frame()
        print(kinect.depth_frame_desc.Height,kinect.depth_frame_desc.Width)
        exit()
        # 转换为图像排列
        image_depth_all = frame.reshape([kinect.depth_frame_desc.Height,
                                         kinect.depth_frame_desc.Width])
        depth_ori = image_depth_all
        return depth_ori
if __name__ == '__main__':
    filename = '.\\temp_data\\'
    RGB_path = os.path.join(filename, 'rgb\\')
    depth_path = os.path.join(filename, 'depth\\')
    point_path = os.path.join(filename, 'point\\')

    shutil.rmtree(os.path.join(filename, 'rgb\\'),)
    shutil.rmtree(os.path.join(filename, 'depth\\'))
    shutil.rmtree(os.path.join(filename, 'point\\'))

    os.makedirs(RGB_path, exist_ok=True)
    os.makedirs(depth_path, exist_ok=True)
    os.makedirs(point_path, exist_ok=True)

    kinect = PyKinectRuntime.PyKinectRuntime(PyKinectV2.FrameSourceTypes_Depth | PyKinectV2.FrameSourceTypes_Color)
    create_point_Data()

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值