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}"
相关推荐
chao_7891 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找
烛阴1 小时前
Python装饰器解除:如何让被装饰的函数重获自由?
前端·python
aramae2 小时前
C++ -- STL -- vector
开发语言·c++·笔记·后端·visual studio
noravinsc2 小时前
django 一个表中包括id和parentid,如何通过parentid找到全部父爷id
python·django·sqlite
lifallen2 小时前
Paimon 原子提交实现
java·大数据·数据结构·数据库·后端·算法
ajassi20002 小时前
开源 python 应用 开发(三)python语法介绍
linux·python·开源·自动化
沉默媛2 小时前
如何安装python以及jupyter notebook
开发语言·python·jupyter
舒一笑3 小时前
PandaCoder重大产品更新-引入Jenkinsfile文件支持
后端·程序员·intellij idea
PetterHillWater3 小时前
AI编程之CodeBuddy的小试
后端·aigc
codervibe4 小时前
如何用 Spring Security 构建无状态权限控制系统(含角色菜单控制)
java·后端