在烧瓶中动态显示图像

时间:2019-09-22 04:00:15

标签: python opencv flask jinja2

我必须流式传输视频,并且在该流式视频下方,必须显示基于同一HTML文件中的脚本动态更改的图像。

  

stream.py

    #!/usr/bin/env python
    from flask import Flask, render_template, Response
    import cv2
    import sys
    import numpy

    app = Flask(__name__)

    @app.route('/')
    def index():
        return render_template('index.html')

    def get_frame():
        camera_port=0
        camera=cv2.VideoCapture(camera_port) #this makes a web cam object

        while True:
            retval, im = camera.read()
            imgencode=cv2.imencode('.jpg',im)[1]
            stringData=imgencode.tostring()
            yield (b'--frame\r\n'
                b'Content-Type: text/plain\r\n\r\n'+stringData+b'\r\n')

        del(camera)

    @app.route('/calc')
    def calc():
         return Response(get_frame(),mimetype='multipart/x-mixed-replace;       boundary=frame')


    if __name__ == '__main__':
        app.run(host='localhost', debug=True, threaded=True)
  

index.html   这是我的渲染模板。其中动态采用calc的路径

    <html>
       <head>
        <title>Video Streaming Demonstration</title>
       </head>
       <body>
        <h1>Video Streaming Demonstration</h1>
        <img src="{{ url_for('calc') }}">
      </body>
    </html>

现在,除了视频显示之外,我还想显示在一定时间范围内以相同HTML格式更改的图像。那怎么办?

0 个答案:

没有答案