Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Getting you shoes into motion

Source: Shoes • The Tutorial Walkthrough [shoooes.net]

The black star will follow the users' mouse movement.

   1  
   2  Shoes.app do
   3    @shape = star :points => 5
   4    motion do |left, top|
   5      @shape.move left, top
   6    end
   7  end

Like a Touch screen in the air... Like a mouse with a pencil.

I had create a motion detection that find a black pixel in the square, and centralize on it.
Its very simple but very fun!

References
http://www.danilocesar.com/blog/2006/12/03/smartphones-aonde-podemos-parar

If you want to see this working:
http://www.youtube.com/watch?v=sjm_g9MSYPc


   1  
   2  
   3  #################################################################
   4  # Developed by Danilo Cesar [http://www.danilocesar.com]
   5  # Inspired on:  http://www.bigbold.com/snippets/posts/show/636
   6  #################################################################
   7  
   8  from appuifw import *
   9  from graphics import Image
  10  import camera, e32
  11  
  12  app.body = c = Canvas()
  13  
  14  running = 1
  15  def quit():
  16      global running
  17      running = 0
  18  
  19  app.exit_key_handler=quit
  20  app.title = u"O controle"
  21  app.screen = 'full'   # or 'normal', 'large'
  22  
  23  def getdata(im, bpp=24):
  24      import struct, zlib
  25      im.save('D:\\pixels.png', bpp=bpp, compression='no')
  26      f = open('D:\\pixels.png', 'rb')
  27      f.seek(8 +8+13+4)
  28      chunk = []
  29      while 1:
  30          n = struct.unpack('>L', f.read(4))[0]
  31          if n==0: break  # 'IEND' chunk
  32          f.read(4) # 'IDAT'
  33          chunk.append(f.read(n))
  34          f.read(4)   # CRC
  35      f.close()
  36      return zlib.decompress(''.join(chunk))  # '\x00' prefix each line
  37  
  38  
  39  X = 80
  40  Y = 60
  41  while running:
  42      if X < 0: X = 0
  43      if Y< 0: Y = 0
  44      if X > 160 - 30: X = 160 - 30
  45      if Y > 120 - 30: Y = 120-30
  46      im = camera.take_photo('RGB', (160,120))
  47      im.rectangle([(X,Y),(X+30,Y+30)], 0xff0000)   # red outline
  48      # check hot spot whether active
  49      box = Image.new((30,30), 'L')  # gray scale
  50      box.blit(im, (X,Y,X+30,Y+30))
  51      data = getdata(box, 8)
  52  
  53      # check black
  54      for i in range(len(data)):
  55          if ord(data[i]) < 30 and ord(data[i]) > 0:
  56              X += i%31 - 15
  57              Y += int(i/31) - 15
  58              break
  59  
  60      c.blit(im, (0,0), (8,12))   # show camera
  61   
  62  
  63  

Javascript Motion tween

// description of your code here
// Motion tween

   1  
   2  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   3          "http://www.w3.org/TR/html4/loose.dtd">
   4  <html>
   5  <head>
   6  
   7    <script type="text/javascript">
   8        var pos=0;
   9  
  10          function move(){
  11              pos = 0;
  12              changeInnerHtml();
  13              setTimeout('move()',1000);
  14              pos = 0 ;
  15  
  16          }
  17  
  18          function changeInnerHtml()
  19          {
  20  
  21                pos = pos +5;
  22                document.getElementById('spid').style.visibility = 'hidden';
  23  //              document.getElementById('div1').style.visibility = 'hidden';
  24  
  25                if( pos <= 200 )
  26                {
  27  
  28                    document.getElementById('b1').style.left = pos + 'px';
  29                    setTimeout("changeInnerHtml()",10);
  30  
  31                }
  32  
  33                
  34             }
  35            
  36  
  37                      
  38  //            document.getElementById('div1').innerHTML = '';
  39  //            document.getElementById('div1').innerHTML = '<input type = "text" name = "hi" />' ;                      }
  40         
  41  
  42    </script>
  43  
  44    <style type="text/css">
  45        .divstyle
  46        {
  47  
  48           width:50px;
  49           height:50px;
  50           position:relative;
  51           left:100px;
  52           border:1px;
  53           visibility:visible;
  54  
  55            
  56        }
  57        
  58    </style>
  59    <title></title>
  60  </head>
  61  <body>
  62  
  63      <div id="div1">
  64          <span id="spid" class="divstyle">
  65              hi
  66          </span>
  67          <input type="submit" id="b1" name="b1" onclick="move()" class="divstyle"/>
  68          <iframe id="iframe1" class="div1" >
  69              
  70          </iframe>
  71      </div>
  72  
  73  </body>
  74  </html>

Motion detection as input

Inspired from the PIL version here
http://gumuz.looze.net/wordpress/index.php/archives/2005/06/06/python-webcam-fun-motion-detection/

First with typical import and Canvas, exit setup
   1  
   2  from appuifw import *
   3  from graphics import Image
   4  import camera, e32
   5  #import miso    # don't dim the light
   6  
   7  app.body = c = Canvas()
   8  
   9  running = 1
  10  def quit():
  11      global running
  12      running = 0
  13  app.exit_key_handler=quit

Then the getdata function that reads pixel data
from image by saving/reading PNG.
   1  
   2  def getdata(im, bpp=24):
   3      import struct, zlib
   4      im.save('D:\\pixels.png', bpp=bpp, compression='no')
   5      f = open('D:\\pixels.png', 'rb')
   6      f.seek(8 +8+13+4)
   7      chunk = []
   8      while 1:
   9          n = struct.unpack('>L', f.read(4))[0]
  10          if n==0: break  # 'IEND' chunk
  11          f.read(4) # 'IDAT'
  12          chunk.append(f.read(n))
  13          f.read(4)   # CRC
  14      f.close()
  15      return zlib.decompress(''.join(chunk))  # '\x00' prefix each line

Lastly, the real code follows.
   1  
   2  last1 = '\x00' * 930    # can be anything
   3  while running:
   4      im = camera.take_photo('RGB', (160,120))
   5      im.rectangle([(10,10),(40,40)], 0xff0000)   # red outline
   6      im.rectangle([(120,10),(150,40)], 0xff0000) # no code for this square
   7      # check hot spot whether active
   8      box = Image.new((30,30), 'L')  # gray scale
   9      box.blit(im, (10,10,40,40))
  10      data = getdata(box, 8)
  11      # check difference for motion
  12      pixdiff = 0
  13      for i in range(len(data)):
  14          if abs(ord(data[i])-ord(last1[i])) > 15:  # pix threshold 15/256
  15              pixdiff += 1
  16              if pixdiff > 90:    # img threshold 90/900
  17                  im.rectangle([(10,10),(40,40)], fill=0xff0000)  # fill
  18                  break           # motion detected
  19      last1 = data
  20      c.blit(im, (0,0), (8,12))   # show camera
  21      #miso.reset_inactivity_time()

When measured, it takes around 1.1 sec for each loop.
Compared this with 0.9 sec without image processing,
out code is quite efficient.

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS