1

我使用opencv 2.4.4,我想用java检测和跟踪人脸。我在互联网上找到了如下示例。

例子

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

float last = millis();
float interval = 100;
int startTime;
int noFaces = 0;
int noFacesPrev = 0;
int dx = 0;
int dy = 0;
int dwidth = 400;
int dheight = 400;

OpenCV opencv;

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



void setup() {

    size( 800,800 );

    opencv = new OpenCV( this );
    opencv.movie( "egypt.mov", 320, 240 );          // load movie file
    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" );

}

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

void draw() {

    // grab a new frame
    opencv.read();
    opencv.contrast( contrast_value );
    opencv.brightness( brightness_value );

    // proceed detection
    Rectangle[] faces = opencv.detect( 1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40 ); // 40,40 is the smallest size its looking for

 // draw face area(s)
  noFaces = faces.length;

  if (faces.length >=1 && noFaces > noFacesPrev) {
    startTime = millis();
    println(startTime);
    println(noFaces);
    println(noFacesPrev);
    noFacesPrev = noFaces;
  }
  if (faces.length == 0) {  // if there is no face, start over
    println("no face");
    startTime = 0;
    noFacesPrev = 0;
  }

 if (startTime > 0) {
    println("started");
    if(millis() - startTime >interval && (faces.length >=1)) {
      for ( int i = 0; i= 800){
      dy = dy+400;
      dx = 0;
      }
      if (dy >= 800){
      dy = 0;
      dx = 0; 
      }
      }
      startTime=0;
      println ("clock reset");
      noFacesPrev=0;
    }
  }
}

// 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 keyPressed()
{

  if (key == 's') {
    opencv.stop(); 
    super.stop();
  }
}

当我想在我的项目中尝试此代码时,我无法初始化“ OpenCV opencv;”!另外,我无法导入“hypermedia.video”!

4

1 回答 1

0

OPENCV 不是数据类型。如果要检测图像中的人脸,可以在代码中导入 OpenCV 库。

并在调用任何 openCV 方法之前在 main 方法中调用 System.loadLibrary("opencv_java244") 。

您可以在OpenCV页面上找到代码和文档。

于 2013-04-23T19:56:47.540 回答