Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
4 tasks done
NguyenDucQuan12 opened this issue May 23, 2024 · 6 comments
Closed
4 tasks done

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

NguyenDucQuan12 opened this issue May 23, 2024 · 6 comments
Labels
INVALID 🛑 This doesn't seem right or non-applicable or missing information QUESTION ❓ User asked about the working/usage of VidGear APIs. WAITING FOR RESPONSE ⏳ Waiting for the user response.

Comments

@NguyenDucQuan12
Copy link

NguyenDucQuan12 commented May 23, 2024

Issue guidelines

Issue Checklist

  • I have searched open or closed issues for my problem and found nothing related or helpful.
  • I have read the Documentation and found nothing related to my problem.
  • I have gone through the Bonus Examples and FAQs and found nothing related or helpful.

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

@NguyenDucQuan12 NguyenDucQuan12 added the QUESTION ❓ User asked about the working/usage of VidGear APIs. label May 23, 2024
Copy link

welcome bot commented May 23, 2024

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

In the meantime:

  • Read our Issue Guidelines, and update your issue accordingly. Please note that your issue will be fixed much faster if you spend about half an hour preparing it, including the exact reproduction steps and a demo.
  • Go comprehensively through our dedicated FAQ & Troubleshooting section.
  • For any quick questions and typos, please refrain from opening an issue, as you can reach us on Gitter community channel.

@abhiTronix
Copy link
Owner

@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
Copy link
Owner

@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.

@abhiTronix abhiTronix added INVALID 🛑 This doesn't seem right or non-applicable or missing information WAITING FOR RESPONSE ⏳ Waiting for the user response. labels May 24, 2024
@NguyenDucQuan12
Copy link
Author

@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
Copy link
Owner

@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
Copy link
Author

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
INVALID 🛑 This doesn't seem right or non-applicable or missing information QUESTION ❓ User asked about the working/usage of VidGear APIs. WAITING FOR RESPONSE ⏳ Waiting for the user response.
Projects
None yet
Development

No branches or pull requests

2 participants