From b9f147f3f72f0ce0eecf7e6598cad4b785aa4719 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Wed, 29 Dec 2010 17:03:42 +0000 Subject: refactored camera code to use poll for MESSAGE_EOS instead of timeout --- (limited to 'TurtleArt') diff --git a/TurtleArt/tacamera.py b/TurtleArt/tacamera.py index a4227ed..2177288 100644 --- a/TurtleArt/tacamera.py +++ b/TurtleArt/tacamera.py @@ -22,14 +22,21 @@ import gst, time -def save_camera_input_to_file(imagepath, pause=2.0): - """ Grab a frame from the video camera and save to a temporary file """ +GST_PIPE = ['v4l2src', 'ffmpegcolorspace', 'pngenc'] - pipeline = gst.parse_launch( - 'v4l2src ! ffmpegcolorspace ! jpegenc ! filesink location=' + imagepath) - pipeline.set_state(gst.STATE_PLAYING) +class Camera(): + """ A class for representing the video camera """ - # Need to pause for pipeline to stablize - time.sleep(pause) + def __init__(self, imagepath): + GST_PIPE.append('filesink location=%s' % imagepath) + self.pipe = gst.parse_launch('!'.join(GST_PIPE)) + self.bus = self.pipe.get_bus() + + def save_camera_input_to_file(self): + """ Grab a frame from the camera """ + self.pipe.set_state(gst.STATE_PLAYING) + self.bus.poll(gst.MESSAGE_EOS, -1) + + def stop_camera_input(self): + self.pipe.set_state(gst.STATE_NULL) - pipeline.set_state(gst.STATE_NULL) diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index 399b5b4..5c8a771 100755 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -44,7 +44,7 @@ from taconstants import TAB_LAYER, BLACK, WHITE, \ SENSOR_DC_BIAS, XO1, XO15 from tagplay import play_audio_from_file, play_movie_from_file, stop_media, \ media_playing -from tacamera import save_camera_input_to_file +from tacamera import Camera from tajail import myfunc, myfunc_import from tautils import get_pixbuf_from_journal, convert, data_from_file, \ text_media_type, round_int, chr_to_ord, strtype, get_path @@ -305,6 +305,8 @@ def stop_logo(tw): tw.step_time = 0 tw.lc.step = _just_stop() stop_media(tw.lc) + if tw.camera_available: + tw.lc.camera.stop_camera_input() tw.active_turtle.show() @@ -526,19 +528,17 @@ class LogoCode: if self.tw.hw == XO1: self.voltage_gain = 0.00002225 self.voltage_bias = 1.140 - self.camera_pause = 0.6 elif self.tw.hw == XO15: self.voltage_gain = -0.0001471 self.voltage_bias = 1.695 - self.camera_pause = 0.5 - else: - self.camera_pause = 2.0 - if self.tw.running_sugar: - self.imagepath = get_path(self.tw.activity, - 'data/turtlepic.jpg') - else: - self.imagepath = '/tmp/turtlepic.jpg' + if self.tw.camera_available: + if self.tw.running_sugar: + self.imagepath = get_path(self.tw.activity, + 'data/turtlepic.png') + else: + self.imagepath = '/tmp/turtlepic.png' + self.camera = Camera(self.imagepath) def _def_prim(self, name, args, fcn, rprim=False): """ Define the primitives associated with the blocks """ @@ -1327,9 +1327,9 @@ class LogoCode: self.filepath = None dsobject = None if string[6:] == 'CAMERA': - if self.tw.camera: - save_camera_input_to_file(self.imagepath, - self.camera_pause) + if self.tw.camera_available: + self.camera.save_camera_input_to_file() + self.camera.stop_camera_input() self.filepath = self.imagepath elif os.path.exists(string[6:]): # is it a path? self.filepath = string[6:] @@ -1476,8 +1476,9 @@ class LogoCode: array = None w = self._w() h = self._h() - if w > 0 and h > 0 and self.tw.camera: - save_camera_input_to_file(self.imagepath, self.camera_pause) + if w > 0 and h > 0 and self.tw.camera_available: + self.camera.save_camera_input_to_file() + self.camera.stop_camera_input() pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(self.imagepath, w, h) try: array = pixbuf.get_pixels() diff --git a/TurtleArt/tawindow.py b/TurtleArt/tawindow.py index a063a2d..ba5b5cf 100755 --- a/TurtleArt/tawindow.py +++ b/TurtleArt/tawindow.py @@ -249,11 +249,6 @@ class TurtleArtWindow(): CONSTANTS['width'] = int(self.canvas.width / self.coord_scale) CONSTANTS['height'] = int(self.canvas.height / self.coord_scale) - self.lc = LogoCode(self) - self.saved_pictures = [] - - self.block_operation = '' - if self.interactive_mode: self._setup_misc() self._show_toolbar_palette(0, False) @@ -264,13 +259,18 @@ class TurtleArtWindow(): PALETTES[PALETTE_NAMES.index('sensor')].append('voltage') self.audio_started = False - self.camera = False + self.camera_available = False v4l2src = gst.element_factory_make('v4l2src') if v4l2src.props.device_name is not None: PALETTES[PALETTE_NAMES.index('sensor')].append('readcamera') PALETTES[PALETTE_NAMES.index('sensor')].append('luminance') PALETTES[PALETTE_NAMES.index('sensor')].append('camera') - self.camera = True + self.camera_available = True + + self.lc = LogoCode(self) + self.saved_pictures = [] + + self.block_operation = '' """ The following code will initialize a USB RFID reader. Please note that -- cgit v0.9.1