├── README.md ├── app.py └── templates └── index.html /README.md: -------------------------------------------------------------------------------- 1 | # Live-Streaming-using-OpenCV-Flask 2 | A Flask Web-App to stream live from local webcam or CCTV (rtsp link) 3 | 4 | ## Use Built-in Webcam of Laptop 5 | 6 | ### Put Zero (O) in cv2.VideoCapture(0) 7 | 8 | ``` cv2.VideoCapture(0) ``` 9 | 10 | ### Use Ip Camera/CCTV/RTSP Link 11 | ``` cv2.VideoCapture('rtsp://username:password@camera_ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp') ``` 12 | 13 | ### Example RTSP Link 14 | ``` cv2.VideoCapture('rtsp://mamun:123456@101.134.16.117:554/user=mamun_password=123456_channel=0_stream=0.sdp') ``` 15 | 16 | ### Change Channel Number to Change the Camera 17 | ``` cv2.VideoCapture('rtsp://mamun:123456@101.134.16.117:554/user=mamun_password=123456_channel=1_stream=0.sdp') ``` 18 | 19 | ### Display the resulting frame in browser 20 | ``` cv2.imencode('.jpg', frame)[1].tobytes() ``` 21 | 22 | ### Or this one 23 | ``` 24 | net , buffer = cv2.imencode('.jpg', frame) 25 | buffer.tobytes() 26 | ``` 27 | 28 | ### [Reference](https://blog.miguelgrinberg.com/post/video-streaming-with-flask) 29 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, Response 2 | import cv2 3 | 4 | app = Flask(__name__) 5 | 6 | camera = cv2.VideoCapture('rtsp://freja.hiof.no:1935/rtplive/_definst_/hessdalen03.stream') # use 0 for web camera 7 | # for cctv camera use rtsp://username:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp' instead of camera 8 | # for local webcam use cv2.VideoCapture(0) 9 | 10 | def gen_frames(): # generate frame by frame from camera 11 | while True: 12 | # Capture frame-by-frame 13 | success, frame = camera.read() # read the camera frame 14 | if not success: 15 | break 16 | else: 17 | ret, buffer = cv2.imencode('.jpg', frame) 18 | frame = buffer.tobytes() 19 | yield (b'--frame\r\n' 20 | b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # concat frame one by one and show result 21 | 22 | 23 | @app.route('/video_feed') 24 | def video_feed(): 25 | #Video streaming route. Put this in the src attribute of an img tag 26 | return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame') 27 | 28 | 29 | @app.route('/') 30 | def index(): 31 | """Video streaming home page.""" 32 | return render_template('index.html') 33 | 34 | 35 | if __name__ == '__main__': 36 | app.run(debug=True) -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | Live Streaming Demonstration 13 | 14 | 15 |
16 |
17 |
18 |

Live Streaming

19 | 20 |
21 |
22 |
23 | 24 | --------------------------------------------------------------------------------