Help met processing face detection!

Status
Niet open voor verdere reacties.

CMDforthewin

Nieuwe gebruiker
Lid geworden
14 jun 2012
Berichten
1
Hallo,
Ik ben CMD student en moet als eindopdracht processing een webcam game maken.
Nu hebben we bedacht om iets uit de lucht laten vallen (nu dus bellen (bubbles)) en dat met je mond te laten poppen (dus als een bel je mond raakt verdwijnt ie).
We hebben al de librarie "OpenCV" geinstalleerd en daar zou je dan face detection moet doen.
Nu hoorde we van de docent dat je je mond kan "detecteren" door er een (onzichtbaar) vierkant in het gezichtsvlak te tekenen waar dan ongeveer je mond zit. Wij hebben veel gezocht op het web, maar niks gevonden. Weten jullie hoe we dat kunnen doen? Alvast bedankt!

Mvg Nick

Dit is de code om bubbles uit de lucht te laten valen en deze te poppen als je het met je lichaam aanraakt:

Code:
import hypermedia.video.*;          //  Imports the OpenCV library

OpenCV opencv;                      //  Creates a new OpenCV object
PImage movementImg;                 //  Creates a new PImage to hold the movement image
int poppedBubbles;                  //  Creates a variable to hold the total number of popped bubbles
ArrayList bubbles;                  //  Creates an ArrayList to hold the Bubble objects
PImage bubblePNG;                   //  Creates a PImage that will hold the image of the bubble
PFont font;                         //  Creates a new font object

void setup()
{
  size ( 640, 480 );                      //  Window size of 640 x 480
  opencv = new OpenCV( this );            //  Initialises the OpenCV library
  opencv.capture( 640, 480 );             //  Sets the capture size to 640 x 480
  movementImg = new PImage( 640, 480 );   //  Initialises the PImage that holds the movement image
  poppedBubbles = 0;                     
  
  bubbles = new ArrayList();              //  Initialises the ArrayList
  
  bubblePNG = loadImage("bubble.png");    //  Load the bubble image into memory
  font = loadFont("Serif-48.vlw");        //  Load the font file into memory
  textFont(font, 32);                       

}

void draw()
{
  bubbles.add(new Bubble( (int)random( 0, width - 40), -bubblePNG.height, bubblePNG.width, bubblePNG.height));   //  Adds a new bubble to the array with a random x position
  
  opencv.read();                              //  Captures a frame from the camera    
  opencv.flip(OpenCV.FLIP_HORIZONTAL);        //  Flips the image horizontally
  image( opencv.image(), 0, 0 );              //  Draws the camera image to the screen
  opencv.absDiff();                           //  Creates a difference image
    
  opencv.convert(OpenCV.GRAY);                //  Converts to greyscale
  opencv.blur(OpenCV.BLUR, 3);                //  Blur to remove camera noise
  opencv.threshold(20);                       //  Thresholds to convert to black and white
  movementImg = opencv.image();               //  Puts the OpenCV buffer into an image object
  
  for ( int i = 0; i < bubbles.size(); i++ ){    //  For every bubble in the bubbles array
    Bubble _bubble = (Bubble) bubbles.get(i);    //  Copies the current bubble into a temporary object
    
    if(_bubble.update() == 1){                  //  If the bubble's update function returns '1'
      bubbles.remove(i);                        //  then remove the bubble from the array
      _bubble = null;                           //  and make the temporary bubble object null
      i--;                                      //  since we've removed a bubble from the array, we need to subtract 1 from i, or we'll skip the next bubble
    
  }else{                                        //  If the bubble's update function doesn't return '1'
      bubbles.set(i, _bubble);                  //  Copys the updated temporary bubble object back into the array
      _bubble = null;                           //  Makes the temporary bubble object null.
    }
  }
  
  opencv.remember(OpenCV.SOURCE, OpenCV.FLIP_HORIZONTAL);    //  Remembers the camera image so we can generate a difference image next frame. Since we've
                                                             //  flipped the image earlier, we need to flip it here too.
  text("Bubbles popped: " + poppedBubbles, 20, 40);          //  Displays some text showing how many bubbles have been popped
  
}

class Bubble
{
  
  int bubbleX, bubbleY, bubbleWidth, bubbleHeight;    //  Some variables to hold information about the bubble
  
  Bubble ( int bX, int bY, int bW, int bH )           //  The class constructor- sets the values when a new bubble object is made
  {
    bubbleX = bX;
    bubbleY = bY;
    bubbleWidth = bW;
    bubbleHeight = bH;
  }
  
