一般来说,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();发布于 2021-05-13 15:28:04
我发现了一个有用的代码片段,它可以在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 bodieshttps://stackoverflow.com/questions/67512150
复制相似问题