-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo.py
365 lines (308 loc) · 12.9 KB
/
video.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import time
import cv2
import person
import settings
import webservice
from datetime import datetime
import base64
from PIL import Image, ImageFile
import math
import subprocess
import json
import urllib
from zipfile import ZipFile
import os
debug = 0
class Video:
def __init__(self, source):
self.camera = cv2.VideoCapture(source)
self.backgroundFrame = None
self.start = time.time()
self.settings = settings.Settings()
self.persons = person.Persons(
self.settings.movementMaximum, self.settings.movementMinimum, self.settings.movementTime)
self.start = time.time()
self.webservice = webservice.Webservice()
self.errorcount = 0
self.alertLog = []
self.frameCount = 1
def nextFrame(self):
grabbed, self.frame = self.camera.read()
if not grabbed: # eof
self.destroyNow()
self.convertFrame()
def showFrame(self):
if debug:
cv2.imshow("Thresh", self.thresh)
cv2.imshow("backgroundFrame", self.backgroundFrame)
cv2.imshow("frameDelta", self.frameDelta)
cv2.imshow("Feed", self.frame)
def destroyNow(self):
self.camera.release()
cv2.destroyAllWindows()
def testDestroy(self):
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
self.destroyNow()
return 1
else:
return 0
def resetbackgroundFrame(self):
grabbed, self.frame = self.camera.read()
self.convertFrame()
self.backgroundFrame = self.currentFrame
self.persons = person.Persons(
self.settings.movementMaximum, self.settings.movementMinimum, self.settings.movementTime)
self.frameCount = 1
print('resetbackgroundFrame')
def testBackgroundFrame(self):
key = cv2.waitKey(1) & 0xFF
if key == ord("n"):
self.backgroundFrame = None
if self.backgroundFrame is None:
self.resetbackgroundFrame()
def testSettings(self):
key = cv2.waitKey(1) & 0xFF
if key == ord("0"):
self.settings.minArea += 50
print("minArea: ", self.settings.minArea)
if key == ord("9"):
self.settings.minArea -= 50
print("minArea: ", self.settings.minArea)
if key == ord("8"):
self.settings.dilationPixels += 1
print("dilationPixels: ", self.settings.dilationPixels)
if key == ord("7"):
self.settings.dilationPixels -= 1
print("dilationPixels: ", self.settings.dilationPixels)
if key == ord("6"):
self.settings.thresholdLimit += 1
print("thresholdLimit: ", self.settings.thresholdLimit)
if key == ord("5"):
self.settings.thresholdLimit -= 1
print("thresholdLimit: ", self.settings.thresholdLimit)
if key == ord("4"):
self.settings.movementMaximum += 1
print("movementMaximum: ", self.settings.movementMaximum)
if key == ord("3"):
self.settings.movementMaximum -= 1
print("movementMaximum: ", self.settings.movementMaximum)
if key == ord("2"):
self.settings.movementMinimum += 1
print("movementMinimum: ", self.settings.movementMinimum)
if key == ord("1"):
self.settings.movementMinimum -= 1
print("movementMinimum: ", self.settings.movementMinimum)
if key == ord("o"):
if self.settings.useGaussian:
self.settings.useGaussian = 0
print("useGaussian: off")
self.resetbackgroundFrame()
else:
self.settings.useGaussian = 1
print("useGaussian: on")
self.resetbackgroundFrame()
if key == ord("+"):
self.settings.movementTime += 1
print("movementTime: ", self.settings.movementTime)
if key == ord("p"):
self.settings.movementTime -= 1
print("movementTime : ", self.settings.movementTime)
def updateBackground(self):
alpha = (1.0/self.frameCount)
self.backgroundFrame = cv2.addWeighted(
self.currentFrame, alpha, self.backgroundFrame, 1.0-alpha, 0)
self.frameCount += 1
def convertFrame(self):
r = 750.0 / self.frame.shape[1]
dim = (750, int(self.frame.shape[0] * r))
self.frame = cv2.resize(self.frame, dim, interpolation=cv2.INTER_AREA)
self.currentFrame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
if self.settings.useGaussian:
self.currentFrame = cv2.GaussianBlur(
self.currentFrame, (self.settings.gaussianPixels, self.settings.gaussianPixels), 0)
def compare(self):
# difference between the current frame and backgroundFrame
self.frameDelta = cv2.absdiff(self.backgroundFrame, self.currentFrame)
self.thresh = cv2.threshold(
self.frameDelta, self.settings.thresholdLimit, 255, cv2.THRESH_BINARY)[1]
self.thresh = cv2.dilate(
self.thresh, None, iterations=self.settings.dilationPixels) # dilate thresh
(_, contours, _) = cv2.findContours(self.thresh.copy(),
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # find contours
self.persons.tick()
detectStatus = "idle"
for contour in contours:
if cv2.contourArea(contour) < self.settings.minArea:
continue
(x, y, w, h) = cv2.boundingRect(contour)
if self.thresh.shape[1] < w+50 and self.thresh.shape[0] < h+50:
self.newLightconditions()
continue
person = self.persons.addPerson(x, y, w, h)
color = (0, 255, 0)
if person.alert:
color = (0, 0, 255)
cv2.line(self.frame, (x, y), (x + w, y + h), color, 2)
cv2.line(self.frame, (x + w, y), (x, y + h), color, 2)
detectStatus = "Alarm, not moving"
if not person.alarmReported:
print('Not moving detected')
person.alarmReported = 1
# capture image
imageFileLocation = self.captureImage()
predictResult = self.getPrediction(imageFileLocation)
# predictResult = self.getPrediction('./images/test.jpg')
if float(predictResult) > 0.9:
# get camera_id
device_id = self.getSerial()
# reduce image size and encode to base64
self.reduceImageSize(imageFileLocation)
imageBase64 = self.convertImageToBase64(
imageFileLocation)
# send image to API
print('send alert to api with image data and device id')
self.webservice.alarm(device_id, imageBase64)
# delete file in images folder
os.remove(imageFileLocation)
cv2.rectangle(self.frame, (x, y), (x + w, y + h), color, 2)
cv2.putText(self.frame, "{}".format(cv2.contourArea(contour)),
(x, y+h+20), cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 1)
cv2.putText(self.frame, "{} : {}".format(
person.id, person.lastmoveTime), (x, y+20), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 1)
# Hud + fps
if debug:
self.end = time.time()
seconds = self.end - self.start
fps = round((1 / seconds), 1)
self.start = time.time()
cv2.putText(self.frame, "Status: {}".format(detectStatus),
(10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 140, 255), 1)
cv2.putText(self.frame, "FPS: {}".format(fps), (400, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 140, 255), 1)
def newLightconditions(self):
self.errorcount += 1
if self.errorcount > 10:
time.sleep(1.0)
self.resetbackgroundFrame()
self.errorcount = 0
# capture image
def captureImage(self):
fileLocation = ""
print 'Capture image...'
while(True):
ret, frame = self.camera.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
# cv2.imshow('frame', rgb) # show capture image
now = datetime.now()
# create file location
fileLocation = './images/' + now.strftime("%Y%m%d-%H%M%S") + '.jpg'
out = cv2.imwrite(fileLocation, frame)
break
return fileLocation
# convert image to base64
def convertImageToBase64(self, imageFileLocation):
print 'Convert image to Base64'
with open(imageFileLocation, "rb") as img_file:
my_string = base64.b64encode(img_file.read())
return my_string.decode('utf-8')
# get raspberry pi's cpuid
def getSerial(self):
# Extract serial from cpuinfo file
cpuserial = "0000000000000000"
try:
f = open('/proc/cpuinfo', 'r')
for line in f:
if line[0:6] == 'Serial':
cpuserial = line[10:26]
f.close()
except:
cpuserial = "ERROR000000000"
return cpuserial
# resize image to 250px
def reduceImageSize(self, imageFileLocation):
print 'Resize image...'
pic = imageFileLocation
im = Image.open(pic)
width, height = im.size
resizeRatio = 250/width
# if width and height more than 1000, do resize
if width > 1000 or height > 1000:
width = width*resizeRatio
height = height*resizeRatio
im = im.resize((int(math.floor(width)), int(
math.floor(height))), Image.ANTIALIAS)
try:
im.save(pic, optimize=True, quality=75)
except IOError:
ImageFile.MAXBLOCK = width * height
im.save(pic, optimize=True, quality=75)
# get prediction from ML
def getPrediction(self, sourceFileLocation):
print 'Analyse by ML...'
# ML model location
modelPb = "tensorflow-for-poets-2/tf_files/retrained_graph.pb"
modelLabels = "tensorflow-for-poets-2/tf_files/retrained_labels.txt"
# create command
cmd = "python3 -m tensorflow-for-poets-2.scripts.label_image --image=" + \
sourceFileLocation + " --graph=" + modelPb + " --labels=" + modelLabels
# run command
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
(output, err) = p.communicate()
p_status = p.wait()
print "Command output : ", output
return output
def checkUpdateVersion(self):
print('Check to software update...')
device_id = self.getSerial()
requireUpdateDetail = self.webservice.checkVersion(device_id)
# check if it need to update
if requireUpdateDetail["error"] == False and requireUpdateDetail["require_update"] == True:
print('Need to update new version: ' +
requireUpdateDetail["new_version"])
# load file for update
updateFile_url = requireUpdateDetail["file_url"]
# download file
print('download file from server...')
self.downloadFile(updateFile_url)
# unzip update.zip and extract to ML folder
print('Unzip update file and extract to ML location...')
self.extractUpdateFile()
# update software verion in database
print('Updated software version on database...')
updateResponse = self.webservice.updateVersion(
device_id, requireUpdateDetail["new_version"])
if updateResponse["error"] == False:
print('Update vesion on database....successful.')
else:
print('Update vesion on database....fail.')
else:
print("Don't have new version to update.\n")
def downloadFile(self, url):
file_name = 'update.zip'
u = urllib.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name, file_size)
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = r"%10d [%3.2f%%]" % (
file_size_dl, file_size_dl * 100. / file_size)
status = status + chr(8)*(len(status)+1)
print status,
print('')
print('Finish download')
f.close()
return True
def extractUpdateFile(self):
zf = ZipFile('update.zip', 'r')
zf.extractall('./tensorflow-for-poets-2/tf_files/')
zf.close()
print('Finish extract and update file in ML location.')