基于OpenCV + flask 實現(xiàn)的網(wǎng)絡(luò)實時視頻流傳輸 | 附源碼
點擊上方“AI算法與圖像處理”,選擇加"星標"或“置頂”
重磅干貨,第一時間送達


01.如何使用Web瀏覽器查看實時流媒體
02.操作步驟
第1步-安裝Flask和OpenCV:
第2步-導(dǎo)入必要的庫,初始化flask應(yīng)用程序:
#Import necessary librariesfrom flask import Flask, render_template, Responseimport cv2#Initialize the Flask appapp = Flask(__name__)
第3步-使用OpenCV捕獲視頻:
camera = cv2.VideoCapture(0)'''for ip camera use - rtsp://username:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp'for local webcam use cv2.VideoCapture(0)'''

def gen_frames():while True:success, frame = camera.read() # read the camera frameif not success:breakelse:ret, buffer = cv2.imencode('.jpg', frame)frame = buffer.tobytes()yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # concat frame one by one and show result
第5步-為網(wǎng)絡(luò)應(yīng)用的默認頁面定義應(yīng)用路由:
@app.route('/')def index():return render_template('index.html')
第6步-定義視頻供稿的應(yīng)用路由:
@app.route('/video_feed')def video_feed():return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
<body><div class="container"><div class="row"><div class="col-lg-8 offset-lg-2"><h3 class="mt-5">Live Streamingh3><img src="{{ url_for('video_feed') }}" width="100%">div>div>div>body>
第7步-啟動Flask服務(wù)器:
if __name__ == "__main__":app.run(debug=True)

只需在運行“ app.py”后在網(wǎng)絡(luò)瀏覽器中鍵入“ localhost:5000”即可打開您的網(wǎng)絡(luò)應(yīng)用程序 app.py —這是我們在上面創(chuàng)建的Flask應(yīng)用程序 模板-此文件夾包含我們的“ index.html”文件。在渲染模板時,這在Flask中是必需的。所有HTML文件都放在此文件夾下。


下載1:leetcode?開源書
在「AI算法與圖像處理」公眾號后臺回復(fù):leetcode,即可下載。每題都 runtime beats 100% 的開源好書,你值得擁有!
下載2 CVPR2020 在「AI算法與圖像處理」公眾號后臺回復(fù):CVPR2020,即可下載1467篇CVPR?2020論文 個人微信(如果沒有備注不拉群!) 請注明:地區(qū)+學(xué)校/企業(yè)+研究方向+昵稱
覺得不錯就點亮在看吧

評論
圖片
表情

