├── README.md └── main.py /README.md: -------------------------------------------------------------------------------- 1 | Facial Recognition using AWS Rekognition 2 | ======================================== 3 | 4 | Perform facial recognition over your images using the AWS facial rekognition API. 5 | 6 | ## Setup 7 | Follow instructions here: 8 | > - [Getting Started with AWS Rekognition](https://aws.amazon.com/rekognition/getting-started/) 9 | > - [Stackoverflow](http://stackoverflow.com/questions/41388926/an-example-of-calling-aws-rekognition-http-api-from-python) 10 | > - [Read the docs](https://boto3.readthedocs.io/en/latest/reference/services/rekognition.html#Rekognition.Client.list_collections) -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import pprint 2 | import json 3 | import boto3 4 | import os 5 | import shutil 6 | import logging 7 | 8 | SIMILARITY_THRESHOLD = 75.0 9 | COLLECTION_ID = "test" # Use a constant for collection ID 10 | 11 | # Set up logging 12 | logging.basicConfig(level=logging.INFO) 13 | 14 | def read_image_bytes(image_path): 15 | """Read image bytes from a file.""" 16 | with open(image_path, 'rb') as image_file: 17 | return image_file.read() 18 | 19 | def compare_images(client, source, target): 20 | """Compare two images using AWS Rekognition.""" 21 | source_bytes = read_image_bytes(source) 22 | target_bytes = read_image_bytes(target) 23 | 24 | response = client.compare_faces( 25 | SourceImage={ 'Bytes': source_bytes }, 26 | TargetImage={ 'Bytes': target_bytes }, 27 | SimilarityThreshold=SIMILARITY_THRESHOLD 28 | ) 29 | return response 30 | 31 | 32 | def create_collection(client, collection_id): 33 | response = client.create_collection(CollectionId = collection_id) 34 | return response 35 | 36 | 37 | def add_face_to_collection(client, source, collection_id, external_image_id): 38 | with open(source, 'rb') as source_image: 39 | source_bytes = source_image.read() 40 | 41 | response = client.index_faces( 42 | CollectionId = collection_id, 43 | Image = {'Bytes': source_bytes}, 44 | ExternalImageId = external_image_id 45 | ) 46 | return response 47 | 48 | 49 | def print_response(response): 50 | for face in response["FaceMatches"]: 51 | print("Image") 52 | print(face["Face"]["FaceId"]) 53 | print(face["Face"]["ExternalImageId"]) 54 | print("=====") 55 | 56 | if __name__ == '__main__': 57 | 58 | #credentials entered through awscli using `aws configure` 59 | client = boto3.client('rekognition') 60 | 61 | # COMPARING 2 IMAGES 62 | response = compare_images(client, "source.jpg","target.jpg") 63 | pprint.pprint(response) 64 | 65 | # CREATE A NEW COLLECTION 66 | response = create_collection(client, COLLECTION_ID) 67 | pprint.pprint(response) 68 | 69 | # ADD A FACE TO A COLLECTION 70 | source = "source.jpg" 71 | external_image_id = "source.jpg" 72 | response = add_face_to_collection(client, source, COLLECTION_ID, external_image_id) 73 | pprint.pprint(response) 74 | 75 | #SEARCH IN COLLECTION USING IMAGE 76 | source = "test_image.jpg" 77 | with open(source, 'rb') as source_image: 78 | source_bytes = source_image.read() 79 | response = client.search_faces_by_image( 80 | CollectionId = "test", 81 | Image = {'Bytes' : source_bytes} 82 | ) 83 | pprint.pprint(response) 84 | 85 | #SEARCH USING FACE ID 86 | response = client.search_faces( 87 | CollectionId='test', 88 | FaceId='ef75efa7-724b-5e07-859e-7c0b0c476b9b', #face id 89 | ) 90 | pprint.pprint(response) 91 | 92 | #PRINT FOUND FACES 93 | print_response(response) 94 | 95 | #ADD FOLDER TO COLLECTION 96 | foldername = "test" 97 | for filename in os.listdir(foldername): 98 | source = os.path.join(foldername, filename) # Use os.path.join 99 | external_image_id = f"{foldername}_{filename}" 100 | response = add_face_to_collection(client, source, COLLECTION_ID, external_image_id) 101 | logging.info(f"{filename} : Added") # Use logging instead of print 102 | 103 | 104 | #iterate over files in Test 105 | for filename in os.listdir("test"): 106 | 107 | #check if file is jpg 108 | if(filename.lower().endswith(".jpg")): 109 | 110 | #create folder with name as filename 111 | foldername = filename[:-4] 112 | os.makedirs("Result/"+foldername) 113 | 114 | #get source file in bytes 115 | source = "test/"+filename 116 | with open(source, 'rb') as source_image: 117 | source_bytes = source_image.read() 118 | 119 | #call api 120 | response = client.search_faces_by_image( 121 | CollectionId = "test", 122 | Image = {'Bytes' : source_bytes} 123 | ) 124 | 125 | #for all matches move photo to folder 126 | for face in response["FaceMatches"]: 127 | print(face["Face"]["ExternalImageId"]) 128 | file = face["Face"]["ExternalImageId"][len("test_"):] 129 | shutil.copyfile("test/"+file,"Result/"+foldername+"/"+file) 130 | 131 | --------------------------------------------------------------------------------