Created
September 9, 2018 00:51
-
-
Save wongcyrus/2e85255bf3ac18d0fa5f05e625879524 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import os | |
import boto3 | |
from botocore.exceptions import ClientError | |
from os import environ | |
rekognition=boto3.client('rekognition') | |
collectionId='IveFaceCollection' | |
image_bucket="wecarebillstatic" | |
prefix = "face/" | |
def delete_collection(): | |
print('Attempting to delete collection ' + collectionId) | |
statusCode='' | |
try: | |
response=rekognition.delete_collection(CollectionId=collectionId) | |
statusCode=response['StatusCode'] | |
except ClientError as e: | |
if e.response['Error']['Code'] == 'ResourceNotFoundException': | |
print ('The collection ' + collectionId + ' was not found ') | |
else: | |
print ('Error other than Not Found occurred: ' + e.response['Error']['Message']) | |
statusCode=e.response['ResponseMetadata']['HTTPStatusCode'] | |
print('Operation returned Status Code: ' + str(statusCode)) | |
print('Done...') | |
def create_collection(): | |
maxResults=2 | |
#Create a collection | |
print('Creating collection:' + collectionId) | |
response=rekognition.create_collection(CollectionId=collectionId) | |
print('Collection ARN: ' + response['CollectionArn']) | |
print('Status code: ' + str(response['StatusCode'])) | |
print('Done...') | |
def index_face(): | |
s3 = boto3.client('s3') # again assumes boto.cfg setup, assume AWS S3 | |
for key in s3.list_objects_v2(Bucket=image_bucket, Prefix=prefix)['Contents']: | |
path, filename = os.path.split(key['Key']) | |
filename = filename.replace(" ", "_") | |
filename, ext = os.path.splitext(filename) | |
if filename == "": | |
continue | |
response=rekognition.index_faces(CollectionId=collectionId, | |
Image={'S3Object':{'Bucket':image_bucket,'Name':key['Key']}}, | |
ExternalImageId=filename, | |
DetectionAttributes=['ALL']) | |
print ('Faces in ' + filename) | |
for faceRecord in response['FaceRecords']: | |
print (faceRecord['Face']['FaceId']) | |
if __name__ == "__main__": | |
delete_collection() | |
create_collection() | |
index_face() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment