New to pykinect and kinect in general -- trying to simply get a count of bodies currently being tracked. No skeletal or joint data required. Just want to get a running count of bodies currently in frame. I am using a kinect-v2 and pykinect2.
Being more specific, I'm trying to track how many bodies are in frame and the time elapsed since that value changed. (0 people to 1 person, 1 person to 2, etc.) Due to the built examples for pykinect and the way that they loop, this has proven difficult however. The latest attempt (Now updated with the solved code):
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();
I found a useful snippet that does what you need within one of the examples provided in the PyKinect2 GitHub repo.
You need to get the body frame, and then count the number of tracked bodies: