Setting up and calling AWS Face Rekognition API through Raspberry PI
- Introduction
- Set up AWS on Raspberry PI
- Camera Set Up on Raspberry PI
- Upload Images to Amazon S3 Bucket
- Upload Faces to Collection
- Identify Faces from Collection, Determine Emotions and Display on Web browser
- Identifying Emotions
- Upload images to S3 bucket and create collection
- Finding faces in the Database
- Opening Web Browser using Python
- Putting it together
Following repo will provide a guide on how to set up your Amazon AWS Rekognition, set up camera on Raspberry PI, identify emotions of the people in the picture and classify each face from known database.
Installation is straighforward and pretty easy when you follow Installation Guide. Then, set up configuration details
Here are some of the applications you have to install before proceeding further: python3
, python2
, boto3
, boto
, opencv
, PIL
-
Hardware
Installing and attaching camera on Raspberry PI is straightforward, which can be found here
-
Software
Python code
camera.py
will start camera on Raspberry PI through web browser. This python code already includes HTML code and some CSS codesNote You should have PIL (Python Image Library), boto3, picamera packages installed
In order to run camera.py type in command prompt
python3 camera.py
Then, open preferred web browser and type
http://0.0.0.0:8000/index.html
Once you snap the picture on your web browser, it will create and save image currIm.jpg
-
Create bucket on AWS S3. Then open
upload_s3.py
file in text editor -
Go to your Console on AWS and find AWS Access and Secret keys. Then, add Access and Secret keys to
upload_s3.py
fileAWS_ACCESS_KEY = Your Access Key AWS_ACCESS_SECRET_KEY = Your Secret Key
-
From file
upload_s3.py
you can call functionupload_main(fileName, bucket)
by providing name of the file you want uploaded
Note I found concept of collection
a bit confusing at first. Essentially, every bucket
can contain a collection
. Moving forward, you will create bucket in S3 and upload your files into it. Bucket will contain physical copies of your files. Then, using those files in S3 bucket, you will create collection
items, which can be characterized as virtually grouped items in S3
Open file collection_faces.py
and do the following:
- Replace variable value
rootdir
to a directory where all the faces are located. In this case, all the people you want to have in your dataset as known people - Replace
bucket
andcollectionId
with bucket and collection where you want to store your dataset of known people. - Helper functions you can (un)comment:
list_collection()
will list all the collections in your Amazon AWSlist_items_collection(collectionId, bucket)
takes 2 arguments (collection name and bucket name), and will return list of items in the collection & bucket specified.
- Function
get_bucket_names(bucket)
takes 1 argument (bucket name) and returns a list of all the items in the bucket. This function will be used in the next step. - Function
add_faces_collection(collectionId, photo_l, bucket)
will take items fromphoto_l
list and put them in collection and bucket you specified. Result will be a bucket & collection which will contain your known people dataset
Note The following code has been used from Amazon AWS Rekognition Documentation
Note: make sure to install opencv pip3 install opencv-python
When you run the following code in your terminal python test_collection.py
, the code will do the following:
- It will wait until
currIm.jpg
file appears in the folder - Then it will run function
recognition_main()
which identifies all the new faces, and compares each new face with faces in your collection- In other words, suppose you created collection
myCollection
which contains some # of faces. Suppose filecurrIm.jpg
already exists. Afterrecognition_main()
function is called all the faces identified in thecurrIm.jpg
will be added tomyCollection
and compared with all the faces in the collection. - In Amazon AWS (as far as I know) in order to recognize and identify many faces in the image, you have to upload all the images to
the same collection. Example: Suppose your collection
myCollection
contains 100 people. Also, suppose new picture you uploaded is a group picture that has 5 faces, and you want to see which of these 5 people are inmyCollection
. In order to do that, you have to upload all the faces the same collection
- In other words, suppose you created collection
- Once all the faces are identified, it will run
emotion_main()
function, which is located indemo.py
python file.- In the
demo.py
file, replacebucket
variable insideemotion_main()
function - The result will be, new file
currIm_emotion.jpg
with all the emotions identified for each face in thecurrIm.jpg
image.
- In the
- Once both
emotion_main()
andrecognition_main()
executed, the result will be projected on web browser of your choosing automatically.
Note The following code has been used from Amazon AWS Rekognition Documentation
- Open terminal and type
python3 camera.py
- Open web browser and type
http://0.0.0.0:8000/index.html
- Open new terminal window, or new tab and type
python test_collection.py
- You can also run both
camera.py
andtest_collection.py
in the same terminal window. In that case, type
$ python camera.py & $ python test_collection.py
- You can also run both
- Result webpage with 3 images: original image (currIm.jpg), identified faces image, and emotion + identified faces