diff --git a/bin/picamserve b/bin/picamserve new file mode 100644 --- /dev/null +++ b/bin/picamserve @@ -0,0 +1,71 @@ +#!env_pi/bin/python +from __future__ import division +from run_local import log +import sys;sys.path.append('/usr/lib/python2.7/dist-packages/') +import io, logging, traceback +import cyclone.web +from twisted.internet import reactor +from light9 import prof + +try: + import picamera + cameraCls = picamera.PiCamera +except ImportError: + class cameraCls(object): + def __enter__(self): return self + def __exit__(self): pass + def capture(self, out, *a, **kw): + out.write(open('yuv.demo').read()) + +@prof.logTime +def getFrame(c, arg): + res = int(arg('res', 480)) + c.resolution = { + 480: (640, 480), + 1080: (1920, 1080), + 1944: (2592, 1944), + }[res] + c.shutter_speed = int(arg('shutter', 50000)) + c.exposure_mode = arg('exposure_mode', 'fixedfps') + c.awb_mode = arg('awb_mode', 'off') + c.brightness = int(arg('brightness', 50)) + + c.awb_gains = (float(arg('redgain', 1)), float(arg('bluegain', 1))) + c.crop = (float(arg('x', 0)), float(arg('y', 0)), + float(arg('w', 1)), float(arg('h', 1))) + rw = rh = int(arg('resize', 100)) + # width 1920, showing w=.3 of image, resize=100 -> scale is 100/.3*1920 + # scl is [ output px / camera px ] + scl1 = rw / (c.crop[2] * c.resolution[0]) + scl2 = rh / (c.crop[3] * c.resolution[1]) + if scl1 < scl2: + # width is the constraint; reduce height to the same scale + rh = int(scl1 * c.crop[3] * c.resolution[1]) + else: + # height is the constraint + rw = int(scl2 * c.crop[2] * c.resolution[0]) + c.ISO = int(arg('iso', 250)) + out = io.BytesIO('w') + prof.logTime(c.capture)(out, 'jpeg', use_video_port=True, resize=(rw, rh)) + return out.getvalue() + +class Pic(cyclone.web.RequestHandler): + def get(self): + try: + self.set_header('Content-Type', 'image/jpeg') + self.write(getFrame(self.settings.camera, self.get_argument)) + except Exception: + traceback.print_exc() + +log.setLevel(logging.INFO) + +with cameraCls() as camera: + port = 8001 + reactor.listenTCP(port, cyclone.web.Application(handlers=[ + (r'/pic', Pic), + (r'/static/(.*)', cyclone.web.StaticFileHandler, {'path': 'static/'}), + (r'/(|gui.js)', cyclone.web.StaticFileHandler, {'path': 'light9/vidref/', + 'default_filename': 'index.html'}), + ], debug=True, camera=camera)) + log.info("serving on %s" % port) + reactor.run() diff --git a/bin/run_local.py b/bin/run_local.py --- a/bin/run_local.py +++ b/bin/run_local.py @@ -16,8 +16,12 @@ def rce(self, exc, val, tb): Failure(val, exc, tb).printDetailedTraceback() Tkinter.Tk.report_callback_exception = rce -import coloredlogs, logging, time, faulthandler -faulthandler.enable() +import coloredlogs, logging, time +try: + import faulthandler + faulthandler.enable() +except ImportError: + pass log = logging.getLogger() class CSH(coloredlogs.ColoredStreamHandler): diff --git a/light9/vidref/gui.js b/light9/vidref/gui.js new file mode 100644 --- /dev/null +++ b/light9/vidref/gui.js @@ -0,0 +1,11 @@ +var model = { + shutters: [], +}; +for (var s=0; s < 1; s+=.04) { + var micro = Math.floor(Math.pow(s, 3) * 100000) + if (micro == 0) { + continue; + } + model.shutters.push(micro); +} +ko.applyBindings(model) diff --git a/light9/vidref/index.html b/light9/vidref/index.html new file mode 100644 --- /dev/null +++ b/light9/vidref/index.html @@ -0,0 +1,37 @@ + + + + picamserve + + + + +
+ Whole view +
+ +
+
+
+ Shutters +
+
+
+ +
+
+
+ + + + diff --git a/makefile b/makefile --- a/makefile +++ b/makefile @@ -51,3 +51,11 @@ gst_packages: packages: sudo aptitude install coffeescript freemind normalize-audio audacity python-pygoocanvas python-pygame + +raspberry_pi_virtualenv: + mkdir -p env_pi + virtualenv --system-site-packages env_pi + +raspberry_pi_packages: + sudo apt-get install python-picamera python-twisted + env_pi/bin/pip install cyclone coloredlogs