Skip to content

Instantly share code, notes, and snippets.

@yuroyoro
Created October 9, 2009 08:34
Show Gist options
  • Save yuroyoro/205867 to your computer and use it in GitHub Desktop.
Save yuroyoro/205867 to your computer and use it in GitHub Desktop.
import java.awt.*;
import java.awt.image.MemoryImageSource;
import hypermedia.video.OpenCV;
public class AnimeFaceDetection extends Frame {
OpenCV cv = null;
AnimeFaceDetection(String imagePath) {
super();
cv = new OpenCV();
cv.loadImage(imagePath);
// 顔の検出
cv.cascade( "./haarcascades/haarcascade_animeface2.xml");
Rectangle[] squares = cv.detect( 1.2f, 2, OpenCV.HAAR_DO_ROUGH_SEARCH,20, 20 );
this.setBounds( 0, 0, cv.width, cv.height );
this.setVisible( true );
MemoryImageSource mis = new MemoryImageSource( cv.width, cv.height, cv.pixels(), 0, cv.width );
Image frame = createImage( mis );
Graphics2D g = (Graphics2D)this.getGraphics();
g.drawImage( frame, 0, 0, null );
// 顔の部分を赤四角で囲む。
g.setColor( Color.RED );
BasicStroke wideStroke = new BasicStroke(4.0f);
g.setStroke(wideStroke);
for( Rectangle rect : squares ){
g.drawRect( rect.x, rect.y, rect.width, rect.height );
}
}
@Override
public synchronized void dispose(){
// OpenCVインスタンスの破棄。
//このサンプルは連続で実行すると写真が表示されないというバグがあります。)
cv.dispose();
}
public static void main( String[] args ) {
new AnimeFaceDetection(args[0]);// 引数はイメージファイルのフルパス。
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment