├── static
├── local.js
├── video.html
├── local.html
├── objDetect.js
└── objDetectOnMotion.js
├── object_detection_api_test.py
├── LICENSE
├── setup.py
├── server.py
├── README.md
├── object_detection_api.py
└── object_detection_tutorial.py
/static/local.js:
--------------------------------------------------------------------------------
1 | //Get camera video
2 | const constraints = {
3 | audio: false,
4 | video: {
5 | width: {min: 640, ideal: 1280, max: 1920},
6 | height: {min: 480, ideal: 720, max: 1080}
7 | }
8 | };
9 |
10 | navigator.mediaDevices.getUserMedia(constraints)
11 | .then(stream => {
12 | document.getElementById("myVideo").srcObject = stream;
13 | console.log("Got local user video");
14 |
15 | })
16 | .catch(err => {
17 | console.log('navigator.getUserMedia error: ', err)
18 | });
19 |
--------------------------------------------------------------------------------
/object_detection_api_test.py:
--------------------------------------------------------------------------------
1 | import object_detection_api
2 | import os
3 | from PIL import Image
4 |
5 | # If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.
6 | PATH_TO_TEST_IMAGES_DIR = 'object_detection/test_images' #cwh
7 | TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 3) ]
8 |
9 | for image_path in TEST_IMAGE_PATHS:
10 | image = Image.open(image_path)
11 | response = object_detection_api.get_objects(image)
12 | print("returned JSON: \n%s" % response)
--------------------------------------------------------------------------------
/static/video.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Tensor Flow Object Detection from a video
6 |
7 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/static/local.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Tensor Flow Object Detection from getUserMedia
6 |
7 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 webrtcHacks
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/setup.py:
--------------------------------------------------------------------------------
1 | '''Setup script for object_detection with webrtc'''
2 |
3 | from setuptools import find_packages
4 | from setuptools import setup
5 |
6 |
7 | REQUIRED_PACKAGES = ['Pillow>=1.0', 'Flask', 'tensorflow', 'six', 'matplotlib']
8 |
9 | setup(
10 | name='webrtc_object_detection',
11 | version='0.1',
12 | install_requires=REQUIRED_PACKAGES,
13 | include_package_data=True,
14 | packages=[p for p in find_packages()],
15 | description='Tensorflow Object Detection with WebRTC',
16 | )
17 |
18 | '''Download the Object Dectection directory'''
19 | import six.moves.urllib as urllib
20 | from zipfile import ZipFile
21 | import os
22 | import re
23 | import shutil
24 |
25 | print("\n\nDownloading the TensorFlow API from Github...")
26 |
27 | REPOSITORY_ZIP_URL = 'https://github.com/tensorflow/models/archive/master.zip'
28 |
29 | try:
30 | filename, headers = urllib.request.urlretrieve(REPOSITORY_ZIP_URL)
31 | #filename = 'models-master.zip'
32 |
33 | target_path = os.path.join(os.getcwd(), 'object_detection/')
34 | temp_path = filename + "_dir"
35 |
36 | with ZipFile(filename, 'r') as zip_file:
37 | files = zip_file.namelist()
38 | files_to_extract = [f for f in files if f.startswith(('models-master/research/object_detection/'))]
39 | zip_file.extractall(temp_path, files_to_extract)
40 | print("Copying TensorFlow Object API files to %s" % target_path)
41 | shutil.move(os.path.join(temp_path, 'models-master/research/object_detection/'), target_path)
42 | os.removedirs(os.path.join(temp_path, 'models-master/research/'))
43 |
44 | except:
45 | print("Problem downloading the TensorFlow Object API. \n"
46 | "Try running `git clone https://github.com/tensorflow/models.git`.\n"
47 | "Then `cp /research/object_detection to /object_detection` instead")
48 |
49 |
50 | '''Compile Protobufs'''
51 | import subprocess
52 | print("Compiling protobufs")
53 | try:
54 | subprocess.Popen('protoc object_detection/protos/*.proto --python_out=.', shell=True)
55 |
56 | except:
57 | print("Error compiling Protobufs")
58 |
--------------------------------------------------------------------------------
/server.py:
--------------------------------------------------------------------------------
1 | import object_detection_api
2 | import os
3 | from PIL import Image
4 | from flask import Flask, request, Response
5 |
6 | app = Flask(__name__)
7 |
8 | # for CORS
9 | @app.after_request
10 | def after_request(response):
11 | response.headers.add('Access-Control-Allow-Origin', '*')
12 | response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
13 | response.headers.add('Access-Control-Allow-Methods', 'GET,POST') # Put any other methods you need here
14 | return response
15 |
16 |
17 | @app.route('/')
18 | def index():
19 | return Response('Tensor Flow object detection')
20 |
21 |
22 | @app.route('/local')
23 | def local():
24 | return Response(open('./static/local.html').read(), mimetype="text/html")
25 |
26 |
27 | @app.route('/video')
28 | def remote():
29 | return Response(open('./static/video.html').read(), mimetype="text/html")
30 |
31 |
32 | @app.route('/test')
33 | def test():
34 | PATH_TO_TEST_IMAGES_DIR = 'object_detection/test_images' # cwh
35 | TEST_IMAGE_PATHS = [os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 3)]
36 |
37 | image = Image.open(TEST_IMAGE_PATHS[0])
38 | objects = object_detection_api.get_objects(image)
39 |
40 | return objects
41 |
42 |
43 | @app.route('/image', methods=['POST'])
44 | def image():
45 | try:
46 | image_file = request.files['image'] # get the image
47 |
48 | # Set an image confidence threshold value to limit returned data
49 | threshold = request.form.get('threshold')
50 | if threshold is None:
51 | threshold = 0.5
52 | else:
53 | threshold = float(threshold)
54 |
55 | # finally run the image through tensor flow object detection`
56 | image_object = Image.open(image_file)
57 | objects = object_detection_api.get_objects(image_object, threshold)
58 | return objects
59 |
60 | except Exception as e:
61 | print('POST /image error: %e' % e)
62 | return e
63 |
64 |
65 | if __name__ == '__main__':
66 | # without SSL
67 | app.run(debug=True, host='0.0.0.0')
68 |
69 | # with SSL
70 | #app.run(debug=True, host='0.0.0.0', ssl_context=('ssl/server.crt', 'ssl/server.key'))
71 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Tensorflow Object Detection API Web Service
2 |
3 | This is an example of how to turn the [TensorFlow Object API](https://github.com/tensorflow/models/tree/master/research/object_detection) into a web service.
4 | A Python Flask web server is used to interact with a JavaScript a client library.
5 | The example shows how you can extract frames from WebRTC's getUserMedia, upload them to the API, and then use the canvas to display them.
6 | This allows use of the TensorFlow Object API on any HTML `