Repository navigation
Expand file tree
/
Copy pathrecognition.py
More file actions
126 lines (93 loc) · 3.76 KB
/
Copy pathrecognition.py
File metadata and controls
126 lines (93 loc) · 3.76 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import boto3
import io
from PIL import Image
if __name__ == "__main__":
client = boto3.client('rekognition')
response = client.create_stream_processor(
Input={
'KinesisVideoStream': {
'Arn': 'arn:aws:kinesisvideo:us-east-1:201247618887:stream/kvs1/1557896017573'
}
},
Output={
'KinesisDataStream': {
'Arn': 'arn:aws:kinesis:us-east-1:201247618887:stream/kds1'
}
},
Name='wangyucam',
Settings={
'FaceSearch': {
'CollectionId': 'wangyu',
'FaceMatchThreshold': 85.0
}
},
RoleArn='arn:aws:iam::201247618887:role/rekrole'
)
print(response)
bucket = 'rekognition-video-console-demo-iad-wangyu-q317min9lbx66vm1ejvh'
collectionId = 'wangyu'
fileName = 'test.jpeg'
threshold = 70
maxFaces = 2
client = boto3.client('rekognition')
response = client.search_faces_by_image(CollectionId=collectionId,
Image={'S3Object': {'Bucket': bucket, 'Name': fileName}},
FaceMatchThreshold=threshold,
MaxFaces=maxFaces)
faceMatches = response['FaceMatches']
print('Matching faces')
for match in faceMatches:
print('FaceId:' + match['Face']['FaceId'])
print('Similarity: ' + "{:.2f}".format(match['Similarity']) + "%")
print("\n")
# Calculate positions from from estimated rotation
def ShowBoundingBoxPositions(imageHeight, imageWidth, box, rotation):
left = 0
top = 0
if rotation == 'ROTATE_0':
left = imageWidth * box['Left']
top = imageHeight * box['Top']
if rotation == 'ROTATE_90':
left = imageHeight * (1 - (box['Top'] + box['Height']))
top = imageWidth * box['Left']
if rotation == 'ROTATE_180':
left = imageWidth - (imageWidth * (box['Left'] + box['Width']))
top = imageHeight * (1 - (box['Top'] + box['Height']))
if rotation == 'ROTATE_270':
left = imageHeight * box['Top']
top = imageWidth * (1 - box['Left'] - box['Width'])
print('Left: ' + '{0:.0f}'.format(left))
print('Top: ' + '{0:.0f}'.format(top))
print('Face Width: ' + "{0:.0f}".format(imageWidth * box['Width']))
print('Face Height: ' + "{0:.0f}".format(imageHeight * box['Height']))
if __name__ == "__main__":
photo = 'test2.jpeg'
client = boto3.client('rekognition')
# Get image width and height
image = Image.open(open(photo, 'rb'))
width, height = image.size
print('Image information: ')
print(photo)
print('Image Height: ' + str(height))
print('Image Width: ' + str(width))
# call detect faces and show face age and placement
# if found, preserve exif info
stream = io.BytesIO()
if 'exif' in image.info:
exif = image.info['exif']
image.save(stream, format=image.format, exif=exif)
else:
image.save(stream, format=image.format)
image_binary = stream.getvalue()
response = client.detect_faces(Image={'Bytes': image_binary}, Attributes=['ALL'])
print('Detected faces for ' + photo)
for faceDetail in response['FaceDetails']:
print('Face:')
if 'OrientationCorrection' in response:
print('Orientation: ' + response['OrientationCorrection'])
ShowBoundingBoxPositions(height, width, faceDetail['BoundingBox'], response['OrientationCorrection'])
else:
print('No estimated orientation. Check Exif data')
print('The detected face is estimated to be between ' + str(faceDetail['AgeRange']['Low'])
+ ' and ' + str(faceDetail['AgeRange']['High']) + ' years')
print(str(faceDetail))