Sending live video frame over network in python opencv

后端 未结 7 1661
南旧
南旧 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:01

    as @Rohan Sawant said i used zmq library without using base64 encoding. here is the new code

    Streamer.py

    import base64
    import cv2
    import zmq
    import numpy as np
    import time
    
    context = zmq.Context()
    footage_socket = context.socket(zmq.PUB)
    footage_socket.connect('tcp://192.168.1.3:5555')
    
    camera = cv2.VideoCapture(0)  # init the camera
    
    while True:
            try:
                    grabbed, frame = camera.read()  # grab the current frame
                    frame = cv2.resize(frame, (640, 480))  # resize the frame
                    encoded, buffer = cv2.imencode('.jpg', frame)
                    footage_socket.send(buffer)
    
    
            except KeyboardInterrupt:
                    camera.release()
                    cv2.destroyAllWindows()
                    break
    

    Viewer.py

    import cv2
    import zmq
    import base64
    import numpy as np
    
    context = zmq.Context()
    footage_socket = context.socket(zmq.SUB)
    footage_socket.bind('tcp://*:5555')
    footage_socket.setsockopt_string(zmq.SUBSCRIBE, np.unicode(''))
    
    while True:
        try:
            frame = footage_socket.recv()
            npimg = np.frombuffer(frame, dtype=np.uint8)
            #npimg = npimg.reshape(480,640,3)
            source = cv2.imdecode(npimg, 1)
            cv2.imshow("Stream", source)
            cv2.waitKey(1)
    
        except KeyboardInterrupt:
            cv2.destroyAllWindows()
            break
    

提交回复
热议问题