Last active
January 17, 2018 17:18
-
-
Save anuragmishra1/f926ce94a6176d37cf6874e39c258ce1 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 time | |
from datetime import timedelta | |
import sys | |
import json | |
import os | |
#We need to get our API credentials in the code for authentication that we have stored as Environment Variables locally. | |
os.environ.get("AWS_ACCESS_KEY_ID") | |
os.environ.get("AWS_SECRET_ACCESS_KEY") | |
AWS_REGION = os.environ.get("AWS_REGION") | |
#Following line is used to save all the console outputs in a text file. | |
sys.stdout = open('rekognition_output.txt','w') | |
start_time = time.monotonic() | |
BUCKET = "kontiki-amazon-rekognition" | |
KEY = "tom.jpg" | |
#Detect Labels | |
def detect_labels(bucket, key, max_labels = 10, min_confidence = 90, region = AWS_REGION): | |
rekognition = boto3.client("rekognition", region) #Initialize amazon_rekognition client function | |
response = rekognition.detect_labels( | |
Image = { | |
"S3Object": { | |
"Bucket": bucket, | |
"Name": key, | |
} | |
}, | |
MaxLabels = max_labels, | |
MinConfidence = min_confidence, | |
) | |
return response['Labels'] | |
print('Detected Labels for:' + KEY) | |
for label in detect_labels(BUCKET, KEY): | |
print("{Name} - {Confidence}%".format(**label)) | |
print('\n') | |
#Detect Faces | |
def detect_faces(bucket, key, attributes = ['ALL'], region = AWS_REGION): | |
rekognition = boto3.client("rekognition", region) #Initialize amazon_rekognition client function | |
response = rekognition.detect_faces( | |
Image = { | |
"S3Object": { | |
"Bucket": bucket, | |
"Name": key, | |
} | |
}, | |
Attributes = attributes, | |
) | |
return response['FaceDetails'] | |
c = detect_faces(BUCKET, KEY) | |
print('Detected Faces for:' + KEY) | |
for face in detect_faces(BUCKET, KEY): | |
print("Face ({Confidence}%)".format(**face)) | |
#Emotions | |
for emotion in face['Emotions']: | |
print("{Type} : {Confidence}%".format(**emotion)) | |
#Quality | |
for quality, value in face['Quality'].items(): | |
print("{quality} : {value}".format(quality = quality, value = value)) | |
#Facial Features | |
for faceDetail in c: | |
print('Here are the other attributes:') | |
print(json.dumps(faceDetail, indent = 4, sort_keys = True)) | |
print('\n') | |
#Compare Faces | |
KEY_SOURCE = "tom.jpg" | |
KEY_TARGET = "tom_cruise.jpg" | |
def compare_faces(bucket, key, bucket_target, key_target, threshold = 80, region = AWS_REGION): | |
rekognition = boto3.client("rekognition", region) #Initialize amazon_rekognition client function | |
response = rekognition.compare_faces( | |
SourceImage = { | |
"S3Object": { | |
"Bucket": bucket, | |
"Name": key, | |
} | |
}, | |
TargetImage = { | |
"S3Object": { | |
"Bucket": bucket_target, | |
"Name": key_target, | |
} | |
}, | |
SimilarityThreshold = threshold, | |
) | |
return response['SourceImageFace'], response['FaceMatches'] | |
source_face, matches = compare_faces(BUCKET, KEY_SOURCE, BUCKET, KEY_TARGET) | |
print("Detected Similarity between", KEY_SOURCE + " and " + KEY_TARGET) | |
#Main Source Face | |
print("Source Face ({Confidence}%)".format(**source_face)) | |
#One match for each Target Face | |
for match in matches: | |
print("Target Face ({Confidence}%)".format(**match['Face'])) | |
print("Similarity : {}%".format(match['Similarity'])) | |
print('\n') | |
#Index Faces | |
KEY = "tom.jpg" | |
IMAGE_ID = KEY # S3 key as ImageId | |
COLLECTION = "mygallery" | |
# Note: you have to create the collection first! | |
def index_faces(bucket, key, collection_id, image_id = None, attributes = (), region = AWS_REGION): | |
rekognition = boto3.client("rekognition", region) #Initialize amazon_rekognition client function | |
#rekognition.create_collection(CollectionId=COLLECTION) #If you're executing this code snippet to index faces in an image for the 'first time', just uncomment this line to create the collection first! | |
response = rekognition.index_faces( | |
Image = { | |
"S3Object": { | |
"Bucket": bucket, | |
"Name": key, | |
} | |
}, | |
CollectionId = collection_id, | |
ExternalImageId = image_id, | |
DetectionAttributes = attributes, | |
) | |
return response['FaceRecords'] | |
print("Detected Faces in:", KEY) | |
count = 0 | |
for record in index_faces(BUCKET, KEY, COLLECTION, IMAGE_ID): | |
count += 1 | |
face = record['Face'] | |
print("Face ({}%)".format(face['Confidence'])) | |
print("FaceId: {}".format(face['FaceId'])) | |
print("ImageId: {}".format(face['ImageId'])) | |
print("Total no. of Faces Detected in ", KEY , ":", count) | |
print('\n') | |
#Search Faces | |
KEY = "tom_cruise.jpg" | |
COLLECTION = "mygallery" | |
def search_faces_by_image(bucket, key, collection_id, threshold = 80, region = AWS_REGION): | |
rekognition = boto3.client("rekognition", region) | |
response = rekognition.search_faces_by_image( | |
Image = { | |
"S3Object": { | |
"Bucket": bucket, | |
"Name": key, | |
} | |
}, | |
CollectionId = collection_id, | |
FaceMatchThreshold = threshold, | |
) | |
return response['FaceMatches'] | |
print("Searched for:", KEY, "in", COLLECTION) | |
for record in search_faces_by_image(BUCKET, KEY, COLLECTION): | |
face = record['Face'] | |
print("Matched Face ({}%)".format(record['Similarity'])) | |
print("FaceId : {}".format(face['FaceId'])) | |
print("ImageId : {}".format(face['ExternalImageId'])) | |
print('\n') | |
end_time = time.monotonic() | |
print("Execution_Time:", timedelta(seconds = end_time - start_time)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment