Exemplo n.º 1
0
def train_batch(self, batch: deque):
    batch_size: int = len(batch)
    if batch_size < 1:
        return
    self.logger.info(f"Training batch of {batch_size} transitions")
    x = np.full((batch_size, FEATURE_SIZE), np.nan)
    action_indices = np.full((batch_size, ), -1, dtype=int)
    y = np.full((batch_size, 1), np.nan)
    for i, (old_features, action, new_features, reward) in enumerate(batch):
        if old_features is not None and action is not None:
            action_index = ACTIONS.index(action)
            if not self.model.is_model_fit:
                old_value = 0
                next_value = 0
            else:
                q_values = self.model.predict(old_features)[0]
                old_value = q_values[action_index]
                if new_features is not None:
                    next_value = np.max(self.model.predict(new_features))
                else:
                    next_value = 0
            q_value_updated = (1 - self.alpha) * old_value + self.alpha * (
                reward + self.gamma * next_value)
            action_indices[i] = action_index
            y[i] = np.array([q_value_updated])
            x[i] = old_features
    batch.clear()
    y_nan = np.any(np.isnan(y), axis=1)
    if np.sum(~y_nan) == 0:
        return
    self.model.fit_actions(x[~y_nan], y[~y_nan], action_indices[~y_nan])
Exemplo n.º 2
0
 def callback(self, client: discord.VoiceClient,
              queue: collections.deque) -> None:
     """Handle callbacks after the voice client finishes playing."""
     if not client.is_connected():
         queue.clear()
     elif queue:
         now = queue.popleft()
         client.play(now, after=lambda e: self.callback(client, queue))
Exemplo n.º 3
0
    def _setLines(self, msg: str, lines: deque, maxChars: int, maxLines: int):
        # replacing utf-8 chars with ascii
        msg = unidecode(msg)
        lines.clear()
        # split, trim, copy first maxLines to lines
        index = 0
        for chunk in chunkstring(msg, maxChars):
            lines.append(chunk.strip())
            index += 1
            if index == maxLines:
                break

        # fill the remaining lines, if any
        while len(lines) < maxLines:
            lines.append('')
        return self
Exemplo n.º 4
0
class WebVideoStream:
    def __init__(self, src="C:\\Tools\\titan_test.mp4"):
        self.config = Config()
        self.packer = Packer()
        # initialize the file video stream along with the boolean
        # used to indicate if the thread should be stopped or not
        os.environ["OPENCV_VIDEOIO_DEBUG"] = "1"
        os.environ["OPENCV_VIDEOIO_PRIORITY_MSMF"] = "0"
        encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 15]
        self.stream = cv2.VideoCapture(src)
        # while True:
        # 	if cv2.waitKey(1) & 0xFF == ord('q'):
        # 		break
        # 	ret, frame = self.stream.read()
        # 	if ret:
        # 		# print(frame.shape)
        # 		# frame = frame.reshape(self.packer.h, self.packer.w, self.packer.d)
        # 		cv2.imshow('read video data.jpg', frame)
        # self.stream.set(cv2.CAP_PROP_MODE, cv2.CAP_MODE_YUYV)
        # print(self.stream.get(cv2.CAP_PROP_FPS)) # 默认帧率30
        # self.stream.set(cv2.CAP_PROP_FPS, 20)   # cv version is 3.4.2
        self.stopped = False

        self.requesting = False
        self.request = False
        self.quit = False

        self.push_sleep = 0.2
        self.push_sleep_min = 0.2
        self.push_sleep_max = 0.5

        self.frame = None
        self.frame_size = 0
        self.piece_size = 0
        self.frame_pieces = 0
        self.init_config()
        self.init_connection()

        # intialize thread and lock
        self.thread = Thread(target=self.update, args=())
        self.thread.daemon = True

        self.Q = Queue()

    def init_config(self):
        config = self.config
        # 初始化连接信息
        host = config.get("server", "host")
        port = config.get("server", "port")
        self.address = (host, int(port))

        # 初始化delay信息
        self.frame_delay = float(config.get("delay", "frame"))
        self.piece_delay = float(config.get("delay", "piece"))

        # 初始化队列大小信息
        self.queue_size = int(config.get("receive", "queue_size"))

    def init_connection(self):
        try:
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            # self.sock.bind(self.address)
        except socket.error as msg:
            print(msg)
            sys.exit(1)

    def close_connection(self):
        self.sock.close()

    def start(self):
        # start a thread to read frames from the file video stream
        self.thread.start()
        return self

    def update(self):
        piece_size = self.packer.piece_size
        # keep looping infinitely until the thread is stopped
        while True:
            # if the thread indicator variable is set, stop the thread
            if self.stopped:
                return

            # otherwise, read the next frame from the stream
            (grabbed, frame_raw) = self.stream.read()

            if self.Q_stuck_control():
                time.sleep(self.push_sleep)
            now = int(time.time() * 1000)
            for i in range(self.packer.frame_pieces):
                self.packer.pack_data(i, now, frame_raw, self.Q)
                # self.Q.put(res)
            # now2 = int(time.time()*1000)
            # print("Time to get a frame:", (now2-now))
        return

    def Q_stuck_control(self):
        if len(self.Q) >= self.packer.send_piece_limit:
            self.push_sleep = min(self.push_sleep / 2.0, self.push_sleep_max)
            return True
        if len(self.Q) <= self.packer.send_piece_min:
            self.push_sleep = max(self.push_sleep / 2.0, self.push_sleep_min)
        return False

    def get_request(self):
        if self.requesting: return

        print("waiting...")
        thread = Thread(target=self.get_request_thread, args=())
        thread.daemon = True
        thread.start()
        self.requesting = True

    def get_request_thread(self):
        while True:
            data = b''
            try:
                data, address = self.sock.recvfrom(4)
            except:
                pass
            if (data == b"get"):
                self.request = True
                break
            elif (data == b"quit"):
                self.quit = True
                break

    def read(self):
        # print(len(self.Q))
        if len(self.Q) == 0: return None
        frame = self.Q.popleft()
        if len(self.Q) > self.packer.send_piece_limit:  # self.queue_size*0.1
            self.Q.clear()
        return frame

    def read_total_frame_and_send(self):

        pass

    def stop(self):
        # indicate that the thread should be stopped
        self.stopped = True
Exemplo n.º 5
0
 def clearLines(self, lines: deque):
     lines.clear()
     lines.append("")
     lines.append("")