Beispiel #1
0
 def poll_connections(self):
     common.print_debug_banner(f"POLLING CONNECTIONS")
     print(f"CURRENT PROCESS COUNT: {len(self.client_to_process.keys())}")
     for client, last_msg_ts in self.client_to_last_msg.items():
         if time.time() - last_msg_ts > self.CLIENT_TIMEOUT:
             common.print_debug_banner(f"FOUND OVERTIME CLIENT: {client}")
             self.cleanup_client(client)
Beispiel #2
0
def server(landmark_queue, prediction_queue):
    common.print_debug_banner("STARTED SERVER")
    UDPClientSocket = socket.socket(family=socket.AF_INET,
                                    type=socket.SOCK_DGRAM)
    UDPClientSocket.setblocking(0)
    while True:
        try:
            landmark = landmark_queue.get()
            encrypted_landmark = encrypt.encrypt_chacha(landmark)
            # Send message to server using created UDP socket
            UDPClientSocket.sendto(encrypted_landmark, serverAddressPort)
            # Receive message from the server
            msgFromServer = UDPClientSocket.recvfrom(2048)[0]
            raw_data = encrypt.decrypt_chacha(msgFromServer)
            prediction_queue.put(raw_data)
        except encrypt.DecryptionError:
            print(f"tried to decrypt {msgFromServer}")
        except socket.error as e:
            # print(f"SOCKET EXCEPTION: {e}")
            pass
        except Exception as e:
            print(f"SERVER EXCEPTION: {e}")
            pass
Beispiel #3
0
    def datagram_received(self, data, addr):
        if addr is None:
            return

        if addr in BLACKLIST_ADDRS:
            common.print_debug_banner(f"BLOCKED {addr}")
            return

        # new client connected
        if addr not in self.client_to_f_q and addr != self.cleaning_process:
            self.start_process(addr)
            return

        self.client_to_last_msg[addr] = time.time()

        # Receive and print the datagram received from client
        try:
            if ENCRYPT:
                data = encrypt.decrypt_chacha(data)
            # received termination signal from client
            if len(data) < 4:
                if data == "END":
                    common.print_debug_banner(f"RECEIVED 'END' FROM {addr}")
                    self.client_to_f_q[addr].put("END")
                    self.cleanup_client(addr)
                elif data == "ACK":
                    common.print_debug_banner(f"RECEIVED 'ACK' FROM {addr}")
                    self.client_to_connected[addr] = True
                return

            landmark_arr = np.array(
                [float(i.strip()) for i in data.split(",")])
            normalized_data = normalize_features(landmark_arr)

            self.client_to_f_q[addr].put_nowait(normalized_data)

            pred = self.client_to_p_q[addr].get_nowait()
            tag = array_to_class(pred, addr, self.client_to_connected[addr])
            self.transport.sendto(tag, addr)

        except encrypt.DecryptionError:
            print(f"tried to decrypt {data}")
        except Exception as e:
            # print(e)
            pass
Beispiel #4
0
    def cleanup_client(self, addr):
        common.print_debug_banner(f"CLEANING UP CLIENT: {addr}")
        self.cleaning_process = addr
        del self.client_to_f_q[addr]
        del self.client_to_p_q[addr]
        del self.client_to_last_msg[addr]
        del self.client_to_connected[addr]

        process_to_del = self.client_to_process[addr]
        process_to_del.terminate()

        common.print_debug_banner("FINISHED TERMINATING")
        # process_to_del.close()
        # common.print_debug_banner("FINISHED CLOSING")
        del self.client_to_process[addr]

        common.print_debug_banner(f"FINISHED CLEANUP")
        print(f"CURRENT PROCESS COUNT: {len(self.client_to_process.keys())}")
        self.cleaning_process = None
Beispiel #5
0
def predict_loop(model_path, f_q, p_q):
    # force predict to run on CPU
    if not GPU:
        import os
        os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
    import tensorflow as tf
    import keras
    from train import TIMESTEPS, init_gpu

    if LOG:
        import timeit
        import logging

        LOG_FILE_NAME = "logs/predict_log"
        logging.basicConfig(level=logging.DEBUG,
                            filemode="a+",
                            filename=LOG_FILE_NAME,
                            format="%(message)s")
        if GPU:
            logging.info(f"\n-----USING GPU------")
        else:
            logging.info(f"\n-----USING CPU------")

        times = []
        time_count = 0
        TIME_FREQ = 60

    def slide(w, new):
        # Discard oldest frame and append new frame to data window
        w[:-1] = w[1:]
        w[-1] = new
        return w

    if GPU:
        init_gpu()
    model = keras.models.load_model(model_path)

    delay = 0
    window = None
    results = None
    results_len = ceil(PRINT_FREQ / PRED_FREQ)

    if DEBUG:
        common.print_debug_banner("STARTED PREDICTION")

    while True:
        row = f_q.get()

        if len(row) == 3 and row == "END":
            break

        if window is None:
            window = np.zeros((TIMESTEPS, len(row)))

        window = slide(window, row)

        if delay >= PRED_FREQ:
            out = model(np.array([window]))

            if results is None:
                results = np.zeros((results_len, len(LABELS)))

            results = slide(results, out)
            pred = np.mean(results, axis=0)
            p_q.put(pred)

            delay = 0

        delay += 1

    common.print_debug_banner("ENDING PREDICT PROCESS")
Beispiel #6
0
                results = np.zeros((results_len, len(LABELS)))

            results = slide(results, out)
            pred = np.mean(results, axis=0)
            p_q.put(pred)

            delay = 0

        delay += 1

    common.print_debug_banner("ENDING PREDICT PROCESS")


def live_predict(model_path, use_holistic):
    # launch UDP server to receive landmark features
    common.start_server(LandmarkReceiver(), SERVER_ADDR)


if __name__ == "__main__":
    if len(argv) < 2:
        model_path = CURRENT_WORKING_DIRECTORY / 'models' / DEFAULT_MODEL
        if not model_path.exists():
            raise MissingModelException("NO MODEL CAN BE USED!")
    else:
        model_path = argv[1]

    if DEBUG:
        common.print_debug_banner(f"using model {model_path}")

    live_predict(model_path, False)
Beispiel #7
0
def video_loop(landmark_queue, prediction_queue, use_holistic=False):
    cap = cv2.VideoCapture(0)
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    cap.set(cv2.CAP_PROP_FOURCC, fourcc)
    if not cap.isOpened():
        print("Error opening Camera")
    fps = cap.get(cv2.CAP_PROP_FPS)
    print("Webcam FPS = {}".format(fps))

    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    mp_drawing = mp.solutions.drawing_utils

    timestamp = None
    started = False
    predicted = None
    initialized = False
    delay = 0
    pred_history = deque([" "] * 5, 5)
    pdecay = time.time()

    print("starting image cap")

    for image, results in holistic.process_capture(cap, use_holistic):
        window_state = cv2.getWindowProperty(APP_NAME, 0)
        if started and window_state == -1:
            print("QUITTING")
            break

        started = True

        newtime = time.time()
        if timestamp is not None:
            diff = newtime - timestamp
            # Uncomment to print time between each frame
            # print(diff)
        timestamp = newtime

        row = holistic.to_landmark_row(results, use_holistic)

        landmark_str = ','.join(np.array(row).astype(np.str))

        # send comma delimited str of flattened landmarks in bytes to server
        try:
            landmark_queue.put_nowait(landmark_str)
        except Exception as e:
            print(e)

        try:
            out = prediction_queue.get_nowait()

            # toggle the server status flag on first message received
            if out and not initialized:
                initialized = True

                common.print_debug_banner(
                    "SENDING ACK TO SERVER FOR CONNECTION")
                # send a one-time ACK to toggle server connected status
                landmark_queue.put_nowait("ACK")

            if delay >= PRINT_FREQ:
                if out and out != pred_history[-1] and out != "None":
                    pred_history.append(out)
                    pdecay = time.time()
                delay = 0
        except:
            pass

        delay += 1
        if time.time() - pdecay > 7:
            pred_history = deque([" "] * 5, 5)
        holistic.draw_landmarks(image, results, use_holistic,
                                ' '.join(pred_history))

        if initialized:
            cv2.circle(image, (20, 450), 10, (0, 255, 0), -1)
            cv2.putText(image, 'online', (40, 458), cv2.FONT_HERSHEY_SIMPLEX,
                        1, (255, 255, 255), 2, cv2.LINE_AA)
            cv2.imshow(APP_NAME, image)
        else:
            cv2.circle(image, (20, 450), 10, (0, 0, 255), -1)
            cv2.putText(image, 'connecting', (40, 458),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2,
                        cv2.LINE_AA)
            cv2.imshow(APP_NAME, image)
    cap.release()
    cv2.destroyAllWindows()

    # send termination message to server
    landmark_queue.put("END")