5 This module defines a UI for capturing images with a (web)camera and imediately
6 proccessing them with Imago.
13 from threading import Thread
14 from Queue import Queue, Empty
17 from pygame.locals import QUIT, KEYDOWN
20 from camera import Camera
23 """Basic PyGame setup."""
24 def __init__(self, res):
26 pygame.display.set_mode(res)
27 pygame.display.set_caption("Go image capture")
28 self._screen = pygame.display.get_surface()
30 def display_picture(self, im):
31 """Display image on PyGame screen."""
32 pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode)
33 self._screen.blit(pg_img, (0,0))
37 """This object maintains communication between the camera, the PyGame screen
39 def __init__(self, device, res):
40 self.cam = Camera(vid=device, res=res)
41 self.screen = Screen(res)
45 self.saving_dir = "./captured/" + time.strftime("%Y-%m-%d %H:%M/")
47 if not os.path.isdir(self.saving_dir):
48 os.makedirs(self.saving_dir)
54 """Run live preview on the screen."""
55 clock = pygame.time.Clock()
59 line = q.get_nowait() # or q.get(timeout=.1)
65 elif line == "exit\n":
68 for event in pygame.event.get():
69 if event.type == pygame.QUIT:
71 elif event.type == pygame.KEYDOWN:
74 im = self.cam.get_image()
75 self.screen.display_picture(im)
78 def auto(self, interval):
79 """Take new image every *interval* seconds.""" #TODO or is it milisecs?
81 clock = pygame.time.Clock()
84 for event in pygame.event.get():
85 if event.type == pygame.QUIT:
87 if time.time() - last > interval:
93 """Take images manualy by pressing a key."""
95 event = pygame.event.wait()
96 if event.type == QUIT:
98 if event.type != KEYDOWN:
103 """Take a new image from the camera."""
104 im = self.cam.get_image()
105 self.screen.display_picture(im)
106 im.save(self.saving_dir + "{0:0>3}.jpg".format(self.im_number), 'JPEG')
111 """Parse the argument and setup the UI."""
112 parser = argparse.ArgumentParser(description=__doc__)
113 parser.add_argument('-c', '--cmd', dest='cmd', action='store_true',
114 help="take commands from stdin")
115 parser.add_argument('-d', type=int, default=0,
116 help="video device id")
117 parser.add_argument('-a', type=int, default=0,
118 help="take picture automaticaly every A seconds")
119 parser.add_argument('-r', type=int, nargs=2, default=[640, 480],
120 help="set camera resolution")
121 args = parser.parse_args()
123 res=(args.r[0], args.r[1])
124 capture = Capture(args.d, res)
128 def enqueue_input(queue):
129 for line in iter(sys.stdin.readline, b''):
133 t = Thread(target=enqueue_input, args=(q,))
139 clock = pygame.time.Clock()
143 line = q.get_nowait() # or q.get(timeout=.1)
149 elif line == "exit\n":
162 if __name__ == '__main__':