def get_frames(cls, frame_buffer_indices, frame_type="FULL", **kwargs): while True: if redis_client.llen(config["frame_grabber"]["redis_key"]) > 149: break time.sleep(0.1) game_frame_buffer = GameFrameBuffer(size=len(frame_buffer_indices)) for i in frame_buffer_indices: redis_key = config["frame_grabber"]["redis_key"] redis_key = redis_key + "_PIPELINE" if frame_type == "PIPELINE" else redis_key frame_data = redis_client.lindex(redis_key, i) timestamp, shape, dtype, frame_bytes = frame_data.split("~".encode("utf-8"), maxsplit=3) if dtype == "PNG".encode("utf-8"): frame_array = frame_bytes else: frame_shape = [int(i) for i in shape.decode("utf-8").split(", ")] frame_array = np.fromstring(frame_bytes, dtype=dtype.decode("utf-8")).reshape(frame_shape) game_frame = GameFrame(frame_array, timestamp=float(timestamp)) game_frame_buffer.add_game_frame(game_frame) return game_frame_buffer
def get_frames_with_pipeline(cls, frame_buffer_indices, **kwargs): print("redis_client.llen") while True: if redis_client.llen(config["frame_grabber"]["redis_key"]) > 149: break time.sleep(0.1) print("end sleep") game_frame_buffers = [ GameFrameBuffer(size=len(frame_buffer_indices)), GameFrameBuffer(size=len(frame_buffer_indices)) ] for i in frame_buffer_indices: redis_keys = [config["frame_grabber"]["redis_key"], config["frame_grabber"]["redis_key"] + "_PIPELINE"] for index, redis_key in enumerate(redis_keys): frame_data = redis_client.lindex(redis_key, i) timestamp, shape, dtype, frame_bytes = frame_data.split("~".encode("utf-8"), maxsplit=3) if dtype == "PNG".encode("utf-8"): frame_array = frame_bytes else: frame_shape = [int(i) for i in shape.decode("utf-8").split(", ")] frame_array = np.fromstring(frame_bytes, dtype=dtype.decode("utf-8")).reshape(frame_shape) game_frame = GameFrame(frame_array, timestamp=float(timestamp)) game_frame_buffers[index].add_game_frame(game_frame) return game_frame_buffers
def handle_collect_frames_for_context(self, game_frame, game_frame_pipeline, **kwargs): context = kwargs.get("context") or config["frame_handlers"][ "COLLECT_FRAMES_FOR_CONTEXT"]["context"] interval = kwargs.get("interval") or config["frame_handlers"][ "COLLECT_FRAMES_FOR_CONTEXT"]["interval"] screen_region = kwargs.get("screen_region") if screen_region is not None: if screen_region not in self.game.screen_regions: raise GameAgentError("Invalid game screen region...") frame_region = serpent.cv.extract_region_from_image( game_frame.frame, self.game.screen_regions[screen_region]) game_frame = GameFrame(frame_region) self.game_frames.append(game_frame) self.collected_frame_count += 1 clear_terminal() print( f"Collected Frame #{self.collected_frame_count} for Context: {context}" ) time.sleep(interval)
def is_alive(self, frames, **kwargs): #print(f"### is_alive") bw_frame = self.get_clean_frame(frames) if not self.game.api.is_alive(GameFrame(frames[-2].frame), self.sprite_identifier): print(f"### Died...") self.input_controller.tap_key(KeyboardKey.KEY_SPACE) return 0 else: # print(f"### Still Alive...") pass
def get_frames(cls, frame_buffer_indices, frame_shape=None): game_frame_buffer = GameFrameBuffer(size=len(frame_buffer_indices)) for i in frame_buffer_indices: frame_bytes = redis_client.lindex( config["frame_grabber"]["redis_key"], i) frame_array = np.fromstring(frame_bytes, dtype="uint8").reshape(frame_shape) game_frame = GameFrame(frame_array) game_frame_buffer.add_game_frame(game_frame) return game_frame_buffer
def get_projection_matrix(self, game_frame, location): reduced_frame = None units_array = None if (location != None): temp = game_frame.frame[location[0] - self.HEIGHT * self.n_HEIGHT:location[2], location[1] - self.WIDTH * self.n_WIDTH:location[3] + self.WIDTH * self.n_WIDTH] reduced_frame = GameFrame(temp) units_array = self._projection(reduced_frame, location) nx, ny = units_array.shape units_array = units_array.reshape(nx * ny) orientation = self._get_orientation(location) units_array = np.insert(units_array, len(units_array), orientation) return (reduced_frame, units_array)
def reward_superflight_simple(self, frames, **kwargs): bw_frame = self._reward_superflight_simple_preprocess(frames) is_scoring = bw_frame.max() == 255 white_pixel_count = bw_frame[bw_frame == 255].size if not self.game.api.is_alive(GameFrame(frames[-2].frame), self.sprite_identifier): return 0 if white_pixel_count > 20000: return 0.5 if is_scoring: return 0.5 + ((white_pixel_count / 20000) / 2) else: return np.random.uniform( 0.1, 0.3 ) # Returning a static value here causes early convergence. Fuzzing!