Mercurial > code > home > repos > light9
annotate bin/picamserve @ 1091:50a68abd2b0e
comment
Ignore-this: dd6216479fa438813e84869f94e15efa
author | Drew Perttula <drewp@bigasterisk.com> |
---|---|
date | Thu, 05 Jun 2014 07:49:48 +0000 |
parents | 2ee97997ee56 |
children | 087f6cbe4b22 |
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)) |
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
36 |
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)) |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
39 |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
40 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
|
41 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
|
42 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
|
43 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
|
44 # 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
|
45 # 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
|
46 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
|
47 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
|
48 if scl1 < scl2: |
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
49 # 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
|
50 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
|
51 else: |
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
52 # height is the constraint |
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
53 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
|
54 return rw, rh |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
55 |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
56 @prof.logTime |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
57 def getFrame(c, arg): |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
58 setCameraParams(c, arg) |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
59 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
|
60 out = io.BytesIO('w') |
1088
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
61 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
|
62 return out.getvalue() |
1088
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
63 |
1087
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
64 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
|
65 def get(self): |
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
66 try: |
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
67 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
|
68 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
|
69 except Exception: |
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
70 traceback.print_exc() |
1088
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
71 |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
72 def captureContinuousAsync(c, resize, onFrame): |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
73 """ |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
74 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
|
75 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
|
76 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
|
77 """ |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
78 def runner(c, resize): |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
79 stream = io.BytesIO() |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
80 t = time.time() |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
81 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
|
82 resize=resize): |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
83 t2 = time.time() |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
84 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
|
85 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
|
86 try: |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
87 # 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
|
88 # 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
|
89 # 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
|
90 # 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
|
91 # 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
|
92 # 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
|
93 threads.blockingCallFromThread(reactor, |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
94 onFrame, t, stream.getvalue()) |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
95 except StopIteration: |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
96 break |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
97 t3 = time.time() |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
98 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
|
99 stream.truncate() |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
100 stream.seek(0) |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
101 t = time.time() |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
102 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
|
103 |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
104 class FpsReport(object): |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
105 def __init__(self): |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
106 self.frameTimes = [] |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
107 self.lastFpsLog = 0 |
1087
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
108 |
1088
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
109 def frame(self): |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
110 now = time.time() |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
111 |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
112 self.frameTimes.append(now) |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
113 |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
114 if len(self.frameTimes) > 15: |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
115 del self.frameTimes[:5] |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
116 |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
117 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
|
118 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
|
119 self.frameTimes[1:])] |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
120 avg = sum(deltas) / len(deltas) |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
121 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
|
122 self.lastFpsLog = now |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
123 |
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 class Pics(cyclone.web.RequestHandler): |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
126 @inlineCallbacks |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
127 def get(self): |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
128 try: |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
129 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
|
130 c = self.settings.camera |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
131 setCameraParams(c, self.get_argument) |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
132 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
|
133 |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
134 self.running = True |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
135 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
|
136 fpsReport = FpsReport() |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
137 |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
138 def onFrame(frameTime, frame): |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
139 if not self.running: |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
140 raise StopIteration |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
141 |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
142 now = time.time() |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
143 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
|
144 self.write(frame) |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
145 self.flush() |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
146 |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
147 fpsReport.frame() |
1091 | 148 # another camera request coming in at the same time breaks |
149 # the server. it would be nice if this request could | |
150 # let-go-and-reopen when it knows about another request | |
151 # coming in | |
1088
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
152 yield captureContinuousAsync(c, resize, onFrame) |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
153 except Exception: |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
154 traceback.print_exc() |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
155 |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
156 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
|
157 log.info("connection closed") |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
158 self.running = False |
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
159 |
1087
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
160 log.setLevel(logging.INFO) |
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
161 |
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
162 with cameraCls() as camera: |
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
163 port = 8001 |
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
164 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
|
165 (r'/pic', Pic), |
1088
bb92c50438ed
picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents:
1087
diff
changeset
|
166 (r'/pics', Pics), |
1087
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
167 (r'/static/(.*)', cyclone.web.StaticFileHandler, {'path': 'static/'}), |
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
168 (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
|
169 'default_filename': 'index.html'}), |
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
170 ], debug=True, camera=camera)) |
1f877950ad28
new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
171 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
|
172 reactor.run() |