-
Notifications
You must be signed in to change notification settings - Fork 15
/
cameras.py
199 lines (161 loc) · 4.65 KB
/
cameras.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import urllib
from subprocess import call, Popen, PIPE
import shutil
import threading
from PIL import Image
import json
import boto
from boto.s3.connection import S3Connection
from boto.s3.key import Key
camera_list = [
'charlie',
'wonkus',
'wallace',
'willy',
'wilma',
'dennis',
'willa'
]
image_files =[]
def describe_face_image(image_filename):
cmd = Popen(["bash", "detect-faces.sh", image_filename], stdout=PIPE)
output = ""
for line in cmd.stdout:
output += line
data = json.loads(output)
if len(data["FaceDetails"]) == 0:
return []
else:
gender = data["FaceDetails"][0]["Gender"]["Value"] # Male or Female
emotion = data["FaceDetails"][0]["Emotions"][0]["Type"] # CALM, etc.
return [gender, emotion]
def identify_face_image(image_filename):
cmd = Popen(["bash", "match-faces.sh", image_filename], stdout=PIPE)
output = ""
for line in cmd.stdout:
output += line
# Typical Return Value
#{
# "SearchedFaceBoundingBox": {
# "Width": 0.2554086446762085,
# "Top": 0.1308760643005371,
# "Left": 0.3861177861690521,
# "Height": 0.4540598392486572
# },
# "SearchedFaceConfidence": 99.9906005859375,
# "FaceMatches": [
# {
# "Face": {
# "BoundingBox": {
# "Width": 0.4717549979686737,
# "Top": 0.16526399552822113,
# "Left": 0.234375,
# "Height": 0.4717549979686737
# },
# "FaceId": "b1e93a17-05c7-5b54-b41a-552b5c50c146",
# "ExternalImageId": "lukas.png",
# "Confidence": 99.99960327148438,
# "ImageId": "b440ba40-6664-5448-86c6-52a905843b42"
# },
# "Similarity": 86.0721206665039
# }
# ]
#}
try:
data = json.loads(output)
except ValueError as e:
return None
if len(data["FaceMatches"]) == 0:
return None
else:
return data["FaceMatches"][0]["Face"]["ExternalImageId"].split(".")[0]
def upload_image(image_filename):
conn = boto.connect_s3()
b = conn.get_bucket('doorcamera')
k = Key(b)
k.key = image_filename
k.set_contents_from_filename(image_filename)
def label_image(image_filename):
cmd = Popen("bash detect-labels.sh %s" % image_filename, shell=True, stdout=PIPE)
output = ""
for line in cmd.stdout:
output += line
#print(output)
data = json.loads(output)
# Sample output
# {
# "Labels": [
# {
# "Confidence": 99.107177734375,
# "Name": "People"
# },
# {
# "Confidence": 99.10718536376953,
# "Name": "Person"
# },
# {
# "Confidence": 99.08333587646484,
# "Name": "Human"
# },
# ],
# "OrientationCorrection": "ROTATE_180"
#}
if not ('Labels' in data):
print("Error in output: %s", output)
return []
labels= [label['Name'] for label in data['Labels']]
return labels
def save_camera(camera):
url = '%s.local/cam.jpg' % camera
file = '%s.jpg' % camera
print "Getting %s" % url
cmd = ["wget", url, '-O', file]
p = call(cmd, stdout=PIPE)
### print("%s: %i" % (camera, p))
if (p == 0):
print "Success!!"
image_files.append(file)
def show_all_cameras():
save_all_cameras()
print image_files
images = map(Image.open, image_files)
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGB', (total_width, max_height))
x_offset = 0
for im in images:
new_im.paste(im, (x_offset,0))
x_offset += im.size[0]
new_im.show()
#new_im.save('test.jpg')
def label_camera(camera):
file = '%s.jpg' % camera
save_camera(camera)
im = Image.open(file)
im.show()
return label_image(file)
def face_camera(camera):
file = '%s.jpg' % camera
save_camera(camera)
im = Image.open(file)
im.show()
person = identify_face_image(file)
data = describe_face_image(file)
if data:
return [person, data[0], data[1]]
else:
return None
def save_all_cameras():
threads = []
for camera in camera_list:
thread = threading.Thread(target=save_camera, args=(camera,))
threads.append(thread)
thread.start()
for thread in threads:
print thread.getName()
thread.join()
if __name__ == "__main__":
#show_all_cameras()
#label_camera("willa")
print face_camera("willa")