Added client with interface to have the possibility to react to emotions
This commit is contained in:
parent
7a68a8cb74
commit
60b8fe71db
44
hackathon/client.py
Normal file
44
hackathon/client.py
Normal file
@ -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()
|
17
hackathon/own_robot.py
Normal file
17
hackathon/own_robot.py
Normal file
@ -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)
|
5
hackathon/robot.py
Normal file
5
hackathon/robot.py
Normal file
@ -0,0 +1,5 @@
|
||||
class Robot:
|
||||
update_interval = 0.5
|
||||
|
||||
def update(self, image, emotions):
|
||||
raise NotImplementedError()
|
Loading…
Reference in New Issue
Block a user