Esempio n. 1
0
def main():
    """Initialize and run the game."""
    pygame.init()

    # Initialize PyGame
    screen = pygame.display.set_mode(WINDOW_SIZE, 0, 16)
    pygame.display.set_caption('PyKinect Skeleton Example')
    screen.fill(pygame.color.THECOLORS["black"])

    with nui.Runtime() as kinect:
        kinect.skeleton_engine.enabled = True
        kinect.skeleton_frame_ready += post_frame

        # Main game loop
        while True:
            event = pygame.event.wait()

            if event.type == pygame.QUIT:
                break
            elif event.type == KINECTEVENT:
                # apply joint filtering
                kinect._nui.NuiTransformSmooth(event.skeleton_frame, SMOOTH_PARAMS)

                draw_skeletons(pygame.display.Info(), screen, event.skeleton_frame.SkeletonData)
                pygame.display.update()
                pass
Esempio n. 2
0
def getImage():
    try:
        kinect = nui.Runtime()
        kinect.video_frame_ready += video_handler_function
        kinect.video_stream.open(nui.ImageStreamType.Video, 2,
                                 nui.ImageResolution.Resolution640x480,
                                 nui.ImageType.color)
        cv2.namedWindow('KINECT Video Stream', cv2.WINDOW_AUTOSIZE)

        time = 0
        while True:
            key = cv2.waitKey(1)
            print(time)
            time += 1

            if key == ord(' ') or time == 500:
                print(" Get Image -- Succesfully !!")
                print(pos)
                break

        kinect.close()
        cv2.destroyAllWindows()

        return pos

    except:
        print(" In Chang ERROR function -- getImage")
Esempio n. 3
0
def kinectLoop():
    kinect = nui.Runtime()
    kinect.skeleton_engine.enabled = True

    def post_frame(frame):
        try:
            pygame.event.post(
                pygame.event.Event(KINECTEVENT, skeletons=frame.SkeletonData))
        except:
            pass

    kinect.skeleton_frame_ready += post_frame
    kinect.depth_frame_ready += depth_frame_ready
    kinect.video_frame_ready += video_frame_ready
    kinect.video_stream.open(nui.ImageStreamType.Video, 2,
                             nui.ImageResolution.Resolution640x480,
                             nui.ImageType.Color)
    kinect.depth_stream.open(nui.ImageStreamType.Depth, 2,
                             nui.ImageResolution.Resolution640x480,
                             nui.ImageType.Depth)
    # main game loop
    done = False
    while not done:
        e = pygame.event.wait()
        dispInfo = pygame.display.Info()
        print(dispInfo)
        if e.type == pygame.QUIT:
            done = True
            break
        elif e.type == KINECTEVENT:
            skeletons = e.skeletons
            if draw_skeleton:
                draw_skeletons(skeletons)
                kinectDisplay.Update(e)
Esempio n. 4
0
    def start(self):
        pygame.init()
        self.kinect = nui.Runtime()

        self.kinect.camera.elevation_angle = self.elevation_angle

        self.kinect.skeleton_engine.enabled = True

        if self.video_display or self.depth_display:
            self.screen_lock = thread.allocate()
            self.screen = pygame.display.set_mode(VIDEO_WINSIZE if self.video_display else DEPTH_WINSIZE, 0, 32 if self.video_display else 16)
            pygame.display.set_caption('PyKinect')
            self.skeletons = None
            self.screen.fill(THECOLORS["black"])
            
            if self.video_display:
                self.kinect.video_frame_ready += self.video_frame_ready
                self.kinect.video_stream.open(nui.ImageStreamType.Video, 2, nui.ImageResolution.Resolution640x480, nui.ImageType.Color)
            if self.depth_display:
                self.kinect.depth_frame_ready += self.depth_frame_ready
                self.kinect.depth_stream.open(nui.ImageStreamType.Depth, 2, nui.ImageResolution.Resolution640x480, nui.ImageType.Depth)

        #self.kinect.skeleton_frame_ready += self.post_frame

        self.dispInfo = pygame.display.Info()

        self.update_skeleton()
Esempio n. 5
0
    def __init__(self):
        super(view, self).__init__()

        # Declare and define variables of view class
        self.time = QTime()
        self.timer = QTimer()
        self.x = 0.0
        self.fruitList = []
        self.isTime = 0
        self.kinecttimer = 0

        # generate the kinect runtime environment
        self.kinect = nui.Runtime()

        # enable the skeleton tracking engine on the kinect
        self.kinect.skeleton_engine.enabled = True

        # wait until skeleton is succesfully tracked by kinect before program starts
        Tracked = False
        while not Tracked:
            frame = self.kinect.skeleton_engine.get_next_frame().SkeletonData
            for skeleton in frame:
                if skeleton.eTrackingState == nui.SkeletonTrackingState.TRACKED:
                    self.position = skeleton.SkeletonPositions[JointId.HandRight]
                    Tracked = True
   

        # link timer to Qt for 60 FPS: tick update function will be called
        self.timer.timeout.connect(self.tick)
Esempio n. 6
0
    def run(self):
        '''Sets up the Kinect data and begins watching for updates.'''
        def display(frame):
            '''Will be called every time the Kinect has a new frame.
            Processes and synchronizes that data.'''
            tracked_enum = nui.SkeletonTrackingState.TRACKED
            index = 1
            current = []
            data = {}
            for index, skeleton in enumerate(frame.SkeletonData):
                if skeleton.eTrackingState == tracked_enum:
                    data[index + 1] = skeleton
                    current.append(index)

            if self.prev != current:
                self.prev = current
                for index, player_number in self.players.items():
                    if index not in data:
                        # player with that id just left
                        self.available.append(player_number)
                        self.available.sort(reverse=True)
                        del self.players[index]
                        self._clear_data(player_number)

                for index, skeleton in data.items():
                    player_number = self.players.get(index, None)
                    if player_number is None:
                        # set new player number, using the lowest one available
                        player_number = self.available.pop()
                        self.players[index] = player_number
                    self._set_data(player_number, skeleton)
            else:
                for index, skeleton in data.items():
                    self._set_data(self.players[index], skeleton)

            self.data['num_tracked'] = index
            self.data['tracked_players'] = list(self.players.values())

        try:
            with nui.Runtime() as kinect:
                kinect.skeleton_engine.enabled = True
                kinect.skeleton_frame_ready += display

                #kinect.video_stream.open(
                #    nui.ImageStreamType.Video,
                #    2,
                #    nui.ImageResolution.Resolution640x480,
                #    nui.ImageType.Color)
                kinect.depth_stream.open(nui.ImageStreamType.Depth, 2,
                                         nui.ImageResolution.Resolution320x240,
                                         nui.ImageType.Depth)

                self.kinect_ready_flag.set()
                self._block()
        except Exception as ex:
            self.kinect_ready_flag.set()
            self.encountered_error_flag.set()
            self.exception = ex
            raise
Esempio n. 7
0
    def __init__(self):
        self.width = 640
        self.height = 480
        pygame.time.set_timer(TIMER_EVENT, 25)
        self.screen_lock = thread.allocate()
        self.last_kinect_event = time.clock()
        self.screen = pygame.display.set_mode((self.width, self.height), 0, 32)

        self.dispInfo = pygame.display.Info()
        self.screen.convert()
        pygame.display.set_caption('Python Kinect Game')
        self.screen.fill(THECOLORS["black"])

        self.background = pygame.Surface((self.width, self.height), 0, 32)
        self.background.fill(THECOLORS["black"])
        self.background.convert()

        self.video_screen = pygame.SurfaceType((self.width, self.height), 0,
                                               32)

        self.ball_group = sprite.Group(
            Ball(self, 'white', direction=math.atan2(.5, 1), x=30, y=410),
            Ball(self, 'white', direction=math.atan2(0, -1), x=600, y=400),
            Ball(self, 'white', direction=math.atan2(0, -1), x=30, y=240),
            Ball(self, 'white', direction=math.atan2(1, -1.1), x=10, y=140),
        )
        self.known_players = {}

        self.score_font = pygame.font.SysFont('Segoe UI', 20, bold=True)

        self.pieces_group = sprite.Group()

        self.left_margin = (self.width / 5)
        self.top_margin = (self.height / 5)
        self.blocks_across = 10
        self.blocks_down = 10
        width = ((self.width - self.left_margin) / self.blocks_across) - 5
        height = ((self.height - self.top_margin) / self.blocks_down) - 5
        for y in xrange(self.blocks_down):
            for x in xrange(self.blocks_across):
                if x not in (3, 4, 5, 6) or not y in (4, 5):
                    x_loc = ((self.width - self.left_margin) /
                             self.blocks_across) * x + (self.left_margin / 2)
                    y_loc = ((self.height - self.top_margin) /
                             self.blocks_down) * y + (self.top_margin / 2)

                    bp = BoardPiece(x_loc, y_loc, width, height)
                    bp.add(self.pieces_group)

        self.bumper_group = sprite.Group()

        self.kinect = nui.Runtime()
        self.kinect.skeleton_engine.enabled = True
        self.kinect.skeleton_frame_ready += post_frame
        self.kinect.video_frame_ready += self.video_frame_ready
        self.kinect.video_stream.open(nui.ImageStreamType.Video, 2,
                                      nui.ImageResolution.Resolution640x480,
                                      nui.ImageType.Color)
Esempio n. 8
0
def kinectvid():
	
	global skeletons
	skeletons = None
	kinect = nui.Runtime()
	kinect.skeleton_engine.enabled = True

	def post_frame(frame):
		try:
			pygame.event.post(pygame.event.Event(KINECTEVENT, skeletons = frame.SkeletonData))
		except:
			# event queue full
			pass
	
	kinect.skeleton_frame_ready += post_frame
	kinect.video_frame_ready += kinect_video_function
	kinect.video_stream.open(nui.ImageStreamType.Video, 2,nui.ImageResolution.Resolution640x480,nui.ImageType.Color)
	
	cv2.namedWindow('KINECT Video Stream', cv2.WINDOW_AUTOSIZE)

	# Initialize writing video
	#fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
	#out = cv2.VideoWriter('output.avi', fourcc, 30.0, (640, 480))

	# Main loop - runs video_handler_function automatically
	done = False
	while done == False:
	
		#e = pygame.event.wait()
		e = pygame.event.poll()

		if e != pygame.NOEVENT:
			if e.type == KINECTEVENT:
				skeletons = e.skeletons
				#draw_skeletons(skeletons)
			elif e.type == pygame.QUIT:
				done = True
				print "Quit"
				kinect.close()
				print "Exit kinect"
				break
		if cv2.waitKey(1) & 0xFF == ord('q'):
			done = True
			print "Quit"
			kinect.close()
			print "Exit kinect"
			break

	# Finish writing to csv
	csvfile.close()

	print "Exit while"
	#kinect.close()
	cv2.destroyAllWindows()
	print "Exit cv2"
	pygame.quit()
	print "Exit pygame"
	quit()
Esempio n. 9
0
 def __init__(self):
     self.lastColorFrame = np.empty((480,640,4),np.uint8)
     self.lastDepthFrame = np.empty((240,320,4),np.uint8)
     self.positions = []
     self.Kinect = nui.Runtime()
     self.Kinect.depth_frame_ready += self.storeDepthFrame
     self.Kinect.video_frame_ready += self.storeColorFrame
     self.Kinect.depth_stream.open(nui.ImageStreamType.Depth, 2, nui.ImageResolution.Resolution320x240, nui.ImageType.Depth)
     self.Kinect.video_stream.open(nui.ImageStreamType.Video, 2, nui.ImageResolution.Resolution640x480, nui.ImageType.Color)
Esempio n. 10
0
    def __init__(self, size=(1600, 900)):
        pygame.init()
        self.kinect = nui.Runtime()
        self.kinect.skeleton_engine.enabled = True

        self.screen_width, self.screen_height = size

        self.people = []
        self.ignore_list = []
Esempio n. 11
0
 def init():
     global g_kinect, g_screen_lock, g_depth_data, g_video_data
     g_screen_lock = thread.allocate()
     g_depth_data, g_video_data = None, None
     g_kinect = nui.Runtime()
     g_kinect.depth_frame_ready += freenect.depth_frame_ready
     g_kinect.video_frame_ready += freenect.video_frame_ready
     #g_kinect.video_stream.open(nui.ImageStreamType.Video, 2, nui.ImageResolution.Resolution1280x1024, nui.ImageType.Color)
     g_kinect.video_stream.open(
         nui.ImageStreamType.Video, 2,
         nui.ImageResolution.Resolution640x480, nui.ImageType.Color)
Esempio n. 12
0
def main():
    with nui.Runtime() as kinect:
        cam = nui.Camera(kinect)

        while True:
            kinect.skeleton_engine.enabled = True
            kinect.skeleton_frame_ready += post_frame

            x = skeleton.Joints[JointID.HandRight].Position.X

            # wait before moving tilt again
            time.sleep(WAIT_INTERVAL)
Esempio n. 13
0
    def __init__(self, addr, port):
        context = zmq.Context()

        self.socket = context.socket(zmq.PUB)
        self.socket.bind('tcp://{}:{}'.format(addr, port))

        self.msg = Queue.Queue(2)

        self.kinect = nui.Runtime()

        self.kinect.skeleton_frame_ready += self.skeleton_frame_ready
        self.kinect.skeleton_engine.enabled = True
Esempio n. 14
0
 def __init__(self, device = None):
     self._dmo = None
     dmo = ctypes.c_voidp()
     if device is None:
         from pykinect import nui
         
         device = nui.Runtime(nui.RuntimeOptions.uses_audio)
             
     _OpenKinectAudio(device._nui, ctypes.byref(dmo))
     self._dmo = dmo
     self._file = None
     self._device = device
Esempio n. 15
0
def getImage_respons():
    try:
        kinect = nui.Runtime()
        kinect.video_frame_ready += video_handler_function_respons
        kinect.video_stream.open(nui.ImageStreamType.Video, 2,
                                 nui.ImageResolution.Resolution640x480,
                                 nui.ImageType.color)
        cv2.namedWindow('Respons', cv2.WINDOW_AUTOSIZE)
        time.sleep(2)
        cv2.destroyAllWindows()

    except:
        print(" In Chang ERROR function -- getImage")
Esempio n. 16
0
    def _init_kinect(self):
        self.kinect = nui.Runtime()
        self.kinect.skeleton_engine.enabled = True
        self.kinect.skeleton_frame_ready += self.post_frame

        self.kinect.depth_frame_ready += self.depth_frame_ready
        self.kinect.video_frame_ready += self.video_frame_ready

        self.kinect.video_stream.open(nui.ImageStreamType.Video, 2,
                                      nui.ImageResolution.Resolution640x480,
                                      nui.ImageType.Color)
        self.kinect.depth_stream.open(nui.ImageStreamType.Depth, 2,
                                      nui.ImageResolution.Resolution320x240,
                                      nui.ImageType.Depth)
Esempio n. 17
0
def get_image_feedback():
    status = True

    try:
        kinect = nui.Runtime()
        kinect.video_frame_ready += video_handler_function_feedback
        kinect.video_stream.open(nui.ImageStreamType.Video, 2,
                                 nui.ImageResolution.Resolution640x480,
                                 nui.ImageType.color)

        time.sleep(1)
        kinect.close()

    except:
        print(" In Chang ERROR function -- getImage")

    return status
    def __init__(self, experiment):
    #"""Called by the kinect_init plugin - Initialize a Kinect object and a log file."""

        global _kinect
        global logfile
        global file

        self.experiment = experiment
        self.recording = False

        # Initiate a sketchpad
        self.init_canvas = canvas(self.experiment)

        # Try to initiate an instance of kinect
        if _kinect == None:
            try:
                _kinect = nui.Runtime()
                debug.msg(u'Success to connect the kinect')
                print "Success to connect the kinect"
            except Exception as e:
                debug.msg(u'Fail to connect the kinect : '+str(e))
                print "Fail to connect the kinect : "+str(e)
                self.set_canvas("Fail to connect the kinect ! (press the spacebar to quit)", 0, 36, True)

        # Enable the detection of the participant skeleton
        _kinect.skeleton_engine.enabled = True
        # Put the kinect at a 0 degree angle
        _kinect.camera.elevation_angle = 0
        # Put a kinect instance in an OpenSesame variable, to access it from an "inline script" element
        self.experiment.kinect = _kinect

        # Prepare the file for logging data
        try :
            logname = self.experiment.logfile + "_kinect.csv"
            file = open(logname, "a")
            logfile = csv.writer(file, delimiter=',', lineterminator='\n')
        except Exception as e:
            debug.msg(u'Error in the log file ('+self.experiment.logfile+') : '+e)
            print "Error in the log file ("+self.experiment.logfile+") : "+e

        # Initiate the counter of trials and the list of Opensesame's variables
        self.experiment.TrialNb = 0
        self.opensesame_var_names_old = None
Esempio n. 19
0
def main():
    """Initialize and run the game."""
    pygame.init()

    # Initialize PyGame
    screen = pygame.display.set_mode(WINDOW_SIZE, 0, 16)
    pygame.display.set_caption('Robot Tecnopolis - Control Gestual')
    screen.fill(pygame.color.THECOLORS["black"])

    ##    iport = 6
    ##    s = serial.Serial(int(iport),9600)
    ##    serial.timeout=1

    print "Enviando comando de inicio al Robot Amigo"
    ##    s.write ("e")
    ##
    ##    sDataTX  = "0"
    with nui.Runtime() as kinect:
        kinect.skeleton_engine.enabled = True
        kinect.skeleton_frame_ready += post_frame
        ##kinect.SkeletonFrame.TrackingMode =
        ## Main game loop
        while True:

            ##            if sDataTX!="0":
            ##                s.write (sDataTX)
            ##                print sDataTX

            event = pygame.event.wait()

            if event.type == pygame.QUIT:
                break
            elif event.type == KINECTEVENT:
                # apply joint filtering
                kinect._nui.NuiTransformSmooth(event.skeleton_frame,
                                               SMOOTH_PARAMS)

                sDataTX = draw_skeletons(pygame.display.Info(), screen,
                                         event.skeleton_frame.SkeletonData)
                print sDataTX

                pygame.display.update()
                pass
Esempio n. 20
0
    def start_tracker(self, log_file_name=None):
        pygame.init()

        if self.kinect is None:
            self.kinect = nui.Runtime(
                nui.RuntimeOptions.uses_depth_and_player_index
                | nui.RuntimeOptions.uses_skeletal_tracking)
            self.kinect.skeleton_engine.enabled = True
            self.kinect.depth_stream.open(
                nui.ImageStreamType.depth, 2,
                nui.ImageResolution.resolution_640x480,
                nui.ImageType.depth_and_player_index)

        self.kinect.skeleton_frame_ready += self.post_frame

        # manage gesture recognition (initialization and depth processing)
        self.kgr = kinect_interactions.KinectGestureRecognizer(self.kinect)
        self.kinect.depth_frame_ready += self.process_depth_for_gestures

        kgr_thread = threading.Thread(target=self.kgr.start_recognition)
        kgr_thread.daemon = True
        kgr_thread.start()

        while True:
            event = pygame.event.wait()

            if event.type == pygame.QUIT:
                break
            elif event.type == self.KINECTEVENT:
                # save unfiltered skeletal data
                self.save_skeletal_data(event.skeleton_frame, True)
                # apply filter
                self.kinect._nui.NuiTransformSmooth(event.skeleton_frame,
                                                    self.SMOOTH_PARAMS)
                # save filtered skeletal data
                self.save_skeletal_data(event.skeleton_frame)

                # process skeletal data for gesture recognition
                self.kgr.process_skeleton(event.skeleton_frame)

                if log_file_name is not None and self.skeleton is not None:
                    self.log_skeletal_data(log_file_name)
def main():
    """Initialize and run the game."""
    pygame.init()

    # Initialize PyGame
    global screen
    screen = pygame.display.set_mode(DEPTH_WINSIZE, 0, 8)
    screen.set_palette(tuple([(i, i, i) for i in range(256)]))
    pygame.display.set_caption('PyKinect Depth Map Example')

    with nui.Runtime() as kinect:
        kinect.depth_frame_ready += depth_frame_ready   
        kinect.depth_stream.open(nui.ImageStreamType.Depth, 2, nui.ImageResolution.Resolution320x240, nui.ImageType.Depth)

        # Main game loop
        while True:
            event = pygame.event.wait()

            if event.type == pygame.QUIT:
                break
Esempio n. 22
0
def main():
    global session_name, num_kinect_sensors, saved_images_counter, current_count_saved_images

    session_name = raw_input('Session name: ')
    dirname = '{}/{}'.format(depth_images_folder, session_name)
    if not os.path.isdir(dirname):
        os.makedirs(dirname)

    num_kinect_sensors = int(raw_input('Number of sensors: '))
    current_count_saved_images = range(num_kinect_sensors)

    print('initializing kinects...')
    kinects = []
    for i in range(num_kinect_sensors):
        try:
            kinects.append(nui.Runtime(index=i))
        except WindowsError:
            sys.exit(
                'Couldn\'t connect all sensors, check if all the sensors are connected and try again.'
            )

        print('Initilaized Kinect sensor {0} ({1}), with depth stream {2}'.
              format(i, kinects[i].instance_index, kinects[i].depth_stream))

    for sensor_index, kinect in enumerate(kinects):
        kinect_callback = functools.partial(depth_frame_ready,
                                            sensor_index=sensor_index)
        kinect.depth_frame_ready += kinect_callback
        kinect_callbacks.append(kinect_callback)
        kinect.depth_stream.open(nui.ImageStreamType.Depth, 2,
                                 nui.ImageResolution.resolution_640x480,
                                 nui.ImageType.Depth)

    # Main game loop
    while True:
        cmd = raw_input('Press enter for new depth images, q to quit.')
        if cmd == 'q':
            sys.exit()

        saved_images_counter += 1
        current_count_saved_images = []
Esempio n. 23
0
def main():
    """Initialize and run the game."""
    pygame.init()

    # Initialize PyGame
    screen = pygame.display.set_mode(WINDOW_SIZE, 0, 16)
    pygame.display.set_caption('Python Kinect Game')
    screen.fill(pygame.color.THECOLORS["black"])

    with nui.Runtime() as kinect:
        kinect.skeleton_engine.enabled = True
        kinect.skeleton_frame_ready += post_frame

        # Main game loop
        while True:
            event = pygame.event.wait()

            if event.type == pygame.QUIT:
                break
            elif event.type == KINECTEVENT:
                # process e.skeletons here
                pass
Esempio n. 24
0
    def __init__(self):
        self.left_win = visual.Window(size=constants.SCREEN_SIZE,
                                      units='norm',
                                      fullscr=True,
                                      color='#000000',
                                      screen=1)
        self.right_win = visual.Window(size=constants.SCREEN_SIZE,
                                       units='norm',
                                       fullscr=True,
                                       color='#000000',
                                       screen=2)

        self.kinect = nui.Runtime()
        self.kinect.skeleton_engine.enabled = True
        self.kinect.skeleton_frame_ready += self.update_positions

        self.position = [np.nan, np.nan]
        self.deadzone = ((-1, 1), (-0.1, 1.2))

        self.main_text_height = 0.3
        self.text_h_offset = 0.0
        self.text_v_offset_left = 0.35
        self.text_v_offset_right = 0.3

        self.main_text_height_left = 0.35
        self.main_text_height_right = 0.2

        self.response_x_threshold = 0.8
        self.response_y_threshold = 3.5

        self.left_amount_pos = (self.text_h_offset, self.text_v_offset_right)
        self.left_delay_pos = (self.text_h_offset, -self.text_v_offset_right)

        self.right_amount_pos = (self.text_h_offset, self.text_v_offset_right)
        self.right_delay_pos = (self.text_h_offset, -self.text_v_offset_right)

        super(WalkingExpUIWalking, self).__init__()
def run():
    runtime = nui.Runtime()
    running = [False]
    allow_cam_swap = [True]
    # What does this do????? vvvvvvvvvvvvvvvvvvvvvvvvv
    runtime.video_frame_ready += video_handler_function
    runtime.video_stream.open(nui.ImageStreamType.Video, 2,
                              nui.ImageResolution.Resolution640x480,
                              nui.ImageType.Color)
    cv2.namedWindow('Video Stream', cv2.WINDOW_AUTOSIZE)

    camera = nui.Camera(runtime)
    Thread(target=epic_constant_rotation_meme, args=(
        camera,
        running,
    )).start()
    Thread(target=epic_cam_swap_meme, args=(CUR_CAM, )).start()
    keyboard.add_hotkey('home', rotate_camera, args=('up', 8, camera))
    keyboard.add_hotkey('end', rotate_camera, args=('down', 8, camera))
    keyboard.add_hotkey('ctrl+shift+space', _rot_quickswap, args=(running, ))
    keyboard.add_hotkey('ctrl+shift+end', _cam_swap, args=(CUR_CAM, ))
    keyboard.add_hotkey('ctrl+shift+page up',
                        adjust_time_delay,
                        args=(TIME_DELAY, ))
    keyboard.add_hotkey('ctrl+shift+page down',
                        adjust_time_delay,
                        args=(-TIME_DELAY, ))
    keyboard.add_hotkey('ctrl+shift+home', _breakthrd_quickswap, args=())

    while True:
        key = cv2.waitKey(0)
        if key == 27:
            RIGHT_CAM.release()
            cv2.destroyWindow('Video Stream')
            runtime.close()
            break
Esempio n. 26
0
            draw_skeletons(skeletons)

        pygame.display.update()


if __name__ == '__main__':
    functionsObj = functionsClass()
    full_screen = False
    draw_skeleton = True
    video_display = False
    screen_lock = thread.allocate()
    screen = pygame.display.set_mode(DEPTH_WINSIZE, 0, 16)
    pygame.display.set_caption('Python Kinect Demo')
    skeletons = None
    screen.fill(THECOLORS["black"])
    kinect = nui.Runtime()
    kinect.skeleton_engine.enabled = True

    def post_frame(frame):

        try:
            pygame.event.post(
                pygame.event.Event(KINECTEVENT, skeletons=frame.SkeletonData))

        except:
            # event queue full
            pass

    kinect.skeleton_frame_ready += post_frame
    kinect.depth_frame_ready += depth_frame_ready
    kinect.video_frame_ready += video_frame_ready
