python如何将视频流实时传输到手机?(有问题,一次只能被一台访问)
生活随笔
收集整理的這篇文章主要介紹了
python如何将视频流实时传输到手机?(有问题,一次只能被一台访问)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 蔣工優化后
基于python實現高速視頻傳輸程序
好像沒啥用
基于OpenCV的網絡實時視頻流傳輸
樹莓派:基于flask的遠程視頻監控
flask 放置圖片 然后瀏覽器訪問圖片的方法
AJAX - 服務器響應
# -*- coding: utf-8 -*- """ @File : app.py @Time : 2020/11/20 11:53 @Author : Dontla_stream @Email : sxana@qq.com @Software: PyCharm """ # Import necessary libraries from flask import Flask, render_template, Response import cv2 import pyrealsense2 as rs import numpy as np# Initialize the Flask app app = Flask(__name__)camera = cv2.VideoCapture('mda-kgtgu5v2zakst4s8.mp4')def gen_frames():# ctx = rs.context()# pipeline = rs.pipeline(ctx)# cfg = rs.config()# cfg.enable_device('011422071925')# cfg.enable_stream(rs.stream.depth, 1280, 720, rs.format.z16, 30)# cfg.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)# pipeline.start(cfg)while True:# frames = pipeline.wait_for_frames()# color_frame = frames.get_color_frame()# color_image = np.asanyarray(color_frame.get_data())# if not color_frame:success, frame = camera.read() # read the camera frameif not success:breakelse:# cv2.imshow('win', color_image)# cv2.waitKey(1)# ret, buffer = cv2.imencode('.jpg', color_image)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# 為網絡應用的默認頁面定義應用路由? @app.route('/') def index():return render_template('index.html')# 定義視頻供稿的應用路由? @app.route('/video_feed') def video_feed():return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')if __name__ == "__main__":app.run(host='0.0.0.0', port=80, debug=True)# app.run(debug=True)host需要’0.0.0.0’,其他不行,端口可以改成其他的,沒被占用就可
<body> <div class="container"><div class="row"><div class="col-lg-8 offset-lg-2"><h3 class="mt-5">Live Streaming</h3><!-- <img src="{{ url_for('video_feed') }}" width="100%">--><video src="/static/mda-kgtgu5v2zakst4s8.mp4" controls="controls"></video></div></div> </div> </body>本機瀏覽器打開http://127.0.0.1/(本機地址?訪問查看)
使用localhost也可以
局域網其他電腦打開服務端主機ip查看192.168.1.161查看
結果:
(以太網連接的本機)
(無線網筆記本)
蔣工優化后
app.py
index.html
<!DOCTYPE html> <html><head><meta charset="utf-8"><title>實時視頻</title><script>function myFunction() {var xhr = new XMLHttpRequest();xhr.open('GET','/image/1',true);xhr.onload = function (e) {if (this.status == 200) {var blob = this.response;console.log(blob);var img = document.getElementById("img");// img.onload = function (e) {// window.URL.revokeObjectURL(img.src);// };// img.src = window.URL.createObjectURL(blob);// img.attr("src", "data:" + "image/jpg" + ";base64," + blob)img.src = "data:" + "image/jpg" + ";base64," + blob;// document.body.appendChild(img);}}xhr.send();// var img = document.getElementById("img");// img.src = '/image/' + parseInt(Math.random()*500000);// console.log(img.src);}var intervalId = window.setInterval(myFunction, 30);function xxx() {alert("xxx")}</script> </head><body><image id='img' src='/image/1'></image> </body></html> 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的python如何将视频流实时传输到手机?(有问题,一次只能被一台访问)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 报错:‘yield‘ ou
- 下一篇: 为什么一次ajax调用会发送两次请求?