-
Notifications
You must be signed in to change notification settings - Fork 0
/
urania.py
76 lines (61 loc) · 2.22 KB
/
urania.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
from tornado.options import define, options
import tornado.ioloop
import tornado.web
import tornado.gen
import time, os
from PIL import Image, ImageDraw
from StringIO import StringIO
import random
import opts
import temp
#Cosas a definir
targetStreamImg = '/tmp/pic.jpg'
class OjeteHandler(tornado.web.RequestHandler):
def get(self):
self.write("Ojete, world")
class MJPEGHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.coroutine
def get(self):
loop = tornado.ioloop.IOLoop.current()
self.set_status(200)
self.set_header('Content-type','multipart/x-mixed-replace; boundary=--boundary')
while True:
staticimg = Image.open(targetStreamImg)
imgx, imgy= staticimg.size
stamp = str(time.strftime("%d/%m/%Y")) + ' - ' + (time.strftime("%H:%M:%S"))
stampimg = ImageDraw.Draw(staticimg)
stampimg.text((imgx-130, imgy-15), stamp, 255)
img_io = StringIO()
staticimg.save(img_io, 'JPEG', quality=80)
self.write("--boundary\n")
self.write("Content-type: image/jpeg\r\n")
self.write("Content-length: %s\r\n\r\n" % str(img_io.len))
self.write(img_io.getvalue())
yield tornado.gen.Task(self.flush)
yield tornado.gen.Task(loop.add_timeout, loop.time() + 0.33)
class JPEGHandler(tornado.web.RequestHandler):
def get(self):
staticimg = Image.open(targetStreamImg)
img_io = StringIO()
staticimg.save(img_io, 'JPEG', quality=70)
self.set_header('Content-type','image/jpeg')
self.set_header('Content-length',str(img_io.len))
self.write(img_io.getvalue())
class CamHandler(tornado.web.RequestHandler):
def get(self):
self.render("templates/cam.html", cpuTemp=temp.getCpuTemp(), gpuTemp=temp.getGpuTemp())
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "static"),
}
application = tornado.web.Application([
(r"/image.mjpg", MJPEGHandler),
(r"/image.jpg", JPEGHandler),
(r"/cam", CamHandler),
(r"/", OjeteHandler),
], **settings)
if __name__ == "__main__":
print 'Platform ' + options.platform + ' with camera ' + options.camera
print 'Serving on ' + str(options.port) + '...'
application.listen(options.port)
tornado.ioloop.IOLoop.instance().start()