From 48891b467f73e94168c992c1d3fc3ff6ab94c267 Mon Sep 17 00:00:00 2001 From: BobElsendoorn Date: Mon, 4 Mar 2024 16:15:57 +0100 Subject: [PATCH] Code van vanochtend --- gui_app.py | 19 +++++++++++++++++++ src/client/communications.py | 9 ++++----- src/client/inputs.py | 6 +++--- src/client/video.py | 12 +++++++----- 4 files changed, 33 insertions(+), 13 deletions(-) create mode 100644 gui_app.py diff --git a/gui_app.py b/gui_app.py new file mode 100644 index 0000000..6b82b54 --- /dev/null +++ b/gui_app.py @@ -0,0 +1,19 @@ +import tkinter as tk +from src.client.video import VideoWindow + +def main(): + #create a window + window = tk.Tk() + #set the window title + window.title("Battlebot GUI") + #load the videostream from raspberry pi (this is defined in the video.py file as an separate window) + video=VideoWindow() + #set the window size + window.geometry("1200x800") + #run the window + window.mainloop() + + +if __name__ == "__main__": + main() + diff --git a/src/client/communications.py b/src/client/communications.py index becd569..5ae8f7b 100644 --- a/src/client/communications.py +++ b/src/client/communications.py @@ -38,7 +38,6 @@ def __init__(self, url, gui): self.connected = False self.send_lock = asyncio.Lock() self.gui = gui - self.command_queue = asyncio.Queue() # Queue for sending commands to the server async def connect(self): async with websockets.connect(self.url) as ws: @@ -54,13 +53,13 @@ async def setup_data_channel(self): self.data_channel = self.pc.createDataChannel("dataChannel") self.data_channel.on("open", self.data_channel_open) self.data_channel.on("message", self.on_data_channel_message) - - asyncio.create_task(self.send_command_queue()) - + self.command_queue = asyncio.Queue() # Queue for sending commands to the server + async def data_channel_open(self): print("Data Channel is open") self.connected = True - + asyncio.create_task(self.send_command_queue()) # create the task here + async def on_data_channel_message(self, message): message = json.loads(message) diff --git a/src/client/inputs.py b/src/client/inputs.py index e5b28c0..8da0b0e 100644 --- a/src/client/inputs.py +++ b/src/client/inputs.py @@ -81,10 +81,10 @@ def get_joystick_position_and_speed(self): y_axis = 0 else: y_axis = round(y_axis / abs(y_axis)) # This will result in -1 or 1 - - if x_axis == 1: + + if x_axis == 1 and y_axis == 0: x_axis = -1 - elif x_axis == -1: + elif x_axis == -1 and y_axis == 0: x_axis = 1 # Get the value of the right trigger (RT on Xbox controller) diff --git a/src/client/video.py b/src/client/video.py index 3c73ed4..2065fbc 100644 --- a/src/client/video.py +++ b/src/client/video.py @@ -12,19 +12,21 @@ def __init__(self, window_name="Video"): def display_frame(self, frame): # Convert the av.VideoFrame to a numpy array in RGB format - img_rgb = frame.to_ndarray(format="rgb24") + img_bgr = frame.to_ndarray(format="rgb24") - # Correctly convert RGB image to BGR for display with OpenCV - img_bgr = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR) + # Correctly convert RGB image to BGR for display with OpenCV and invert the colors + #img_bgr = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR) # Convert the image to BGR - img_bgr_flipped = cv2.flip(img_bgr, 0) + + img_bgr_flipped = cv2.flip(img_bgr, -1) + scale_percent = 50 # percent of original size width = int(img_bgr.shape[1] * scale_percent / 100) height = int(img_bgr.shape[0] * scale_percent / 100) dim = (width, height) # resize image - img_bgr = cv2.resize(img_bgr_flipped, dim, interpolation = cv2.INTER_AREA) + img_bgr = cv2.resize(img_bgr_flipped, dim, interpolation = cv2.INTER_AREA) cv2.imshow(self.window_name, img_bgr)