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}"
相关推荐
考虑考虑6 分钟前
Springboot3.5.x结构化日志新属性
spring boot·后端·spring
涡能增压发动积7 分钟前
一起来学 Langgraph [第三节]
后端
sky_ph20 分钟前
JAVA-GC浅析(二)G1(Garbage First)回收器
java·后端
涡能增压发动积26 分钟前
一起来学 Langgraph [第二节]
后端
行云流水剑30 分钟前
【学习记录】如何使用 Python 提取 PDF 文件中的内容
python·学习·pdf
hello早上好1 小时前
Spring不同类型的ApplicationContext的创建方式
java·后端·架构
roman_日积跬步-终至千里1 小时前
【Go语言基础【20】】Go的包与工程
开发语言·后端·golang
心扬1 小时前
python生成器
开发语言·python
mouseliu1 小时前
python之二:docker部署项目
前端·python
狂小虎2 小时前
亲测解决self.transform is not exist
python·深度学习