opencv通過webcam可以獲取本地實時視頻流,但是如果需要將視頻流共享給其他機器調用,就可以將利用flask框架構建一個實時視頻流服務器,然后其他機器可以通過向這個服務器發送請求來獲取這台機器上的實時視頻流。[這篇文章](https://blog.miguelgrinberg.com/post/video-streaming-with-flask)包含非常詳細的理論介紹和具體實現,力薦!
首先需要說明的是這里flask提供視頻流是通過generator函數進行的,不了解的可以去查下文檔這里就不具體講了。flask通過將一連串獨立的jpeg圖片輸出來實現視頻流,這種方法叫做motion JPEG,好處是延遲很低,但是成像質量一般,因為jpeg壓縮圖片的質量對motion stream不太夠用。
multipart 模式
想要將后一次請求得到的圖片覆蓋到前一次從而達到動畫的效果就需要使用在response的時候使用multipart模式。Multipart response由以下幾部分組成:包含multipart content類型的header,分界符號分隔的各個part,每個part都具有特定的content類型。multipart視頻流的結構如下:
HTTP/1.1 200 OK
Content-Type: multipart/x-mixed-replace; boundary=frame
--frame
Content-Type: image/jpeg
<jpeg data here>
--frame
Content-Type: image/jpeg
<jpeg data here>
...
flask server
具體實現代碼:main.py
from flask import Flask, render_template, Response
import opencv
class VideoCamera(object):
def __init__(self):
# 通過opencv獲取實時視頻流
self.video = cv2.VideoCapture(0)
def __del__(self):
self.video.release()
def get_frame(self):
success, image = self.video.read()
# 因為opencv讀取的圖片並非jpeg格式,因此要用motion JPEG模式需要先將圖片轉碼成jpg格式圖片
ret, jpeg = cv2.imencode('.jpg', image)
return jpeg.tobytes()
app = Flask(__name__)
@app.route('/') # 主頁
def index():
# jinja2模板,具體格式保存在index.html文件中
return render_template('index.html')
def gen(camera):
while True:
frame = camera.get_frame()
# 使用generator函數輸出視頻流, 每次請求輸出的content類型是image/jpeg
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()),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, port=5000)
index.html
<html>
<head>
<title>Video Streaming Demonstration</title>
</head>
<body>
<h1>Video Streaming Demonstration</h1>
<img src="{{ url_for('video_feed') }}">
</body>
</html>
注:圖片地址由大括號內的字典給出,指向app的第二個地址video_feed,在multipart模式下瀏覽器會將每次請求得到的地址對大括號進行更新。
局限性
如果視頻流一直存在的話,這個app能輸出視頻流的的客戶端的數量和web worker的數量相同,在debug模式下,這個數量是1,也就是說只有一個瀏覽器上能夠看到視頻流輸出。
如果要克服這種局限的話,使用基於協同網絡服務的框架比如gevent,可以用一個worker線程服務多個客戶端。
參考
https://blog.miguelgrinberg.com/post/video-streaming-with-flask
https://github.com/mattmakai/video-service-flask
http://www.chioka.in/python-live-video-streaming-example/