├── templates
└── index.html
├── README.md
├── camera.py
└── main.py
/templates/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Video Streaming Demonstration
4 |
5 |
6 | Video Streaming Demonstration
7 |
8 |
9 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Video Streaming with Flask Example
2 |
3 | ### Website
4 | http://www.chioka.in
5 |
6 | ### Description
7 | Modified to support streaming out with webcams, and not just raw JPEGs.
8 |
9 | ### Credits
10 | Most of the code credits to Miguel Grinberg, except that I made a small tweak. Thanks!
11 | http://blog.miguelgrinberg.com/post/video-streaming-with-flask
12 |
13 | ### Usage
14 | 1. Install Python dependencies: cv2, flask. (wish that pip install works like a charm)
15 | 2. Run "python main.py".
16 | 3. Navigate the browser to the local webpage.
--------------------------------------------------------------------------------
/camera.py:
--------------------------------------------------------------------------------
1 | import cv2
2 |
3 | class VideoCamera(object):
4 | def __init__(self):
5 | # Using OpenCV to capture from device 0. If you have trouble capturing
6 | # from a webcam, comment the line below out and use a video file
7 | # instead.
8 | self.video = cv2.VideoCapture(0)
9 | # If you decide to use video.mp4, you must have this file in the folder
10 | # as the main.py.
11 | # self.video = cv2.VideoCapture('video.mp4')
12 |
13 | def __del__(self):
14 | self.video.release()
15 |
16 | def get_frame(self):
17 | success, image = self.video.read()
18 | # We are using Motion JPEG, but OpenCV defaults to capture raw images,
19 | # so we must encode it into JPEG in order to correctly display the
20 | # video stream.
21 | ret, jpeg = cv2.imencode('.jpg', image)
22 | return jpeg.tobytes()
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | #
3 | # Project: Video Streaming with Flask
4 | # Author: Log0
5 | # Date: 2014/12/21
6 | # Website: http://www.chioka.in/
7 | # Description:
8 | # Modified to support streaming out with webcams, and not just raw JPEGs.
9 | # Most of the code credits to Miguel Grinberg, except that I made a small tweak. Thanks!
10 | # Credits: http://blog.miguelgrinberg.com/post/video-streaming-with-flask
11 | #
12 | # Usage:
13 | # 1. Install Python dependencies: cv2, flask. (wish that pip install works like a charm)
14 | # 2. Run "python main.py".
15 | # 3. Navigate the browser to the local webpage.
16 | from flask import Flask, render_template, Response
17 | from camera import VideoCamera
18 |
19 | app = Flask(__name__)
20 |
21 | @app.route('/')
22 | def index():
23 | return render_template('index.html')
24 |
25 | def gen(camera):
26 | while True:
27 | frame = camera.get_frame()
28 | yield (b'--frame\r\n'
29 | b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
30 |
31 | @app.route('/video_feed')
32 | def video_feed():
33 | return Response(gen(VideoCamera()),
34 | mimetype='multipart/x-mixed-replace; boundary=frame')
35 |
36 | if __name__ == '__main__':
37 | app.run(host='0.0.0.0', debug=True)
--------------------------------------------------------------------------------