flask直播流 截图

python 复制代码
class VideoCapture:
    def __init__(self, name):
        self.cap = cv2.VideoCapture(name)
        self.q = queue.Queue()
        t = threading.Thread(target=self._reader)
        t.daemon = True
        t.start()
    # 帧可用时立即读取帧,只保留最新的帧
    def _reader(self):
        while True:
            ret, frame = self.cap.read()
            if not ret:
                break
            if not self.q.empty():
                try:
                    self.q.get_nowait()  # 删除上一个(未处理的)帧
                except queue.Empty:
                    pass
            self.q.put(frame)
    def read(self):
        return self.q.get()
python 复制代码
@app.route('/ZL00132/front',methods=["GET"])
def index():

    # vehicle = request.args.get('vehicle')
    # direction = request.args.get('direction')

    # 直播流URL
    # stream_url = 'http://127.0.0.1:8096/z/live/'+vehicle+'/'+direction+'.flv'
    # 尝试打开直播流

    # 检查直播流是否成功打开
    # if not cap.isOpened():
    #     print("Error: Could not open video stream.")
    #     exit()
    # frame_count = 0  # 用于给截图命名的计数器
    stimestamp = time.time()
    while True:
            # 读取一帧
            frame = cap.read()
            now = datetime.datetime.now()
            date_str = now.strftime("%Y-%m-%d-%H:%M:%S")
            filename = f'/home/zhanyuan/static/{date_str}.jpg'
            cv2.imwrite(filename, frame)
            date_str1 = now.strftime("%Y-%m-%d-%H-%M-%S")
            etimestamp = time.time()
            print(f"文件 :{filename} 时间:{date_str1} 时间差: {etimestamp - stimestamp}")
            if etimestamp - stimestamp > 0.01:
                break
    url = f"http://127.0.0.1:10800/static/{date_str}.jpg"
    etimestamp1 = time.time()
    print(f"时间差: {etimestamp1 - stimestamp}")
    return f"{url}"
相关推荐
离开以后5 分钟前
java封装BigDecimal进行高精度计算
后端
SimonSkywalke5 分钟前
鸟哥的Linux私房菜快速阅读笔记(一) 指令使用、常见指令、寻求帮助
后端·面试
刘晓飞12 分钟前
import type 和 import 区别
后端
野犬寒鸦20 分钟前
SAP后端实习开发面试:操作系统与网络核心考点及Linux与Redis
java·服务器·网络·后端·面试
ServBay22 分钟前
代码减半,10分钟彻底告别 Java 开发旧习
java·后端
Soofjan23 分钟前
Go Map SwissTable Rehash 扩容与再哈希(源码笔记 6)
后端
future021025 分钟前
Spring 核心原理学习路线(完结汇总):7 篇文章串起 IOC、AOP、事务与 Boot
后端·学习·spring
阿杆29 分钟前
五分钟配好向日葵 MCP,让 AI 替你远程安装 OpenClaw!
后端·aigc·mcp
梁正雄30 分钟前
Python前端-2-css基础
前端·python·html
MoRanzhi12031 小时前
Pillow 图像颜色模式与颜色空间转换
图像处理·python·数学建模·pillow·颜色空间转换·颜色模式·图像通道