版本:
python: Python 3.9.0
pip: pip 20.2.3 from /usr/local/python3/lib/python3.9/site-packages/pip (python 3.9)
安装依赖
pip3 install --upgrade pip
pip3 install opencv-python
pip3 install sanic
yum install mesa-libGL.x86_64 -y
截图实现
video_stream_util.py
注 : 参数1为视频流地址, 参数2为截图保存路径(挂载即可)
import cv2
import sys
def save_image(rtsp_url,file_path):
cap = cv2.VideoCapture(rtsp_url)
# 每隔125帧保存一张图片、1S 为 25帧
frame_interval = 125
# 统计当前帧
frame_count = 0
# 保存图片个数
count = 1
while True:
rval, frame = cap.read()
if not rval:
cap.release()
cv2.destroyAllWindows()
if frame_count % frame_interval == 0:
cv2.imencode(".jpg", frame)[1].tofile(file_path)
count += 1
print("保存图片:{}".format(file_path))
frame_count += 1
if count == 2:
cap.release() # 释放摄像头
cv2.destroyAllWindows() # 释放所有显示图像窗口
break
if __name__ == '__main__':
rtsp_url = sys.argv[1]
file_path = sys.argv[2]
save_image(rtsp_url,file_path)
简单HTTP服务
http_main.py
# -*- coding: UTF-8 -*-
# !/usr/bin/python
# @time :2023/12/14 10:05
# @author :
# @function :get/post of sanic
from sanic.response import json, text
from sanic import Sanic, request
import video_stream_util
app = Sanic("calculate")
app.config.HEALTH = True
@app.route("/pyutil/save_image", methods=["GET", "POST"])
async def calculate_add(request):
""" 分类 """
if request.method == "POST":
params = request.form if request.form else request.json
elif request.method == "GET":
params = request.args
else:
params = {}
video_stream_url = params.get("video_stream_url", 0)
save_path = params.get("save_path", 0)
video_stream_util.save_image(video_stream_url,save_path)
status_code = 200
res_dict = {"code": status_code,
"data": "",
"message": "success"
}
return json(res_dict, status=status_code, ensure_ascii=False)
if __name__ == "__main__":
app.run(single_process=True,
access_log=True,
host="0.0.0.0",
port=8001,
workers=1,
)
容器化部署
基于centos7.8创建基础容器
Dockerfile
FROM centos:7.8.2003
WORKDIR /home
COPY file ./
CMD ["sh","/home/startup.sh"]
startup.sh
while true
do
date
sleep 10s
done
构建
docker build -t centos_dev:0.1 .
启动容器
docker run -d --name=centos_dev0.1 centos_dev:0.1
进入容器, 安装python3和pip3,然后安装依赖, 再修改[/home/startup.sh]内容如下
python3 http_main.py
然后保存为镜像即可
docker commit centos_dev0.1 pyutil:0.1
需要用的时候直接启动镜像和挂载目录即可