如何使用freenect2-python

问题描述

我正在使用freenect2-python从kinectv2中读取帧。以下是我的代码

from freenect2 import Device,FrameType
import cv2
import numpy as np

def callback(type_,frame):
    print(f'{type_},{frame.format}') 
    if type_ is FrameType.Color: # FrameFormat.BGRX
        rgb = frame.to_array().astype(np.uint8)
        cv2.imshow('rgb',rgb[:,:,0:3])

device = Device()
while True:
    device.start(callback)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        device.stop()
        break

色框格式为FrameFormat.BGRX,所以我采用前3个通道来显示图像。但是它显示一个空白的黑色窗口。

我使用了PIL,但是它为接收到的每一帧打开了一个新窗口。是否可以在PIL的同一窗口中显示帧?

解决方法

cv2.imshow无法显示任何内容,因为它不断被新框架更新。我在cv2.waitkey(100)之后添加了cv2.imshow,它可以正常工作。

def callback(type_,frame):
    print(f'{type_},{frame.format}') 
    if type_ is FrameType.Color: # FrameFormat.BGRX
        rgb = frame.to_array().astype(np.uint8)
        cv2.imshow('rgb',rgb[:,:,0:3])
        # added the following line of code
        cv2.imshow(100)