PyKinectv2 Body 跟踪计数

PyKinectv2 Body tracking count

pykinect 和一般 kinect 的新手 -- 尝试简单地获取当前正在跟踪的物体的数量。不需要骨骼或关节数据。只想获得 运行 当前帧中的身体数量。我正在使用 kinect-v2 和 pykinect2。

更具体地说,我正在尝试跟踪帧中有多少物体以及该值更改后经过的时间。 (0 人对 1 人,1 人对 2 人,等等)由于 pykinect 的内置示例及其循环方式,这已被证明是困难的。最新尝试(现已更新解决代码):

from pykinect2 import PyKinectV2
from pykinect2.PyKinectV2 import *
from pykinect2 import PyKinectRuntime

import ctypes
import _ctypes
import pygame
import sys
#from timer import Timer
import time
from datetime import datetime

if sys.hexversion >= 0x03000000:
    import _thread as thread
else:
    import thread

class Runtime(object):
    def __init__(self):
        pygame.init()
        
        self._kinect = PyKinectRuntime.PyKinectRuntime(PyKinectV2.FrameSourceTypes_Color | PyKinectV2.FrameSourceTypes_Body)
        self._done = False
        self._clock = pygame.time.Clock()
        self._bodies = None


    def run(self):
        peeps = 0
        end_time = 0
        while not self._done:
            n_bodies = 0

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self._done = True
                    
            if self._kinect.has_new_body_frame():
                self._bodies = self._kinect.get_last_body_frame()


                if self._bodies is not None:                    
                    for i in range(0, self._kinect.max_body_count):
                        body = self._bodies.bodies[i]
                        if body.is_tracked:
                            n_bodies +=1
                            if not body.is_tracked:
                                continue


                    if n_bodies == 0:
                        if peeps == 0:
                            pass
                            
                        
                        if peeps != 0:
                            end_time = time.perf_counter() - start_time
                            round_time = round(end_time)
                            print(peeps, "person stood in front of the kinect for", round_time, "seconds at", dateTimeObj.hour, ":", dateTimeObj.minute, ":", dateTimeObj.second)
                            end_time = 0
                            start_time = 0
                            peeps = 0
                            
                                
                    if n_bodies == 1:
                        if peeps == [2, 3, 4, 5, 6]:
                            end_time = time.perf_counter() - start_time
                            round_time = round(end_time)
                            print(peeps, "person stood in front of the kinect for", round_time, "seconds at", dateTimeObj.hour, ":", dateTimeObj.minute, ":", dateTimeObj.second)
                            end_time = 0
                            start_time = 0
                            peeps = 1

                        if peeps == 0:
                            #print(n_bodies)
                            peeps = 1
                            start_time = time.perf_counter()
                            dateTimeObj = datetime.now()
                            

                        if peeps == 1:
                            pass

                    if n_bodies == 2:
                        if peeps == [1, 3, 4, 5, 6]:
                            end_time = time.perf_counter() - start_time
                            round_time = round(end_time)
                            print(peeps, "person stood in front of the kinect for", round_time, "seconds at", dateTimeObj.hour, ":", dateTimeObj.minute, ":", dateTimeObj.second)
                            end_time = 0
                            start_time = 0
                            peeps = 2
                            start_time = time.perf_counter()
                            dateTimeObj = datetime.now()
                            
                        if peeps == 0:
                            #print(n_bodies)
                            peeps = 2
                            start_time = time.perf_counter()
                            dateTimeObj = datetime.now()
                            

                        if peeps == 2:
                            pass

                    if n_bodies == 3:
                        if peeps == [1, 2, 4, 5, 6]:
                            end_time = time.perf_counter() - start_time
                            round_time = round(end_time)
                            print(peeps, "person stood in front of the kinect for", round_time, "seconds at", dateTimeObj.hour, ":", dateTimeObj.minute, ":", dateTimeObj.second)
                            end_time = 0
                            start_time = 0
                            peeps = 3
                            start_time = time.perf_counter()
                            dateTimeObj = datetime.now()
                            
                        if peeps == 0:
                            #print(n_bodies)
                            peeps = 3
                            start_time = time.perf_counter()
                            dateTimeObj = datetime.now()
                            

                        if peeps == 3:
                            pass

                    if n_bodies == 4:
                        if peeps == [1, 3, 4, 5, 6]:
                            end_time = time.perf_counter() - start_time
                            round_time = round(end_time)
                            print(peeps, "person stood in front of the kinect for", round_time, "seconds at", dateTimeObj.hour, ":", dateTimeObj.minute, ":", dateTimeObj.second)
                            end_time = 0
                            start_time = 0
                            peeps = 4
                            start_time = time.perf_counter()
                            dateTimeObj = datetime.now()

                        if peeps == 0:
                            #print(n_bodies)
                            peeps = 4
                            start_time = time.perf_counter()
                            dateTimeObj = datetime.now()
                            

                        if peeps == 4:
                            pass

                    if n_bodies == 5:
                        if peeps == [1, 2, 3, 4, 6]:
                            end_time = time.perf_counter() - start_time
                            round_time = round(end_time)
                            print(peeps, "person stood in front of the kinect for", round_time, "seconds at", dateTimeObj.hour, ":", dateTimeObj.minute, ":", dateTimeObj.second)
                            end_time = 0
                            start_time = 0
                            peeps = 5
                            start_time = time.perf_counter()
                            dateTimeObj = datetime.now()
                            
                        if peeps == 0:
                            #print(n_bodies)
                            peeps = 5
                            start_time = time.perf_counter()
                            dateTimeObj = datetime.now()
                            

                        if peeps == 5:
                            pass

                    if n_bodies == 6:
                        if peeps == [1, 2, 3, 4, 5]:
                            end_time = time.perf_counter() - start_time
                            round_time = round(end_time)
                            print(peeps, "person stood in front of the kinect for", round_time, "seconds at", dateTimeObj.hour, ":", dateTimeObj.minute, ":", dateTimeObj.second)
                            end_time = 0
                            start_time = 0
                            peeps = 6
                            start_time = time.perf_counter()
                            dateTimeObj = datetime.now()

                        if peeps == 0:
                            #print(n_bodies)
                            peeps = 6
                            start_time = time.perf_counter()
                            dateTimeObj = datetime.now()
                            

                        if peeps == 6:
                            pass

            self._clock.tick(60)
                        
        self._kinect.close()
        pygame.quit()

game = Runtime();
game.run();

我在 one of the examples provided in the PyKinect2 GitHub repo.

中找到了一个有用的片段,可以满足您的需求

您需要获取body frame,然后统计跟踪的body数量:

kinect = PyKinectRuntime.PyKinectRuntime(PyKinectV2.FrameSourceTypes_Color | PyKinectV2.FrameSourceTypes_Body)
body_frame = kinect.get_last_body_frame()

n_bodies = 0
if body_frame is not None: 
    for i in range(0, kinect.max_body_count):
        if body_frame.bodies[i].is_tracked: 
            n_bodies += 1

# n_bodies contains now the number of tracked bodies