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}"
相关推荐
nightunderblackcat2 分钟前
新手向:C语言、Java、Python 的选择与未来指南
java·c语言·python
笃行3506 分钟前
KingbaseES读写分离集群架构解析
后端
夏日麋鹿~40 分钟前
逐时nc数据批量处理为日平均
python
程序员三明治1 小时前
Python编辑器的安装及配置(Pycharm、Jupyter的安装)从0带你配置,小土堆视频
python·pycharm·编辑器
理想国的女研究僧1 小时前
Jupyter Notebook操作指南(1)
ide·python·学习·jupyter
酷飞飞1 小时前
PyQt 界面布局与交互组件使用指南
python·qt·交互·pyqt
GilgameshJSS1 小时前
【学习K230-例程19】GT6700-TCP-Client
网络·python·网络协议·学习·tcp/ip
yuanpan1 小时前
python标准库有哪些模块,简单总结下。
开发语言·python
IT_陈寒1 小时前
Python 3.12 新特性实战:10个性能优化技巧让你的代码快如闪电⚡
前端·人工智能·后端
程序员杰哥1 小时前
什么是Jmeter? Jmeter工作原理是什么?
自动化测试·软件测试·python·测试工具·jmeter·职场和发展·测试用例