Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.47 KB | None | 0 0
  1. import blobscanner.*;
  2. import processing.video.*;
  3. import processing.serial.*;
  4. import org.firmata.*;
  5. import cc.arduino.*;
  6.  
  7.  
  8. // Include Networking things required for the hue
  9. import org.apache.http.HttpEntity;
  10. import org.apache.http.HttpResponse;
  11. import org.apache.http.client.methods.HttpPut;
  12. import org.apache.http.impl.client.DefaultHttpClient;
  13. import java.io.*;
  14. import java.awt.*;
  15. import java.lang.Object.*;
  16. import java.util.Date;
  17.  
  18. /* EDIT THIS TO MATCH YOUR HUE BRIDGE */
  19. final static String HUE_KEY = "JorenBroekema";
  20. final static String HUE_IP  = "192.168.1.101";
  21.  
  22. PFont font;
  23.  
  24. //Arduino related
  25. Arduino arduino;
  26. int sensorPin0 = 0;
  27. int sensorValue0;
  28. int sensorPin1 = 1;
  29. int sensorValue1;
  30. int led = 3;
  31.  
  32. //Blob detection related
  33. Detector bd;
  34. color boundingBoxCol = color(255, 0, 0);
  35. int boundingBoxThickness = 1;
  36. int blobNum;
  37.  
  38. // Capture device related
  39. Capture video;
  40. PImage prevFrame;
  41. // How different must a pixel be to be a "motion" pixel
  42. float threshold = 30;
  43.  
  44. //Timer related for hue (10 fps)
  45. long startTime = 0;
  46. int frameDiff = 100;
  47.  
  48. //Distance calculation related
  49. float distanceFromLamp1; float twoDistanceFromLamp1; float threeDistanceFromLamp1; float fourDistanceFromLamp1;
  50. float distanceFromLamp2; float twoDistanceFromLamp2; float threeDistanceFromLamp2; float fourDistanceFromLamp2;
  51. float distanceFromLamp3; float twoDistanceFromLamp3; float threeDistanceFromLamp3; float fourDistanceFromLamp3;
  52. float maxDistance = 220;
  53.  
  54. boolean[] playerArray = new boolean[3];
  55.  
  56. //Hue lamps
  57. int NUM_HUE_LAMPS = 7;
  58. int h; int h1; int h2; int h3;
  59. int s; int s1; int s2; int s3;
  60. int b; int b1; int b2; int b3;
  61.  
  62. float[][] lampValues = new float[NUM_HUE_LAMPS][3];
  63. int lampIndex = 0;
  64.  
  65. void setup()
  66. {
  67.   size(640,480);
  68.   video = new Capture(this, width, height, 30);
  69.   // Create an empty image the same size as the video
  70.   prevFrame = createImage(video.width,video.height,RGB);
  71.   video.start();
  72.   font = loadFont("Aharoni-Bold-24.vlw");
  73.   textFont(font, 24);
  74.   smooth();
  75.  
  76.   arduino = new Arduino(this, Arduino.list()[0], 57600);
  77.   arduino.pinMode(sensorPin0, Arduino.INPUT);
  78.   arduino.pinMode(sensorPin1, Arduino.INPUT);
  79.   //arduino.pinMode(led, Arduino.OUTPUT);
  80.   //arduino.analogWrite(led, 0);
  81.  
  82.   setupHue();
  83. }
  84.  
  85. void draw()
  86. {
  87.  
  88.  
  89.  
  90.   for(int i = 0; i < NUM_HUE_LAMPS; i++)
  91.   {
  92.      lampValues[0][0] = h; lampValues[1][0] = h1; lampValues[2][0] = h2; lampValues[6][0] = h3;
  93.      lampValues[0][1] = s; lampValues[1][1] = s1; lampValues[2][1] = s2; lampValues[6][1] = s3;
  94.      lampValues[0][2] = b; lampValues[1][2] = b1; lampValues[2][2] = b2; lampValues[6][2] = b3;
  95.   }
  96.  
  97.   if (millis() - startTime > frameDiff)
  98.   {
  99.      startTime = millis();
  100.      sendToHue();
  101.   }
  102.  
  103.  
  104.   // Capture video
  105.   if (video.available())
  106.   {
  107.     // Save previous frame for motion detection!!
  108.     prevFrame.copy(video,0,0,video.width,video.height,0,0,video.width,video.height); // Before we read the new frame, we always save the previous frame for comparison!
  109.     prevFrame.updatePixels();
  110.     video.read();
  111.   }
  112.  
  113.   loadPixels();
  114.   video.loadPixels();
  115.   prevFrame.loadPixels();
  116.  
  117.  
  118.   // Begin loop to walk through every pixel
  119.   for (int x = 0; x < video.width; x ++ )
  120.   {
  121.     for (int y = 0; y < video.height; y ++ )
  122.     {
  123.      
  124.       int loc = x + y*video.width;            // Step 1, what is the 1D pixel location
  125.       color current = video.pixels[loc];      // Step 2, what is the current color
  126.       color previous = prevFrame.pixels[loc]; // Step 3, what is the previous color
  127.      
  128.       // Step 4, compare colors (previous vs. current)
  129.       float r1 = red(current); float g1 = green(current); float b1 = blue(current);
  130.       float r2 = red(previous); float g2 = green(previous); float b2 = blue(previous);
  131.       float diff = dist(r1,g1,b1,r2,g2,b2);
  132.      
  133.       // Step 5, How different are the colors?
  134.       // If the color at that pixel has changed, then there is motion at that pixel.
  135.    //   boolean isBlack = false; // Added
  136.       if (diff > threshold)
  137.       {
  138.         // If motion, display black
  139.         pixels[loc] = color(0);
  140.   //      isBlack = true;
  141.       }
  142.       else
  143.       {
  144.         // If not, display white
  145.         pixels[loc] = color(255);
  146.       }
  147.     }
  148.   }
  149.   updatePixels();
  150.  
  151.   video.filter(THRESHOLD);
  152.   bd = new Detector(this, 255);
  153.   bd.findBlobs(video.pixels, video.width, video.height);
  154.   bd.loadBlobsFeatures();
  155.   bd.findCentroids();
  156.  
  157.   stroke(255, 100, 0);
  158.   fill(120, 250, 0);
  159.   ellipse(640, 480, 20, 20);
  160.   ellipse(0, 0, 20, 20);
  161.   ellipse(640, 0, 20, 20);
  162.   ellipse(0, 480, 20, 20);
  163.  
  164.   for(int i = 0; i < bd.getBlobsNumber(); i++)
  165.   {
  166.        
  167.     //print(bd.getBlobsNumber());
  168.    
  169.     stroke(0, 255, 0);
  170.     strokeWeight(5);
  171.      
  172.     //...and draws a point to their location.
  173.     point(bd.getCentroidX(i), bd.getCentroidY(i));
  174.    
  175.     if (bd.getBlobsNumber() == 1)
  176.     {
  177.       playerArray[0] = true; playerArray[1] = false; playerArray[2] = false; // only 1 player active
  178.      
  179.       //distance from lamp 1
  180.       distanceFromLamp1 = dist(640, 480, bd.getCentroidX(0), bd.getCentroidY(0));
  181.      
  182.       //distance from lamp 2
  183.       twoDistanceFromLamp1 = dist(0, 0, bd.getCentroidX(0), bd.getCentroidY(0));
  184.      
  185.       //distance from lamp 3
  186.       threeDistanceFromLamp1 = dist(640, 0, bd.getCentroidX(0), bd.getCentroidY(0));
  187.      
  188.       //distance from lamp 4
  189.       fourDistanceFromLamp1 = dist(0, 480, bd.getCentroidX(0), bd.getCentroidY(0));
  190.      
  191.     }
  192.    
  193.     if ( bd.getBlobsNumber() > 1 )
  194.     {
  195.       playerArray[0] = true; playerArray[1] = true; playerArray[2] = false; // only 2 players active
  196.      
  197.       distanceFromLamp1 = dist(640, 480, bd.getCentroidX(0), bd.getCentroidY(0));
  198.       distanceFromLamp2 = dist(640, 480, bd.getCentroidX(1), bd.getCentroidY(1));
  199.      
  200.       //distance from lamp 2
  201.       twoDistanceFromLamp1 = dist(0, 0, bd.getCentroidX(0), bd.getCentroidY(0));
  202.       twoDistanceFromLamp2 = dist(0, 0, bd.getCentroidX(1), bd.getCentroidY(1));
  203.      
  204.       //distance from lamp 3
  205.       threeDistanceFromLamp1 = dist(640, 0, bd.getCentroidX(0), bd.getCentroidY(0));
  206.       threeDistanceFromLamp2 = dist(640, 0, bd.getCentroidX(1), bd.getCentroidY(1));
  207.      
  208.       //distance from lamp 4
  209.       fourDistanceFromLamp1 = dist(0, 480, bd.getCentroidX(0), bd.getCentroidY(0));
  210.       fourDistanceFromLamp2 = dist(0, 480, bd.getCentroidX(1), bd.getCentroidY(1));
  211.      
  212.     }
  213.    
  214.     if ( bd.getBlobsNumber() > 2 )
  215.     {
  216.       playerArray[0] = true; playerArray[1] = true; playerArray[2] = true; // only 3 players active
  217.      
  218.       //distance from lamp 1
  219.       distanceFromLamp1 = dist(250, 250, bd.getCentroidX(0), bd.getCentroidY(0));
  220.       distanceFromLamp2 = dist(250, 250, bd.getCentroidX(1), bd.getCentroidY(1));
  221.       distanceFromLamp3 = dist(250, 250, bd.getCentroidX(2), bd.getCentroidY(2));
  222.      
  223.       //distance from lamp 2
  224.       twoDistanceFromLamp1 = dist(0, 0, bd.getCentroidX(0), bd.getCentroidY(0));
  225.       twoDistanceFromLamp2 = dist(0, 0, bd.getCentroidX(1), bd.getCentroidY(1));
  226.       twoDistanceFromLamp3 = dist(0, 0, bd.getCentroidX(2), bd.getCentroidY(2));
  227.      
  228.       //distance from lamp 3
  229.       threeDistanceFromLamp1 = dist(640, 0, bd.getCentroidX(0), bd.getCentroidY(0));
  230.       threeDistanceFromLamp2 = dist(640, 0, bd.getCentroidX(1), bd.getCentroidY(1));
  231.       threeDistanceFromLamp3 = dist(640, 0, bd.getCentroidX(2), bd.getCentroidY(2));
  232.      
  233.       //distance from lamp 4
  234.       fourDistanceFromLamp1 = dist(0, 480, bd.getCentroidX(0), bd.getCentroidY(0));
  235.       fourDistanceFromLamp2 = dist(0, 480, bd.getCentroidX(1), bd.getCentroidY(1));
  236.       fourDistanceFromLamp3 = dist(0, 480, bd.getCentroidX(2), bd.getCentroidY(2));
  237.  
  238.     }
  239.                            
  240.   }                        
  241.  
  242.   if (distanceFromLamp1 <= maxDistance || distanceFromLamp2 <= maxDistance || distanceFromLamp3 <= maxDistance)
  243.   {
  244.      
  245.     if (distanceFromLamp1 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
  246.     {
  247.       distanceFromLamp1 = maxDistance;
  248.     }
  249.    
  250.     if (distanceFromLamp2 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
  251.     {
  252.       distanceFromLamp2 = maxDistance;
  253.     }
  254.    
  255.     if (distanceFromLamp3 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
  256.     {
  257.       distanceFromLamp3 = maxDistance;
  258.     }
  259.    
  260.    
  261.     if(playerArray[0] == true && playerArray[1] == false && playerArray[2] == false)
  262.     {
  263.      
  264.       b = int(map(distanceFromLamp1, 0, maxDistance, 255, 0));  
  265.      
  266.     }
  267.    
  268.     if(playerArray[0] == true && playerArray[1] == true && playerArray[2] == false)
  269.     {
  270.      
  271.       b = int(map(distanceFromLamp1, 0, maxDistance, 255, 0)) + int(map(distanceFromLamp2, 0, maxDistance, 255, 0));  
  272.      
  273.       if(b >= 255)
  274.       {
  275.         b = 255;
  276.       }
  277.      
  278.       println(b);
  279.      
  280.     }
  281.    
  282.     if(playerArray[0] == true && playerArray[1] == true && playerArray[2] == true)
  283.     {
  284.      
  285.       b = int(map(distanceFromLamp1, 0, maxDistance, 255, 0)) + int(map(distanceFromLamp2, 0, maxDistance, 255, 0)) + int(map(distanceFromLamp3, 0, maxDistance, 255, 0));  
  286.      
  287.       if(b >= 255)
  288.       {
  289.         b = 255;
  290.       }
  291.      
  292.       println(b);
  293.      
  294.     }
  295.    
  296.   }
  297.  
  298.  
  299.  
  300.  
  301.   if (twoDistanceFromLamp1 <= maxDistance || twoDistanceFromLamp2 <= maxDistance || twoDistanceFromLamp3 <= maxDistance)
  302.   {
  303.      
  304.     if (twoDistanceFromLamp1 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
  305.     {
  306.       twoDistanceFromLamp1 = maxDistance;
  307.     }
  308.    
  309.     if (twoDistanceFromLamp2 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
  310.     {
  311.       twoDistanceFromLamp2 = maxDistance;
  312.     }
  313.    
  314.     if (twoDistanceFromLamp3 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
  315.     {
  316.       twoDistanceFromLamp3 = maxDistance;
  317.     }
  318.    
  319.    
  320.     if(playerArray[0] == true && playerArray[1] == false && playerArray[2] == false)
  321.     {
  322.      
  323.       b1 = int(map(twoDistanceFromLamp1, 0, maxDistance, 255, 0));  
  324.      
  325.     }
  326.    
  327.     if(playerArray[0] == true && playerArray[1] == true && playerArray[2] == false)
  328.     {
  329.      
  330.       b1 = int(map(twoDistanceFromLamp1, 0, maxDistance, 255, 0)) + int(map(twoDistanceFromLamp2, 0, maxDistance, 255, 0));  
  331.      
  332.       if(b1 >= 255)
  333.       {
  334.         b1 = 255;
  335.       }
  336.      
  337.     }
  338.    
  339.     if(playerArray[0] == true && playerArray[1] == true && playerArray[2] == true)
  340.     {
  341.      
  342.       b1 = int(map(twoDistanceFromLamp1, 0, maxDistance, 255, 0)) + int(map(twoDistanceFromLamp2, 0, maxDistance, 255, 0)) + int(map(twoDistanceFromLamp3, 0, maxDistance, 255, 0));  
  343.      
  344.       if(b1 >= 255)
  345.       {
  346.         b1 = 255;
  347.       }
  348.      
  349.     }
  350.    
  351.   }
  352.  
  353.  
  354.   if (threeDistanceFromLamp1 <= maxDistance || threeDistanceFromLamp2 <= maxDistance || threeDistanceFromLamp3 <= maxDistance)
  355.   {
  356.      
  357.     if (threeDistanceFromLamp1 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
  358.     {
  359.       threeDistanceFromLamp1 = maxDistance;
  360.     }
  361.    
  362.     if (threeDistanceFromLamp2 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
  363.     {
  364.       threeDistanceFromLamp2 = maxDistance;
  365.     }
  366.    
  367.     if (threeDistanceFromLamp3 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
  368.     {
  369.       threeDistanceFromLamp3 = maxDistance;
  370.     }
  371.    
  372.    
  373.     if(playerArray[0] == true && playerArray[1] == false && playerArray[2] == false)
  374.     {
  375.      
  376.       b2 = int(map(threeDistanceFromLamp1, 0, maxDistance, 255, 0));  
  377.      
  378.     }
  379.    
  380.     if(playerArray[0] == true && playerArray[1] == true && playerArray[2] == false)
  381.     {
  382.      
  383.       b2 = int(map(threeDistanceFromLamp1, 0, maxDistance, 255, 0)) + int(map(threeDistanceFromLamp2, 0, maxDistance, 255, 0));  
  384.      
  385.       if(b2 >= 255)
  386.       {
  387.         b2 = 255;
  388.       }
  389.      
  390.      
  391.     }
  392.    
  393.     if(playerArray[0] == true && playerArray[1] == true && playerArray[2] == true)
  394.     {
  395.      
  396.       b2 = int(map(threeDistanceFromLamp1, 0, maxDistance, 255, 0)) + int(map(threeDistanceFromLamp2, 0, maxDistance, 255, 0)) + int(map(threeDistanceFromLamp3, 0, maxDistance, 255, 0));  
  397.      
  398.       if(b2 >= 255)
  399.       {
  400.         b2 = 255;
  401.       }
  402.      
  403.     }
  404.    
  405.   }
  406.  
  407.  
  408.   if (fourDistanceFromLamp1 <= maxDistance || fourDistanceFromLamp2 <= maxDistance || fourDistanceFromLamp3 <= maxDistance)
  409.   {
  410.      
  411.     if (fourDistanceFromLamp1 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
  412.     {
  413.       fourDistanceFromLamp1 = maxDistance;
  414.     }
  415.    
  416.     if (fourDistanceFromLamp2 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
  417.     {
  418.       fourDistanceFromLamp2 = maxDistance;
  419.     }
  420.    
  421.     if (fourDistanceFromLamp3 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
  422.     {
  423.       fourDistanceFromLamp3 = maxDistance;
  424.     }
  425.    
  426.    
  427.     if(playerArray[0] == true && playerArray[1] == false && playerArray[2] == false)
  428.     {
  429.      
  430.       b3 = int(map(fourDistanceFromLamp1, 0, maxDistance, 255, 0));  
  431.      
  432.     }
  433.    
  434.     if(playerArray[0] == true && playerArray[1] == true && playerArray[2] == false)
  435.     {
  436.      
  437.       b3 = int(map(fourDistanceFromLamp1, 0, maxDistance, 255, 0)) + int(map(fourDistanceFromLamp2, 0, maxDistance, 255, 0));  
  438.      
  439.       if(b3 >= 255)
  440.       {
  441.         b3 = 255;
  442.       }
  443.      
  444.      
  445.     }
  446.    
  447.     if(playerArray[0] == true && playerArray[1] == true && playerArray[2] == true)
  448.     {
  449.      
  450.       b3 = int(map(fourDistanceFromLamp1, 0, maxDistance, 255, 0)) + int(map(fourDistanceFromLamp2, 0, maxDistance, 255, 0)) + int(map(fourDistanceFromLamp3, 0, maxDistance, 255, 0));  
  451.      
  452.       if(b3 >= 255)
  453.       {
  454.         b3 = 255;
  455.       }
  456.      
  457.     }
  458.    
  459.   }
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.   sensorValue0 = arduino.analogRead(sensorPin0);
  474.   h = int(map(sensorValue0, 0, 1023, 0, 255));
  475.   sensorValue1 = arduino.analogRead(sensorPin1);
  476.   h1 = int(map(sensorValue1, 0, 1023, 0, 255));
  477.   s = s1 = s2 = s3 = 255;
  478.   h2 = h3 = 166;
  479.  
  480. }
  481.  
  482. void sendToHue(){
  483.  
  484.     sendHSBToHue(lampIndex+1, int(lampValues[lampIndex][0]), int(lampValues[lampIndex][1]), int(lampValues[lampIndex][2]));
  485.      
  486.      lampIndex++;
  487.      if(lampIndex >= NUM_HUE_LAMPS)
  488.      {
  489.       lampIndex = 0;
  490.      }
  491.  
  492. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement