From 1a7efcfa23f7c16a18e80bcbd872fb24cb532c0e Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 2 Jun 2023 15:33:22 +0200 Subject: [PATCH] change from asyncio to normal thread --- src/camera/camera/camera_controller.py | 4 --- test_stream.py | 46 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 test_stream.py diff --git a/src/camera/camera/camera_controller.py b/src/camera/camera/camera_controller.py index 9fe411fd..dab35e37 100644 --- a/src/camera/camera/camera_controller.py +++ b/src/camera/camera/camera_controller.py @@ -57,10 +57,6 @@ class CameraController(Node): return response def handle_video_connection(self): - self.get_logger().info('Starting video thread') - asyncio.run(self.send_video()) - - async def send_video(self): self.get_logger().info('Starting sending video') vid = cv2.VideoCapture(0) diff --git a/test_stream.py b/test_stream.py new file mode 100644 index 00000000..25822610 --- /dev/null +++ b/test_stream.py @@ -0,0 +1,46 @@ +import cv2 +import requests + +video_url = "http://10.1.1.41:8080/video" +# Set the headers for the POST request +headers = {'Content-Type': 'application/octet-stream'} + +vid = cv2.VideoCapture(0) + +# vid.set(cv2.CAP_PROP_FRAME_WIDTH, RES_4K_W) +# vid.set(cv2.CAP_PROP_FRAME_HEIGHT, RES_4K_H) +while True: + try: + while vid.isOpened(): + pass + ret, frame = vid.read() + + if not ret: + # If reading the frame failed, break the loop + break + + # Convert the frame to bytes + _, img_encoded = cv2.imencode('.jpg', frame) + frame_data = img_encoded.tobytes() + + # Send the frame data as the request body + response = requests.post(video_url, data=frame_data, headers=headers) + + # Check the response status + if response.status_code == 200: + print('Frame sent successfully.') + else: + print('Failed to send frame.') + # if self.websocket is not None: + # img,frame = vid.read() + # frame = cv2.resize(frame,(640,480)) + # encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 65] + # man = cv2.imencode('.jpg', frame, encode_param)[1] + # self.get_logger().info('Sending video') + # asyncio.ensure_future(self.websocket.send(man.tobytes()),loop=self.event_loop) + # await asyncio.sleep(1) + # else: + # self.get_logger().info('No websocket connection') + + except Exception as e: + print('Something went wrong while reading and sending video: ' + str(e)) \ No newline at end of file