From 9b9061f8dbbfef4696226a43285109e3a63146b7 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Fri, 24 Dec 2010 19:35:01 +0000 Subject: adding luminance block; checking for camera before creating palette entries --- (limited to 'TurtleArt') diff --git a/TurtleArt/audiograb.py b/TurtleArt/audiograb.py index 0335813..3ecdc11 100644..100755 --- a/TurtleArt/audiograb.py +++ b/TurtleArt/audiograb.py @@ -22,7 +22,6 @@ # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. import pygst -pygst.require("0.10") import gst import gst.interfaces from numpy import fromstring diff --git a/TurtleArt/taconstants.py b/TurtleArt/taconstants.py index 4f45663..77ebefb 100755 --- a/TurtleArt/taconstants.py +++ b/TurtleArt/taconstants.py @@ -136,7 +136,7 @@ PALETTES = [['clean', 'forward', 'back', 'show', 'left', 'right', 'myfunc1arg', 'userdefined', 'cartesian', 'width', 'height', 'polar', 'addturtle', 'reskin', 'sandwichtop_no_label', 'sandwichbottom'], - ['kbinput', 'keyboard', 'readpixel', 'see', 'readcamera', 'camera', + ['kbinput', 'keyboard', 'readpixel', 'see', 'sound', 'volume', 'pitch'], ['journal', 'audio', 'video', 'description', 'hideblocks', 'showblocks', 'fullscreen', 'savepix', 'savesvg', 'mediawait', @@ -222,7 +222,8 @@ BOX_STYLE = ['number', 'xcor', 'ycor', 'heading', 'pensize', 'color', 'shade', 'toppos', 'rightpos', 'bottompos', 'width', 'height', 'pop', 'keyboard', 'red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple', 'white', 'black', 'titlex', 'titley', 'leftx', 'topy', 'rightx', 'bottomy', - 'sound', 'volume', 'pitch', 'voltage', 'resistance', 'gray', 'see', 'rfid'] + 'sound', 'volume', 'pitch', 'voltage', 'resistance', 'gray', 'see', 'rfid', + 'luminance'] BOX_STYLE_MEDIA = ['description', 'audio', 'journal', 'video', 'camera'] NUMBER_STYLE = ['plus2', 'product2', 'myfunc'] NUMBER_STYLE_VAR_ARG = ['myfunc1arg', 'myfunc2arg', 'myfunc3arg'] @@ -353,6 +354,7 @@ BLOCK_NAMES = { 'leftx': [_('picture left')], 'less2': ['<'], 'list': ['list'], + 'luminance': [_('brightness')], 'mediawait': [_('media wait')], 'minus2': ['–'], 'myfunc': [_('Python'), 'f(x)', 'x'], @@ -518,6 +520,7 @@ PRIMITIVES = { 'leftx': 'leftx', 'less2': 'less?', 'list': 'bulletlist', + 'luminance': 'luminance', 'mediawait': 'mediawait', 'minus2': 'minus', 'myfunc': 'myfunction', @@ -834,6 +837,7 @@ HELP_STRINGS = { 'leftpos': _("xcor of left of screen"), 'left': _("turns turtle counterclockwise (angle in degrees)"), 'less2': _("logical less-than operator"), + 'luminance': _("light level detected by camera"), 'mediawait': _("wait for current video or audio to complete"), 'minus2': _("subtracts bottom numeric input from top numeric input"), 'myfunc': _("a programmable block: used to add advanced math equations, e.g., sin(x)"), diff --git a/TurtleArt/tagplay.py b/TurtleArt/tagplay.py index 4be7f5c..dbce7ed 100644..100755 --- a/TurtleArt/tagplay.py +++ b/TurtleArt/tagplay.py @@ -33,7 +33,6 @@ import gobject gobject.threads_init() import pygst -pygst.require('0.10') import gst import gst.interfaces import gtk diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index 2da8685..37809b8 100755 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -375,6 +375,7 @@ class LogoCode: 'leftx': [0, lambda self: CONSTANTS['leftx']], 'lpos': [0, lambda self: CONSTANTS['leftpos']], 'less?': [2, lambda self, x, y: _less(x, y)], + 'luminance': [0, lambda self: self._read_camera(True)], 'mediawait': [0, self._media_wait, True], 'minus': [2, lambda self, x, y: _minus(x, y)], 'mod': [2, lambda self, x, y: _mod(x, y)], @@ -1322,8 +1323,9 @@ class LogoCode: self.filepath = None dsobject = None if string[6:] == 'CAMERA': - save_camera_input_to_file(self.imagepath) - self.filepath = self.imagepath + if self.tw.camera: + save_camera_input_to_file(self.imagepath) + self.filepath = self.imagepath elif os.path.exists(string[6:]): # is it a path? self.filepath = string[6:] elif self.tw.running_sugar: # is it a datastore object? @@ -1461,23 +1463,25 @@ class LogoCode: self.heap.append(g) self.heap.append(r) - def _read_camera(self): + def _read_camera(self, luminance_only=False): """ Read average pixel from camera and push b, g, r to the stack """ - save_camera_input_to_file(self.imagepath) pixbuf = None + array = None w = self._w() h = self._h() - if w < 1 or h < 1: - return - pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(self.imagepath, w, h) - - array = pixbuf.get_pixels() - length = len(array) / 3 - r = 0 - g = 0 - b = 0 - i = 0 + if w > 0 and h > 0 and self.tw.camera: + save_camera_input_to_file(self.imagepath) + 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 = 0 + g = 0 + b = 0 + i = 0 for j in range(length): r += ord(array[i]) i += 1 @@ -1485,9 +1489,19 @@ class LogoCode: i += 1 b += ord(array[i]) i += 1 - self.heap.append(int((b / length))) - self.heap.append(int((g / length))) - self.heap.append(int((r / length))) + if luminance_only: + return int((r * 0.3 + g * 0.6 + b * 0.1) / length) + else: + self.heap.append(int((b / length))) + self.heap.append(int((g / length))) + self.heap.append(int((r / length))) + else: + if luminance_only: + return -1 + else: + self.heap.append(-1) + self.heap.append(-1) + self.heap.append(-1) def _get_volume(self): """ return mic in value """ diff --git a/TurtleArt/tawindow.py b/TurtleArt/tawindow.py index ceb9a69..45c5282 100755 --- a/TurtleArt/tawindow.py +++ b/TurtleArt/tawindow.py @@ -25,6 +25,7 @@ import pygtk pygtk.require('2.0') import gtk import gobject +import gst import os import os.path import dbus @@ -262,6 +263,14 @@ class TurtleArtWindow(): PALETTES[PALETTE_NAMES.index('sensor')].append('voltage') self.audio_started = False + self.camera = 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 + """ The following code will initialize a USB RFID reader. Please note that in order to make this initialization function work, it is necessary to -- cgit v0.9.1