Arduino Uno + Processing + OpenCV (Face Tracking Robot)

I need help kind Sirs! I am a beginner and I have not much knowledge of these programming codes and languages.
But I need the most is can someone please help me put a buzzer code on these program for my finals project.

I need a buzzer code for this project and I've tried many codes but it won't work. When a face is detected it should alarm or buzz. Using a simple buzzer for about 15 ~ 20 seconds or more.

This project is from SparkFun Face Tracking Robot. These codes are not mine its from a developer of SparkFun.

I've tweaked the code a bit and this is working with Arduino Uno + Processing + OpenCV, 2 Servos and a camera.

I don't own any of these codes but please can anyone help me put codes on the Arduino and Processing for buzzer alarm if a face is detected.

Any help would be appreciated and I will do my best to understand.

Processing Code:

import hypermedia.video.*;  //Include the video library to capture images from the webcam
import java.awt.Rectangle;  //A rectangle class which keeps track of the face coordinates.
import processing.serial.*; //The serial library is needed to communicate with the Arduino.

OpenCV opencv;  //Create an instance of the OpenCV library.

//Screen Size Parameters
int width = 320;
int height = 240;

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

Serial port; // The serial port

//Variables for keeping track of the current servo positions.
char servoTiltPosition = 90;
char servoPanPosition = 90;
//The pan/tilt servo ids for the Arduino serial command interface.
char tiltChannel = 0;
char panChannel = 1;

//These variables hold the x and y location for the middle of the detected face.
int midFaceY=0;
int midFaceX=0;

//The variables correspond to the middle of the screen, and will be compared to the midFace values
int midScreenY = (height/2);
int midScreenX = (width/2);
int midScreenWindow = 10;  //This is the acceptable 'error' for the center of the screen. 

//The degree of change that will be applied to the servo each time we update the position.
int stepSize=1;

void setup() {
  //Create a window for the sketch.
  size( width, height );

  opencv = new OpenCV( this );
  opencv.capture( width, height );                   // open video stream
  opencv.cascade( "C:\\Program Files (x86)\\OpenCV\\data\\haarcascades\\haarcascade_frontalface_alt.xml" );  // load detection description, here-> front face detection : "haarcascade_frontalface_alt.xml"

 // println("Face Detection Robot");

  //select first com-port from the list (change the number in the [] if your sketch fails to connect to the Arduino)
  port = new Serial(this, Serial.list()[1], 57600);   //Baud rate is set to 57600 to match the Arduino baud rate.


  
  //Send the initial pan/tilt angles to the Arduino to set the device up to look straight forward.
  port.write(tiltChannel);    //Send the Tilt Servo ID
  port.write(servoTiltPosition);  //Send the Tilt Position (currently 90 degrees)
  port.write(panChannel);         //Send the Pan Servo ID
  port.write(servoPanPosition);   //Send the Pan Position (currently 90 degrees)
}


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



void draw() {
  // grab a new frame
  // and convert to gray
  opencv.read();
  //opencv.convert( GRAY );
  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,0,0);
  for( int i=0; i<faces.length; i++ ) {
    rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height );
  }
  
  //Find out if any faces were detected.
  if(faces.length > 0){
   
    //If a face was found, find the midpoint of the first face in the frame.
    //NOTE: The .x and .y of the face rectangle corresponds to the upper left corner of the rectangle,
    //      so we manipulate these values to find the midpoint of the rectangle.
    midFaceY = faces[0].y + (faces[0].height/2);
    midFaceX = faces[0].x + (faces[0].width/2);

 //Find out if the Y component of the face is below the middle of the screen.
    if (midFaceY > (midScreenY + midScreenWindow)) {
      if (servoTiltPosition >= 5)servoTiltPosition -= stepSize; //If it is below the middle of the screen, update the tilt position variable to lower the tilt servo.
    }
    //Find out if the Y component of the face is above the middle of the screen.
    else if (midFaceY < (midScreenY - midScreenWindow)) {
      if (servoTiltPosition <= 175)servoTiltPosition +=stepSize; //Update the tilt position variable to raise the tilt servo.
    }
    //Find out if the X component of the face is to the left of the middle of the screen.
    if(midFaceX < (midScreenX - midScreenWindow)){
      if(servoPanPosition >= 5)servoPanPosition -= stepSize; //Update the pan position variable to move the servo to the left.
    }
    //Find out if the X component of the face is to the right of the middle of the screen.
    else if(midFaceX > (midScreenX + midScreenWindow)){
      if(servoPanPosition <= 175)servoPanPosition +=stepSize; //Update the pan position variable to move the servo to the right.
    }
  }
  //Update the servo positions by sending the serial command to the Arduino.
  port.write(tiltChannel);      //Send the tilt servo ID
  port.write(servoTiltPosition); //Send the updated tilt position.
  port.write(panChannel);        //Send the Pan servo ID
  port.write(servoPanPosition);  //Send the updated pan position.
    
    if(faces.length == 0){
    servoTiltPosition = 90;
    servoPanPosition = 90;

    }   
  //delay(1);
}



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

Arduino Code:

#include <Servo.h>  //Used to control the Pan/Tilt Servos

//These are variables that hold the servo IDs.
char tiltChannel=0, panChannel=1;

//These are the objects for each servo.
Servo servoTilt, servoPan;

//This is a character that will hold data from the Serial port.
char serialChar=0;

void setup(){
  servoTilt.attach(2);  //The Tilt servo is attached to pin 2.
  servoPan.attach(3);   //The Pan servo is attached to pin 3.
  servoTilt.write(90);  //Initially put the servos both
  servoPan.write(90);      //at 90 degress.
  
  Serial.begin(57600);  //Set up a serial connection for 57600 bps.
}

void loop(){
  while(Serial.available() <=0);  //Wait for a character on the serial port.
  serialChar = Serial.read();     //Copy the character from the serial port to the variable
  if(serialChar == tiltChannel){  //Check to see if the character is the servo ID for the tilt servo
    while(Serial.available() <=0);  //Wait for the second command byte from the serial port.
    servoTilt.write(Serial.read());  //Set the tilt servo position to the value of the second command byte received on the serial port
  }
  else if(serialChar == panChannel){ //Check to see if the initial serial character was the servo ID for the pan servo.
    while(Serial.available() <= 0);  //Wait for the second command byte from the serial port.
    servoPan.write(Serial.read());   //Set the pan servo position to the value of the second command byte received from the serial port.
  }
  //If the character is not the pan or tilt servo ID, it is ignored.
}

I need help please give me advice or solutions to my problems and if possible please teach me how to do it for dummies :cold_sweat: I don't know much about programming.

That is not how you post code. You need to enclose it in [ code ] [ /code ] tags to stop the forum software from munging it. When posting multiple files you can include the code name inside the [ code ] tag such as [ code=processing ]. You can insert the code tags by editing your post, selecting the code and clicking the # button in the editor window.

Do you intend your solution to move the cemera round as well as sound the buzzer? If so you would need to redefine the message format between the Processing and Arduino parts of your solution so that it indicated whether the sound was on. You'd need to change the Processing side to decide when to turn the sound on and off and include that in the messages it sends, and change the Arduino sketch to turn the buzzer on or off based on the received value.

No, I'm not going to write that code for you. If you expect to get academic credit for it, it should be your own work.

Another approach you might consider is making the Processing application output the sound via the PC buzzer or speakers.

Oh Thank You Sir. I don't know how to post a code here I am new and Thanks for the advice!

Sorry if I was using this forum to make people make a code for me because I am just desperate, frustrated and tired.

I am not a very good programmer. But I am taking note about what you said PC speaker output would be nice I will look unto it.

I've been working on the code for a week now and every time I put in a code it buzz or doesn't compile.

My apologies for my rudeness but I don't really know much about programming. I don't have much knowledge in programming and I am sorry for now :frowning:

If a face is detected it should buzz at least 10 ~ 15 seconds. I am just working on a solution here and I need professional help :frowning: . I don't know anyone that could help me with the project so I resorted to go to forums and such. But any help will do for me or any suggestions :cold_sweat:

NewbieCopyPaster:
But any help will do for me or any suggestions :cold_sweat:

If you follow the advice I gave you to post your code correctly you will make it easier for people to understand, and that will improve your chances of somebody responding to your code. However, if you want somebody to work with/for you rather than help you solve the problem for yourself, you would be better off asking in the Gigs and Collaborations section. Note that most people would expect to be paid for working for you, and also that trying to pass other people's work off as your own is a very bad idea. It would IMO be far better to solve this problem for yourself, and if so this is the right place to ask for advice about doing it, but don't expect us to write the code for you.

Thank you sir. Sorry for posting something rude or anything. My deepest apologies. Thank you for the advice. I will refrain from posting something like making a code for me. I'll put in just, can anyone give me help or advice :cold_sweat:

Hey PeterH dont be a asshole, the dude is new to programming and just wants some help.

Do add the buzzer i would recommend to alter the code in processing, in the part where it checks if a face is detected add a line of code so that it outputs a byte to the serial montitor as a char or a int eg. port.write("A");

And then in the arduino code, write some code to check the serial port for the letter A , and if it recieves it, activtate the buzzer.