annotate bin/picamserve @ 1243:73efcda34af2

picamserve port Ignore-this: 3eccbef256cedb403513ba724d2f4445
author drewp@bigasterisk.com
date Fri, 12 Jun 2015 10:03:14 +0000
parents 95dfce5c12ce
children 7772cc48e016
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
1 #!env_pi/bin/python
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
2 from __future__ import division
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
3 from run_local import log
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
4 import sys;sys.path.append('/usr/lib/python2.7/dist-packages/')
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
5 import io, logging, traceback, time
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
6 import cyclone.web
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
7 from twisted.internet import reactor, threads
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
8 from twisted.internet.defer import inlineCallbacks
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
9 from light9 import prof
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
10
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
11 try:
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
12 import picamera
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
13 cameraCls = picamera.PiCamera
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
14 except ImportError:
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
15 class cameraCls(object):
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
16 def __enter__(self): return self
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
17 def __exit__(self, *a): pass
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
18 def capture(self, out, *a, **kw):
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
19 out.write(open('yuv.demo').read())
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
20 def capture_continuous(self, *a, **kw):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
21 for i in range(1000):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
22 time.sleep(1)
1089
2ee97997ee56 vidref now reads from picamserve
Drew Perttula <drewp@bigasterisk.com>
parents: 1088
diff changeset
23 yield str(i)
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
24
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
25 def setCameraParams(c, arg):
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
26 res = int(arg('res', 480))
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
27 c.resolution = {
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
28 480: (640, 480),
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
29 1080: (1920, 1080),
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
30 1944: (2592, 1944),
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
31 }[res]
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
32 c.shutter_speed = int(arg('shutter', 50000))
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
33 c.exposure_mode = arg('exposure_mode', 'fixedfps')
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
34 c.awb_mode = arg('awb_mode', 'off')
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
35 c.brightness = int(arg('brightness', 50))
1096
087f6cbe4b22 vidrefsetup tool now prepares a url that vidref will use for rpi camera requests
Drew Perttula <drewp@bigasterisk.com>
parents: 1091
diff changeset
36 c.exposure_compensation= int(arg('exposure_compensation', 0))
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
37 c.awb_gains = (float(arg('redgain', 1)), float(arg('bluegain', 1)))
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
38 c.ISO = int(arg('iso', 250))
1105
0440fb0f458b add rotation param to vidref
Drew Perttula <drewp@bigasterisk.com>
parents: 1096
diff changeset
39 c.rotation = int(arg('rotation', '0'))
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
40
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
41 def setupCrop(c, arg):
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
42 c.crop = (float(arg('x', 0)), float(arg('y', 0)),
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
43 float(arg('w', 1)), float(arg('h', 1)))
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
44 rw = rh = int(arg('resize', 100))
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
45 # width 1920, showing w=.3 of image, resize=100 -> scale is 100/.3*1920
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
46 # scl is [ output px / camera px ]
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
47 scl1 = rw / (c.crop[2] * c.resolution[0])
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
48 scl2 = rh / (c.crop[3] * c.resolution[1])
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
49 if scl1 < scl2:
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
50 # width is the constraint; reduce height to the same scale
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
51 rh = int(scl1 * c.crop[3] * c.resolution[1])
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
52 else:
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
53 # height is the constraint
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
54 rw = int(scl2 * c.crop[2] * c.resolution[0])
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
55 return rw, rh
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
56
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
57 @prof.logTime
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
58 def getFrame(c, arg):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
59 setCameraParams(c, arg)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
60 resize = setupCrop(c, arg)
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
61 out = io.BytesIO('w')
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
62 prof.logTime(c.capture)(out, 'jpeg', use_video_port=True, resize=resize)
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
63 return out.getvalue()
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
64
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
65 class Pic(cyclone.web.RequestHandler):
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
66 def get(self):
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
67 try:
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
68 self.set_header('Content-Type', 'image/jpeg')
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
69 self.write(getFrame(self.settings.camera, self.get_argument))
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
70 except Exception:
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
71 traceback.print_exc()
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
72
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
73 def captureContinuousAsync(c, resize, onFrame):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
74 """
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
75 Calls c.capture_continuous is called in another thread. onFrame is
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
76 called in this reactor thread with each (frameTime, frame)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
77 result. Runs until onFrame raises StopIteration.
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
78 """
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
79 def runner(c, resize):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
80 stream = io.BytesIO()
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
81 t = time.time()
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
82 for nextFrame in c.capture_continuous(stream, 'jpeg', use_video_port=True,
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
83 resize=resize):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
84 t2 = time.time()
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
85 log.debug(" - framecap got %s bytes in %.1f ms",
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
86 len(stream.getvalue()), 1000 * (t2 - t))
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
87 try:
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
88 # This is slow, like 13ms. Hopefully
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
89 # capture_continuous is working on gathering the next
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
90 # pic during this time instead of pausing.
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
91 # Instead, we could be stashing frames onto a queue or
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
92 # something that the main thread can pull when
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
93 # possible (and toss if it gets behind).
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
94 threads.blockingCallFromThread(reactor,
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
95 onFrame, t, stream.getvalue())
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
96 except StopIteration:
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
97 break
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
98 t3 = time.time()
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
99 log.debug(" - sending to onFrame took %.1fms", 1000 * (t3 - t2))
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
100 stream.truncate()
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
101 stream.seek(0)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
102 t = time.time()
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
103 return threads.deferToThread(runner, c, resize)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
104
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
105 class FpsReport(object):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
106 def __init__(self):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
107 self.frameTimes = []
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
108 self.lastFpsLog = 0
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
109
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
110 def frame(self):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
111 now = time.time()
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
112
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
113 self.frameTimes.append(now)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
114
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
115 if len(self.frameTimes) > 15:
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
116 del self.frameTimes[:5]
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
117
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
118 if now > self.lastFpsLog + 2 and len(self.frameTimes) > 5:
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
119 deltas = [(b - a) for a, b in zip(self.frameTimes[:-1],
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
120 self.frameTimes[1:])]
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
121 avg = sum(deltas) / len(deltas)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
122 log.info("fps: %.1f", 1 / avg)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
123 self.lastFpsLog = now
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
124
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
125
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
126 class Pics(cyclone.web.RequestHandler):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
127 @inlineCallbacks
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
128 def get(self):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
129 try:
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
130 self.set_header('Content-Type', 'x-application/length-time-jpeg')
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
131 c = self.settings.camera
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
132 setCameraParams(c, self.get_argument)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
133 resize = setupCrop(c, self.get_argument)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
134
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
135 self.running = True
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
136 log.info("connection open from %s", self.request.remote_ip)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
137 fpsReport = FpsReport()
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
138
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
139 def onFrame(frameTime, frame):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
140 if not self.running:
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
141 raise StopIteration
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
142
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
143 now = time.time()
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
144 self.write("%s %s\n" % (len(frame), frameTime))
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
145 self.write(frame)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
146 self.flush()
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
147
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
148 fpsReport.frame()
1091
50a68abd2b0e comment
Drew Perttula <drewp@bigasterisk.com>
parents: 1089
diff changeset
149 # another camera request coming in at the same time breaks
50a68abd2b0e comment
Drew Perttula <drewp@bigasterisk.com>
parents: 1089
diff changeset
150 # the server. it would be nice if this request could
50a68abd2b0e comment
Drew Perttula <drewp@bigasterisk.com>
parents: 1089
diff changeset
151 # let-go-and-reopen when it knows about another request
50a68abd2b0e comment
Drew Perttula <drewp@bigasterisk.com>
parents: 1089
diff changeset
152 # coming in
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
153 yield captureContinuousAsync(c, resize, onFrame)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
154 except Exception:
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
155 traceback.print_exc()
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
156
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
157 def on_connection_close(self, *a, **kw):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
158 log.info("connection closed")
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
159 self.running = False
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
160
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
161 log.setLevel(logging.INFO)
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
162
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
163 with cameraCls() as camera:
1243
73efcda34af2 picamserve port
drewp@bigasterisk.com
parents: 1212
diff changeset
164 port = 8208
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
165 reactor.listenTCP(port, cyclone.web.Application(handlers=[
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
166 (r'/pic', Pic),
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
167 (r'/pics', Pics),
1212
95dfce5c12ce rearrange /static. new bin/homepage
drewp@bigasterisk.com
parents: 1105
diff changeset
168 (r'/static/(.*)', cyclone.web.StaticFileHandler, {'path': 'light9/web/'}),
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
169 (r'/(|gui.js)', cyclone.web.StaticFileHandler, {'path': 'light9/vidref/',
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
170 'default_filename': 'index.html'}),
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
171 ], debug=True, camera=camera))
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
172 log.info("serving on %s" % port)
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
173 reactor.run()