├── .gitmodules
├── README.md
├── templates
└── index.html
├── .gitignore
├── main.py
├── camera.py
└── LICENSE
/.gitmodules:
--------------------------------------------------------------------------------
1 | [submodule "opencv"]
2 | path = opencv
3 | url = https://github.com/opencv/opencv
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Video stream with face recognition on OpenCV
2 |
3 | # 1. Install Python dependencies: cv2, flask. (wish that pip install works like a charm)
4 | # 2. Run "python main.py".
5 | # 3. Navigate the browser to the local webpage.
6 |
--------------------------------------------------------------------------------
/templates/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Video Streaming Demonstration
4 |
5 |
6 | Video Streaming Demonstration
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | env/
12 | build/
13 | develop-eggs/
14 | dist/
15 | downloads/
16 | eggs/
17 | .eggs/
18 | lib/
19 | lib64/
20 | parts/
21 | sdist/
22 | var/
23 | *.egg-info/
24 | .installed.cfg
25 | *.egg
26 |
27 | # PyInstaller
28 | # Usually these files are written by a python script from a template
29 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
30 | *.manifest
31 | *.spec
32 |
33 | # Installer logs
34 | pip-log.txt
35 | pip-delete-this-directory.txt
36 |
37 | # Unit test / coverage reports
38 | htmlcov/
39 | .tox/
40 | .coverage
41 | .coverage.*
42 | .cache
43 | nosetests.xml
44 | coverage.xml
45 | *,cover
46 | .hypothesis/
47 |
48 | # Translations
49 | *.mo
50 | *.pot
51 |
52 | # Django stuff:
53 | *.log
54 |
55 | # Sphinx documentation
56 | docs/_build/
57 |
58 | # PyBuilder
59 | target/
60 |
61 | #Ipython Notebook
62 | .ipynb_checkpoints
63 |
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | #
3 | # Project: Video Streaming with face recognition
4 | # Author: agametov [at] gmail [dot] com>
5 | # Date: 2016/02/11
6 | # Website: http://www.agametov.ru/
7 | # Usage:
8 | # 1. Install Python dependencies: cv2, flask. (wish that pip install works like a charm)
9 | # 2. Run "python main.py".
10 | # 3. Navigate the browser to the local webpage.
11 | from flask import Flask, render_template, Response
12 | from camera import VideoCamera
13 |
14 | app = Flask(__name__)
15 |
16 | @app.route('/')
17 | def index():
18 | return render_template('index.html')
19 |
20 | def gen(camera):
21 | while True:
22 | frame = camera.get_frame()
23 | yield (b'--frame\r\n'
24 | b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
25 |
26 | @app.route('/video_feed')
27 | def video_feed():
28 | return Response(gen(VideoCamera()),
29 | mimetype='multipart/x-mixed-replace; boundary=frame')
30 |
31 | if __name__ == '__main__':
32 | app.run(host='0.0.0.0', debug=False)
33 |
--------------------------------------------------------------------------------
/camera.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | import cv2
3 | face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
4 | class VideoCamera(object):
5 | def __init__(self):
6 | # Using OpenCV to capture from device 0. If you have trouble capturing
7 | # from a webcam, comment the line below out and use a video file
8 | # instead.
9 | self.video = cv2.VideoCapture(0)
10 | # If you decide to use video.mp4, you must have this file in the folder
11 | # as the main.py.
12 | # self.video = cv2.VideoCapture('video.mp4')
13 |
14 | def __del__(self):
15 | self.video.release()
16 |
17 | def get_frame(self):
18 | success, image = self.video.read()
19 | # We are using Motion JPEG, but OpenCV defaults to capture raw images,
20 | # so we must encode it into JPEG in order to correctly display the
21 | # video stream.
22 | faces = face_cascade.detectMultiScale(image, 1.3, 5)
23 | for (x, y, w, h) in faces:
24 | cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
25 | ret, jpeg = cv2.imencode('.jpg', image)
26 | return jpeg.tobytes()
27 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | This is free and unencumbered software released into the public domain.
2 |
3 | Anyone is free to copy, modify, publish, use, compile, sell, or
4 | distribute this software, either in source code form or as a compiled
5 | binary, for any purpose, commercial or non-commercial, and by any
6 | means.
7 |
8 | In jurisdictions that recognize copyright laws, the author or authors
9 | of this software dedicate any and all copyright interest in the
10 | software to the public domain. We make this dedication for the benefit
11 | of the public at large and to the detriment of our heirs and
12 | successors. We intend this dedication to be an overt act of
13 | relinquishment in perpetuity of all present and future rights to this
14 | software under copyright law.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | OTHER DEALINGS IN THE SOFTWARE.
23 |
24 | For more information, please refer to
25 |
--------------------------------------------------------------------------------