Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Issues with TargetRegion in frame differencing

$
0
0
Hi all

Im working with sound and spaces and trying to set an installation, based on webcam triggers. 

I was wondering if anyone can help. 

I have 5 targetRegions to trigger and play a file, however only one is actually being triggered. 

If anyone can point out where Im going wrong thats would be awesome. 

Many thanks

Rina

  1. import processing.video.*;
  2. // minim stuff
  3. import ddf.minim.*;

  4. Minim minim;
  5. AudioPlayer player;
  6. //


  7. // webcam stuff
  8. int numPixels;
  9. int[] previousFrame;
  10. Capture video;
  11. //

  12. // target regions specified as a rectangle x,y,width,height
  13. int[] targetRegion_1 = { 0,0, 50,480 };
  14. int[] targetRegion_2 = { 100,0, 50,480 };
  15. int [] targetRegion_3 = {200,0, 50, 480};
  16. int [] targetRegion_4 = {300,0, 50, 480};
  17. int [] targetRegion_5 = {400, 0, 50, 480};
  18. //int [] targetRegion_6 = {500, 0, 50, 480};

  19. // amount of delay before allowing trigger
  20. int totDelay = 5; // just using cycles, not time
  21. int delayCounter = 0;

  22. // to determine when sound has finished
  23. boolean isPlaying=false;

  24. String soundFileName = "spotsound1.mp3";


  25. // amount of change required before trigger active, not true percentage
  26. float thresholdPercentage = 2.0;

  27. void setup() {
  28.   size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
  29.   // Uses the default video input, see the reference if this causes an error
  30.   video = new Capture(this, width, height, 24);
  31.   numPixels = video.width * video.height;
  32.   // Create an array to store the previously captured frame
  33.   previousFrame = new int[numPixels];
  34.   loadPixels();
  35.   noFill();


  36. // minim stuff
  37.   minim = new Minim(this);
  38.   // load a file, give the AudioPlayer buffers that are 2048 samples long
  39.   player = minim.loadFile(soundFileName, 2046);
  40.   // play the file
  41. //  player.play(); // actually, not here, only start after motion


  42. }

  43. void draw() {
  44.   if (video.available()) 
  45.   {
  46.     // When using video to manipulate the screen, use video.available() and
  47.     // video.read() inside the draw() method so that it's safe to draw to the screen
  48.     video.read(); // Read the new frame from the camera
  49.     video.loadPixels(); // Make its pixels[] array available
  50.     
  51.     float movementAsPercentage_1 = checkMovement(targetRegion_1[0],targetRegion_1[1],targetRegion_1[2],targetRegion_1[3]);
  52.     float movementAsPercentage_2 = checkMovement(targetRegion_2[0],targetRegion_2[1],targetRegion_2[2],targetRegion_2[3]);
  53.     float movementAsPercentage_3 = checkMovement(targetRegion_3[0],targetRegion_3[1],targetRegion_3[2],targetRegion_3[3]);
  54.     float movementAsPercentage_4 = checkMovement(targetRegion_4[0],targetRegion_4[1],targetRegion_4[2],targetRegion_4[3]);
  55.     float movementAsPercentage_5 = checkMovement(targetRegion_5[0],targetRegion_5[1],targetRegion_5[2],targetRegion_5[3]);
  56.     //float movementAsPercentage 6 = checkMovement(targetRegion_6[0],targetRegion_6[1],targetRegion_6[2],targetRegion_6[3]);
  57.     
  58.     if(!player.isPlaying())
  59.     {
  60.       if(isPlaying) // so the sound has just finished
  61.       {
  62.         delayCounter=0; // start delay before allowing restart of playing sound
  63.         isPlaying=false;
  64.       }
  65.     }
  66.     
  67.     if(movementAsPercentage_1>0 || movementAsPercentage_2>0 || movementAsPercentage_3>0 || movementAsPercentage_4>0 || movementAsPercentage_5>0)
  68.     {
  69. //      println(movementAsPercentage); // for debugging...
  70.       updatePixels();
  71.     }

  72. //
  73. //  add one to delay counter if in delay period
  74.     if(delayCounter<totDelay)
  75.     {
  76.       delayCounter++;
  77.     }
  78.     
  79.     if(movementAsPercentage_1 > thresholdPercentage || movementAsPercentage_2 > thresholdPercentage || movementAsPercentage_3 > thresholdPercentage || movementAsPercentage_4 > thresholdPercentage || movementAsPercentage_5 > thresholdPercentage) // if movement detected
  80.     {
  81.       noFill();
  82.       stroke(255,0,0); // red box if in delay period
  83.  
  84.       // if not in the delay period
  85.       // check if reached delay before allowing trigger
  86.       if(delayCounter==totDelay)
  87.       {
  88.         stroke(255,255,0); // yellow box=triggered but already playing sound
  89.         // if the sound is not already playing
  90.         if(!player.isPlaying())
  91.         {
  92.           stroke(0,255,0); // green box = start sound
  93.           // then play the sound from the beginning (0)
  94.           isPlaying=true;
  95.           player.play(0);
  96.         }
  97.       }
  98.         
  99.       // draw the rectangle around the region
  100.       rect(targetRegion_1[0],targetRegion_1[1],targetRegion_1[2],targetRegion_1[3]);
  101.       rect(targetRegion_2[0],targetRegion_2[1],targetRegion_2[2],targetRegion_2[3]);
  102.       rect(targetRegion_3[0],targetRegion_3[1],targetRegion_3[2],targetRegion_3[3]);
  103.       rect(targetRegion_4[0],targetRegion_4[1],targetRegion_4[2],targetRegion_4[3]);
  104.       rect(targetRegion_5[0],targetRegion_5[1],targetRegion_5[2],targetRegion_5[3]);
  105.       //rect(targetRegion_6[0],targetRegion_6[1],targetRegion_6[2],targetRegion_6[3]);
  106.       
  107.       
  108.     }
  109.     else
  110.     {
  111.       if(player.isPlaying())
  112.       {
  113.           noFill();
  114.         stroke(155,155,155); // grey box = sound is playing
  115.         rect(targetRegion_1[0],targetRegion_1[1],targetRegion_1[2],targetRegion_1[3]);
  116.         rect(targetRegion_2[0],targetRegion_2[1],targetRegion_2[2],targetRegion_2[3]);
  117.         rect(targetRegion_3[0],targetRegion_3[1],targetRegion_3[2],targetRegion_3[3]);
  118.         rect(targetRegion_4[0],targetRegion_4[1],targetRegion_4[2],targetRegion_4[3]);
  119.         rect(targetRegion_5[0],targetRegion_5[1],targetRegion_5[2],targetRegion_5[3]);
  120.         //rect(targetRegion_6[0],targetRegion_6[1],targetRegion_6[2],targetRegion_6[3]);
  121.         
  122.       }
  123. //        player.pause();
  124.     }
  125.     
  126.   }    
  127. }


  128. //=========================
  129. // returns the amount of movement in the specified region as a percentage(ish)
  130. float checkMovement(int x, int y, int w, int h)
  131. {
  132.   int movementSum = 0; // Amount of movement in the frame
  133.   for(int ypos = 0;ypos < video.height; ypos ++)
  134.   {
  135.     for(int xpos = 0;xpos < video.width; xpos ++)
  136.     {
  137.       int index = xpos + (ypos * video.width);
  138.       color currColor = video.pixels[index];
  139.       if( xpos >= x && xpos < x+w && ypos >= y && ypos < y+h)
  140.       {
  141.         color prevColor = previousFrame[index];

  142.         int currR = (currColor >> 16) & 0xFF; // Like red(), but faster
  143.         int currG = (currColor >> 8) & 0xFF;
  144.         int currB = currColor & 0xFF;
  145.       // Extract red, green, and blue components from previous pixel
  146.         int prevR = (prevColor >> 16) & 0xFF;
  147.         int prevG = (prevColor >> 8) & 0xFF;
  148.         int prevB = prevColor & 0xFF;
  149.       // Compute the difference of the red, green, and blue values
  150.         int diffR = abs(currR - prevR);
  151.         int diffG = abs(currG - prevG);
  152.         int diffB = abs(currB - prevB);

  153. //
  154.         movementSum += diffR + diffG + diffB;
  155.       // Render the difference image to the screen
  156.         pixels[index] = color(diffR, diffG, diffB);
  157.       // The following line is much faster, but more confusing to read
  158.       //pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB;
  159.       // Save the current color into the 'previous' buffer
  160.       }
  161.       else
  162.       {
  163.         // if this pixel isnt in our area of interest, just show current video frame colour
  164.         pixels[index] = currColor;
  165.       }
  166.       
  167.       // keep the current pixel colour for comparison next time
  168.       previousFrame[index] = currColor;
  169.     }
  170.   }

  171.   int totalPossibleMovement = w*h*255*3; // width * height (of area of interest) * 255 (is the max difference per colour) * 3 (number of colour channels eg:rgb)
  172.   float percentage = (float)movementSum / (float)totalPossibleMovement;
  173.   percentage = percentage * 100.0f;
  174.   
  175.   return percentage;
  176. }


  177. void stop()
  178. {
  179.   // always close Minim audio classes when you are done with them
  180.   player.close();
  181.   // always stop Minim before exiting
  182.   minim.stop();
  183.   
  184.   super.stop();
  185. }


Viewing all articles
Browse latest Browse all 1768

Trending Articles