0032-利用flask调用摄像头

index.html

<!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
 
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
          integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
 
    <title>Live Streaming Demonstration</title>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-lg-8  offset-lg-2">
            <h3 class="mt-5">视频显示</h3>
            <img src="{
   
   { url_for('video_feed') }}" width="100%">
        </div>
    </div>
</div>
</body>
</html>

app.py

#coding:utf-8
from flask import Flask,render_template,Response
import cv2

app = Flask(__name__)
camera = cv2.VideoCapture(0) #use 0 for web camera

# Use Ip Camera/CCTV/RTSP Link
# cv2.VideoCapture('rtsp://username:password@camera_ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp')
### Example RTSP Link
# cv2.VideoCapture('rtsp://mamun:[email protected]:554/user=mamun_password=123456_channel=0_stream=0.sdp') ```

def gen_frames(): #generate frame by frame from camera
    while True:
        #capture frame-by-frame
        success,frame = camera.read()
        print('==frame.shape',frame.shape)
        if not success:
             break
        else:
            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('/video_feed')
def video_feed():
    # Video streaming route. Put this in the src attribute of an img tag
    return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')


@app.route('/video', methods=["get"])
def index():
    """Video streaming home page."""
    return render_template('index.html')


if __name__ == '__main__':
    # app.run(debug=True, port=6006)
    app.run(host="0.0.0.0", port=6006)

猜你喜欢

转载自blog.csdn.net/zhonglongshen/article/details/113173556