├── README.md ├── app.py ├── requirements.txt └── templates └── index.html /README.md: -------------------------------------------------------------------------------- 1 | ## Live Streaming with Flask and Open-CV 2 | ```python 3 | pip install -r requirements.txt 4 | ``` 5 | ### Run Server 6 | ```python 7 | python app.py 8 | ``` 9 | #### Use Built-in Webcam of Laptop 10 | ##### Put Zero (O) in cv2.VideoCapture(0) 11 | ```python 12 | cv2.VideoCapture(0) 13 | ``` 14 | #### Use Ip Camera/CCTV/RTSP Link 15 | ```python 16 | cv2.VideoCapture('rtsp://username:password@camera_ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp') 17 | ``` 18 | #### Example RTSP Link 19 | ```python 20 | cv2.VideoCapture('rtsp://mamun:123456@101.134.16.117:554/user=mamun_password=123456_channel=0_stream=0.sdp') 21 | ``` 22 | #### Change Channel Number to Change the Camera 23 | ```python 24 | cv2.VideoCapture('rtsp://mamun:123456@101.134.16.117:554/user=mamun_password=123456_channel=1_stream=0.sdp') 25 | ``` 26 | #### Display the resulting frame in browser 27 | ```python 28 | cv2.imencode('.jpg', frame)[1].tobytes() 29 | ``` 30 | ## Or this one 31 | 32 | ```python 33 | net , buffer = cv2.imencode('.jpg', frame) 34 | buffer.tobytes() 35 | ``` 36 | 37 | ## Credit 38 | Learn More about Streaming with flask 39 | - https://blog.miguelgrinberg.com/post/video-streaming-with-flask 40 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, Response 2 | import cv2 3 | 4 | app = Flask(__name__) 5 | 6 | camera = cv2.VideoCapture(0) # 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 | 9 | def gen_frames(): # generate frame by frame from camera 10 | while True: 11 | # Capture frame-by-frame 12 | success, frame = camera.read() # read the camera frame 13 | if not success: 14 | break 15 | else: 16 | ret, buffer = cv2.imencode('.jpg', frame) 17 | frame = buffer.tobytes() 18 | yield (b'--frame\r\n' 19 | b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # concat frame one by one and show result 20 | 21 | 22 | @app.route('/video_feed') 23 | def video_feed(): 24 | """Video streaming route. Put this in the src attribute of an img tag.""" 25 | return Response(gen_frames(), 26 | 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(host='0.0.0.0') 37 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask==2.3.2 2 | opencv-python==4.2.0.32 3 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 |