From 2b2ac5c384ccf1294b13f4477aa3d5bb60945cfb Mon Sep 17 00:00:00 2001 From: Tomas Musil Date: Sat, 8 Sep 2012 13:37:31 +0200 Subject: [PATCH] timer + capture --- capture.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++---------------- timer.py | 20 ++++++++++++++++ 2 files changed, 81 insertions(+), 20 deletions(-) diff --git a/capture.py b/capture.py index 963f6ed..a8b6878 100755 --- a/capture.py +++ b/capture.py @@ -6,6 +6,8 @@ import os import sys import argparse import time +from threading import Thread +from Queue import Queue, Empty import pygame from pygame.locals import QUIT, KEYDOWN @@ -40,16 +42,25 @@ class Capture: def __del__(self): del self.cam - def live(self): + def live(self, q): done = False clock = pygame.time.Clock() while not done: #live preview - for event in pygame.event.get(): - if event.type == pygame.QUIT: - done = True - sys.exit() - if event.type == pygame.KEYDOWN: - done = True + if q: + try: + line = q.get_nowait() # or q.get(timeout=.1) + except Empty: + pass + else: + if line == "stop\n": + done = True + else: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + done = True + sys.exit() + elif event.type == pygame.KEYDOWN: + done = True im = self.cam.get_image() self.screen.display_picture(im) @@ -66,10 +77,7 @@ class Capture: done = True if time.time() - last > interval: last = time.time() - im = self.cam.get_image() - self.screen.display_picture(im) - im.save(self.saving_dir + "{0:0>3}.jpg".format(self.im_number), 'JPEG') - self.im_number += 1 + self.take() clock.tick(15) def manual(self): @@ -79,16 +87,20 @@ class Capture: break if event.type != KEYDOWN: continue + self.take() + - im = cam.get_image() - self.screen.display_picture(im) - im.save(self.saving_dir + "{0:0>3}.jpg".format(self.im_number), 'JPEG') - self.im_number += 1 - + def take(self): + im = self.cam.get_image() + self.screen.display_picture(im) + im.save(self.saving_dir + "{0:0>3}.jpg".format(self.im_number), 'JPEG') + self.im_number += 1 def main(): parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('-c', '--cmd', dest='cmd', action='store_true', + help="take commands from stdin") parser.add_argument('-d', type=int, default=0, help="video device id") parser.add_argument('-a', type=int, default=0, @@ -100,12 +112,41 @@ def main(): res=(args.r[0], args.r[1]) capture = Capture(args.d, res) - capture.live() - if args.a > 0: - capture.auto(args.a) + if args.cmd: + + def enqueue_input(queue): + for line in iter(sys.stdin.readline, b''): + queue.put(line) + + q = Queue() + t = Thread(target=enqueue_input, args=(q,)) + t.daemon = True + t.start() + + capture.live(q) + + clock = pygame.time.Clock() + while True: + + try: + line = q.get_nowait() # or q.get(timeout=.1) + except Empty: + pass + else: + if line == "take\n": + capture.take() + elif line == "exit\n": + break + clock.tick(10) + else: - capture.manual() + capture.live(None) + if args.a > 0: + capture.auto(args.a) + else: + capture.manual() + del capture if __name__ == '__main__': diff --git a/timer.py b/timer.py index 916d578..587b436 100755 --- a/timer.py +++ b/timer.py @@ -6,6 +6,7 @@ import os import sys import time import argparse +import subprocess import pygame @@ -45,6 +46,12 @@ class Timer: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('-m', type=int, default=45, help="main time in minutes") +parser.add_argument('-c', '--camera', dest='cam', action='store_true', + help="camera on") +parser.add_argument('-d', type=int, default=0, + help="video device id") +parser.add_argument('-r', type=int, nargs=2, default=[640, 480], + help="set camera resolution") args = parser.parse_args() @@ -64,6 +71,11 @@ main_time = args.m * 60 timers = (Timer(main_time), Timer(main_time)) +if args.cam: + capture = subprocess.Popen(['python', 'capture.py', '-c', '-d', str(args.d), '-r', + str(args.r[0]), + str(args.r[1])], stdin=subprocess.PIPE) + while not done: for event in pygame.event.get(): @@ -73,7 +85,12 @@ while not done: if first: timers[0].start() first = False + if args.cam: + print >> capture.stdin, "stop" + print >> capture.stdin, "take" continue + if args.cam: + print >> capture.stdin, "take" for timer in timers: if timer.is_running(): timer.stop() @@ -87,3 +104,6 @@ while not done: screen.blit(text2, [300, 10]) pygame.display.flip() clock.tick(15) + + +print >> capture.stdin, "exit" -- 2.4.2