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.37k stars 253 forks source link

[Question]: There is a problem with the thread #400

Closed NguyenDucQuan12 closed 5 months ago

NguyenDucQuan12 commented 5 months ago

Issue guidelines

Issue Checklist

Describe your Question

If I run the netgear tutorial with cv2 it works great, but the lag is huge so I tested the new thread for reading frames and it improved the camera lag significantly. However, when I run the code again it fails to connect to the server and client. I don't know why, after the first successful run I didn't edit my code but the error still appeared.

Terminal log output(Optional)

(venv stream) PS D:\virtua_env> python .stream_camera\stream_ony_one_device_using_thread.py
09:57:46 ::    Helper     ::   INFO   :: Running VidGear Version: 0.3.2
09:57:46 ::    NetGear    ::  DEBUG   :: Reliable transmission is enabled for this pattern with max-retries: 3 and timeout: 4.0 secs.
09:57:46 ::    NetGear    ::  DEBUG   :: Successfully connected to address: tcp://172.31.99.35:5455 with pattern: 0.
09:57:46 ::    NetGear    ::  DEBUG   :: JPEG Frame-Compression is activated for this connection with Colorspace:`BGR`, Quality:`90`%, Fastdct:`enabled`, and Fastupsample:`disabled`.
09:57:46 ::    NetGear    ::  DEBUG   :: Unique System ID is O0INTY02.
09:57:46 ::    NetGear    ::  DEBUG   :: Send Mode is successfully activated and ready to send data.
09:57:50 ::    NetGear    :: CRITICAL :: No response from Client, Reconnecting again...
09:57:54 ::    NetGear    :: CRITICAL :: No response from Client, Reconnecting again...
09:57:59 ::    NetGear    :: CRITICAL :: No response from Client, Reconnecting again...
09:57:59 ::    NetGear    ::  ERROR   :: Client failed to respond on repeated attempts.
Traceback (most recent call last):
  File "D:\virtua_env\.stream_camera\stream_ony_one_device_using_thread.py", line 57, in <module>
    stream.send_frame()
  File "D:\virtua_env\.stream_camera\stream_ony_one_device_using_thread.py", line 42, in send_frame
    self.server.send(self.frame)
  File "D:\virtua_env\.stream_camera\Lib\site-packages\vidgear\gears\netgear.py", line 1452, in send
    raise RuntimeError(

Python Code(Optional)

server.py:
# import required libraries
from vidgear.gears import NetGear
import cv2
import threading

class stream_camera():
    def __init__(self):
        # Open suitable video stream, such as webcam on first index(i.e. 0)
        self.stream = cv2.VideoCapture("rtsp://169.254.184.113:554/1/stream1/Profile1")
        self.ret = None
        self.frame = None
        # 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 !!!
        self.server = NetGear(
            address="172.31.99.35",
            port="5455",
            protocol="tcp",
            pattern=0,
            logging=True,
            **options
        )

        # Khởi tạo thread, tham số daemon có nghĩa là khi chương trình tắt thì thread cũng tự động đóng
        self.thread = threading.Thread(target=self.process)
        self.thread.daemon = True
        self.thread.start()

    def process(self):
        while True:
            self.ret, self.frame = self.stream.read()
    # loop over until KeyBoard Interrupted
    def send_frame(self):
        while True:

            try:
                if self.ret:
                    # send frame to server
                    self.server.send(self.frame)

            except KeyboardInterrupt:

                self.close_stream()
                break

    # safely close video stream
    def close_stream(self):
        self.stream.release()

        # safely close server
        self.server.close()
if __name__ == "__main__":
    stream = stream_camera()
    stream.send_frame()

and server.py:
# import required libraries
from vidgear.gears import NetGear
import cv2

# define various 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(
    address="172.31.99.35",
    port="5454",
    protocol="tcp",
    pattern=1,
    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 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()

VidGear Version

0.3.2

Python version

3.11.8

Operating System version

window 10 pro

Any other Relevant Information?

image

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

@NguyenDucQuan12 It is a bad Idea to put multi-threading over a already multi-threaded API like NetGear. It is not connecting because that thread is still be running in the background as zombie process, since you used self.thread.daemon = True. The zombie process might be holding the address already 172.31.99.35 causing it to fail when trying to connect. Hope this helps.

abhiTronix commented 5 months ago

@NguyenDucQuan12 Also, 172.31.99.35 is IP address of server or client machine? You need to put IP address of machine running NetGear with recieve_mode=True on both server and client. See if that is true or not.

NguyenDucQuan12 commented 5 months ago

@abhiTronix That is the ip address of the machine without an ip camera. If I don't use multithreading, the frames lag, the fps is only 8 while the camera has a fps configuration of 25, (1920x1080)

abhiTronix commented 5 months ago

@NguyenDucQuan12 Don't use OpenCV then, use CamGear or VideoGear instead like official examples does, which essentially does same multi-threading you're doing and in much safer way. Also if you want to auto reconnect then see this example: https://abhitronix.github.io/vidgear/v0.3.2-stable/help/videogear_ex/#using-videogear-for-capturing-rtsprtmp-urls

NguyenDucQuan12 commented 5 months ago

@abhiTronix Please let me know if there are device hardware requirements. I am currently able to stream images from the camera to another computer device, but the latency problem is very large, do you think I should use vidgear, camgear or netgear for better results. I am familiar with opencv, should I switch to using videogear?