abhiTronix / vidgear

A High-performance cross-platform Video Processing Python framework powerpacked with unique trailblazing features :fire:
https://abhitronix.github.io/vidgear
Apache License 2.0
3.39k stars 255 forks source link

[Question]: Client-server issues using Linux and Windows #404

Closed IvanZamora closed 5 months ago

IvanZamora commented 5 months ago

Issue guidelines

Issue Checklist

Describe your Question

I am trying to use a client-server architecture, using NetGear to get the video from the webcam, I have 4 scripts where 2 that act as client/server works on Linux but not on Windows and the other 2 scripts work on Windows but not on Linux, plus my goal is to connect the client and server independently of the OS.

Between Linux client and Windows server they don't connect, if I try Windows client with Linux server, they don't connect either.

Terminal log output(Optional)

No response

Python Code(Optional)

Linux Server:
# import required libraries
from vidgear.gears import NetGear
import cv2

# Open suitable video stream, such as webcam on first index(i.e. 0)
stream = cv2.VideoCapture(0)

# define tweak flags
options = {"flag": 0, "copy": False, "track": False}

# Define Netgear Client at given IP address and define parameters 
# !!! change following IP address '192.168.x.xxx' with yours !!!
server = NetGear(
    address="X.X.X.X",
    port="5454",
    protocol="tcp",
    pattern=0,
    logging=True,
    **options
)

# loop over until KeyBoard Interrupted
while True:

    try:
        # read frames from stream
        (grabbed, frame) = stream.read()

        # check for frame if not grabbed
        if not grabbed:
            break

        # {do something with the frame here}

        # send frame to server
        server.send(frame)

    except KeyboardInterrupt:
        break

# safely close video stream
stream.release()

# safely close server
server.close()

Linux client:
# import required libraries
from vidgear.gears import NetGear
import cv2

# define tweak flags
options = {"flag": 0, "copy": False, "track": False}

# Define Netgear Client at given IP address and define parameters 
# !!! change following IP address '192.168.x.xxx' with yours !!!
client = NetGear(
    port="5454",
    protocol="tcp",
    pattern=0,
    receive_mode=True,
    logging=True,
    **options
)

# loop over
while True:

    # receive frames from network
    frame = client.recv()

    # check for received frame if Nonetype
    if frame is None:
        break

    # {do something with the received frame here}

    # Show output window
    cv2.imshow("Output Frame", frame)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

# close output window
cv2.destroyAllWindows()

# safely close client
client.close()

Windows server:
# import library
from vidgear.gears.asyncio import NetGear_Async
import cv2, asyncio

# initialize Server without any source
# server = NetGear_Async(address="X.X.X.X", port="5555", pattern=0, source=None, logging=True)

#server = NetGear_Async(
#    source=None,
#    address="X.X.X.X",
#    port="5454",
#    protocol="tcp",
#    pattern=1,
#    logging=True,
#).launch()

# !!! define your own video source here !!!
# Open any video stream such as live webcam
# video stream on first index(i.e. 0) device
stream = cv2.VideoCapture(2)

# Create a async frame generator as custom source
async def my_frame_generator():

    # loop over stream until its terminated
    while True:

        # read frames
        (grabbed, frame) = stream.read()

        # check if frame empty
        if not grabbed:
            break

        # do something with the frame to be sent here

        # yield frame
        yield frame
        # sleep for sometime
        await asyncio.sleep(0)

if __name__ == "__main__":
    # Setup Vidgear server
    server = NetGear_Async(address="localhost", port="5555", source=None)
    # set event loop
    asyncio.set_event_loop(server.loop)
    # Add your custom source generator to Server configuration
    server.config["generator"] = my_frame_generator()
    # Launch the Server
    server.launch()
    try:
        # run your main function task until it is complete
        server.loop.run_until_complete(server.task)
    except (KeyboardInterrupt, SystemExit):
        # wait for interrupts
        pass
    finally:
        # close stream
        stream.release()
        # finally close the server
        server.close()

Windows client:

# import libraries
from vidgear.gears.asyncio import NetGear_Async
import cv2, asyncio

#client = NetGear_Async(
#    address="X.X.X.X",
#    port="5454",
#    protocol="tcp",
#    pattern=1,
#    receive_mode=True,
#    logging=True,
#).launch()

# Create a async function where you want to show/manipulate your received frames
async def main():
    # loop over Client's Asynchronous Frame Generator
    async for frame in client.recv_generator():

        # {do something with received frames here}

        # Show output window
        cv2.imshow("Output Frame", frame)
        key = cv2.waitKey(1) & 0xFF

        # await before continuing
        await asyncio.sleep(0)

if __name__ == "__main__":
    # define and launch Client with `receive_mode=True`
    client = NetGear_Async(port="5555", receive_mode=True, timeout=float("inf")).launch()
    # Set event loop to client's
    asyncio.set_event_loop(client.loop)
    try:
        # run your main function task until it is complete
        client.loop.run_until_complete(main())
    except (KeyboardInterrupt, SystemExit):
        # wait for interrupts
        pass

    # close all output window
    cv2.destroyAllWindows()
    # safely close client
    client.close()

VidGear Version

0.3.2

Python version

3.10

Operating System version

Ubuntu 20.04.6 LTS and Windows 11

Any other Relevant Information?

No response

welcome[bot] commented 5 months ago

Thanks for opening this issue, a maintainer will get back to you shortly!

In the meantime:

abhiTronix commented 5 months ago

@IvanZamora You need to provide the actual IP address of the both machines pair wise to make connection. This makes no sense that It can't work between windows, linux or macOS.

abhiTronix commented 5 months ago

Change as two pair must have different address="X.X.X.X" and port="5454", also, netgear_async and netgear are two different APIs, and are not compatible.