  int update()      //   The Bubble update function
  {
    int movementAmount;          //  Create and set a variable to hold the amount of white pixels detected in the area where the bubble is
    movementAmount = 0;
    
    for( int y = bubbleY; y < (bubbleY + (bubbleHeight-1)); y++ ){    //  For loop that cycles through all of the pixels in the area the bubble occupies
      for( int x = bubbleX; x < (bubbleX + (bubbleWidth-1)); x++ ){
        
        if ( x < width && x > 0 && y < height && y > 0 ){             //  If the current pixel is within the screen bondaries
          if (brightness(movementImg.pixels[x + (y * width)]) > 127)  //  and if the brightness is above 127 (in this case, if it is white)
          {
            movementAmount++;                                         //  Add 1 to the movementAmount variable.
          }
        }
      }
    }
    
    if (movementAmount > 5)               //  If more than 5 pixels of movement are detected in the bubble area
    {
      poppedBubbles++;                    //  Add 1 to the variable that holds the number of popped bubbles
      return 1;                           //  Return 1 so that the bubble object is destroyed
   
   }else{                                 //  If less than 5 pixels of movement are detected,
      bubbleY += 10;                      //  increase the y position of the bubble so that it falls down
      
      if (bubbleY > height)               //  If the bubble has dropped off of the bottom of the screen
      {  return 1; }                      //  Return '1' so that the bubble object is destroyed
      
      image(bubblePNG, bubbleX, bubbleY);    //  Draws the bubble to the screen
      return 0;                              //  Returns '0' so that the bubble isn't destroyed
    }
    
  }
  
}
 
Laatst bewerkt door een moderator:
kan je de code tag toevoegen vierkantehaken en code ertussen
Code:
package be.helpmij.cmdforthewin;
import java.util.ArrayList;

import sun.font.FileFont;

