Beispiel #1
0
    def __init__(self, skin, joy_index):
        self.cfg = configurator.PadConfig(skin)

        self.js = pygame.joystick.Joystick(joy_index)
        self.js.init()

        self.fb = frame_buffer.FrameBuffer(
            self.cfg.size, self.cfg.size, scale_smooth=self.cfg.anti_aliasing,
            background_color=self.cfg.background_color)

        self.gfx = visualizer.PadImage(self.cfg, self.fb)
Beispiel #2
0
    def __init__(self):
        '''
        Constructor to initialize the Handwash system
        '''

        # Initialize the Frame Buffer of size 15
        self.frame_buffer = FrameBuffer.FrameBuffer(15)

        # HandwashSteps
        # self.handwash_steps = HandwashSteps.HandwashSteps()

        # Initialize the Video Stream
        self.live_stream = cv2.VideoCapture(0)
        print("Video Stream started successfully.")
        print("Changing video stream resolution..")
        self.live_stream.set(3, 1080)
        self.live_stream.set(4, 1080)
        time.sleep(1)

        print("Video Stream resolution changed to: 1080x1080")

        # Previous frame stored to generate optical flow
        self.previous_frame = None

        # Circular system counter to keep count of frames
        self.frame_count = 1

        # Sampling rate of frames for prediction
        # In a 30 FPS Video, a sampling rate of 5
        # would mean: 1 in 5 frames are sampled for prediction
        # Therefore, every second, 6 frames are sampled
        self.sampling_rate = 5

        # Property to set default FPS of the video stream
        self.video_fps = 30

        # Interval to check buffer in (milliseconds)
        self.check_buffer_interval = 1000

        self.step_name = {
            "step_1": "STEP 1",
            "step_2_left": "STEP 2 LEFT",
            "step_2_right": "STEP 2 RIGHT",
            "step_3": "STEP 3",
            "step_4_left": "STEP 4 LEFT",
            "step_4_right": "STEP 4 RIGHT",
            "step_5_left": "STEP 5 LEFT",
            "step_5_right": "STEP 5 RIGHT",
            "step_6_left": "STEP 6 LEFT",
            "step_6_right": "STEP 6 RIGHT",
            "step_7_left": "STEP 7 LEFT",
            "step_7_right": "STEP 7 RIGHT"
        }
Beispiel #3
0
    def __init__(self):
        '''
        Constructor to initialize the Action system
        '''

        # Initialize the Frame Buffer of size 15
        self.frame_buffer = FrameBuffer.FrameBuffer(15)

        # Initialize the Video Stream
        self.live_stream = cv2.VideoCapture(0)
        print("Video Stream started successfully.")
        self.live_stream.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
        self.live_stream.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
        self.live_stream.set(cv2.CAP_PROP_FPS, 30)
        self.live_stream.set(cv2.CAP_PROP_AUTO_EXPOSURE, 0)
        self.live_stream.set(cv2.CAP_PROP_EXPOSURE, 100)

        time.sleep(1)

        # Previous frame stored to generate optical flow
        self.previous_frame = None

        # Circular system counter to keep count of frames
        self.frame_count = 1

        # Sampling rate of frames for prediction
        # In a 30 FPS Video, a sampling rate of 5
        # would mean: 1 in 5 frames are sampled for prediction
        # Therefore, every second, 6 frames are sampled
        self.sampling_rate = 5

        # Property to set default FPS of the video stream
        self.video_fps = 30

        # Interval to check buffer in (milliseconds)
        self.check_buffer_interval = 1000

        self.step_name = {
            "step_1": "STEP 1",
            "step_2_left": "STEP 2 LEFT",
            "step_2_right": "STEP 2 RIGHT",
            "step_3": "STEP 3",
            "step_4_left": "STEP 4 LEFT",
            "step_4_right": "STEP 4 RIGHT",
            "step_5_left": "STEP 5 LEFT",
            "step_5_right": "STEP 5 RIGHT",
            "step_6_left": "STEP 6 LEFT",
            "step_6_right": "STEP 6 RIGHT",
            "step_7_left": "STEP 7 LEFT",
            "step_7_right": "STEP 7 RIGHT"
        }
    def __init__(self):
        threading.Thread.__init__(self)
        self._rects_output = queue.Queue(300)
        self._frame_buffer = frame_buffer.FrameBuffer(300)
        self._enabled = True
        #self._diff = DifferentiateFrame()
        self._current_count = 0

        self._finder = FindPedestrian(self._frame_buffer, self._rects_output)
        self._finder.start()

        self._viewer_frame = frame_viewer.FrameViewer()
        self._viewer_frame.set_interval(33)
        self._viewer_frame.start()
        self._object_list = object_list.ObjectList()
Beispiel #5
0
    def __init__(self, mode="basic"):

        libs.libs_env.env.Env.__init__(self)

        self.game = DoomGame()

        self.mode = mode

        self.positive_reward_factor = 1.0
        self.negative_reward_factor = 1.0

        if self.mode == "basic":
            self.positive_reward_factor = 0.1
            self.negative_reward_factor = 0.1

            self.game.load_config("scenarios/basic.cfg")

            left = [1, 0, 0]
            right = [0, 1, 0]
            attack = [0, 0, 1]

            self.actions = [left, right, attack]

        if self.mode == "health_gathering":
            self.positive_reward_factor = 0.02
            self.negative_reward_factor = 0.1

            self.game.load_config("scenarios/health_gathering.cfg")

            turn_left = [1, 0, 0]
            turn_right = [0, 1, 0]
            move_forward = [0, 0, 1]
            self.actions = [turn_left, turn_right, move_forward]

        if self.mode == "defend_the_center":
            self.positive_reward_factor = 5.0
            self.negative_reward_factor = 10.0

            self.game.load_config("scenarios/defend_the_center.cfg")

            turn_left = [1, 0, 0]
            turn_right = [0, 1, 0]
            attack = [0, 0, 1]
            self.actions = [turn_left, turn_right, attack]

        if self.mode == "defend_the_line":
            self.positive_reward_factor = 1.0
            self.negative_reward_factor = 10.0

            self.game.load_config("scenarios/defend_the_line.cfg")

            left = [1, 0, 0]
            right = [0, 1, 0]
            attack = [0, 0, 1]

            self.actions = [left, right, attack]

        if self.mode == "deadly_corridor":
            self.positive_reward_factor = 1.0
            self.negative_reward_factor = 0.5

            self.game.load_config("scenarios/deadly_corridor.cfg")

            move_left = [1, 0, 0, 0, 0, 0, 0]
            move_right = [0, 1, 0, 0, 0, 0, 0]
            attack = [0, 0, 1, 0, 0, 0, 0]
            move_forward = [0, 0, 0, 1, 0, 0, 0]
            move_backward = [0, 0, 0, 0, 1, 0, 0]
            turn_left = [0, 0, 0, 0, 0, 1, 0]
            turn_right = [0, 0, 0, 0, 0, 0, 1]

            self.actions = [
                move_left, move_right, attack, move_forward, move_backward,
                turn_left, turn_right
            ]

        if self.mode == "multi":
            self.positive_reward_factor = 0.1
            self.negative_reward_factor = 0.1

            self.game.load_config("scenarios/multi.cfg")

            turn_left = [1, 0, 0, 0, 0, 0, 0, 0, 0]
            turn_right = [0, 1, 0, 0, 0, 0, 0, 0, 0]
            attack = [0, 0, 1, 0, 0, 0, 0, 0, 0]

            move_right = [0, 0, 0, 1, 0, 0, 0, 0, 0]
            move_left = [0, 0, 0, 0, 1, 0, 0, 0, 0]
            move_forward = [0, 0, 0, 0, 0, 1, 0, 0, 0]
            move_backward = [0, 0, 0, 0, 0, 0, 1, 0, 0]

            turn_left_right_delta = [0, 0, 0, 0, 0, 0, 0, 1, 0]
            move_left_right_delta = [0, 0, 0, 0, 0, 0, 0, 0, 1]

            self.actions = [
                turn_left, turn_right, attack, move_right, move_left,
                move_forward, move_backward, turn_left_right_delta,
                move_left_right_delta
            ]

        if self.mode == "deathmatch":
            self.positive_reward_factor = 1.0
            self.negative_reward_factor = 1.0

            self.game.load_config("scenarios/deathmatch.cfg")
            attack = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
            strafe = [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
            move_right = [
                0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            ]
            move_left = [
                0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            ]
            move_forward = [
                0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            ]
            move_backward = [
                0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            ]
            turn_left = [
                0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            ]
            turn_right = [
                0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            ]

            select_weapon_1 = [
                0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            ]
            select_weapon_2 = [
                0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
            ]
            select_weapon_3 = [
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0
            ]
            select_weapon_4 = [
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
            ]
            select_weapon_5 = [
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0
            ]
            select_weapon_6 = [
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0
            ]
            select_next_weapon = [
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0
            ]
            select_prev_weapon = [
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0
            ]

            look_up_down_delta = [
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0
            ]
            turn_left_right_delta = [
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0
            ]
            move_left_right_delta = [
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
            ]

            self.actions = [
                attack, strafe, move_right, move_left, move_forward,
                move_backward, turn_left, turn_right, select_weapon_1,
                select_weapon_2, select_weapon_3, select_weapon_4,
                select_weapon_5, select_weapon_6, select_next_weapon,
                select_prev_weapon, look_up_down_delta, turn_left_right_delta,
                move_left_right_delta
            ]

        #self.game.add_available_game_variable(GameVariable.KILLCOUNT)
        #self.game.add_available_game_variable(GameVariable.DEATHCOUNT)

        #self.game.set_sound_enabled(True)
        self.game.init()
        self.game.new_episode()

        self.reset()

        self.raw_state_shape = self.game.get_state().screen_buffer.shape

        buffer_size = 64
        self.time_steps = 8
        self.colors_count = 3

        self.width = 80
        self.height = 80
        self.depth = self.colors_count * self.time_steps
        self.time = 1

        self.width_scale = 5
        self.height_scale = 5

        self.width_scaled = self.raw_state_shape[2] // self.width_scale
        self.height_scaled = self.raw_state_shape[1] // self.height_scale

        self.width_crop = self.raw_state_shape[
            2] // self.width_scale - self.width
        self.height_crop = self.raw_state_shape[
            1] // self.height_scale - self.height

        print("raw state shape ", self.raw_state_shape[2],
              self.raw_state_shape[1])
        print("scaling factor ", self.width_scale, self.height_scale)
        print("rescaled state ", self.width_scaled, self.height_scaled)
        print("crop ", self.width_crop, self.height_crop)

        self.actions_count = len(self.actions)
        self.observation_init()

        self.game_idx = 0

        self.frame_buffer = frame_buffer.FrameBuffer(self.width, self.height,
                                                     self.colors_count,
                                                     buffer_size)

        self.size_ratio = self.width / self.height
        self.gui = libs_gl_gui.GLVisualisation()
        self.window_name = "DOOM - " + self.mode
Beispiel #6
0
def main(skin, joy_index,  is_main_process):
    pygame.display.init()
    pygame.joystick.init()
    pygame.font.init()

    joy = pygame.joystick.Joystick(joy_index)
    joy.init()
    mappings = configurator.load_mappings(skin)
    if joy.get_name() not in mappings:
        print 'Please run the mapper on', joy.get_name(), 'with', skin, 'skin.'
        return

    mappings = mappings[joy.get_name()]

    button_map = mappings.get('button', dict())
    axis_map = mappings.get('axis', dict())
    hat_map = mappings.get('hat', dict())

    pad_cfg = configurator.PadConfig(skin)

    height = pad_cfg.size[1]
    size = pad_cfg.size if is_main_process else (pad_cfg.size[0], height * 10)
    caption = "padpyght" if is_main_process else "input history"
    fb = frame_buffer.FrameBuffer(size, size, caption,
                                  scale_smooth=pad_cfg.anti_aliasing,
                                  background_color=pad_cfg.background_color)
    pad_gfx = images.PadImage(pad_cfg, fb)

    timer_font = pygame.font.SysFont(None, 16)

    # Start the input history service process
    if is_main_process:
        p = Process(target=main, args=(skin, joy_index, False))
        p.start()

    # TODO: May want to initialize all inputs
    last_state = InputState()
    last_font_rect = None

    # Main input/render loop
    while not pygame.event.peek(pygame.QUIT):

        new_state = copy.deepcopy(last_state)

        for event in pygame.event.get():
            if event.type == pygame.JOYBUTTONDOWN:
                if str(event.button) in button_map:
                    elt = button_map[str(event.button)]
                    new_state.button_map[elt['name']] = 1
                    _get_target(pad_gfx, elt).push(1)
            elif event.type == pygame.JOYBUTTONUP:
                if str(event.button) in button_map:
                    elt = button_map[str(event.button)]
                    new_state.button_map[elt['name']] = 0
                    _get_target(pad_gfx, elt).push(0)
            elif event.type == pygame.JOYAXISMOTION:
                if str(event.axis) in axis_map:
                    for change, elt in axis_map[str(event.axis)].iteritems():
                        change = int(change)
                        value = event.value
                        if abs(change) == 2:
                            value += change / abs(change)
                        value /= change
                        value = max(0, value)
                        _get_target(pad_gfx, elt).push(value)
            elif event.type == pygame.JOYHATMOTION:
                if str(event.hat) in hat_map:
                    direction_map = hat_map[str(event.hat)]
                    x, y = event.value
                    if 'up' in direction_map:
                        new_state.button_map[direction_map['up']['name']] = y
                        _get_target(pad_gfx, direction_map['up']).push(y)
                    if 'down' in direction_map:
                        new_state.button_map[direction_map['down']['name']] = -y
                        _get_target(pad_gfx, direction_map['down']).push(-y)
                    if 'left' in direction_map:
                        new_state.button_map[direction_map['left']['name']] = -x
                        _get_target(pad_gfx, direction_map['left']).push(-x)
                    if 'right' in direction_map:
                        new_state.button_map[direction_map['right']['name']] = x
                        _get_target(pad_gfx, direction_map['right']).push(x)
            fb.handle_event(event)

        if is_main_process:
            pad_gfx.draw()
            fb.flip()
        else:
            if last_state != new_state:
                new_state.elapsed_frames = 0
                # print("L: " + str(last_state) + ", N:" + str(new_state))
                fb.scroll(0, height)
                pad_gfx.draw()
                fb.flip()
            else:
                new_state.elapsed_frames += 1

                if last_font_rect is not None:
                    clear_surf = pygame.Surface((last_font_rect.width, last_font_rect.height))
                    clear_surf.fill(pad_cfg.background_color)
                    fb.blit(clear_surf, (0, 0))

                # render the font
                font_image = timer_font.render(
                    str(new_state.elapsed_frames),
                    True,
                    pygame.Color(255, 255, 255),
                    pad_cfg.background_color)
                last_font_rect = font_image.get_rect()
                fb.blit(font_image, (0, 0))
                fb.flip()
            last_state = new_state

        fb.limit_fps(set_caption=True)
Beispiel #7
0
def main(skin, joy_index):
    pygame.display.init()
    pygame.joystick.init()

    joy = pygame.joystick.Joystick(joy_index)
    joy.init()
    mappings = configurator.load_mappings(skin)
    if joy.get_name() not in mappings:
        print 'Please run the mapper on', joy.get_name(), 'with', skin, 'skin.'
        return

    mappings = mappings[joy.get_name()]

    button_map = mappings.get('button', dict())
    axis_map = mappings.get('axis', dict())
    hat_map = mappings.get('hat', dict())

    pad_cfg = configurator.PadConfig(skin)

    fb = frame_buffer.FrameBuffer(pad_cfg.size, pad_cfg.size,
                                  scale_smooth=pad_cfg.anti_aliasing,
                                  background_color=pad_cfg.background_color)
    pad_gfx = PadImage(pad_cfg, fb)

    while not pygame.event.peek(pygame.QUIT):
        for event in pygame.event.get():
            if event.type == pygame.JOYBUTTONDOWN:
                if str(event.button) in button_map:
                    elt = button_map[str(event.button)]
                    _get_target(pad_gfx, elt).push(1)
            elif event.type == pygame.JOYBUTTONUP:
                if str(event.button) in button_map:
                    elt = button_map[str(event.button)]
                    _get_target(pad_gfx, elt).push(0)
            elif event.type == pygame.JOYAXISMOTION:
                if str(event.axis) in axis_map:
                    for change, elt in axis_map[str(event.axis)].iteritems():
                        change = int(change)
                        value = event.value
                        if abs(change) == 2:
                            value += change / abs(change)
                        value /= change
                        value = max(0, value)
                        _get_target(pad_gfx, elt).push(value)
            elif event.type == pygame.JOYHATMOTION:
                if str(event.hat) in hat_map:
                    direction_map = hat_map[str(event.hat)]
                    x, y = event.value
                    if 'up' in direction_map:
                        _get_target(pad_gfx, direction_map['up']).push(y)
                    if 'down' in direction_map:
                        _get_target(pad_gfx, direction_map['down']).push(-y)
                    if 'left' in direction_map:
                        _get_target(pad_gfx, direction_map['left']).push(-x)
                    if 'right' in direction_map:
                        _get_target(pad_gfx, direction_map['right']).push(x)
            fb.handle_event(event)

        pad_gfx.draw()
        fb.flip()
        fb.limit_fps(set_caption=True)