-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatSvr.py
186 lines (166 loc) · 7.72 KB
/
catSvr.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
#!/usr/bin/env python
# Based on https://stackoverflow.com/questions/40460846/using-flask-inside-class
import os
import flask
import datetime
import time
import cv2
try:
import gpiozero
except:
gpiozero = None
import psutil
class CatSvr():
def __init__(self, catoCam):
'''
Initialise the catocam web server - catoCam should be an instance of the CatoCam class that is
doing the cat detection
'''
self.cc = catoCam
self.app = flask.Flask("CatoCam", template_folder="/home/graham/catocam/www/templates",
static_folder="/home/graham/catocam/www/static", static_url_path='/')
self.app.add_url_rule(rule="/index.html", view_func=self.getIndex)
self.app.add_url_rule(rule="/", view_func=self.getIndex)
self.app.add_url_rule(rule="/history/<dateStr>", view_func=self.getHistory)
self.app.add_url_rule(rule="/history/<dateStr>/<idx>", view_func=self.getHistory)
self.app.add_url_rule(rule="/status", view_func=self.getStatus)
self.app.add_url_rule(rule="/mjpeg", view_func=self.getMjpeg)
self.app.add_url_rule(rule="/currimg", view_func=self.getCurImg)
self.app.add_url_rule(rule="/lastimg", view_func=self.getLastPositiveImg)
self.app.add_url_rule(rule="/histimg/<dateStr>/<idx>", view_func=self.getHistoryImg)
#self.app.add_url_rule(rule="/favicon.ico", view_func=self.getFavicon)
self.lastImgTime = None
def run(self, nameStr):
print("CatSvr.run(): %s" % nameStr)
self.app.run(host='0.0.0.0', port=8082, debug=True, use_reloader=False)
print("CatSvr.run() finished.")
def getIndex(self):
outputFolderLst = self.cc.getOutputFoldersLst()
statusObj = self.getStatus()
return flask.render_template('index.html',
time=datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),
folders=outputFolderLst,
fps = "%.2f" % statusObj['fps'],
cpuT = "%.1f" % statusObj['cpuT']
)
def getHistory(self, dateStr, idx=None):
#print("getHistory() - dateStr=%s, idx=%s" % (dateStr, idx))
outputFolderLst = self.cc.getOutputFoldersLst()
imgLst = self.cc.getSavedImgLst(dateStr)
statusObj = self.getStatus()
#print("getHistory() - imgLst=", imgLst)
if idx is not None and idx != "None":
idx = int(idx)
if idx>=0 and idx<=len(imgLst):
imgStr = imgLst[idx]
if idx>0:
idxPrev = idx-1
else:
idxPrev = None
if idx<len(imgLst)-1:
idxNext = idx+1
else:
idxNext = None
else:
imgStr = None
idxPrev = None
idxNext = None
return flask.render_template('history.html',
time=datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),
folders=outputFolderLst,
dir=dateStr,
img=imgStr,
idx=idx,
idxPrev=idxPrev,
idxNext=idxNext,
imgLst=imgLst,
fps = "%.2f" % statusObj['fps'],
cpuT = "%.1f" % statusObj['cpuT']
)
def getHistoryByFilename(self, dateStr, imgStr=None):
print("getHistory() - dateStr=%s, imgStr=%s" % (dateStr, imgStr))
outputFolderLst = self.cc.getOutputFoldersLst()
imgLst = self.cc.getSavedImgLst(dateStr)
statusObj = self.getStatus()
#print("getHistory() - imgLst=", imgLst)
return flask.render_template('history.html',
time=datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),
folders=outputFolderLst,
dir=dateStr,
img=imgStr,
imgLst=imgLst,
fps = "%.2f" % statusObj['fps'],
cpuT = "%.1f" % statusObj['cpuT']
)
def getStatus(self):
now = datetime.datetime.now()
timeString = now.strftime("%Y-%m-%d %H:%M")
if gpiozero is not None:
cpuTemp = gpiozero.CPUTemperature().temperature
else:
print(psutil.sensors_temperatures())
cpuTemp = psutil.sensors_temperatures()['coretemp'][0][1]
statusData = {
'title' : 'CatoCam',
'time': timeString,
'cpuT': cpuTemp,
'foundCat': self.cc.foundCat,
'foundSomething': self.cc.foundSomething,
'fps': self.cc.fps,
'imgTime': self.cc.imgTime
}
return statusData
def prepareImgStream(self):
while True:
time.sleep(0.2)
if self.lastImgTime != self.cc.imgTime:
a , frame = cv2.imencode('.jpg', self.cc.currImg)
yield (b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + frame.tobytes() + b'\r\n')
def getMjpeg(self):
return flask.Response(self.prepareImgStream(), mimetype='multipart/x-mixed-replace; boundary=frame')
def getCurImg(self):
if self.cc.currImg is None:
return "", 204
# Based on https://stackoverflow.com/questions/42787927/displaying-opencv-image-using-python-flask
retval, buffer = cv2.imencode('.png', self.cc.currImg)
response = flask.make_response(buffer.tobytes())
response.headers['Content-Type'] = 'image/png'
return response
def getLastPositiveImg(self):
if self.cc.lastPositiveImg is None:
return "", 204
# Based on https://stackoverflow.com/questions/42787927/displaying-opencv-image-using-python-flask
retval, buffer = cv2.imencode('.png', self.cc.lastPositiveImg)
response = flask.make_response(buffer.tobytes())
response.headers['Content-Type'] = 'image/png'
return response
def getHistoryImg(self, dateStr, idx):
if idx is None:
return None
idx = int(idx)
print("getHistoryImg() - dateStr=%s, imgStr=%s" % (dateStr, idx))
img = self.cc.getHistoryImgByIndex(dateStr, idx)
if img is None:
return "", 204
# Based on https://stackoverflow.com/questions/42787927/displaying-opencv-image-using-python-flask
retval, buffer = cv2.imencode('.png', img)
response = flask.make_response(buffer.tobytes())
response.headers['Content-Type'] = 'image/png'
return response
def getHistoryImgByName(self, dateStr, imgStr):
print("getHistoryImg() - dateStr=%s, imgStr=%s" % (dateStr, imgStr))
img = self.cc.getHistoryImg(dateStr, imgStr)
if img is None:
return "", 204
# Based on https://stackoverflow.com/questions/42787927/displaying-opencv-image-using-python-flask
retval, buffer = cv2.imencode('.png', img)
response = flask.make_response(buffer.tobytes())
response.headers['Content-Type'] = 'image/png'
return response
def getFavicon(self):
return flask.send_from_directory(os.path.join(self.app.root_path, 'www'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
if __name__ == "__main__":
#app.run(host='0.0.0.0', port=8082, debug=True)
catSvr = CatSvr(None)
catSvr.run()