Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/plugins/camera_sensor/camera_sensor.py
blob: 6509a88980fde8068262ba9db389e05a432ba504 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python
#Copyright (c) 2011, 2012 Walter Bender
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import gst
import gtk
from fcntl import ioctl
import os
from time import time

from gettext import gettext as _

from plugins.camera_sensor.tacamera import Camera
from plugins.camera_sensor.v4l2 import v4l2_control, V4L2_CID_AUTOGAIN, \
    VIDIOC_G_CTRL, VIDIOC_S_CTRL

from plugins.plugin import Plugin

from TurtleArt.tapalette import make_palette
from TurtleArt.talogo import media_blocks_dictionary, primitive_dictionary
from TurtleArt.tautils import get_path, debug_output
from TurtleArt.taconstants import MEDIA_SHAPES, NO_IMPORT, SKIN_PATHS, \
    BLOCKS_WITH_SKIN


class Camera_sensor(Plugin):

    def __init__(self, parent):
        ''' Make sure there is a camera device '''
        self._parent = parent
        self._status = False
        self._ag_control = None
        self.devices = []
        self.cameras = []

        if os.path.exists('/dev/video0'):
            self.devices.append('/dev/video0')
        if os.path.exists('/dev/video1'):
            self.devices.append('/dev/video1')
        if len(self.devices) > 0:
            self._status = True
        else:
            self._status = False

    def setup(self):
        ''' Set up the palettes '''
        sensors_palette = make_palette('sensor',
                                       colors=["#FF6060", "#A06060"],
                                       help_string=_(
                'Palette of sensor blocks'),
                                       position=6)
        media_palette = make_palette('media',
                                     colors=["#A0FF00", "#80A000"],
                                     help_string=_('Palette of media objects'),
                                     position=7)

        # set up camera-specific blocks
        primitive_dictionary['read_camera'] = self.prim_read_camera
        media_blocks_dictionary['camera'] = self.prim_take_picture0
        media_blocks_dictionary['camera1'] = self.prim_take_picture1

        SKIN_PATHS.append('plugins/camera_sensor/images')

        if self._status:
            sensors_palette.add_block('luminance',
                                      style='box-style',
                                      label=_('brightness'),
                                      help_string=_(
                    'light level detected by camera'),
                                      value_block=True,
                                      prim_name='luminance')
            self._parent.lc.def_prim('luminance', 0,
                lambda self: primitive_dictionary['read_camera'](
                    luminance_only=True))

            # Depreciated block
            sensors_palette.add_block('read_camera',
                                      hidden=True,
                                      style='box-style',
                                      label=_('brightness'),
                                      help_string=_(
                    'Average RGB color from camera \
is pushed to the stack'),
                                      value_block=True,
                                      prim_name='read_camera')
            self._parent.lc.def_prim('read_camera', 0,
                lambda self: primitive_dictionary['read_camera']())

            media_palette.add_block('camera',
                                    style='box-style-media',
                                    label=' ',
                                    default='CAMERA',
                                    help_string=_('camera output'),
                                    content_block=True)
            if len(self.devices) > 1:
                media_palette.add_block('camera1',
                                        style='box-style-media',
                                        label=' ',
                                        default='CAMERA',
                                        help_string=_('camera output'),
                                        content_block=True)
            else:
                media_palette.add_block('camera1',
                                        hidden=True,
                                        style='box-style-media',
                                        label=' ',
                                        default='CAMERA',
                                        help_string=_('camera output'),
                                        content_block=True)

        else:  # No camera, so blocks should do nothing
            sensors_palette.add_block('luminance',
                                      hidden=True,
                                      style='box-style',
                                      label=_('brightness'),
                                      help_string=\
                                          _('light level detected by camera'),
                                      value_block=True,
                                      prim_name='read_camera')
            self._parent.lc.def_prim('luminance', 0,
                lambda self: primitive_dictionary['read_camera'](
                    luminance_only=True))

            # Depreciated block
            sensors_palette.add_block('read_camera',
                                      hidden=True,
                                      style='box-style',
                                      label=_('brightness'),
                                      help_string=_(
                    'Average RGB color from camera \
is pushed to the stack'),
                                      value_block=True,
                                      prim_name='read_camera')
            self._parent.lc.def_prim('read_camera', 0,
                lambda self: primitive_dictionary['read_camera']())

            media_palette.add_block('camera',
                                    hidden=True,
                                    style='box-style-media',
                                    label=' ',
                                    default='CAMERA',
                                    help_string=_('camera output'),
                                    content_block=True)

            media_palette.add_block('camera1',
                                    hidden=True,
                                    style='box-style-media',
                                    label=' ',
                                    default='CAMERA',
                                    help_string=_('camera output'),
                                    content_block=True)

        NO_IMPORT.append('camera')
        BLOCKS_WITH_SKIN.append('camera')
        NO_IMPORT.append('camera1')
        BLOCKS_WITH_SKIN.append('camera1')
        MEDIA_SHAPES.append('camerasmall')
        MEDIA_SHAPES.append('cameraoff')
        MEDIA_SHAPES.append('camera1small')
        MEDIA_SHAPES.append('camera1off')

    def start(self):
        ''' Initialize the camera if there is an camera block in use '''
        if len(self._parent.block_list.get_similar_blocks('block',
            ['camera', 'camera1', 'read_camera', 'luminance'])) > 0:
            if self._status and len(self.cameras) == 0:
                for device in self.devices:
                    self.cameras.append(Camera(device))

    def quit(self):
        ''' This gets called when the activity quits '''
        self._reset_the_camera()

    def stop(self):
        ''' This gets called by the stop button '''
        self._reset_the_camera()

    def clear(self):
        ''' This gets called by the clean button and erase button '''
        self._reset_the_camera()

    def _reset_the_camera(self):
        if self._status and len(self.cameras) > 0:
            for i, camera in enumerate(self.cameras):
                camera.stop_camera_input()
                self._set_autogain(1, camera=i)  # enable AUTOGAIN

    def _status_report(self):
        debug_output('Reporting camera status: %s' % (str(self._status)),
                     self._parent.running_sugar)
        return self._status

    # Block primitives used in talogo

    def prim_take_picture0(self):
        self._take_picture(camera=0)

    def prim_take_picture1(self):
        self._take_picture(camera=1)

    def _take_picture(self, camera=0):
        ''' method called by media block '''
        self._set_autogain(1, camera)  # enable AUTOGAIN
        self._get_pixbuf_from_camera(camera)
        self._parent.lc.pixbuf = self.cameras[camera].pixbuf

    def prim_read_camera(self, luminance_only=False, camera=0):
        """ Read average pixel from camera and push b, g, r to the stack """
        self.luminance_only = luminance_only
        if not self._status:
            if self.luminance_only:
                return -1
            else:
                self._parent.lc.heap.append(-1)
                self._parent.lc.heap.append(-1)
                self._parent.lc.heap.append(-1)
            return

        array = None
        self._set_autogain(0, camera=camera)  # disable AUTOGAIN
        self._get_pixbuf_from_camera(camera=camera)
        self.calc_luminance(camera=camera)
        if self.luminance_only:
            self._parent.lc.update_label_value('luminance', self.luminance)
            return self.luminance
        else:
            self._parent.lc.heap.append(self.b)
            self._parent.lc.heap.append(self.g)
            self._parent.lc.heap.append(self.r)

    def calc_luminance(self, camera=0):
        array = self.cameras[camera].pixbuf.get_pixels()
        width = self.cameras[camera].pixbuf.get_width()
        height = self.cameras[camera].pixbuf.get_height()

        if array is not None:
            length = int(len(array) / 3)
            if length != width * height:
                debug_output('array length != width x height (%d != %dx%d)' % \
                                 (length, width, height),
                             self._parent.running_sugar)

            # Average the 100 pixels in the center of the screen
            r, g, b = 0, 0, 0
            row_offset = int((height / 2 - 5) * width * 3)
            column_offset = int(width / 2 - 5) * 3
            for y in range(10):
                i = row_offset + column_offset
                for x in range(10):
                    r += ord(array[i])
                    i += 1
                    g += ord(array[i])
                    i += 1
                    b += ord(array[i])
                    i += 1
                row_offset += width * 3
            if self.luminance_only:
                self.luminance = int((r * 0.3 + g * 0.6 + b * 0.1) / 100)
            else:
                self.r = int(r / 100)
                self.g = int(g / 100)
                self.b = int(b / 100)
        else:
            if self.luminance_only:
                self.luminance = -1
            else:
                self.r = -1
                self.g = -1
                self.b = -1

    def _set_autogain(self, state, camera=0):
        ''' 0 is off; 1 is on '''
        if self._ag_control is not None and self._ag_control.value == state:
            return
        try:
            video_capture_device = open(self.devices[camera], 'rw')
        except:
            video_capture_device = None
            debug_output('video capture device not available',
                         self._parent.running_sugar)
            return
        self._ag_control = v4l2_control(V4L2_CID_AUTOGAIN)
        try:
            ioctl(video_capture_device, VIDIOC_G_CTRL, self._ag_control)
            self._ag_control.value = state
            ioctl(video_capture_device, VIDIOC_S_CTRL, self._ag_control)
        except:
            pass
        video_capture_device.close()

    def _get_pixbuf_from_camera(self, camera):
        ''' Regardless of how we get it, we want to return a pixbuf '''
        self._parent.lc.pixbuf = None
        if self._status:
            self.cameras[camera].start_camera_input()