[OpenCV] problems with serial writing of facedetection to arduino, to use it in a if-statement
in
Contributed Library Questions
•
10 months ago
Hello, for my study project I want to integrate face detection to use it in a if-statement to output a signal with arduino.
So what I want to accomplish:
If Processing/OpenCV detects a face, it writes the number of faces to my arduino. And in arduino I want a simple statement that it only digitalWrites to a pin when the number of faces is 1 and above. The problem is, that at the moment, the led turns on when it detects my face. After covering the camera it turns off, and after removing my finger and let it recognize my face, it turns back on. But then it stays on, i can't seem to turn it off as it should.
EDIT: It seems that the led turns off at a random time, so sometimes it is very fast (even faster then waiting for 1000 ms) and sometimes it takes really long (about 10 sec). It even seems that, how longer it detects my face, how longer the led stays on. So if I let it detect my face for a short time, the led will go turn off faster.
Hopefully I am not too vague and that my english is good enough to understand.
The code in Processing
- import hypermedia.video.*;
- import java.awt.Rectangle;
- import processing.serial.*;
- OpenCV opencv;
- Serial port;
- // contrast/brightness values
- int contrast_value = 0;
- int brightness_value = 0;
- void setup() {
- size( 640, 480 );
- opencv = new OpenCV( this );
- opencv.capture( width, height ); // open video stream
- opencv.cascade( "C:\\OpenCV\\data\\haarcascades\\haarcascade_frontalface_alt.xml" ); // load detection description, here-> front face detection : "haarcascade_frontalface_alt.xml"
- println(Serial.list());
- port = new Serial(this, Serial.list()[0],9600);
- // 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" );
- }
- 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 );
- port.write(faces.length);
- }
- }
- /**
- * Changes contrast/brigthness values
- */
- void mouseDragged() {
- contrast_value = (int) map( mouseX, 0, width, -128, 128 );
- brightness_value = (int) map( mouseY, 0, width, -128, 128 );
- }
The code on my Arduino
- const int ledPin = 13; // the pin that the LED is attached to
- int incomingByte;
- void setup() {
- Serial.begin(9600);
- pinMode(ledPin, OUTPUT);
- }
- void loop() {
- if (Serial.available() > 0) {
- incomingByte = Serial.read();
- if (incomingByte = 1) {{
- digitalWrite(ledPin, HIGH);
- delay (1000);
- digitalWrite(ledPin, LOW);
- }
- }
- }
- }
1