import gohai.glvideo.*;
import gab.opencv.*;
import java.awt.Rectangle;
import processing.io.*;

SPI adc;
GLCapture video;
OpenCV opencv;
Rectangle[] faces;
int threshold = 60;

void setup()
{
  //init adc
  adc = spiInit(0);

  //set window size
  size(640, 480, P2D);

  //init camera
  String device[] = GLCapture.list();
  video = new GLCapture(this, device[0]);
  video.play(); 

  //load opencv
  opencv = new OpenCV(this, 640, 480);

  //using color or gray
  opencv.useColor();

  //load cascade data
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
}

void draw()
{  
  if (video.available())
  {
    //read camera image
    video.read();
    opencv.loadImage(video);

    //show image
    image(opencv.getOutput(), 0, 0, 640, 480);

    //face detect
    faces = opencv.detect();

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

    //get value
    int val = getAdcValue(adc, 0);
    println("val   :" + val);
    println("faces :"+ faces.length);    

    if (val > threshold && faces.length > 0)
    {
      println("upload image");
      PImage temp = video;
      Upload("http://localhost/pictures/upload.php", temp);
      say("画像をアップロードします");
      delay(5000);
    }
  }
}