Skip to content

Commit 695db41

Browse files
committed
Initial Commit
0 parents  commit 695db41

File tree

1,192 files changed

+1141406
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,192 files changed

+1141406
-0
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# AWS-Rekognition
2+
3+
AWS Rekognition Chalice
4+
5+
## Supported File Extensions
6+
7+
* Image: JPEG, JPG, PNG
8+
* Video: MOV, MPEG-4, FLV (Based on H264)
9+
10+
## Supported Function
11+
12+
* Labels(identify thousands of objects)
13+
* Face detection and analysis(emotion, gender, smiling etc)

app.py

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import json
2+
import os
3+
4+
import boto3
5+
from chalice import Chalice
6+
from chalice import NotFoundError
7+
from chalicelib import db
8+
from chalicelib import rekognition
9+
10+
app = Chalice(app_name='media-query')
11+
12+
_MEDIA_DB = None
13+
_REKOGNITION_CLIENT = None
14+
_SUPPORTED_IMAGE_EXTENSIONS = (
15+
'.jpg',
16+
'.jpeg',
17+
'.png',
18+
)
19+
_SUPPORTED_VIDEO_EXTENSIONS = (
20+
'.mp4',
21+
'.flv',
22+
'.mov',
23+
)
24+
25+
26+
def get_media_db():
27+
global _MEDIA_DB
28+
if _MEDIA_DB is None:
29+
_MEDIA_DB = db.DynamoMediaDB(
30+
boto3.resource('dynamodb').Table(
31+
os.environ['MEDIA_TABLE_NAME']))
32+
return _MEDIA_DB
33+
34+
35+
def get_rekognition_client():
36+
global _REKOGNITION_CLIENT
37+
if _REKOGNITION_CLIENT is None:
38+
_REKOGNITION_CLIENT = rekognition.RekognitonClient(
39+
boto3.client('rekognition'))
40+
return _REKOGNITION_CLIENT
41+
42+
43+
# Start of Event Handlers.
44+
@app.on_s3_event(bucket=os.environ['MEDIA_BUCKET_NAME'],
45+
events=['s3:ObjectCreated:*'])
46+
def handle_object_created(event):
47+
if _is_image(event.key):
48+
_handle_created_image(bucket=event.bucket, key=event.key)
49+
elif _is_video(event.key):
50+
_handle_created_video(bucket=event.bucket, key=event.key)
51+
52+
53+
@app.on_s3_event(bucket=os.environ['MEDIA_BUCKET_NAME'],
54+
events=['s3:ObjectRemoved:*'])
55+
def handle_object_removed(event):
56+
if _is_image(event.key) or _is_video(event.key):
57+
get_media_db().delete_media_file(event.key)
58+
59+
60+
@app.on_sns_message(topic=os.environ['VIDEO_TOPIC_NAME'])
61+
def add_video_file(event):
62+
message = json.loads(event.message)
63+
labels = get_rekognition_client().get_video_job_labels(message['JobId'])
64+
get_media_db().add_media_file(
65+
name=message['Video']['S3ObjectName'],
66+
media_type=db.VIDEO_TYPE,
67+
labels=labels)
68+
69+
70+
@app.on_sns_message(topic=os.environ['VIDEO_TOPIC_FACE_NAME'])
71+
def add_video_file_face(event):
72+
message = json.loads(event.message)
73+
emotions = get_rekognition_client().get_video_job_faces(message['JobId'])
74+
get_media_db().add_media_file_face(
75+
name=message['Video']['S3ObjectName'],
76+
media_type=db.VIDEO_TYPE,
77+
emotions=emotions)
78+
79+
80+
@app.route('/',cors=True)
81+
def list_media_files():
82+
params = {}
83+
if app.current_request.query_params:
84+
params = _extract_db_list_params(app.current_request.query_params)
85+
return get_media_db().list_media_files(**params)
86+
87+
88+
@app.route('/{name}',cors=True)
89+
def get_media_file(name):
90+
item = get_media_db().get_media_file(name)
91+
if item is None:
92+
raise NotFoundError('Media file (%s) not found' % name)
93+
return item
94+
# End of Event Handlers.
95+
96+
97+
def _extract_db_list_params(query_params):
98+
valid_query_params = [
99+
'startswith',
100+
'media-type',
101+
'label'
102+
]
103+
return {
104+
k.replace('-', '_'): v
105+
for k, v in query_params.items() if k in valid_query_params
106+
}
107+
108+
109+
def _is_image(key):
110+
return key.endswith(_SUPPORTED_IMAGE_EXTENSIONS)
111+
112+
113+
def _handle_created_image(bucket, key):
114+
labels = get_rekognition_client().get_image_labels(bucket=bucket, key=key)
115+
emotions = get_rekognition_client().get_image_emotions(bucket=bucket, key=key)
116+
117+
if 'public/' in key:
118+
index = key.find('public/')
119+
key = key[index+len('public/'):]
120+
121+
122+
get_media_db().add_media_file(key, media_type=db.IMAGE_TYPE, labels=labels)
123+
get_media_db().add_media_file_face(key, media_type=db.IMAGE_TYPE, emotions=emotions)
124+
125+
126+
def _is_video(key):
127+
return key.endswith(_SUPPORTED_VIDEO_EXTENSIONS)
128+
129+
130+
def _handle_created_video(bucket, key):
131+
get_rekognition_client().start_video_label_job(
132+
bucket=bucket, key=key, topic_arn=os.environ['VIDEO_TOPIC_ARN'],
133+
role_arn=os.environ['VIDEO_ROLE_ARN']
134+
)
135+
get_rekognition_client().start_video_face_job(
136+
bucket=bucket, key=key,
137+
topic_arn=os.environ['VIDEO_TOPIC_FACE_ARN'],
138+
role_arn=os.environ['VIDEO_ROLE_ARN']
139+
)
+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
===============================
2+
Boto3 - The AWS SDK for Python
3+
===============================
4+
5+
|Build Status| |Version| |Gitter|
6+
7+
Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for
8+
Python, which allows Python developers to write software that makes use
9+
of services like Amazon S3 and Amazon EC2. You can find the latest, most
10+
up to date, documentation at our `doc site`_, including a list of
11+
services that are supported.
12+
13+
14+
.. _boto: https://docs.pythonboto.org/
15+
.. _`doc site`: https://boto3.amazonaws.com/v1/documentation/api/latest/index.html
16+
.. |Build Status| image:: http://img.shields.io/travis/boto/boto3/develop.svg?style=flat
17+
:target: https://travis-ci.org/boto/boto3
18+
:alt: Build Status
19+
.. |Gitter| image:: https://badges.gitter.im/boto/boto3.svg
20+
:target: https://gitter.im/boto/boto3
21+
:alt: Gitter
22+
.. |Downloads| image:: http://img.shields.io/pypi/dm/boto3.svg?style=flat
23+
:target: https://pypi.python.org/pypi/boto3/
24+
:alt: Downloads
25+
.. |Version| image:: http://img.shields.io/pypi/v/boto3.svg?style=flat
26+
:target: https://pypi.python.org/pypi/boto3/
27+
:alt: Version
28+
.. |License| image:: http://img.shields.io/pypi/l/boto3.svg?style=flat
29+
:target: https://github.com/boto/boto3/blob/develop/LICENSE
30+
:alt: License
31+
32+
Getting Started
33+
---------------
34+
Assuming that you have Python and ``virtualenv`` installed, set up your environment and install the required dependencies like this or you can install the library using ``pip``:
35+
36+
.. code-block:: sh
37+
38+
$ git clone https://github.com/boto/boto3.git
39+
$ cd boto3
40+
$ virtualenv venv
41+
...
42+
$ . venv/bin/activate
43+
$ python -m pip install -r requirements.txt
44+
$ python -m pip install -e .
45+
46+
.. code-block:: sh
47+
48+
$ python -m pip install boto3
49+
50+
51+
Using Boto3
52+
~~~~~~~~~~~~~~
53+
After installing boto3
54+
55+
Next, set up credentials (in e.g. ``~/.aws/credentials``):
56+
57+
.. code-block:: ini
58+
59+
[default]
60+
aws_access_key_id = YOUR_KEY
61+
aws_secret_access_key = YOUR_SECRET
62+
63+
Then, set up a default region (in e.g. ``~/.aws/config``):
64+
65+
.. code-block:: ini
66+
67+
[default]
68+
region=us-east-1
69+
70+
Other credentials configuration method can be found `here <https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html>`__
71+
72+
Then, from a Python interpreter:
73+
74+
.. code-block:: python
75+
76+
>>> import boto3
77+
>>> s3 = boto3.resource('s3')
78+
>>> for bucket in s3.buckets.all():
79+
print(bucket.name)
80+
81+
Running Tests
82+
~~~~~~~~~~~~~
83+
You can run tests in all supported Python versions using ``tox``. By default,
84+
it will run all of the unit and functional tests, but you can also specify your own
85+
``nosetests`` options. Note that this requires that you have all supported
86+
versions of Python installed, otherwise you must pass ``-e`` or run the
87+
``nosetests`` command directly:
88+
89+
.. code-block:: sh
90+
91+
$ tox
92+
$ tox -- unit/test_session.py
93+
$ tox -e py26,py33 -- integration/
94+
95+
You can also run individual tests with your default Python version:
96+
97+
.. code-block:: sh
98+
99+
$ nosetests tests/unit
100+
101+
102+
Getting Help
103+
------------
104+
105+
We use GitHub issues for tracking bugs and feature requests and have limited
106+
bandwidth to address them. Please use these community resources for getting
107+
help:
108+
109+
* Ask a question on `Stack Overflow <https://stackoverflow.com/>`__ and tag it with `boto3 <https://stackoverflow.com/questions/tagged/boto3>`__
110+
* Come join the AWS Python community chat on `gitter <https://gitter.im/boto/boto3>`__
111+
* Open a support ticket with `AWS Support <https://console.aws.amazon.com/support/home#/>`__
112+
* If it turns out that you may have found a bug, please `open an issue <https://github.com/boto/boto3/issues/new>`__
113+
114+
115+
Contributing
116+
------------
117+
118+
We value feedback and contributions from our community. Whether it's a bug report, new feature, correction, or additional documentation, we welcome your issues and pull requests. Please read through this `CONTRIBUTING <https://github.com/boto/boto3/blob/develop/CONTRIBUTING.rst>`__ document before submitting any issues or pull requests to ensure we have all the necessary information to effectively respond to your contribution.
119+
120+
121+
More Resources
122+
--------------
123+
124+
* `NOTICE <https://github.com/boto/boto3/blob/develop/NOTICE>`__
125+
* `Changelog <https://github.com/boto/boto3/blob/develop/CHANGELOG.rst>`__
126+
* `License <https://github.com/boto/boto3/blob/develop/LICENSE.txt>`__
127+
128+
129+

0 commit comments

Comments
 (0)