-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
176 lines (150 loc) · 6.53 KB
/
main.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
import cgi
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from SocketServer import ThreadingMixIn
from os import curdir, sep
import time
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
from train_lego import *
# Verkrijgt de JSON credentials die gebruikt worden om in te loggen op firebase
cred = credentials.Certificate('robocamera.json')
# initialiseerd firebase admin
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://robocamera-322ea.firebaseio.com/'
})
#zet de reference op "tabel" recognized. Tabel tussen aanhalingstekens want firebase is NOSQL
DB_recognized = db.reference("recognized")
#leegt de database aan het begin ff
DB_recognized.set("")
#de volgende klasses worden onderzocht
classes = ['blauw_2x2', 'blauw_4x2','groen_2x2','groen_4x2','rood_2x2','rood_4x2']
trainPaths = ['./data/' + c + '/train/' for c in classes]
testPaths = ['./data/' + c + '/test/' for c in classes]
#de klassen waarmee de camera wordt getrained.
trainer = Trainlego(classes, trainPaths)
cam = Camera(0)
nteller =0
className = "niks nada noppes nog niets...."
tree = None
cameraQuality = 30
class httpServerRoboCamera(BaseHTTPRequestHandler):
"""
do_GET is een functie van baseHTTPRequesthandler of althans dat de naam van de functie die je overwrite dit is wanneer er
een get request wordt gestuurd naar de server
do_POST dit is de functie die code afhandeld wanneer er een POST request wordt verstuurd naar de server.
In ons geval heeft dit meer met de quality van de camera te maken. Hoe hoger deze is des te meer vertraging erin zit maar als die te laag staat zie je niks.
"""
def do_GET(self):
global cameraQuality
global nteller
global tree
global className
global cam
try:
self.path=re.sub('[^.a-zA-Z0-9]', "",str(self.path))
if self.path=="" or self.path==None or self.path[:1]==".":
return
if self.path.endswith(".html"):
#laad de HTML pagina in index.html
f = open(curdir + sep + self.path)
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(f.read())
f.close()
return
if self.path.endswith(".mjpeg"):
#laad de camera in
self.send_response(200)
self.wfile.write("Content-Type: multipart/x-mixed-replace; boundary=--aaboundary")
self.wfile.write("\r\n\r\n")
while 1:
#constante flow van de camera zolang het script draait vandaar de while 1
#verkrijg camera image
img = cam.getImage()
#voorkom dat de cam te snel update
if nteller > 4:
#classify wat je ziet
className = tree.classify(img)
# f = open('currentClass.txt', 'w')
# f.write(classN ame)
# f.close()
#push het naar de realtime firebase database.
DB_recognized.push({
'type': className,
'time': str(time.time())
})
nteller = 0
else:
nteller += 1
#update de livestream met wat je ziet in het rood
img.drawText(className, 10, 10, fontsize=60, color=Color.RED)
img = img.applyLayers()
#convert en voeg de juiste headers toe
cv2mat=cv.EncodeImage(".jpeg",img.getBitmap(),(cv.CV_IMWRITE_JPEG_QUALITY,cameraQuality))
JpegData=cv2mat.tostring()
self.wfile.write("--aaboundary\r\n")
self.wfile.write("Content-Type: image/jpeg\r\n")
self.wfile.write("Content-length: "+str(len(JpegData))+"\r\n\r\n" )
self.wfile.write(JpegData)
self.wfile.write("\r\n\r\n\r\n")
time.sleep(0.05)
return
if self.path.endswith(".jpeg"):
#zoekt de jpeg file die die moet zoeken
f = open(curdir + sep + self.path)
self.send_response(200)
self.send_header('Content-type','image/jpeg')
self.end_headers()
self.wfile.write(f.read())
f.close()
return
return
except IOError:
#gooi 404
self.send_error(404,'File Not Found: %s' % self.path)
def do_POST(self):
global rootnode, cameraQuality
try:
#update camera
ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
if ctype == 'multipart/form-data':
query=cgi.parse_multipart(self.rfile, pdict)
self.send_response(301)
self.end_headers()
upfilecontent = query.get('upfile')
print "filecontent", upfilecontent[0]
value=int(upfilecontent[0])
cameraQuality=max(2, min(99, value))
self.wfile.write("<HTML>POST OK. Camera Set to<BR><BR>");
self.wfile.write(str(cameraQuality));
except :
pass
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
#class ThreadedHTTPServer(HTTPServer):
"""Handle requests in a separate thread."""
def main():
global tree
#train de classifiers met traindata
trainer.trainen()
tree = trainer.classifiers[1]
#test random afbeelding
imgs = ImageSet()
for p in testPaths:
imgs += ImageSet(p)
random.shuffle(imgs)
#ff tussendoor updateje
print "Result padum padum spannond..."
trainer.testen(testPaths)
#try catch die de server opstart onder localhost:12345
try:
server = ThreadedHTTPServer(('localhost', 12345), httpServerRoboCamera)
print 'started httpserver...'
print 'dit ff runnen in die git bash om het naar buiten te zetton'
print 'ssh -R robostream:80:localhost:12345 serveo.net'
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down server'
server.socket.close()
main()