zoukankan      html  css  js  c++  java
  • flask读取摄像头并实时显示

    camera.py

    import cv2
    
    class VideoCamera(object):
        def __init__(self):
            # Using OpenCV to capture from device 0. If you have trouble capturing
            # from a webcam, comment the line below out and use a video file
            # instead.
            self.video = cv2.VideoCapture(0)
            # If you decide to use video.mp4, you must have this file in the folder
            # as the main.py.
            # self.video = cv2.VideoCapture('video.mp4')
        
        def __del__(self):
            self.video.release()
        
        def get_frame(self):
            success, image = self.video.read()
            # We are using Motion JPEG, but OpenCV defaults to capture raw images,
            # so we must encode it into JPEG in order to correctly display the
            # video stream.
            ret, jpeg = cv2.imencode('.jpg', image)
            return jpeg.tobytes()

    main.py

    import os
    
    from flask import Flask, render_template, Response, make_response
    from camera import VideoCamera
    
    
    app = Flask(__name__)
    
    #相机推流
    def gen(camera):
        while True:
            frame = camera.get_frame()
            yield (b'--frame
    '
                   b'Content-Type: image/jpeg
    
    ' + frame + b'
    
    ')
    #相机喂流
    @app.route('/video_feed')
    def video_feed():
        return Response(gen(VideoCamera()),
                        mimetype='multipart/x-mixed-replace; boundary=frame')
    
    #当前实时相机画面
    @app.route('/cur_camera')
    def cur_camera():
        return render_template('cur_camer.html')
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', debug=False)

    cur_camer.html

    <html>
      <head>
        <title>指纹监控</title>
      </head>
      <body>
        <h1>监控视频</h1>
        <img id="bg" src="{{ url_for('video_feed') }}">
      </body>
    </html>
  • 相关阅读:
    计算闰年
    三个数比较大小
    剪刀石头布编辑
    二进制转换,八进制,十六进制转换
    原来我学的还是不够。。。
    认知是一切的基础
    spark学习笔记-java调用spark简单demo
    spark学习笔记-RDD
    Sublime Text3时间戳查看转换插件开发
    Spring Boot + Freemarker多语言国际化的实现
  • 原文地址:https://www.cnblogs.com/Archger/p/12774633.html
Copyright © 2011-2022 走看看