Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

OpenCV 2.3 and processing example (for windows)

By: a guest on Sep 24th, 2012  |  syntax: None  |  size: 4.49 KB  |  hits: 79  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  
  3. tags:
  4. Processing face detection opencv sketch pde example windows
  5.  
  6. Date: september 2012
  7. This sketch works for me in Windows, using the latest processing 1.5.1 and the latest version of OpenCV (2.3 I believe)
  8.  
  9. This is how I got openCV installed:
  10. http://codeanticode.wordpress.com/2011/11/21/opencv-2-in-processing/
  11.  
  12. I've installed processsing and openCV directly in C:\ (root of the disk)
  13. My sketches are in a 'sketches' folder directly in me processing folder. (you can change location in Processing's settings.)
  14. My libraries are in a libraries folder inside the sketches folder (this is how processing always wants it)
  15. The only library in there is the GSVideo library, which makes my laptop's webcam accessible to Processing (and thus openCV).
  16. If you look through the code, you'll see that I put a dogface in front of people's face. Make sure you have a dog.png or some other picture you want in the same folder as the processing sketch.
  17.  
  18. (this sketch used to be some code that would make your face invisible, which I've hacked to show a picture instead)
  19.  
  20. */
  21.  
  22.     import codeanticode.gsvideo.*;
  23.     import monclubelec.javacvPro.*;
  24.     import java.awt.*;
  25.  
  26. OpenCV opencv;  //Main OpenCV variable
  27. GSCapture cam;   // Connects to a camera
  28.  
  29. Rectangle[] faces;
  30.  
  31. boolean detectBackground = true;
  32. PImage bg, fg;
  33.  
  34. void setup() {
  35.   size( 320, 240 );  //Larger screen sizes make it difficult to do this in real time
  36.  
  37.     //PImage currentImg = loadImage("dog.png");
  38.  
  39.     cam = new GSCapture(this, width,height);
  40.     cam.start();
  41.     bg = createImage(width,height,RGB);  // Create an image to hold the background
  42.     fg = createImage(width,height,RGB); // Image for the foreground
  43.     //Create the openCV buffer that's used to detect faces
  44.     opencv = new OpenCV(this);
  45.     opencv.allocate( width, height ); //Create a buffer for OpenCV where we can detect faces
  46.  
  47.     // Make this point to where you've put this file on your harddrive:
  48.     opencv.cascade("C:\\processing-1.5.1\\sketches\\Haarfeatures_video\\","haarcascade_frontalface_alt.xml");
  49.  
  50.  
  51. }
  52.  
  53. //Computes an enlarged version of the face box
  54. Rectangle enlargeFaceBox (float incPct, int x, int y, int w,int h) {
  55.     float r = dist(0,0,w,h) / 2;  //Computes radius of the center diagonal
  56.     float theta = atan2(h,w);  //Computes the angle of the diagonal
  57.     float dx = r*incPct*cos(theta); //Finds
  58.     float dy = r*incPct*sin(theta);
  59.     return new Rectangle( (int) (x - dx), (int) (y - dy), (int) (w + 2*dx), (int) (h + 2*dy));
  60. }
  61.  
  62. void draw() {
  63.     cam.read();  //Pull in the image from the webcam
  64.     if (detectBackground) {
  65.       image( cam, 0, 0 );
  66.       text("Step out of scene and press any key", 10,20);
  67.     } else {
  68.       fg.copy(cam,0,0,width,height,0,0,width,height);  //Copy the camera image into  fg
  69.       opencv.copy(cam);  //Copy the image into openCV's buffer  
  70.       //Detect faces and replace them with background image
  71.       Rectangle[] faces = opencv.detect();  // detect anything ressembling a FRONTALFACe
  72.       bg.loadPixels();
  73.       Rectangle faceBox = new Rectangle();  //Create a rectangle that will hold the enlarged box for the face
  74.       for( int i=0; i<faces.length; i++ ) {
  75.          faceBox = enlargeFaceBox(0.1, faces[i].x, faces[i].y, faces[i].width, faces[i].height);
  76.          //Test boundaries to make sure box is still inside the visible screen area
  77.          if (faceBox.x < 0) {faceBox.x = 0; }
  78.          if (faceBox.x + faceBox.width > width) { faceBox.width = width - faceBox.x; }
  79.          if (faceBox.y < 0) { faceBox.y = 0; }
  80.          if (faceBox.y + faceBox.height >  height) { faceBox.height = height - faceBox.y; }
  81.          //Now replace the foreground image of the face with the same positions in the background
  82.          fg.loadPixels();
  83.          PImage currentImg = loadImage("dog.png");
  84.          currentImg.resize(faceBox.width, faceBox.height);
  85.          fg.copy(currentImg, 0, 0, currentImg.height, currentImg.width,  faceBox.x, faceBox.y, faceBox.width, faceBox.height );
  86.                        
  87.                        // image(currentImg, startx, starty);
  88.                       // currentImg.resize(memeWidth, memeHeight);
  89.          //image(currentImg, startx, starty);
  90.          
  91.          fg.updatePixels();
  92.       }
  93.       fg.updatePixels();
  94.       image(fg,0,0);
  95.       //image(opencv.image(), 0, 0);
  96.     }
  97. }
  98.  
  99. //Flip the mode
  100. void keyPressed() {
  101.   detectBackground = !detectBackground;
  102.   //Copy the image into a background
  103.   bg.copy(cam,0,0,width,height,0,0,width,height);
  104.   bg.updatePixels();
  105. }