need help urgently

hi I recently attended a workshop and it inspired me to do a face and mouth gesture controlled robot. I can get the robot to work on codes for face and mouth separately but I am unable to combine the two codes. any help in combing the two codes will be highly appreciated :) here are the codes

*******************************************************face**********************************************************

import hypermedia.video.*;// Library for video processing
import java.awt.Rectangle;//Library for rectangular face draw
import codeanticode.gsvideo.*;//Library for face detection
import processing.serial.*;//Library for sending data serially 
String fileName = "C:/haarcascades/haarcascade_frontalface_alt.xml";
OpenCV opencv;
GSCapture cam;
Serial myPort ;
int baudrate = 9600;// the serial rate must be similar arduino rate

// Contrast/brightness values
int contrast_value    = 0;//intialize brightnes and contrast
int brightness_value  = 0;

void setup() {
  size(640, 480);//webcam image size

  cam = new GSCapture(this, 640, 480);// function for calling image and its camera attributes.
  cam.start();//to start the camera of webcam

  opencv = new OpenCV(this);//calling index 

  opencv.allocate(640, 480); //allocate the buffer image   

  // Load detection description, here-> front face detection : "haarcascade_frontalface_alt.xml"
  opencv.cascade( fileName );  


  // Print usage
  println("Drag mouse on X-axis inside this sketch window to change contrast");
  println("Drag mouse on Y-axis inside this sketch window to change brightness");

  //println(Serial.list());//serial data printing
  //myPort = new Serial(this, Serial.list()[0], 9600);//sending data serially to variable myPort
}
//function for capturing and reading image
void captureEvent(GSCapture c) 
{
  c.read();
}
//cleans up all allocated memories
public void stop()
{
  opencv.stop();
  super.stop();
}

void draw() {
  opencv.copy(cam);//copying image into current buffer frame

  opencv.convert(GRAY);//gray scale image
  opencv.contrast(contrast_value);//brightness and contrast specified for webcam
  opencv.brightness(brightness_value);

  // Proceed with detection
  Rectangle[] faces = opencv.detect(1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 10, 40);
  // Display the image
  image(cam, 0, 0);

  // Draw face area(s)
  noFill();
  stroke(255, 0, 0);
  for (int i = 0; i < faces.length; i++) {
    rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height); //rect(x-coord,y-coord,rect width,rect height)
    println (faces[i].width +"," + faces[i].height); //printing coordinates amd length
    println (faces[i].x +"," + faces[i].y );
    //println ( faces[i].height);
    //Logic for understanding movement and aiding directions to robo

    if ((faces[i].x > 0))

    {
      delay(1000);

      if (((faces[i].x)<212)&&((faces[i].x)>50))
      { 
        //myPort.write('a');
        println("right");
      }

      else if (((faces[i].x)>212)&&(faces[i].x<380))
      { 
        if((faces[i].y)>210)//myPort.write('w');
        println("down");
        else 
        println("up");
      }

      else if (((faces[i].x) > 380)&&((faces[i].x < 550)))
      { 
        //myPort.write('d');
        println("left");
      }
      else
      { 
        //myPort.write('x');
        println("stop");
      }
    }
    else
    {   
      //myPort.write('x');
      println("stop");
    }
  }
}

//Changes contrast/brigthness values
void mouseDragged() 
{
  contrast_value   = (int)map(mouseX, 0, width, -128, 128);
  brightness_value = (int)map(mouseY, 0, width, -128, 128);
}

********************************************************mouth********************************************************

import hypermedia.video.*;// Library for video processing
import java.awt.Rectangle;//Library for rectangular face draw
import codeanticode.gsvideo.*;//Library for face detection
import processing.serial.*;//Library for sending data serially 
String fileName = "C:/haarcascades/haarcascade_mcs_mouth.xml";
OpenCV opencv;
GSCapture cam;
Serial myPort ;
int val ;
int baudrate = 9600;
int loc = 0 ;
// the serial rate must be similar arduino rate
// Contrast/brightness values
int contrast_value    = 20;//intialize brightnes and contrast
int brightness_value  = 60;
int wi =0;
int u, v, w, x ;
void setup() {
  size(320, 240);//webcam image size

  cam = new GSCapture(this, 320, 240);// function for calling image and its camera attributes.
  cam.start();//to start the camera of webcam

  opencv = new OpenCV(this);//calling index 

  opencv.allocate(320, 240); //allocate the buffer image   

  // Load detection description, here-> front face detection : "haarcascade_frontalface_alt.xml"
  opencv.cascade( fileName);  


  // Print usage
  println("Drag mouse on X-axis inside this sketch window to change contrast");
  println("Drag mouse on Y-axis inside this sketch window to change brightness");

  //println(Serial.list());//serial data printing
  //myPort = new Serial(this, Serial.list()[0], 9600);//sending data serially to variable myPort
}  

//function for capturing and reading image
void captureEvent(GSCapture c) 
{
  c.read();
}
//cleans up all allocated memories
public void stop()
{
  opencv.stop();
  super.stop();
}

void draw() 
{
  delay(250);
  u=0;
  v=0;
  x=0;
  w=0;

  int   threshold =200;
  opencv.copy(cam);//copying image into current buffer frame

  opencv.convert(GRAY);//gray scale image
  opencv.contrast(contrast_value);//brightness and contrast specified for webcam
  opencv.brightness(brightness_value);

  // Proceed with detection
  Rectangle[] mouth = opencv.detect(1.1, 3, OpenCV.HAAR_DO_CANNY_PRUNING, 45, 11);
  // Display the image 
  tint(255);
  image(cam, 0, 0);


  // Draw face area(s)
  noFill();
  stroke(255, 0, 0);
  int j;
  for ( j = 0; j < mouth.length; j++) {
    if (( mouth[j].y > 130)&&( mouth[j].y < 160))
    {
      rect(mouth[j].x, mouth[j].y, mouth[j].width, mouth[j].height); //rect(x-coord,y-coord,rect width,rect height)
      println("nw");

      println (mouth[j].width +"," + mouth[j].height); //printing coordinates amd length
      println (mouth[j].x +"," + mouth[j].y );  



      //println ( faces[i].height);
      //Logic for understanding movement and aiding directions to robo
      u = mouth[j].width + 0 ;
      //println(u);
      v = mouth[j].height+ 0 ;
      //println(v);
      w = mouth[j].x+ 0;
      //println(w);
      x = mouth[j].y+ 0;
      //println(x);

      float r=0;
      float g=0;
      float B=0;

      if ( ( (u < 130)&& ( u > 40) ) && ( (v > 30)&& (v < 100) ) )
      {
        for (int b= x ; b < (u+x) ; b++ ) // Loop through every pixel row
        {
          for (int a= w ; a < (v+w) ; a++ )// Use the formula to find the 1D location
          {

            loc = a + (b * (u+x) )   ;
            //println(loc);
            r+= red(loc);
            g+= green(loc);
            B+= blue(loc);

            //println(B);
            //println(r);
            //println(g);
            ////println(B);
          }
        }
        r = r/(x*w);
        g = g/(x*w);
        B = B/(x*w);
        //r = r/(x*w);
        //g = g/(x*w);
        float val =B+g ;
        //println(r);
        println(val);
        if (( val > 21 ))
        {
          println("mouth opened");
          B=0;
          r=0;
          g=0;
          //myPort.write('w');
          println("start");
        }

        else
        {
          B=0;
          r=0;
          g=0;
          println("mouth closed");
          println("stop");
          //myPort.write('x');
        }
      }
    }
  }
}

the codes were written and compiled in processing 1.5.1

Answers

Sign In or Register to comment.