1

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

pimm245
  • 13
  • 3
  • The pykinect2 github repo provides some examples https://github.com/Kinect/PyKinect2/tree/master/examples. Try goinf through those first and ask a more specific question – dracarys May 13 '21 at 09:18

1 Answers1

1

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:

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
Vito Gentile
  • 13,336
  • 9
  • 61
  • 96
  • This is fantastic. I had all the pieces but just couldn't get that extra line or two to make it click. Thank you! – pimm245 May 13 '21 at 17:21
  • You're welcome! Since you're new to StackOverflow, please don't forget to accept and upvote the answer if you think it has been useful – Vito Gentile May 13 '21 at 20:13
  • I think I need a little more reputation to upvote. Maybe this comment will help. But I've run into another issue. Because of the way the example is looped, the code above produces a constant stream of values that oscillate between 0 and the true count -- as n_bodies is reset to 0 upon each loop. My goal is to build a simple tracker that notes when the values change (ie. how much time a person is in frame) but due to the loop, the values are never fixed. So it's more that it needs to set an outside variable, and change the variable when number of bodies changes... – pimm245 May 17 '21 at 22:34
  • I put some code in the post to illustrate what I am attempting – pimm245 May 17 '21 at 22:41
  • I was very in the weeds with this, but did eventually solve it. I have updated the above code for answers to question. – pimm245 May 19 '21 at 00:20