Sending live video frame over network in python opencv

后端 未结 7 1626
南旧
南旧 2020-11-28 06:39

I\'m trying to send live video frame that I catch with my camera to a server and process them. I\'m usig opencv for image processing and python for the language. Here is my

相关标签:
7条回答
  • 2020-11-28 07:08

    I'm kind of late but my powerful & threaded VidGear Video Processing python library now provide NetGear API, which is exclusively designed to transfer video frames synchronously between interconnecting systems over the network in real-time. Here's an example:

    A. Server End:(Bare-Minimum example)

    Open your favorite terminal and execute the following python code:

    Note: You can end streaming anytime on both server and client side by pressing [Ctrl+c] on your keyboard on server end!

    # import libraries
    from vidgear.gears import VideoGear
    from vidgear.gears import NetGear
    
    stream = VideoGear(source='test.mp4').start() #Open any video stream
    server = NetGear() #Define netgear server with default settings
    
    # infinite loop until [Ctrl+C] is pressed
    while True:
        try: 
            frame = stream.read()
            # read frames
    
            # check if frame is None
            if frame is None:
                #if True break the infinite loop
                break
    
            # do something with frame here
    
            # send frame to server
            server.send(frame)
    
        except KeyboardInterrupt:
            #break the infinite loop
            break
    
    # safely close video stream
    stream.stop()
    # safely close server
    writer.close()
    

    B. Client End:(Bare-Minimum example)

    Then open another terminal on the same system and execute the following python code and see the output:

    # import libraries
    from vidgear.gears import NetGear
    import cv2
    
    #define netgear client with `receive_mode = True` and default settings
    client = NetGear(receive_mode = True)
    
    # infinite loop
    while True:
        # receive frames from network
        frame = client.recv()
    
        # check if frame is None
        if frame is None:
            #if True break the infinite loop
            break
    
        # do something with frame here
    
        # Show output window
        cv2.imshow("Output Frame", frame)
    
        key = cv2.waitKey(1) & 0xFF
        # check for 'q' key-press
        if key == ord("q"):
            #if 'q' key-pressed break out
            break
    
    # close output window
    cv2.destroyAllWindows()
    # safely close client
    client.close()
    

    More advanced usage and related docs can be found here: https://github.com/abhiTronix/vidgear/wiki/NetGear

    0 讨论(0)
提交回复
热议问题