1

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

4

1 回答 1

0

我在 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
于 2021-05-13T07:28:03.840 回答