1. 需求背景
由于项目需求,需要在一个网页显示9个摄像头过算法的实时画面,项目初期,拟用ffmpeg实现二次推流过算法,后期由于ffmpeg仅能用于命令行命令,而且不易实现音频同步,故而使用ffmpeg进阶版pyav实现,后因pyav太占用服务器CPU性能,升级为将视频流的每一帧转为图片存入redis,前端实时从redis获取,后因redis效果不好,太卡,模糊(卡与模糊的原因是redis与后端不在同一个服务器上,导致两次图片传输走无线网,从而卡顿,都放在一台服务器器就好了,目前正在用django自带的内存取代redis实现),进而考虑通过imagezmq消息队列实现
2. 实现代码
准备工作
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple imagezmq
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple simplejpeg
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple picamera
# 理论上最后一个没用到,这个是官网方法用的
发送端
import socket
import time
import cv2
import imagezmq
import traceback
import simplejpeg
# capture=cv2.VideoCapture(0) # 获取摄像头视频
# capture = cv2.VideoCapture(r"E:\桌面\新建文件夹\test.mp4")
capture = cv2.VideoCapture("rtsp://192.168.110.219:8554/live")
# 192.168.100.104 为发送端主机ip地址
sender = imagezmq.ImageSender(connect_to='tcp://192.168.110.239: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.110.239: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
参考:https://blog.csdn.net/hanshuobest/article/details/105131157
imageZMQ官网