2011年12月27日 星期二

Perl臉部辨識

事先聲名:以下程式碼我沒試過,預防以後用的到,純粹是做做筆記而已。



#!/usr/local/bin/perl
use strict;
use warnings;

use Imager;
use Image::ObjectDetect;

my $file = 'picture.jpg';
my $image = Imager->new->read(file => $file);
my $cascade = '/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml';
my $detector = Image::ObjectDetect->new($cascade);
my @faces = $detector->detect($file);
# or my @faces = detect_objects($cascade, $file);
for my $face (@faces) {
    $image->box(
        xmin   => $face->{x},
        ymin   => $face->{y},
        xmax   => $face->{x} + $face->{width},
        ymax   => $face->{y} + $face->{height},
        color  => 'red',
        filled => 0,
    );
}
$image->write(file => 'output.jpg');

底下是把臉涂黑的作法。
#!/usr/bin/perl
use strict;
use warnings;

use Imager;
use Image::ObjectDetect;

my $file = 'picture.jpg';
my $image = Imager->new->read(file => $file);
my $cascade = '/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml';
my $detector = Image::ObjectDetect->new($cascade);
my @faces = $detector->detect($file);

for my $face (@faces) {
    $image->box(
        xmin   => $face->{x},
        ymin   => $face->{y},
        xmax   => $face->{x} + $face->{width},
        ymax   => $face->{y} + $face->{height},
                color  => 'black',
                filled => 1,
    );
}
$image->write(file => 'output.jpg');

print "Content-type: text/html\n\n";
print "\n";
print "\n";
print "\n";
print "顔を黒く塗りつぶしてみるテスト\n";
print "\n";
print "\n";
print "変換した畫像が下に表示されます。

\n";
print "
\n";
print "\n";
print "\n";
print "\n";


參考引用來源:
http://d.hatena.ne.jp/spiritloose/20070308/1173314595 
http://pastebin.com/6VTWz2qz 
http://webcache.googleusercontent.com/search?q=cache:BnpuDysd1pkJ:zapanet.info/blog/item/940+Image::ObjectDetect+perl&cd=31&hl=zh-TW&ct=clnk&gl=tw

沒有留言:

張貼留言