python实现视频的实时传送_如何在python中通过网络发送实时视频

该博客介绍了一个使用VidGear库在客户端和服务器之间实时同步传输视频帧的问题解决方案。通过NetGear API,实现了基于ZeroMQ的消息系统,支持PAIR和REQ/REP模式,提供TCP、UDP等传输协议。
摘要由CSDN通过智能技术生成

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)

解决方案

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'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值