-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplication.py
222 lines (159 loc) · 6.58 KB
/
application.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from flask import *
import json
import numpy as np
import argparse
import os
import time
import cv2
import ConfigParser
import urllib
import thread
import logging
import requests
import boto3
from botocore.exceptions import NoCredentialsError
app = Flask(__name__)
@app.route('/motion', methods=['POST'])
def motion():
global motionStatus
if motionStatus == 0:
print("Motion is detected")
content = request.json
motionStatus = 1
return 'Success'
def flaskThread():
global dockerPort
app.run(host= '0.0.0.0',port=dockerPort)
def upload_to_aws(local_file, bucket, s3_file):
session = boto3.Session(
aws_access_key_id=accessKey,
aws_secret_access_key=secretKey,
aws_session_token=sessionKey,)
s3 = session.client('s3')
try:
s3.upload_file(local_file, bucket, s3_file)
return True
except Exception as e:
return False
def checkWithRelay(objectList):
relay_pin = int(config.get('birdRepellent', 'relay_pin'))
#print objectList
global frameCounter
global detectorFlag
global serviceIP
global servicePort
print objectList
if ("bird" in objectList):
print ("Detect : Bird")
print ("Activate : Repeller")
url = "http://"+serviceIP+":"+str(servicePort)+"/gpioAc"
data = {'repeller': relay_pin}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(data), headers=headers)
#time.sleep(int(config.get('birdRepellent', 'actuator_runtime')))
frameCounter = 0
return 1
else:
frameCounter = frameCounter+1
return 0
def startCamera():
# initialize the camera and grab a reference to the raw camera capture
# capture streams from the camera
global detectorCount
global serviceIP
global servicePort
print("Activate : Camera")
while True:
start = time.time()
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
objectList = [];
url = "http://"+serviceIP+":"+str(servicePort)+"/camera"
print(url)
url_response = urllib.urlopen(url)
img_array = np.array(bytearray(url_response.read()), dtype=np.uint8)
frame = cv2.imdecode(img_array, -1)
# grab the frame dimensions and convert it to a blob
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(frame, (int(config.get('birdRepellent', 'blob_width')), int(config.get('birdRepellent', 'blob_hight')))),
0.007843, (int(config.get('birdRepellent', 'blob_width')), int(config.get('birdRepellent', 'blob_hight'))), 127.5)
# pass the blob through the network and obtain the detections and
# predictions
net.setInput(blob)
detections = net.forward()
# loop over the detections
for i in np.arange(0, detections.shape[2]):
# extract the confidence (i.e., probability) associated with
# the prediction
confidence = detections[0, 0, i, 2]
# filter out weak detections by ensuring the `confidence` is
# greater than the minimum confidence
if confidence > 0.2:
# extract the index of the class label from the
# `detections`, then compute the (x, y)-coordinates of
# the bounding box for the object
idx = int(detections[0, 0, i, 1])
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
# draw the prediction on the frame
label = "{}: {:.2f}%".format(CLASSES[idx],
confidence * 100)
objectList.append(CLASSES[idx])
cv2.rectangle(frame, (startX, startY), (endX, endY),
COLORS[idx], 2)
y = startY - 15 if startY - 15 > 15 else startY + 15
cv2.putText(frame, label, (startX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
end = time.time()
print("[INFO] Capture and Analysis took {:.6f} seconds".format(end - start))
detectorFlag = checkWithRelay(objectList)
if detectorFlag == 1:
outpath = str(detectorCount)+".jpg"
cv2.imwrite(outpath, frame)
detectorCount = detectorCount + 1
if bucketName!="x":
destName = str(int(time.time()))+".jpg"
upload_to_aws(outpath, bucketName, destName)
# if the `q` key was pressed, break from the loop
if frameCounter == int(config.get('birdRepellent', 'camera_runtime')):
#cv2.destroyWindow("Stream")
print("Close : Camera")
break
#if key == ord("q"):
#break
if __name__ == "__main__":
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
motionStatus = 0;
dockerIP = os.environ['dockerIP']
dockerPort = os.environ['dockerPort']
serviceIP = os.environ['serviceIP']
servicePort = os.environ['servicePort']
accessKey = os.environ['accessKey']
secretKey = os.environ['secretKey']
sessionKey = os.environ['sessionKey']
bucketName = os.environ['bucketName']
thread.start_new_thread(flaskThread,())
frameCounter = 0
detectorCount = 0
#reading configuration files
config = ConfigParser.ConfigParser()
config.readfp(open(r'appConfig.txt'))
# initialize the list of class labels MobileNet SSD was trained to
# detect, then generate a set of bounding box colors for each class
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
"dog", "horse", "motorbike", "person", "pottedplant", "sheep",
"sofa", "train", "tvmonitor"]
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
# load our serialized model from disk
#print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe('MobileNetSSD_deploy.prototxt.txt', 'MobileNetSSD_deploy.caffemodel')
time.sleep(2)
while True:
if motionStatus == 1:
startCamera()
motionStatus = 0
frameCounter = 0
if detectorCount > int(config.get('birdRepellent', 'saved_image_number')) :
detectorCount = 0