OPEN CV FACE DETECTION/PROCESSING/ARDUINO/ SERVO HELP!

ok, so i am pretty new to processing, and familar with arduino. I have working code that uses the opencv library facial tracking software to serially communicate with a servo in arduino.

i basically want to use the facial detection software to move the servos degrees of movements on the movements of the face recognized. (make sense?)

heres my code on both ends

processing code:

import hypermedia.video.*;
import java.awt.Rectangle;
import processing.serial.*;        


int gx = 15;
int gy = 35;
int spos=90;

Serial port; 

OpenCV opencv;

// contrast/brightness values
int contrast_value    = 0;
int brightness_value  = 0;



void setup() {

    size( 320, 240 );

    opencv = new OpenCV( this );
    opencv.capture( width, height );                   // open video stream
    opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );  // load detection description, here-> front face detection : "haarcascade_frontalface_alt.xml"

    // 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());
 port = new Serial(this, Serial.list()[1], 9600);
}


public void stop() {
    opencv.stop();
    super.stop();
}



void draw() {

    // grab a new frame
    // and convert to gray
    opencv.read();
    opencv.convert( RGB );
    opencv.contrast( contrast_value );
    opencv.brightness( brightness_value );

    // proceed detection
    Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40 );

    // display the image
    image( opencv.image(), 0, 0 );

    // draw face area(s)
    noFill();
    stroke(255,1,1);
    for( int i=0; i<faces.length; i++ ) {
        rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height ); 
    }
    for( int i=0; i<faces.length; i++ ) {
        update( faces[i].x); 
    }

}



/**
 * Changes contrast/brigthness values
 */
void mouseDragged() {
    contrast_value   = (int) map( mouseX, 0, width, -128, 128 );
    brightness_value = (int) map( mouseY, 0, width, -128, 128 );
}
void update(int x) 
{
  //Calculate servo postion from mouseX
  spos= x/30;

  //Output the servo position ( from 0 to 180)
  port.write(spos + "a"); 
    gx = x/80;
  gy = 100-x/120;

}

and Arduino code:

#include <Servo.h>

Servo neck;

int xPos=0;
int yPos = 0;

void setup()  {
  Serial.begin(9600);
  neck.attach(13);
}

void loop()  {
  if(Serial.available() >0)  {
    xPos=Serial.read();
    yPos=Serial.read();
  }
  int xServo = map(xPos, 0, 320, 20, 70);
  int yServo = map(yPos, 0, 240, 20, 70);
  neck.write(xServo);
  Serial.println("testing....");  
}

thanks in advance!!!

thanks in advance!!!

Your welcome.

Did you have a problem you need help with?

Hi, the subject interests me too. XD

may i know where the processing code placed? is it on the arduino or on the openCV?

im trying to use openCV too, but im confused how to combine openCV and arduino inside a robot. any suggestion? do i have to add another processor on the robot?

thanks!

@lucandut

Although I can't help with original topic, I can tell you that yes, to enable visual processing (e.g. opencv) on a robot, you will need a more powerful processor than an atmega (arduino). Processing, for example, is run on a computer and is used to communicate over usb-->serial with an arduino, which is running its own code (independent of the visual processing code - it's just getting commands on what to do if whatever command is sent to it from the computer)

jezuz:
@lucandut

Although I can't help with original topic, I can tell you that yes, to enable visual processing (e.g. opencv) on a robot, you will need a more powerful processor than an atmega (arduino). Processing, for example, is run on a computer and is used to communicate over usb-->serial with an arduino, which is running its own code (independent of the visual processing code - it's just getting commands on what to do if whatever command is sent to it from the computer)

I get it.. thanks for the help! :slight_smile:

I am working on a batting robot which tracks the ball and positions the servos of the arms in appropriate positions to hit the ball in time. I have the tracking software working on my computer but I do not know how to communicate the coordinates obtained from this software to the arduino, which will position the servos. I am using OpenCV for tracking purposes and have an Arduino Mega. Thanks in advance.

but I do not know how to communicate the coordinates obtained from this software to the arduino

The PC can talk to the Arduino using the serial port. What "coordinates" do you need to send? What kind of servos do you have? If they are standard 0 to 180 servos, sending two bytes - one for each servo - containing a value from 0 to 180 requires the minimum time to process, transmit, receive, and prepare to use.

Could you please tell me how did you setup your OpenCV. Whenever I run my processing IDE they show me error saying that "The library is not compatible with the version of processing". Could you help me out which library i should use?