flask实时播放cv2读取的视频

flask实时播放cv2读取的视频

app.py

class VideoCamera(object):
    def __init__(self, url):
        self.cap = cv2.VideoCapture(url)

    def __del__(self):
        self.cap.release()

    def get_frame(self):
        success, image = self.cap.read()
        ret, jpeg = cv2.imencode('.jpg', image)
        return jpeg.tobytes()


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


def gen(camera):
    while True:
        frame = camera.get_frame()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')


@app.route('/video_feed')
def video_feed():
    return Response(gen(VideoCamera("http://127.0.0.1:5000/static/video.mp4")), mimetype='multipart/x-mixed-replace; boundary=frame')

cv2_online.html

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

本文作者:bitterteaer

本文链接:https://www.cnblogs.com/bitterteaer/p/16635620.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   bitterteaer  阅读(98)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起