Python Forum
Multi-Threaded Camera Feed issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multi-Threaded Camera Feed issue
#1
Exclamation 
I have written a python code, for feeding multiple cameras using opencv and websockets ( not socket io ).

The first segment of the code consists of a function that handles the camera inputs and sending it to the websocket reciever side.

async def server_handler(websocket, path, camera_index):
    cap = cv2.VideoCapture(camera_index)

    while cap.isOpened():
        try:
            ret, frame = cap.read()
            if not ret:
                break
            
            frame = cv2.resize(frame, (400, 300))
            _, buffer = cv2.imencode('.jpg', frame, [int(cv2.IMWRITE_JPEG_QUALITY), 50])
            frame_data = base64.b64encode(buffer).decode('utf-8')

            await websocket.send(frame_data)

        except websockets.exceptions.ConnectionClosed:
            print(f"Connection closed for camera {camera_index}.")
            break

    cap.release()
The second segment is running the server with asyncio event loop that runs forever.

`
def run_server(camera_index, port):
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    start_server = websockets.serve(
        lambda websocket, path, ci=camera_index: server_handler(websocket, path, ci),
        config.IP, port
    )

    loop.run_until_complete(start_server)
    loop.run_forever()
`


The third function which runs in main via asyncio.run is a fucntion that uses ThreadPoolExecutor to connect to a single ip but different ports. The idea is that for different camera feeds i communicate to different ports.

`
async def main():
    with ThreadPoolExecutor() as executor:
        futures = [executor.submit(run_server, camera_index, port) for camera_index, port in zip(config.CAMERA_INDICES, config.PORTS)]
        await asyncio.gather(*futures)
`

Now, THE PROBLEM here is that, the code works works fine for the first 30 seconds, but after that it takes nearly 14-15 seconds delay to RESTART the thread and back to feeding, In other words there is 14 seconds freeze in the middle of every run and ive used normal threading library but the issue remains same. What could be the solution to this? and what can be done instead? Thanks in advance Smile
Gribouillis write May-05-2024, 10:07 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 246 May-13-2024, 05:57 AM
Last Post: Larz60+
  Draw bounding boxes on live feed Jerome 0 353 Jan-20-2024, 10:50 PM
Last Post: Jerome
  Get image from PI camera and analyze it korenron 0 1,202 Apr-28-2022, 06:49 AM
Last Post: korenron
  How to parse a live feed in Python? Daring_T 2 4,252 Jan-20-2022, 04:17 AM
Last Post: Daring_T
  Create RTSP stream from camera? korenron 1 3,334 Jan-04-2022, 10:38 AM
Last Post: Larz60+
  Improves performance on socket multi-threaded servers for object detection pennant 1 1,952 Aug-31-2021, 08:43 AM
Last Post: Larz60+
  How to get OpenCV to display entire camera frame? George713 1 3,322 Aug-12-2021, 02:45 AM
Last Post: Pedroski55
  Feed List items with Integer euras 9 4,075 May-19-2021, 07:45 PM
Last Post: snippsat
  Get return value from a threaded function Reverend_Jim 3 17,357 Mar-12-2021, 03:44 AM
Last Post: Reverend_Jim
  splitting UAV/sat images to smaller pieces in order to feed a CNN hobbyist 0 1,560 Dec-08-2020, 11:48 AM
Last Post: hobbyist

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020