Esempio n. 27
0
safeTime = 7.0
safetyTimer = threading.Timer(safeTime, resetSafeBool)
shoot_boolean = False
curr_unknowns = 0
max_unknowns = 2
curr_head = None
prev_head = None
skeleton_frames = 0
max_skeleton_frames = 20
stop = False

if __name__ == "__main__":
    print("Initializing Kinect...")
    # subprocess.call([r"E:\\Documents\Programming\\facialRecog\\Skeleton\\Debug\\KinectTutorial4.exe"])
    with nui.Runtime() as kinect:
        kinect.skeleton_engine.set_enabled(True)

        kinect.video_frame_ready += video_handler_function
        kinect.video_stream.open(nui.ImageStreamType.Video, 2,
                                 nui.ImageResolution.Resolution640x480,
                                 nui.ImageType.Color)

        kinect.skeleton_frame_ready += skeleton_frame_function

        cv2.namedWindow('KINECT Video Stream', cv2.WINDOW_AUTOSIZE)

        print("Kinect Initialized")
        start = time.time()
        thing = False
        while True:
Esempio n. 28
0
from pykinect import nui
from pykinect.audio import KinectAudioSource
from winspeech import recognition

with nui.Runtime(nui.RuntimeOptions.UsesAudio) as kinect, KinectAudioSource() as source:
    rec = recognition.SpeechRecognitionEngine()
    audio_file = source.start()
    print audio_file
    rec.set_input_to_audio_file(audio_file)
    rec.load_grammar('Grammar.xml')
    print 'Recognizing...', rec.recognize_sync()
 def StartKinect(self):
     if self.kinect == None:
         self.kinect = nui.Runtime()
     else:
         print 'Kinect Is Already Open'
