-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection_manager.py
executable file
·95 lines (71 loc) · 2.92 KB
/
collection_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/python
import boto3
import json
class CollectionManager:
client = boto3.client('rekognition')
def __init__(self, collectionName):
self.collectionId = collectionName
# make call idempotent
existingCollections = listCollections()
if collectionName in existingCollections:
# collection already exists
pass
else:
resp = self.client.create_collection(CollectionId = collectionName)
print "Collection ARN: ", resp['CollectionArn']
def addImageWithFace(self, imageFile):
with open(imageFile, 'rb') as image:
global resp
resp = self.client.index_faces(CollectionId=self.collectionId,
Image={'Bytes' : image.read()})
# return only id of the biggest face
return resp['FaceRecords'][0]['Face']['FaceId']
def listFacesInCollection(self, maxResults = 5):
resp = self.client.list_faces(CollectionId = self.collectionId, MaxResults = maxResults)
result= []
while True:
faces = resp['Faces']
for face in faces:
result.append(face['FaceId'])
if 'NextToken' in resp:
nextToken = resp['NextToken']
resp = self.client.list_faces(CollectionId = self.collectionId,
NextToken = nextToken,
MaxResults = maxResults)
else:
break
return result
def removeFacesFromCollection(self, faceIds):
if len(faceIds) < 1:
print "Nothing to delete"
return
resp = self.client.delete_faces(CollectionId=self.collectionId,
FaceIds=faceIds)
deletedFaces = resp['DeletedFaces']
print "Deleted faces:"
for face in deletedFaces:
print face
remainingFaces = [face for face in faceIds if face not in deletedFaces]
if len(remainingFaces) > 0:
print "Couldn't delete:"
for face in remainingFaces:
print face
def listCollections(maxResults = 5) :
client = boto3.client('rekognition')
resp = client.list_collections(MaxResults = maxResults)
resultList = []
while True:
collections = resp['CollectionIds']
for collection in collections:
resultList.append(collection)
if 'NextToken' in resp:
nextToken = resp['NextToken']
resp = client.list_collections(NextToken = nextToken, MaxResults = maxResults)
else:
break
return resultList
if __name__ == '__main__':
collection = CollectionManager("TestCollection")
#print listCollections(2)
faces = collection.listFacesInCollection()
collection.removeFacesFromCollection(faces)