-
Notifications
You must be signed in to change notification settings - Fork 25
/
cloud-lambda.py
190 lines (152 loc) · 5.92 KB
/
cloud-lambda.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
import json
import boto3
import time
import os
def matchPersonsAndHats(personsList, hardhatsList):
persons = []
hardhats = []
personsWithHats = []
for person in personsList:
persons.append(person)
for hardhat in hardhatsList:
hardhats.append(hardhat)
h = 0
matched = 0
totalHats = len(hardhats)
while(h < totalHats):
hardhat = hardhats[h-matched]
totalPersons = len(persons)
p = 0
while(p < totalPersons):
person = persons[p]
if(not (hardhat['BoundingBoxCoordinates']['x2'] < person['BoundingBoxCoordinates']['x1']
or hardhat['BoundingBoxCoordinates']['x1'] > person['BoundingBoxCoordinates']['x2']
or hardhat['BoundingBoxCoordinates']['y4'] < person['BoundingBoxCoordinates']['y1']
or hardhat['BoundingBoxCoordinates']['y1'] > person['BoundingBoxCoordinates']['y4']
)):
personsWithHats.append({'Person' : person, 'Hardhat' : hardhat})
del persons[p]
del hardhats[h - matched]
matched = matched + 1
break
p = p + 1
h = h + 1
return (personsWithHats, persons, hardhats)
def getBoundingBoxCoordinates(boundingBox, imageWidth, imageHeight):
x1 = 0
y1 = 0
x2 = 0
y2 = 0
x3 = 0
y3 = 0
x4 = 0
y4 = 0
boxWidth = boundingBox['Width']*imageWidth
boxHeight = boundingBox['Height']*imageHeight
x1 = boundingBox['Left']*imageWidth
y1 = boundingBox['Top']*imageWidth
x2 = x1 + boxWidth
y2 = y1
x3 = x2
y3 = y1 + boxHeight
x4 = x1
y4 = y3
return({'x1': x1, 'y1' : y1, 'x2' : x2, 'y2' : y2, 'x3' : x3, 'y3' : y3, 'x4' : x4, 'y4' : y4})
def getPersonsAndHardhats(labelsResponse, imageWidth, imageHeight):
persons = []
hardhats = []
for label in labelsResponse['Labels']:
if label['Name'] == 'Person' and 'Instances' in label:
for person in label['Instances']:
persons.append({'BoundingBox' : person['BoundingBox'], 'BoundingBoxCoordinates' : getBoundingBoxCoordinates(person['BoundingBox'], imageWidth, imageHeight), 'Confidence' : person['Confidence']})
elif ((label['Name'] == 'Hardhat' or label['Name'] == 'Helmet') and 'Instances' in label):
for hardhat in label['Instances']:
hardhats.append({'BoundingBox' : hardhat['BoundingBox'], 'BoundingBoxCoordinates' : getBoundingBoxCoordinates(hardhat['BoundingBox'], imageWidth, imageHeight), 'Confidence' : hardhat['Confidence']})
return (persons, hardhats)
def detectWorkerSafety(bucketName, imageName, imageWidth, imageHeight):
rekognition = boto3.client('rekognition', region_name='us-east-1')
labelsResponse = rekognition.detect_labels(
Image={
'S3Object': {
'Bucket': bucketName,
'Name': imageName,
}
},
MaxLabels=20,
MinConfidence=60)
persons, hardhats = getPersonsAndHardhats(labelsResponse, imageWidth, imageHeight)
return matchPersonsAndHats(persons, hardhats)
def sendMessageToIoTTopic(iotMessage):
topicName = "worker-safety"
if "iot_topic" in os.environ:
topicName = os.environ['iot_topic']
iotClient = boto3.client('iot-data', region_name='us-east-1')
response = iotClient.publish(
topic=topicName,
qos=1,
payload=json.dumps(iotMessage)
)
print("Send message to topic: " + topicName)
def pushToCloudWatch(name, value):
try:
cloudwatch = boto3.client('cloudwatch', region_name='us-east-1')
response = cloudwatch.put_metric_data(
Namespace='string',
MetricData=[
{
'MetricName': name,
'Value': value,
'Unit': 'Count'
},
]
)
#print("Metric pushed: {}".format(response))
except Exception as e:
print("Unable to push to cloudwatch\n e: {}".format(e))
return True
def lambda_handler(event, context):
bucketName = event['Records'][0]['s3']['bucket']['name']
imageName = event['Records'][0]['s3']['object']['key']
scaleFactor = 4
imageWidth = 2688/scaleFactor
imageHeight = 1520/scaleFactor
personsWithHats, personsWithoutHats, hatsWihoutPerson = detectWorkerSafety(bucketName, imageName, imageWidth, imageHeight)
personsWithHatsCount = len(personsWithHats)
personsWithoutHatsCount = len(personsWithoutHats)
hatsWihoutPersonCount = len(hatsWihoutPerson)
pushToCloudWatch('PersonsWithSafetyHat', personsWithHatsCount)
pushToCloudWatch('PersonsWithoutSafetyHat', personsWithoutHatsCount)
outputMessage = "Person(s): {}".format(personsWithHatsCount+personsWithoutHatsCount)
outputMessage = outputMessage + "\nPerson(s) With Safety Hat: {}\nPerson(s) Without Safety Hat: {}".format(personsWithHatsCount, personsWithoutHatsCount)
print(outputMessage)
#imageUrl = "https://s3.amazonaws.com/{}/{}".format(bucketName, imageName)
s3_client = boto3.client('s3')
imageUrl = s3_client.generate_presigned_url('get_object', Params={'Bucket': bucketName, 'Key': imageName })
iotMessage = {'ImageUrl' :imageUrl, 'PersonsWithHat' : personsWithHats, 'PersonsWithoutHat' : personsWithoutHats, 'Message' : outputMessage}
sendMessageToIoTTopic(iotMessage)
return {
'statusCode': 200,
'body': json.dumps(outputMessage)
}
def localTest():
bucketName = "ki-worker-safety"
#imageName = "persons/11_11/4_49/1541929754_0.jpg"
#imageName = "worker-safety/00.jpg"
imageName = "persons/1541974066_0.jpg"
#imageName = "persons/yard-work.jpg"
event = {
"Records": [
{
"s3": {
"bucket": {
"name": bucketName,
},
"object": {
"key": imageName,
}
}
}
]
}
lambda_handler(event, None)
#localTest()