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}"
相关推荐
onceco25 分钟前
领域LLM九讲——第5讲 为什么选择OpenManus而不是QwenAgent(附LLM免费api邀请码)
人工智能·python·深度学习·语言模型·自然语言处理·自动化
狐凄1 小时前
Python实例题:基于 Python 的简单聊天机器人
开发语言·python
悦悦子a啊2 小时前
Python之--基本知识
开发语言·前端·python
Piper蛋窝2 小时前
深入 Go 语言垃圾回收:从原理到内建类型 Slice、Map 的陷阱以及为何需要 strings.Builder
后端·go
笑稀了的野生俊4 小时前
在服务器中下载 HuggingFace 模型:终极指南
linux·服务器·python·bash·gpu算力
Naiva4 小时前
【小技巧】Python+PyCharm IDE 配置解释器出错,环境配置不完整或不兼容。(小智AI、MCP、聚合数据、实时新闻查询、NBA赛事查询)
ide·python·pycharm
路来了4 小时前
Python小工具之PDF合并
开发语言·windows·python
蓝婷儿4 小时前
Python 机器学习核心入门与实战进阶 Day 3 - 决策树 & 随机森林模型实战
人工智能·python·机器学习
六毛的毛5 小时前
Springboot开发常见注解一览
java·spring boot·后端
AntBlack5 小时前
拖了五个月 ,不当韭菜体验版算是正式发布了
前端·后端·python