-
-
Save Jasperabez/3655402640ae8b99946213d525228c59 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 io | |
from PIL import Image | |
rekognition = boto3.client('rekognition') | |
def readAsBin(img_path,file_type): | |
image = Image.open(img_path) | |
stream = io.BytesIO() | |
image.save(stream,format=file_type) | |
image_binary = stream.getvalue() | |
return image_binary | |
def check_face(col_id,img_path,file_type): | |
image_binary = readAsBin(img_path,file_type) | |
response = rekognition.search_faces_by_image( | |
CollectionId=col_id, | |
Image={'Bytes':image_binary} | |
) | |
print(response) | |
print('-'*100) | |
if len(response['FaceMatches']) != 1: | |
person_name = "unknown" | |
else: | |
person_name = response['FaceMatches'][0]['Face']['ExternalImageId'] | |
return person_name | |
def add2col(col_id,img_path,file_type,img_id): | |
collectionId= col_id | |
image_binary = readAsBin(img_path,file_type) | |
#print(rekognition.describe_collection(CollectionId=collectionId)) | |
rekognition.index_faces(Image={'Bytes':image_binary},ExternalImageId=img_id,CollectionId=collectionId) | |
#print(rekognition.describe_collection(CollectionId=collectionId)) | |
try: | |
#Creates collection in rekognition, change CollectionID as desired | |
rekognition.create_collection(CollectionId='test-collection') | |
except rekognition.exceptions.ResourceAlreadyExistsException: | |
#replaces Example.jpeg and PersonNAME as desired | |
add2col('test-collection','Example.jpeg','JPEG','PersonNAME') | |
print(check_face("test-collection","testExample.jpeg","JPEG")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment