'how to send live video over network in python
i'am trying to send video from client to server and showing it at the server side. the probleme is that when i run the code i get an empty non respond window !
here is the code i'am using which i have found here https://stackoverflow.com/a/30988516/4663461
client.py
import cv2
import numpy as np
import socket
import sys
import pickle
import struct
cap=cv2.VideoCapture(path_to_video)
clientsocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
clientsocket.connect(('localhost',8089))
while True:
ret,frame=cap.read()
data = pickle.dumps(frame)
clientsocket.sendall(struct.pack("L", len(data))+data)
server.py
import socket
import sys
import cv2
import pickle
import numpy as np
import struct
HOST=''
PORT=8089
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print ('Socket created')
s.bind((HOST,PORT))
print ('Socket bind complete')
s.listen(10)
print ('Socket now listening')
conn,addr=s.accept()
data = b''
payload_size = struct.calcsize("L")
while True:
while len(data) < payload_size:
data += conn.recv(4096)
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack("L", packed_msg_size)[0]
while len(data) < msg_size:
data += conn.recv(4096)
frame_data = data[:msg_size]
data = data[msg_size:]
###
frame=pickle.loads(frame_data)
print frame
cv2.imshow('frame',frame)
Solution 1:[1]
I'm kind of late but I've created powerful & threaded VidGear Video Processing python library that now provides NetGear API, which is exclusively designed to transfer video frames synchronously between interconnecting systems over the network in real-time through ZmQ messaging system. Here's a bare-minimum example for your convenience:
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()
Note: NetGear currently supports only two ZeroMQ messaging patterns: i.e zmq.PAIR and zmq.REQ and zmq.REP and the supported protocol are: 'tcp', 'upd', 'pgm', 'inproc', 'ipc'
More advanced usage can be found here: https://github.com/abhiTronix/vidgear/wiki/NetGear
Solution 2:[2]
frame=pickle.loads(frame_data)
print frame
cv2.imshow('frame',frame)
# add this
if cv2.waitKey(1) & 0xFF == ord('q'):
break
Solution 3:[3]
I had the same Problem. It turned out to be my laptop. Check your graphics card driver update and your version of python. Not all versions work with opencv properly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | abhiTronix |
| Solution 2 | Serhiy Dovgopolik |
| Solution 3 | m3ntor |
