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}"
相关推荐
架构师老Y2 分钟前
010:API网关调试手记:路由、认证与限流的那些坑
开发语言·前端·python
架构谨制@涛哥4 分钟前
架构谨制:重新定义软件从业者的本质
后端·系统架构·软件构建
老刘说AI5 分钟前
Dify:从入门到精通
人工智能·python·神经网络·低代码·ai作画·开源软件
zhangzeyuaaa10 分钟前
Python 闭包详解
开发语言·python
万粉变现经纪人11 分钟前
如何解决 pip install tensorflow-gpu 报错 未检测到 CUDA 驱动 问题
人工智能·python·深度学习·aigc·tensorflow·bug·pip
ん贤12 分钟前
Go GC 非玄学,而是 CPU 和内存的权衡
开发语言·后端·golang·性能调优·gc
架构师老Y15 分钟前
009、容器编排实战:Kubernetes上的Python服务
python·容器·kubernetes
Freak嵌入式15 分钟前
MicroPython LVGL基础知识和概念:底层渲染与性能优化
人工智能·python·单片机·性能优化·嵌入式·lvgl·micropython
ZhengEnCi6 小时前
M3-markconv库找不到wkhtmltopdf问题
python
码事漫谈8 小时前
当AI开始“思考”:我们是否真的准备好了?
前端·后端