import os import subprocess import shutil import signal import tempfile import time try: from pyA20.gpio import gpio, port except ImportError: from unittest.mock import Mock gpio = Mock() class Tmpobj(): pass port = Tmpobj() port.PG0 = 42 from bureau import Bureau, add_api class Photography(Bureau): """ The Photography dept. provides document camera and other image capture services for other bureaus. """ name = "Photography Dept." prefix = "PX" version = 0 doc_light_pin = port.PG0 def __init__(self): Bureau.__init__(self) # set up the gpios for lamp & knobs gpio.init() gpio.setcfg(self.doc_light_pin, gpio.OUTPUT) gpio.output(self.doc_light_pin, gpio.LOW) def _doc_light_on(self): gpio.output(self.doc_light_pin, gpio.HIGH) def _doc_light_off(self): gpio.output(self.doc_light_pin, gpio.LOW) @add_api("photo", "Get Document Photo") def photo(self): """ Takes a photograph using the document camera. Returns the file name. """ self._doc_light_on() tmpimg = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) tmpimg.close() # TODO: make more reliable # TODO: make resolution config variable # this is a dirty hack to keep the webcam system calls from hanging #cmd1 = "fswebcam --jpeg 95 --no-banner --resolution 320x240 /dev/null" #cmd2 = "fswebcam --jpeg 95 --no-banner --resolution 1920x1080 " #cmd2 += "-F 2 -S 1" + tmpimg.name #cmd1 = "uvccapture -d/dev/video1 -x320 -y240 -o /dev/null" #cmd2 = "/usr/local/bin/uvccapture -m -x3264 -y2448 -o" + tmpimg.name #subprocess.check_output(cmd1.split()) modpath = os.path.dirname(__file__) mjpg = os.path.join(modpath, "..", "..", "lib", "mjpg-streamer", "mjpg-streamer-experimental", "run.sh") mjpg_dir = os.path.dirname(mjpg) cmd = mjpg.split() + ["-i", "input_uvc.so -r 3264x2448 -n", "-o", "output_file.so -f /tmp/webcam -d 500 -s 2"] print(cmd) #proc = subprocess.Popen(cmd, shell=True, env={"LD_LIBRARY_PATH": mjpg_dir}) proc = subprocess.Popen(cmd) time.sleep(2) proc.kill() time.sleep(1) # copy last image to the tmpfile filelist = os.listdir("/tmp/webcam") newest = max(filelist, key=lambda x: os.stat(x).st_mtime) shutil.copyfile(newest, tmpimg.name) self._doc_light_off() return {"photo": tmpimg.name} def main(): px = Photography() px.run() if __name__ == "__main__": main()