├── .gitignore ├── README.md ├── app.py ├── camera.py ├── requirements.txt └── templates └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | # Editor directories and files 2 | .idea 3 | *.pyc 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Camera streaming(Open-CV + Flask) and display on different threads with safe synchronization 2 | ```python 3 | pip install requirements.txt 4 | 5 | ``` 6 | ### Run Server 7 | ```python 8 | python app.py 9 | 10 | ``` 11 | #### Use Built-in Webcam of Laptop 12 | ##### Put Zero (O) in cv2.VideoCapture(0) 13 | ```python 14 | cv2.VideoCapture(0) 15 | 16 | ``` 17 | #### Use Ip Camera/CCTV/RTSP Link 18 | ```python 19 | cv2.VideoCapture('rtsp://username:password@camera_ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp') 20 | 21 | ``` 22 | #### Example RTSP Link 23 | ```python 24 | cv2.VideoCapture('rtsp://mamun:123456@101.134.16.117:554/user=mamun_password=123456_channel=0_stream=0.sdp') 25 | 26 | ``` 27 | #### Change Channel Number to Change the Camera 28 | ```python 29 | cv2.VideoCapture('rtsp://mamun:123456@101.134.16.117:554/user=mamun_password=123456_channel=1_stream=0.sdp') 30 | 31 | ``` 32 | #### Display the resulting frame in browser 33 | ```python 34 | cv2.imencode('.jpg', frame)[1].tobytes() 35 | 36 | ``` 37 | #### Display the resulting frame in window 38 | ##### Write this end of the line [camera.py](/camera.py) 39 | ```python 40 | if __name__ == "__main__" : 41 | cap = CameraStream().start() 42 | while True : 43 | frame = cap.read() 44 | cv2.imshow('webcam', frame) 45 | if cv2.waitKey(1) == 27 : 46 | break 47 | cap.stop() 48 | cv2.destroyAllWindows() 49 | ``` 50 | #### Credit 51 | - Multi Thread Use From [allskyee's](https://github.com/allskyee) [Open-CV Multi-Thread Gist](https://gist.github.com/allskyee/7749b9318e914ca45eb0a1000a81bf56) 52 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, Response 2 | from camera import CameraStream 3 | import cv2 4 | app = Flask(__name__) 5 | 6 | cap = CameraStream().start() 7 | 8 | 9 | @app.route('/') 10 | def index(): 11 | """Video streaming home page.""" 12 | return render_template('index.html') 13 | 14 | 15 | def gen_frame(): 16 | """Video streaming generator function.""" 17 | while cap: 18 | frame = cap.read() 19 | convert = cv2.imencode('.jpg', frame)[1].tobytes() 20 | yield (b'--frame\r\n' 21 | b'Content-Type: image/jpeg\r\n\r\n' + convert + b'\r\n') # concate frame one by one and show result 22 | 23 | 24 | @app.route('/video_feed') 25 | def video_feed(): 26 | """Video streaming route. Put this in the src attribute of an img tag.""" 27 | return Response(gen_frame(), 28 | mimetype='multipart/x-mixed-replace; boundary=frame') 29 | 30 | 31 | if __name__ == '__main__': 32 | app.run(host='0.0.0.0', threaded=True) -------------------------------------------------------------------------------- /camera.py: -------------------------------------------------------------------------------- 1 | from threading import Thread, Lock 2 | import cv2 3 | 4 | 5 | class CameraStream(object): 6 | def __init__(self, src=0): 7 | self.stream = cv2.VideoCapture(src) 8 | 9 | (self.grabbed, self.frame) = self.stream.read() 10 | self.started = False 11 | self.read_lock = Lock() 12 | 13 | def start(self): 14 | if self.started: 15 | print("already started!!") 16 | return None 17 | self.started = True 18 | self.thread = Thread(target=self.update, args=()) 19 | self.thread.start() 20 | return self 21 | 22 | def update(self): 23 | while self.started: 24 | (grabbed, frame) = self.stream.read() 25 | self.read_lock.acquire() 26 | self.grabbed, self.frame = grabbed, frame 27 | self.read_lock.release() 28 | 29 | def read(self): 30 | self.read_lock.acquire() 31 | frame = self.frame.copy() 32 | self.read_lock.release() 33 | return frame 34 | 35 | def stop(self): 36 | self.started = False 37 | self.thread.join() 38 | 39 | def __exit__(self, exc_type, exc_value, traceback): 40 | self.stream.release() 41 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask==1.0.2 2 | opencv-python==4.2.0.32 3 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Video Streaming Demonstration 4 | 5 | 6 |

Video Streaming Demonstration

7 | 8 | 9 | --------------------------------------------------------------------------------