From 60b8fe71db441af296099bc7acf81881fe8375aa Mon Sep 17 00:00:00 2001 From: DJE98 Date: Sat, 30 Sep 2023 15:56:17 +0200 Subject: [PATCH] Added client with interface to have the possibility to react to emotions --- hackathon/client.py | 44 ++++++++++++++++++++++++++++++++++++++++++ hackathon/own_robot.py | 17 ++++++++++++++++ hackathon/robot.py | 5 +++++ 3 files changed, 66 insertions(+) create mode 100644 hackathon/client.py create mode 100644 hackathon/own_robot.py create mode 100644 hackathon/robot.py diff --git a/hackathon/client.py b/hackathon/client.py new file mode 100644 index 0000000..4313421 --- /dev/null +++ b/hackathon/client.py @@ -0,0 +1,44 @@ +from threading import Thread +from queue import Queue +import time + +from config import SERVER_IP, SERVER_PORT +from communication import create_client_socket, receive_image_and_emotions +from robot import Robot +from own_robot import OwnRobot + +from cli import print_connect, print_disconnect + + +def update_robot(queue: Queue, robot: Robot): + while True: + photo, emotions = queue.get() + with queue.mutex: + queue.queue.clear() + robot.update(photo, emotions) + time.sleep(robot.update_interval) + + +def update_from_server(queue: Queue): + while True: + client_socket = create_client_socket() + try: + client_socket.connect((SERVER_IP, SERVER_PORT)) + print_connect() + while True: + result = receive_image_and_emotions(client_socket) + if result is not None: + queue.put(result) + except Exception: #Normally only if disconnect EOFError + print_disconnect() + client_socket.close() + time.sleep(1) + + +if __name__ == "__main__": + data_queue = Queue() + own_robot = OwnRobot() + update_from_server_thread = Thread(target=update_from_server, args=(data_queue,)) + update_robot_thread = Thread(target=update_robot, args=(data_queue, own_robot)) + update_from_server_thread.start() + update_robot_thread.start() diff --git a/hackathon/own_robot.py b/hackathon/own_robot.py new file mode 100644 index 0000000..8d9fecf --- /dev/null +++ b/hackathon/own_robot.py @@ -0,0 +1,17 @@ +from robot import Robot +import cv2 as cv + +from cli import print_emotions + +import os + + +class OwnRobot(Robot): + count = 0 + + def update(self, image, emotions): + self.count += 1 + if self.count > 10: + self.count = 1 + print_emotions(emotions) + cv.imwrite(os.sep.join(["img", f"ownRobot{self.count}.png"]), image) diff --git a/hackathon/robot.py b/hackathon/robot.py new file mode 100644 index 0000000..ac1e4f4 --- /dev/null +++ b/hackathon/robot.py @@ -0,0 +1,5 @@ +class Robot: + update_interval = 0.5 + + def update(self, image, emotions): + raise NotImplementedError() \ No newline at end of file