python实现不同电脑之间视频传输功能
作者:景唯acr 时间:2021-01-07 00:03:40
1. imageZMQ库实现
imageZMQ库链接:https://github.com/jeffbass/imagezmq
该库原本是用于树莓派上的视频传输,其包含很多示例,有兴趣可以去看看。
上图中间的笔记本为发送端,其余两个屏幕显示的是接收端视频。本文视频传输实现的前提是确保发送端和接收端接在同一个局域网下。
发送端
import socket
import time
import cv2
import imagezmq
import traceback
import simplejpeg
# capture=cv2.VideoCapture(0) # 获取摄像头视频
capture=cv2.VideoCapture(r"D:\project\dataset\video\测试.mp4")
# 192.168.100.104 为发送端主机ip地址
sender = imagezmq.ImageSender(connect_to='tcp://192.168.100.104:5555', REQ_REP=False)
rpi_name = socket.gethostname() # 获取主机名
time.sleep(2.0)
jpeg_quality = 95 #调整图片压缩质量,95%
while(True):
try:
ref, frame=capture.read(0)
time.sleep(1/60)
image = cv2.resize(frame,(1280,720))
curtime = time.time()
msg = rpi_name+'*'+str(curtime)
# 通过simplejpeg函数将图片编码为jpeg格式,提高传输效率
jpg_buffer = simplejpeg.encode_jpeg(image, quality=jpeg_quality,
colorspace='BGR')
sender.send_jpg(msg, jpg_buffer)
cv2.imshow(rpi_name, image)
cv2.waitKey(1)
except:
print(traceback.print_exc())
break
接收端(可以多台设备同时接收)
import cv2
import imagezmq
import traceback
import time
import simplejpeg
# 接收发送端数据,输入发送端的ip地址
image_hub = imagezmq.ImageHub(open_port='tcp://192.168.100.104:5555',REQ_REP=False)
frame_count = 1
time1 = 0
while True:
try:
time1 = time.time() if frame_count == 1 else time1
name, image = image_hub.recv_jpg()
# 解码
image = simplejpeg.decode_jpeg(image, colorspace='BGR')
cv2.imshow(name.split('*')[0], image)
cv2.waitKey(1)
time2 = time.time()
print(image.shape[:2], int(frame_count/(time2-time1)))
frame_count += 1
except:
print(traceback.format_exc())
break
2. TCP协议实现
import socket
import cv2
import sys
import numpy as np
address = ('192.168.100.104', 5005) # 服务端地址和端口
ser = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ser.bind(address)
ser.listen(5)
# 阻塞式
print('waiting。。。')
conn, addr = ser.accept()
print('建立连接...')
print('连接对象:', addr)
cap = cv2.VideoCapture(r"D:\project\dataset\video\测试.mp4")
# cap = cv2.VideoCapture(0)
frames_num=cap.get(7)
print('视频总帧数:',frames_num)
print('发送目标...')
count = 0
while cap.isOpened():
try:
data = conn.recv(1024)
data = data.decode()
if not data:
break
ret, frame = cap.read()
frame = cv2.resize(frame,(1280,720))
cv2.imshow('send', frame)
cv2.waitKey(1)
count += 1
# 数据打包有很多方式,也可以用json打包
img_encode = cv2.imencode('.jpg', frame)[1]
data_encode = np.array(img_encode)
str_encode = data_encode.tostring()
conn.sendall(str_encode)
except KeyboardInterrupt:
print('KeyboardInterrupt')
sys.exit(0)
接收端
import socket
import sys
import cv2
import numpy as np
import time
address = ('192.168.100.104', 5005) # 服务端地址和端口
cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
cli.connect(address) # 尝试连接服务端
except Exception:
print('[!] Server not found or not open')
sys.exit()
frame_count = 1
while True:
time1 = time.time() if frame_count == 1 else time1
trigger = 'ok'
cli.sendall(trigger.encode())
data = cli.recv(1024*1024*20)
image = np.frombuffer(data, np.uint8)
image = cv2.imdecode(image,cv2.IMREAD_COLOR)
cv2.imshow('video',image)
cv2.waitKey(1)
end_time = time.time()
time2 = time.time()
print(image.shape[:2], int(frame_count / (time2 - time1)))
frame_count += 1
cli.close()
来源:https://blog.csdn.net/weixin_41735859/article/details/118160856
标签:python,视频传输
0
投稿
猜你喜欢
Mootools 1.2教程(2)——DOM选择器
2008-11-13 12:46:00
用CSS实现柱状图(Bar Graph)的方法(三)——复杂柱状图的实现
2008-05-26 13:36:00
python基于xmlrpc实现二进制文件传输的方法
2022-10-07 09:24:43
PyCharm导入python项目并配置虚拟环境的教程详解
2023-08-18 13:31:41
Python挑选文件夹里宽大于300图片的方法
2023-08-13 11:35:19
python中print()函数的“,”与java中System.out.print()函数中的“+”功能详解
2021-12-18 10:27:02
高效的mysql分页方法及原理
2024-01-20 04:23:46
Python django框架应用中实现获取访问者ip地址示例
2022-01-06 08:08:20
利用Python实现Picgo图床工具
2023-10-09 16:10:45
Vue中使用webpack别名的方法实例详解
2024-05-11 09:13:41
MySQL事务处理与应用简析
2024-01-21 09:01:24
MySQL临时表的简单用法介绍
2024-01-13 10:03:56
15个用户体验设计剖析
2010-05-11 16:43:00
OpenCV图像分割之分水岭算法与图像金字塔算法详解
2022-11-27 07:59:24
Access创建一个简单MIS多媒体管理系统
2008-10-13 12:31:00
总结分析Python的5个硬核函数
2022-04-12 11:05:42
详解Python的Django框架中的templates设置
2021-12-15 15:16:06
PHP实现简易计算器功能
2024-05-11 09:25:27
javascript创建数组的最简代码
2013-09-01 21:43:04
python中安装Scrapy模块依赖包汇总
2023-11-08 07:06:25