from flask import Flask, render_template, Response
import cv2 as cv
import numpy
app = Flask(__name__)
camera0 = cv.VideoCapture(0)
camera1 = cv.VideoCapture(1)
@app.route("/")
def index():
return render_template("index.html")
def gen_frames0():
while 1:
ret,frame= camera0.read()
if not ret:break
ret1,buffer = cv.imencode('.jpg',frame)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
def gen_frames1():
while 1:
ret,frame= camera1.read()
if not ret:break
ret1,buffer = cv.imencode('.jpg',frame)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/video_feed0')
def video_feed0():
return Response(gen_frames0(),mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/video_feed1')
def video_feed1():
return Response(gen_frames1(),mimetype = 'multipart/x-mixed-replace; boundary=frame')
if __name__=='__main__':
app.run()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>camera</title>
<style>
.con{
float:left;
height:600px;width:600px;
margin-right:10px;
}
</style>
</head>
<body>
<h3>接受传输来的视频</h3>
<div class="con">
<img src="{{url_for('video_feed0')}}" alt="" width="100%">
</div>
<div class="con">
<img src="{{url_for('video_feed1')}}" alt="" width="100%">
</div>
</body>
</html>