【发布时间】:2018-05-01 15:38:59
【问题描述】:
我将 Raspberry Pi 3B 流式视频传输到本地网络。
我的问题是:我想从任何地方访问流,而现在我只能在连接到同一个网络的情况下通过浏览器观看。
这是我正在使用的代码。
class Streamer:
app = Flask(__name__, template_folder='../resources/templates')
stream = None
@staticmethod
@app.route('/')
def index():
return render_template('streaming.html')
@staticmethod
def gen():
while True:
frame = Camara.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@staticmethod
@app.route('/video_feed')
def video_feed():
return Response(Streamer.gen(),
mimetype='multipart/x-mixed-replace; boundary=frame')
@staticmethod
def start():
Camara.set_stream()
Streamer.stream = Process(target=Streamer.app.run, args=('0.0.0.0', 5000))
Streamer.stream.start()
@staticmethod
def shutdown():
if Streamer.stream:
Streamer.stream.terminate()
Streamer.stream.join()
Streamer.stream = None
return True
else:
return False
相机类
class Camara:
cap = None
out = None
@staticmethod
def set_stream():
try:
if Camara.cap:
logging.debug("Camera busy")
Camara.cap.release()
Camara.cap = cv2.VideoCapture(0)
except Exception as exception:
logging.error("Error setting up camera: " + exception)
@staticmethod
def get_frame():
ret1, frame = Camara.cap.read()
ret2, jpeg = cv2.imencode('.jpg', frame)
return jpeg.tobytes()
html 模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>YoloDoor Stream</title>
</head>
<body>
<img id="bg" src="{{ url_for('video_feed') }}">
</body>
</html>
【问题讨论】:
-
您尝试从本地网络外部查看流的主机名/IP 是什么?听起来您可能需要公开您的 Pi 的 IP
标签: python flask video-streaming raspberry-pi3 cv2