-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
94 lines (72 loc) · 2.43 KB
/
test.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
import os
import boto3
from cv2 import *
REGION = 'us-east-1'
# s3 bucket name
BUCKET = "images-bucket-mn"
# image filename
KEY = "image.jpg"
FEATURES_BLACKLIST = ("Landmarks", "Emotions", "Pose", "Quality", "BoundingBox", "Confidence")
def capture_image():
# OpenCV (cv2)
cam = VideoCapture(0)
s, img = cam.read()
if s:
# show the pic on screen, wait for user to hit 0 key to continue
# namedWindow('cam', WINDOW_AUTOSIZE)
# imshow('cam', img)
# waitKey(0)
# destroyWindow('cam')
imwrite(KEY, img)
def detect_labels(bucket, key, max_labels = 5, min_confidence = 95):
rekognition = boto3.client('rekognition', REGION)
response = rekognition.detect_labels(
Image = {
'S3Object': {
'Bucket': bucket,
'Name': key,
}
},
MaxLabels = max_labels,
MinConfidence = min_confidence,
)
for label in response['Labels']:
print ("{Name} - {Confidence}".format(**label))
def detect_faces(bucket, key, attributes = ['ALL']):
rekognition = boto3.client('rekognition', REGION)
response = rekognition.detect_faces(
Image = {
'S3Object': {
'Bucket': bucket,
'Name': key,
}
},
Attributes = attributes,
)
for face in response['FaceDetails']:
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 feature, data in face.items():
if feature not in FEATURES_BLACKLIST:
print (" {feature} : {data}".format(feature=feature, data=data))
def main():
opt = int(input("Type 0 for face detection, 1 for label detection, or -1 to exit: "))
while opt != -1:
capture_image()
s3 = boto3.client('s3')
# upload to s3
with open(KEY, "rb") as f:
s3.upload_fileobj(f, BUCKET, KEY)
if opt == 0:
detect_faces(BUCKET, KEY)
elif opt == 1:
detect_labels(BUCKET, KEY)
opt = int(input("Type 0 for face detection, 1 for label detection, or -1 to exit: "))
if __name__ == "__main__":
main()