annotate bin/picamserve @ 2335:54cf7034bee0

EE don't abort compile on partial data
author drewp@bigasterisk.com
date Fri, 02 Jun 2023 14:54:11 -0700
parents 3c523c71da29
children
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
1859
f066d6e874db 2to3 with these fixers: all idioms set_literal
drewp@bigasterisk.com
parents: 1858
diff changeset
2
1087
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
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
4 import sys
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
5 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
6 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
7 import cyclone.web
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
8 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
9 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
10 from light9 import prof
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
11
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
12 try:
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
13 import picamera
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
14 cameraCls = picamera.PiCamera
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
15 except ImportError:
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
16
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
17 class cameraCls(object):
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
18
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
19 def __enter__(self):
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
20 return self
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
21
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
22 def __exit__(self, *a):
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
23 pass
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
24
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
25 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
26 out.write(open('yuv.demo').read())
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
27
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
28 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
29 for i in range(1000):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
30 time.sleep(1)
1089
2ee97997ee56 vidref now reads from picamserve
Drew Perttula <drewp@bigasterisk.com>
parents: 1088
diff changeset
31 yield str(i)
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
32
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
33
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
34 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
35 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
36 c.resolution = {
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
37 480: (640, 480),
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
38 1080: (1920, 1080),
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
39 1944: (2592, 1944),
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
40 }[res]
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
41 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
42 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
43 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
44 c.brightness = int(arg('brightness', 50))
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
45 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
46 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
47 c.ISO = int(arg('iso', 250))
1105
0440fb0f458b add rotation param to vidref
Drew Perttula <drewp@bigasterisk.com>
parents: 1096
diff changeset
48 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
49
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
50
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
51 def setupCrop(c, arg):
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
52 c.crop = (float(arg('x', 0)), float(arg('y', 0)), float(arg('w', 1)),
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
53 float(arg('h', 1)))
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
54 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
55 # 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
56 # 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
57 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
58 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
59 if scl1 < scl2:
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
60 # 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
61 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
62 else:
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
63 # height is the constraint
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
64 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
65 return rw, rh
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
66
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
67
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
68 @prof.logTime
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
69 def getFrame(c, arg):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
70 setCameraParams(c, arg)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
71 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
72 out = io.BytesIO('w')
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
73 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
74 return out.getvalue()
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
75
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
76
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
77 class Pic(cyclone.web.RequestHandler):
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
78
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
79 def get(self):
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
80 try:
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
81 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
82 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
83 except Exception:
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
84 traceback.print_exc()
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
85
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
86
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
87 def captureContinuousAsync(c, resize, onFrame):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
88 """
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
89 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
90 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
91 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
92 """
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
93
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
94 def runner(c, resize):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
95 stream = io.BytesIO()
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
96 t = time.time()
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
97 for nextFrame in c.capture_continuous(stream,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
98 'jpeg',
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
99 use_video_port=True,
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
100 resize=resize):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
101 t2 = time.time()
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
102 log.debug(" - framecap got %s bytes in %.1f ms",
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
103 len(stream.getvalue()), 1000 * (t2 - t))
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
104 try:
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
105 # 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
106 # 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
107 # 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
108 # 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
109 # 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
110 # possible (and toss if it gets behind).
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
111 threads.blockingCallFromThread(reactor, onFrame, t,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
112 stream.getvalue())
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
113 except StopIteration:
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
114 break
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
115 t3 = time.time()
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
116 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
117 stream.truncate()
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
118 stream.seek(0)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
119 t = time.time()
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
120
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
121 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
122
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
123
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
124 class FpsReport(object):
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
125
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
126 def __init__(self):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
127 self.frameTimes = []
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
128 self.lastFpsLog = 0
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
129
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
130 def frame(self):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
131 now = time.time()
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
132
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
133 self.frameTimes.append(now)
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
134
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
135 if len(self.frameTimes) > 15:
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
136 del self.frameTimes[:5]
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
137
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
138 if now > self.lastFpsLog + 2 and len(self.frameTimes) > 5:
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
139 deltas = [(b - a)
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
140 for a, b in zip(self.frameTimes[:-1], self.frameTimes[1:])
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
141 ]
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
142 avg = sum(deltas) / len(deltas)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
143 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
144 self.lastFpsLog = now
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
145
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
146
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
147 class Pics(cyclone.web.RequestHandler):
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
148
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
149 @inlineCallbacks
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
150 def get(self):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
151 try:
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
152 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
153 c = self.settings.camera
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
154 setCameraParams(c, self.get_argument)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
155 resize = setupCrop(c, self.get_argument)
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
156
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
157 self.running = True
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
158 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
159 fpsReport = FpsReport()
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
160
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
161 def onFrame(frameTime, frame):
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
162 if not self.running:
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
163 raise StopIteration
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
164
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
165 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
166 self.write(frame)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
167 self.flush()
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
168
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
169 fpsReport.frame()
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
170
1091
50a68abd2b0e comment
Drew Perttula <drewp@bigasterisk.com>
parents: 1089
diff changeset
171 # another camera request coming in at the same time breaks
50a68abd2b0e comment
Drew Perttula <drewp@bigasterisk.com>
parents: 1089
diff changeset
172 # the server. it would be nice if this request could
50a68abd2b0e comment
Drew Perttula <drewp@bigasterisk.com>
parents: 1089
diff changeset
173 # let-go-and-reopen when it knows about another request
50a68abd2b0e comment
Drew Perttula <drewp@bigasterisk.com>
parents: 1089
diff changeset
174 # coming in
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
175 yield captureContinuousAsync(c, resize, onFrame)
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
176 except Exception:
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
177 traceback.print_exc()
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
178
1088
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
179 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
180 log.info("connection closed")
bb92c50438ed picamserve now has /pics that streams jpegs at 10fps
Drew Perttula <drewp@bigasterisk.com>
parents: 1087
diff changeset
181 self.running = False
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
182
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
183
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
184 log.setLevel(logging.INFO)
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
185
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
186 with cameraCls() as camera:
1243
73efcda34af2 picamserve port
drewp@bigasterisk.com
parents: 1212
diff changeset
187 port = 8208
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
188 reactor.listenTCP(
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
189 port,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
190 cyclone.web.Application(handlers=[
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
191 (r'/pic', Pic),
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
192 (r'/pics', Pics),
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
193 (r'/static/(.*)', cyclone.web.StaticFileHandler, {
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
194 'path': 'light9/web/'
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
195 }),
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
196 (r'/(|gui.js)', cyclone.web.StaticFileHandler, {
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
197 'path': 'light9/vidref/',
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
198 'default_filename': 'index.html'
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
199 }),
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
200 ],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
201 debug=True,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1243
diff changeset
202 camera=camera))
1087
1f877950ad28 new picamserve for raspberry pi camera -> http, especially with crop control
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
203 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
204 reactor.run()