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}"
相关推荐
M ? A20 分钟前
Vue 转 React | VuReact 实时监听开发指南
前端·vue.js·后端·react.js·面试·开源·vureact
贺国亚23 分钟前
Kafka系统设计与编码
后端·kafka
_Evan_Yao29 分钟前
零基础学编程,第一门语言选Python还是C?
c语言·开发语言·python
步步为营DotNet38 分钟前
深入探究.NET 11 中.NET Aspire 在云原生应用持续集成与交付安全加固
python
南方的耳朵44 分钟前
谨慎使用git rebase --onto A B C
后端
Soari1 小时前
深度办公革命:拆解 Claude for Microsoft 365,打造金融级智能办公生态
python·microsoft·金融·flask
何陋轩1 小时前
Spring AI Alibaba实战:通义千问与Java的完美融合
人工智能·后端·ai编程
.唉1 小时前
06. FastAPI框架从入门到实战
python·fastapi·web
Copy_Paste_Coder1 小时前
小程序失败后,换个方向,终于成功搞到收益
前端·javascript·后端
情绪总是阴雨天~1 小时前
Flask Web 开发入门笔记
笔记·flask