From 17b5303975703d82a6b02966f017b5b882e1a2c8 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Fri, 18 Feb 2011 13:14:52 +0000 Subject: adding new plugin for camera --- (limited to 'plugins') diff --git a/plugins/__init__.py b/plugins/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/plugins/__init__.py diff --git a/plugins/__init__.pyc b/plugins/__init__.pyc new file mode 100644 index 0000000..ce06608 --- /dev/null +++ b/plugins/__init__.pyc Binary files differ diff --git a/plugins/camera_plugin.py b/plugins/camera_plugin.py new file mode 100644 index 0000000..ff47e74 --- /dev/null +++ b/plugins/camera_plugin.py @@ -0,0 +1,161 @@ +#!/usr/bin/env python +#Copyright (c) 2011 Walter Bender + +#Permission is hereby granted, free of charge, to any person obtaining a copy +#of this software and associated documentation files (the "Software"), to deal +#in the Software without restriction, including without limitation the rights +#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +#copies of the Software, and to permit persons to whom the Software is +#furnished to do so, subject to the following conditions: + +#The above copyright notice and this permission notice shall be included in +#all copies or substantial portions of the Software. + +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +#THE SOFTWARE. + +import gst +import gtk +from fcntl import ioctl + +from gettext import gettext as _ + +from camera.tacamera import Camera +from camera.v4l2 import v4l2_control, V4L2_CID_AUTOGAIN, VIDIOC_G_CTRL, \ + VIDIOC_S_CTRL + +from plugin import Plugin +from TurtleArt.taconstants import PALETTES, PALETTE_NAMES, BOX_STYLE_MEDIA, \ + CONTENT_BLOCKS, BLOCK_NAMES, DEFAULTS, SPECIAL_NAMES, HELP_STRINGS, \ + BOX_STYLE +from TurtleArt.talogo import VALUE_BLOCKS, MEDIA_BLOCKS_DICTIONARY, \ + PLUGIN_DICTIONARY + +import logging +_logger = logging.getLogger('turtleart-activity camera plugin') + + +class Camera_plugin(Plugin): + + def __init__(self, parent): + self._parent = parent + self._status = False + + v4l2src = gst.element_factory_make('v4l2src') + if v4l2src.props.device_name is not None: + + if self._parent.running_sugar: + self._imagepath = get_path(self._parent.activity, + 'data/turtlepic.png') + else: + self._imagepath = '/tmp/turtlepic.png' + self._camera = Camera(self._imagepath) + + self._status = True + + def setup(self): + # set up camera-specific blocks + if self._self_test(): + PALETTES[PALETTE_NAMES.index('sensor')].append('luminance') + BOX_STYLE.append('luminance') + BLOCK_NAMES['luminance'] = [_('brightness')] + HELP_STRINGS['luminance'] = _("light level detected by camera") + VALUE_BLOCKS.append('luminance') + PLUGIN_DICTIONARY['luminance'] = self.prim_read_camera + self._parent.lc._def_prim('luminance', 0, + lambda self: PLUGIN_DICTIONARY['luminance'](True)) + + PALETTES[PALETTE_NAMES.index('sensor')].append('camera') + BOX_STYLE_MEDIA.append('camera') + CONTENT_BLOCKS.append('camera') + BLOCK_NAMES['camera'] = [' '] + DEFAULTS['camera'] = ['CAMERA'] + SPECIAL_NAMES['camera'] = _('camera') + HELP_STRINGS['camera'] = _('camera output') + MEDIA_BLOCKS_DICTIONARY['camera'] = self.prim_take_picture + + def stop(self): + # Ths gets called by the stop button + if self._self_test(): + self._camera.stop_camera_input() + + def _self_test(self): + print 'reporting Camera status %s' % (str(self._status)) + return self._status + + # Block primitives used in talogo + + def prim_take_picture(self): + if self._self_test(): + ''' method called by media block ''' + self._camera.save_camera_input_to_file() + self._camera.stop_camera_input() + self._parent.lc.filepath = self._imagepath + + def prim_read_camera(self, luminance_only=False): + """ Read average pixel from camera and push b, g, r to the stack """ + pixbuf = None + array = None + w, h = self._parent.lc._w(), self._parent.lc._h() + if w > 0 and h > 0 and self._self_test(): + try: + self._video_capture_device = open('/dev/video0', 'rw') + except: + self._video_capture_device = None + _logger.debug('video capture device not available') + + if self._video_capture_device is not None: + self._ag_control = v4l2_control(V4L2_CID_AUTOGAIN) + try: + ioctl(self._video_capture_device, VIDIOC_G_CTRL, + self._ag_control) + self._ag_control.value = 0 # disable AUTOGAIN + ioctl(self._video_capture_device, VIDIOC_S_CTRL, + self._ag_control) + except: + _logger.debug('AUTOGAIN control not available') + pass + + if self._video_capture_device is not None: + self._video_capture_device.close() + + 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() + except: + array = None + + if array is not None: + length = len(array) / 3 + r, g, b, i = 0, 0, 0, 0 + for j in range(length): + r += ord(array[i]) + i += 1 + g += ord(array[i]) + i += 1 + b += ord(array[i]) + i += 1 + if luminance_only: + lum = int((r * 0.3 + g * 0.6 + b * 0.1) / length) + self._parent.lc.update_label_value('luminance', lum) + return lum + else: + self._parent.lc.heap.append(int((b / length))) + self._parent.lc.heap.append(int((g / length))) + self._parent.lc.heap.append(int((r / length))) + else: + if luminance_only: + return -1 + else: + self._parent.lc.heap.append(-1) + self._parent.lc.heap.append(-1) + self._parent.lc.heap.append(-1) + + diff --git a/plugins/camera_plugin.pyc b/plugins/camera_plugin.pyc new file mode 100644 index 0000000..a557a12 --- /dev/null +++ b/plugins/camera_plugin.pyc Binary files differ diff --git a/plugins/camera_plugin.py~ b/plugins/camera_plugin.py~ new file mode 100644 index 0000000..6a8d223 --- /dev/null +++ b/plugins/camera_plugin.py~ @@ -0,0 +1,146 @@ +#!/usr/bin/env python +#Copyright (c) 2011 Walter Bender + +#Permission is hereby granted, free of charge, to any person obtaining a copy +#of this software and associated documentation files (the "Software"), to deal +#in the Software without restriction, including without limitation the rights +#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +#copies of the Software, and to permit persons to whom the Software is +#furnished to do so, subject to the following conditions: + +#The above copyright notice and this permission notice shall be included in +#all copies or substantial portions of the Software. + +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +#THE SOFTWARE. + +import gst +import gtk +from camera.tacamera import Camera + +from plugin import Plugin +from TurtleArt.taconstants import PALETTES, PALETTE_NAMES +from TurtleArt.talogo import VALUE_BLOCKS, MEDIA_BLOCKS_DICTIONARY + +import logging +_logger = logging.getLogger('turtleart-activity camera plugin') + + +class Camera_plugin(Plugin): + + def __init__(self, parent): + self._parent = parent + self._status = False + + v4l2src = gst.element_factory_make('v4l2src') + if v4l2src.props.device_name is not None: + + if self._parent.running_sugar: + self._imagepath = get_path(self._parent.activity, + 'data/turtlepic.png') + else: + self._imagepath = '/tmp/turtlepic.png' + self._camera = Camera(self._imagepath) + + self._status = True + + def self_test(self): + print 'reporting Camera status %s' % (str(self._status)) + return self._status + + def setup(self): + if self.self_test(): + PALETTES[PALETTE_NAMES.index('sensor')].append('readcamera') + PALETTES[PALETTE_NAMES.index('sensor')].append('luminance') + PALETTES[PALETTE_NAMES.index('sensor')].append('camera') + + # TODO: define these blocks in taconstants.py + + VALUE_BLOCKS.append('luminance') + MEDIA_BLOCKS_DICTIONARY['camera'] = self.take_picture + + self._parent.lc._def_prim('luminance', 0, + lambda self: self.read_camera(True)) + self._parent.lc._def_prim('readcamera', 0, + lambda self: self.read_camera()) + + def stop(self): + if self._self_test(): + self._camera.stop_camera_input() + + ### + + def take_picture(self): + if self.self_test(): + ''' method called by media block ''' + self._camera.save_camera_input_to_file() + self._camera.stop_camera_input() + self._parent.lc.filepath = self._imagepath + + def read_camera(self, luminance_only=False): + """ Read average pixel from camera and push b, g, r to the stack """ + pixbuf = None + array = None + w, h = self._parent.lc._w(), self._parent.lc._h() + if w > 0 and h > 0 and self.self_test(): + try: + self._video_capture_device = open('/dev/video0', 'rw') + except: + self._video_capture_device = None + _logger.debug('video capture device not available') + + if self._video_capture_device is not None: + self._ag_control = v4l2.v4l2_control(v4l2.V4L2_CID_AUTOGAIN) + try: + ioctl(self._video_capture_device, v4l2.VIDIOC_G_CTRL, + self._ag_control) + self._ag_control.value = 0 # disable AUTOGAIN + ioctl(self._video_capture_device, v4l2.VIDIOC_S_CTRL, + self._ag_control) + except: + _logger.debug('AUTOGAIN control not available') + pass + + if self._video_capture_device is not None: + self._video_capture_device.close() + + 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() + except: + array = None + + if array is not None: + length = len(array) / 3 + r, g, b, i = 0, 0, 0, 0 + for j in range(length): + r += ord(array[i]) + i += 1 + g += ord(array[i]) + i += 1 + b += ord(array[i]) + i += 1 + if luminance_only: + lum = int((r * 0.3 + g * 0.6 + b * 0.1) / length) + self._parent.lc.update_label_value('luminance', lum) + return lum + else: + self._parent.lc.heap.append(int((b / length))) + self._parent.lc.heap.append(int((g / length))) + self._parent.lc.heap.append(int((r / length))) + else: + if luminance_only: + return -1 + else: + self._parent.lc.heap.append(-1) + self._parent.lc.heap.append(-1) + self._parent.lc.heap.append(-1) + + diff --git a/plugins/plugin.py b/plugins/plugin.py new file mode 100644 index 0000000..366c1bc --- /dev/null +++ b/plugins/plugin.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +#Copyright (c) 2011 Walter Bender +#Copyright (c) 2011 Collabora Ltd. + +#Permission is hereby granted, free of charge, to any person obtaining a copy +#of this software and associated documentation files (the "Software"), to deal +#in the Software without restriction, including without limitation the rights +#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +#copies of the Software, and to permit persons to whom the Software is +#furnished to do so, subject to the following conditions: + +#The above copyright notice and this permission notice shall be included in +#all copies or substantial portions of the Software. + +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +#THE SOFTWARE. + +import gobject + + +class Plugin(gobject.GObject): + def __init__(self): + gobject.GObject.__init__(self) + + def setup(self): + raise RuntimeError("You need to define setup for your plugin.") + + def stop(self): + raise RuntimeError("You need to define stop for your plugin.") diff --git a/plugins/plugin.pyc b/plugins/plugin.pyc new file mode 100644 index 0000000..cf6e6fd --- /dev/null +++ b/plugins/plugin.pyc Binary files differ diff --git a/plugins/plugin.py~ b/plugins/plugin.py~ new file mode 100644 index 0000000..165e341 --- /dev/null +++ b/plugins/plugin.py~ @@ -0,0 +1,34 @@ +#!/usr/bin/env python +#Copyright (c) 2011 Walter Bender +#Copyright (c) 2011 Collabora Ltd. + +#Permission is hereby granted, free of charge, to any person obtaining a copy +#of this software and associated documentation files (the "Software"), to deal +#in the Software without restriction, including without limitation the rights +#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +#copies of the Software, and to permit persons to whom the Software is +#furnished to do so, subject to the following conditions: + +#The above copyright notice and this permission notice shall be included in +#all copies or substantial portions of the Software. + +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +#THE SOFTWARE. + +import gobject + + +class Plugin(gobject.GObject): + def __init__(self): + gobject.GObject.__init__(self) + + def self_test(self): + raise RuntimeError("You need to define self_test for your plugin.") + + def stop(self): + raise RuntimeError("You need to define stop for your plugin.") -- cgit v0.9.1