def process():
    KINECTEVENT = pygame.USEREVENT
    DEPTH_WINSIZE = 320, 240
    VIDEO_WINSIZE = 640, 480
    pygame.init()

    SKELETON_COLORS = [
        THECOLORS["red"], THECOLORS["blue"], THECOLORS["green"],
        THECOLORS["orange"], THECOLORS["purple"], THECOLORS["yellow"],
        THECOLORS["violet"]
    ]

    LEFT_ARM = (JointId.ShoulderCenter, JointId.ShoulderLeft,
                JointId.ElbowLeft, JointId.WristLeft, JointId.HandLeft)
    RIGHT_ARM = (JointId.ShoulderCenter, JointId.ShoulderRight,
                 JointId.ElbowRight, JointId.WristRight, JointId.HandRight)
    LEFT_LEG = (JointId.HipCenter, JointId.HipLeft, JointId.KneeLeft,
                JointId.AnkleLeft, JointId.FootLeft)
    RIGHT_LEG = (JointId.HipCenter, JointId.HipRight, JointId.KneeRight,
                 JointId.AnkleRight, JointId.FootRight)
    SPINE = (JointId.HipCenter, JointId.Spine, JointId.ShoulderCenter,
             JointId.Head)

    skeleton_to_depth_image = nui.SkeletonEngine.skeleton_to_depth_image

    def draw_skeleton_data(pSkelton, index, positions, width=4):
        start = pSkelton.SkeletonPositions[positions[0]]

        for position in itertools.islice(positions, 1, None):
            next = pSkelton.SkeletonPositions[position.value]

            curstart = skeleton_to_depth_image(start, dispInfo.current_w,
                                               dispInfo.current_h)
            curend = skeleton_to_depth_image(next, dispInfo.current_w,
                                             dispInfo.current_h)

            pygame.draw.line(screen, SKELETON_COLORS[index], curstart, curend,
                             width)

            start = next

    # recipe to get address of surface: http://archives.seul.org/pygame/users/Apr-2008/msg00218.html
    if hasattr(ctypes.pythonapi, 'Py_InitModule4'):
        Py_ssize_t = ctypes.c_int
    elif hasattr(ctypes.pythonapi, 'Py_InitModule4_64'):
        Py_ssize_t = ctypes.c_int64
    else:
        raise TypeError("Cannot determine type of Py_ssize_t")

    _PyObject_AsWriteBuffer = ctypes.pythonapi.PyObject_AsWriteBuffer
    _PyObject_AsWriteBuffer.restype = ctypes.c_int
    _PyObject_AsWriteBuffer.argtypes = [
        ctypes.py_object,
        ctypes.POINTER(ctypes.c_void_p),
        ctypes.POINTER(Py_ssize_t)
    ]

    def surface_to_array(surface):
        buffer_interface = surface.get_buffer()
        address = ctypes.c_void_p()
        size = Py_ssize_t()
        _PyObject_AsWriteBuffer(buffer_interface, ctypes.byref(address),
                                ctypes.byref(size))
        bytes = (ctypes.c_byte * size.value).from_address(address.value)
        bytes.object = buffer_interface
        return bytes

    def draw_skeletons(skeletons):
        for index, data in enumerate(skeletons):
            # draw the Head
            HeadPos = skeleton_to_depth_image(
                data.SkeletonPositions[JointId.Head], dispInfo.current_w,
                dispInfo.current_h)
            draw_skeleton_data(data, index, SPINE, 10)
            pygame.draw.circle(screen, SKELETON_COLORS[index],
                               (int(HeadPos[0]), int(HeadPos[1])), 20, 0)

            # drawing the limbs
            draw_skeleton_data(data, index, LEFT_ARM)
            draw_skeleton_data(data, index, RIGHT_ARM)
            draw_skeleton_data(data, index, LEFT_LEG)
            draw_skeleton_data(data, index, RIGHT_LEG)

    def depth_frame_ready(frame):
        if video_display:
            return

        with screen_lock:
            address = surface_to_array(screen)
            frame.image.copy_bits(address)
            del address
            if skeletons is not None and draw_skeleton:
                draw_skeletons(skeletons)
            pygame.display.update()

    def video_frame_ready(frame):
        if not video_display:
            return

        with screen_lock:
            address = surface_to_array(screen)
            frame.image.copy_bits(address)
            del address
            if skeletons is not None and draw_skeleton:
                draw_skeletons(skeletons)
            pygame.display.update()

    full_screen = False
    draw_skeleton = True
    video_display = False

    screen_lock = thread.allocate()

    screen = pygame.display.set_mode(DEPTH_WINSIZE, 0, 16)
    pygame.display.set_caption('Python Kinect Demo')
    skeletons = None
    screen.fill(THECOLORS["black"])

    kinect = nui.Runtime()
    kinect.skeleton_engine.enabled = True

    def post_frame(frame):
        try:
            pygame.event.post(
                pygame.event.Event(KINECTEVENT, skeletons=frame.SkeletonData))
        except:
            # event queue full
            pass

    kinect.skeleton_frame_ready += post_frame

    kinect.depth_frame_ready += depth_frame_ready
    kinect.video_frame_ready += video_frame_ready

    kinect.video_stream.open(nui.ImageStreamType.Video, 2,
                             nui.ImageResolution.Resolution640x480,
                             nui.ImageType.Color)
    kinect.depth_stream.open(nui.ImageStreamType.Depth, 2,
                             nui.ImageResolution.Resolution320x240,
                             nui.ImageType.Depth)

    print('Controls: ')
    print('     d - Switch to depth view')
    print('     v - Switch to video view')
    print('     s - Toggle displaing of the skeleton')
    print('     u - Increase elevation angle')
    print('     j - Decrease elevation angle')

    # main game loop
    done = False

    while not done:
        e = pygame.event.wait()
        dispInfo = pygame.display.Info()
        if e.type == pygame.QUIT:
            done = True
            break
        elif e.type == KINECTEVENT:
            skeletons = e.skeletons
            if draw_skeleton:
                draw_skeletons(skeletons)
                pygame.display.update()
        elif e.type == KEYDOWN:
            if e.key == K_ESCAPE:
                done = True
                break
            elif e.key == K_d:
                with screen_lock:
                    screen = pygame.display.set_mode(DEPTH_WINSIZE, 0, 16)
                    video_display = False
            elif e.key == K_v:
                with screen_lock:
                    screen = pygame.display.set_mode(VIDEO_WINSIZE, 0, 32)
                    video_display = True
            elif e.key == K_s:
                draw_skeleton = not draw_skeleton
            elif e.key == K_u:
                kinect.camera.elevation_angle = kinect.camera.elevation_angle + 2
            elif e.key == K_j:
                kinect.camera.elevation_angle = kinect.camera.elevation_angle - 2
            elif e.key == K_x:
                kinect.camera.elevation_angle = 2