-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
71 lines (61 loc) · 1.84 KB
/
app.py
File metadata and controls
71 lines (61 loc) · 1.84 KB
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
#! /usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
app.py
python カメラモジュール操作のデーモン
"""
__author__ = 'Yoshiya Ito <myon53@gmail.com>'
__version__ = '0.0.1'
__date__ = '16 Nov 2015'
import threading
import io
import time
import picamera
import tornado
import tornado.web
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
class HTTP(tornado.web.RequestHandler):
def get(self):
self.render('./index.html');
class WS(tornado.websocket.WebSocketHandler):
def initialize(self):
camera = picamera.PiCamera()
camera.resolution = (450, 300)
camera.framerate = 30
camera.stream = io.BytesIO()
time.sleep(2)
self.camera = camera
self.is_rec = True
def open(self):
print('{0}:connection open'.format(self.request.remote_ip))
t = threading.Thread(target=self.rec)
t.setDaemon(True)
t.start()
print('start rec...')
def rec(self):
for _ in self.camera.capture_continuous(self.camera.stream, 'jpeg', use_video_port=True):
self.camera.stream.seek(0)
try:
self.write_message(self.camera.stream.read(), binary=True)
except tornado.websocket.WebSocketClosedError:
pass
self.camera.stream.seek(0)
self.camera.stream.truncate()
if not self.is_rec:
break
def on_close(self):
self.is_rec = False
self.close()
self.camera.stream.seek(0)
self.camera.stream.truncate()
print('{0}:connection close'.format(self.request.remote_ip))
if __name__ == '__main__':
app = tornado.web.Application([
('/', HTTP),
('/live', WS)
]);
http = tornado.httpserver.HTTPServer(app);
http.listen(5000)
tornado.ioloop.IOLoop.instance().start();