import hypermedia.video.*; // Imports the OpenCV library
public class setup{
	private static final String FileFont = null;
	OpenCV opencv; // Creates a new OpenCV object
	PImage movementImg; // Creates a new PImage to hold the movement image
	int poppedBubbles; // Creates a variable to hold the total number of popped bubbles
	ArrayList bubbles; // Creates an ArrayList to hold the Bubble objects
	PImage bubblePNG; // Creates a PImage that will hold the image of the bubble
	PFont font; // Creates a new font object
	private int width;
	void setup(){
		size ( 640, 480 ); // Window size of 640 x 480
		opencv = new OpenCV( this ); // Initialises the OpenCV library
		opencv.capture( 640, 480 ); // Sets the capture size to 640 x 480
		movementImg = new PImage( 640, 480 ); // Initialises the PImage that holds the movement image
		poppedBubbles = 0;
		bubbles = new ArrayList(); // Initialises the ArrayList
		bubblePNG = loadImage("bubble.png"); // Load the bubble image into memory
		font = loadFont("Serif-48.vlw"); // Load the font file into memory
		textFont(FileFont, 32);
	}
	private void size(int i, int j) {
		// TODO Auto-generated method stub
		
	}
	private void textFont(String filefont2, int i) {
		// TODO Auto-generated method stub
		
	}
	private PFont loadFont(String string) {
		// TODO Auto-generated method stub
		return null;
	}
	private PImage loadImage(String string) {
		// TODO Auto-generated method stub
		return null;
	}
	void draw(){
		bubbles.add(new Bubble( (int)random( 0, width - 40), -bubblePNG.height, bubblePNG.width, bubblePNG.height)); // Adds a new bubble to the array with a random x position
		opencv.read(); // Captures a frame from the camera
		opencv.flip(OpenCV.FLIP_HORIZONTAL); // Flips the image horizontally
		image( opencv.image(), 0, 0 ); // Draws the camera image to the screen
		opencv.absDiff(); // Creates a difference image
		opencv.convert(OpenCV.GRAY); // Converts to greyscale
		opencv.blur(OpenCV.BLUR, 3); // Blur to remove camera noise
		opencv.threshold(20); // Thresholds to convert to black and white
		movementImg = opencv.image(); // Puts the OpenCV buffer into an image object
		for ( int i = 0; i < bubbles.size(); i++ ){ // For every bubble in the bubbles array
			Bubble _bubble = (Bubble) bubbles.get(i); // Copies the current bubble into a temporary object
			if(_bubble.update() == 1){ // If the bubble's update function returns '1'
				bubbles.remove(i); // then remove the bubble from the array
				_bubble = null; // and make the temporary bubble object null
				i--; // since we've removed a bubble from the array, we need to subtract 1 from i, or we'll skip the next bubble
			}else{ // If the bubble's update function doesn't return '1'
				bubbles.set(i, _bubble); // Copys the updated temporary bubble object back into the array
				_bubble = null; // Makes the temporary bubble object null.
			}
		}
		opencv.remember(OpenCV.SOURCE, OpenCV.FLIP_HORIZONTAL); // Remembers the camera image so we can generate a difference image next frame. Since we've
		// flipped the image earlier, we need to flip it here too.
		text("Bubbles popped: " + poppedBubbles, 20, 40); // Displays some text showing how many bubbles have been popped
	}
	private int random(int i, int j) {
		// TODO Auto-generated method stub
		return 0;
	}
	private void image(PImage image, int i, int j) {
		// TODO Auto-generated method stub
		
	}
	private void text(String string, int i, int j) {
		// TODO Auto-generated method stub
		
	}
	class Bubble{
		int bubbleX, bubbleY, bubbleWidth, bubbleHeight; // Some variables to hold information about the bubble
		private int width;
		private int height;
		Bubble ( int bX, int bY, int bW, int bH ){ // The class constructor- sets the values when a new bubble object is made
			bubbleX = bX;
			bubbleY = bY;
			bubbleWidth = bW;
			bubbleHeight = bH;
		}
		int update(){ // The Bubble update function
			int movementAmount; // Create and set a variable to hold the amount of white pixels detected in the area where the bubble is
			movementAmount = 0;
			for( int y = bubbleY; y < (bubbleY + (bubbleHeight-1)); y++ ){ // For loop that cycles through all of the pixels in the area the bubble occupies
				for( int x = bubbleX; x < (bubbleX + (bubbleWidth-1)); x++ ){
					if ( x < width && x > 0 && y < height && y > 0 ){ // If the current pixel is within the screen bondaries
						if (brightness(movementImg.pixels[x + (y * width)]) > 127){ // and if the brightness is above 127 (in this case, if it is white)
							movementAmount++; // Add 1 to the movementAmount variable.
						}
					}
				}
			}
			if (movementAmount > 5){ // If more than 5 pixels of movement are detected in the bubble area
				poppedBubbles++; // Add 1 to the variable that holds the number of popped bubbles
				return 1; // Return 1 so that the bubble object is destroyed
			}else{ // If less than 5 pixels of movement are detected,
				bubbleY += 10; // increase the y position of the bubble so that it falls down
				if (bubbleY > height){  // If the bubble has dropped off of the bottom of the screen
					return 1; 
				} // Return '1' so that the bubble object is destroyed
				image(bubblePNG, bubbleX, bubbleY); // Draws the bubble to the screen
				return 0; // Returns '0' so that the bubble isn't destroyed
			}
		}
		private int brightness(Object object) {
			// TODO Auto-generated method stub
			return 0;
		}
		private void image(PImage bubblePNG, int bubbleX2, int bubbleY2) {
			// TODO Auto-generated method stub
		}
	} 
}
package be.helpmij.cmdforthewin;
class OpenCV {
	public static final String BLUR = null;
	public static final String FLIP_HORIZONTAL = null;
	public static final String SOURCE = null;
	public static Object GRAY;
	public OpenCV(setup setup) {
		// TODO Auto-generated constructor stub
	}
	public void capture(int i, int j) {
		// TODO Auto-generated method stub
		
	}
	public void read() {
		// TODO Auto-generated method stub
		
	}
	public void flip(String flipHorizontal) {
		// TODO Auto-generated method stub
		
	}
	public void threshold(int i) {
		// TODO Auto-generated method stub
		
	}
	public void blur(String blur2, int i) {
		// TODO Auto-generated method stub
		
	}
	public void convert(Object gRAY2) {
		// TODO Auto-generated method stub
		
	}
	public void absDiff() {
		// TODO Auto-generated method stub
		
	}
	public PImage image() {
		// TODO Auto-generated method stub
		return null;
	}
	public void remember(String source2, String flipHorizontal) {
		// TODO Auto-generated method stub
		
	}
}
class PImage {
	public int width;
	public int height;
	public Object[] pixels;
	public PImage(int i, int j) {
		// TODO Auto-generated constructor stub
	}
}
package be.helpmij.cmdforthewin;
class PFont {

}
 
Laatst bewerkt:
best nick heb je gezien dat ik alles had en ben je al iets verder? Gelieve nog concreter te zijn wat je wil omdat ik het niet direct zie wat je wil bereiken
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan