如何在python中通过网络发送实时视频 [英] how to send live video over network in python

查看:1218
本文介绍了如何在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 !

这是我在这里使用的代码, https://stackoverflow.com/a/30988516/4663461

here is the code i'am using which i have found here https://stackoverflow.com/a/30988516/4663461

client.py

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

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)

推荐答案

我有点晚了,但是我创建了功能强大的&线程 VidGear 视频处理python库,现在提供

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:

打开您喜欢的终端并执行以下python代码:

Open your favorite terminal and execute the following python code:

注意: 您可以通过在服务器端按键盘上的[Ctrl + c]来随时在服务器和客户端上结束流式传输!

# 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.客户端:(最低限度的示例)

然后在同一系统上打开另一个终端,并执行以下python代码并查看输出:

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()

注意: NetGear当前仅支持两种ZeroMQ消息传递模式:即zmq.PAIRzmq.REQ and zmq.REP,支持的协议为:'tcp', 'upd', 'pgm', 'inproc', 'ipc'

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'

可在此处找到更多高级用法: https://github.com/abhiTronix/vidgear/wiki/NetGear

这篇关于如何在python中通过网络发送实时视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