#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)