gpt4 book ai didi

python - 如何在python中通过网络发送实时视频

转载 作者:太空宇宙 更新时间:2023-11-03 23:14:09 25 4
gpt4 key购买 nike

我正在尝试将视频从客户端发送到服务器并在服务器端显示。问题是,当我运行代码时,我得到一个空的无响应窗口!

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

客户端.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)

服务器.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 库 NetGear API , 它专门设计用于通过 ZmQ 消息系统在网络上的互连系统之间实时同步传输视频帧。为了您的方便,这里有一个最简单的例子:

A.服务器端:(最低限度示例)

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

注意: 您可以随时在服务器端和客户端按下键盘上的 [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.客户端:(Bare-Minimum example)

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

# 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 和 zmq.REP以及zmq.PUB和zmq.SUB,支持的协议(protocol)有:'tcp', 'ipc'

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

关于python - 如何在python中通过网络发送实时视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43299440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com