简体   繁体   中英

sending video stream from server to client using socket programming in python

I am trying to send video stream from server to client using python sockets but facing errors. Here is my server side and client side code.There is some problem with sending frame from server to client ,

Here is my SERVER SIDE code

    #!/usr/bin/env python 



import socket 

import cv2

import json

host = ' ' 

port = 50058

backlog = 5 

size = 1024 

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
**strong text**s.bind((host,port)) 
s.listen(backlog) 

vc=cv2.VideoCapture(0)    
if vc.isOpened():

    rval, frame = vc.read()

while 1: 

    client, address = s.accept()
    rval, frame = vc.read()

    #data = client.recv(size) 
    if rval:  
        #print "recieved data " + str(data)
        #print "sending data to %s" % str(address)

        #p=json.dumps(frame)
        client.send(frame) 
    client.close()

And **CLIENT SIDE CODE **

#!/usr/bin/env python """ A simple echo client """ import socket import cv2 import json host = '169.254.132.51' port = 50058 size = 1024000000 cv2.namedWindow("preview") s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host,port)) while True: data = s.recv() #frame=json.loads(data) cv2.imshow("preview", data) s.close()

You can only send a string or a buffer on a socket. Frame is a numpy array. You have to convert it to string first. Use the following code to convert it to string.

frame = frame.flatten()
data = frame.tostring()

Send data across the network

On the other side use

frame = numpy.fromstring(data, dtype=numpy.uint8)
frame = numpy.reshape(frame, (240,320,3))

to convert it back to a frame that you can pass to cv2.imshow()

I would suggest using UDP instead of TCP as you will have a lower latency.

1) The Server closes the connection after 1 send, but the clients seems to expect more than one frame

2) SOCK_STREAM (TCP) only supports a stream without delimiting packets. you need to build the delimiting parts yourself.

3) recvall isn't even a function

maybe you should try implementing it with remote multiprocessing: https://docs.python.org/2/library/multiprocessing.html#using-a-remote-manager

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM