Image-ObjectDetect

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

NAME
    Image::ObjectDetect - detects objects from picture(using opencv)

SYNOPSIS
      use Image::ObjectDetect;

      my $cascade = 'haarcascade_frontalface_alt2.xml';
      my $file = 'picture.jpg';
      my $detector = Image::ObjectDetect->new($cascade);
      @faces = $detector->detect($file);
      for my $face (@faces) {
          print $face->{x}, "\n";
          print $face->{y}, "\n";
          print $face->{width}, "\n";
          print $face->{height}, "\n";
      }
      # or just like this
      my @faces = detect_objects($cascade, $file);

DESCRIPTION
    Image::ObjectDetect is a simple module to detect objects from picture
    using opencv.

    It is available at: http://sourceforge.net/projects/opencvlibrary/

METHODS
    new($cascade)
        Returns an instance of this module.

    detect($file)
        Detects objects from picture.

FUNCTIONS
    detect_objects($cascade, $file)
        Detects objects from picture.

EXPORT
    detect_objects

AUTHOR
    Jiro Nishiguchi <jiro@cpan.org>

    This library is free software; you can redistribute it and/or modify it
    under the same terms as Perl itself.

eg/facedetect.pl  view on Meta::CPAN

use strict;
use warnings;

use Pod::Usage;
use Getopt::Long;
use Imager;
use Image::ObjectDetect;

Getopt::Long::Configure('bundling');
GetOptions(
    'cascade=s' => \my $cascade,
    'output=s'  => \my $output,
    'input=s'   => \my $input,
    'version|v' => \my $version,
    'help|h'    => \my $help,
);
if ($version) {
    print "Image::ObjectDetect version $Image::ObjectDetect::VERSION\n";
    exit;
}
pod2usage(0) if $help or !$cascade or !$output or !$input;

my $detector = Image::ObjectDetect->new($cascade);
my @faces = $detector->detect($input);
my $image = Imager->new->read(file => $input);
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,

eg/facedetect.pl  view on Meta::CPAN


=head1 NAME

facedetect.pl - detects faces from picture.

=head1 SYNOPSIS

facedetect.pl [options]

 Options:
   -c -cascade        cascade file
   -o -output         output filename
   -i -input          input filename
   -v -version        print version
   -h -help           print this help

 See also:
   perldoc Image::ObjectDetect

=head1 DESCRIPTION

lib/Image/ObjectDetect.pm  view on Meta::CPAN

__END__

=head1 NAME

Image::ObjectDetect - detects objects from picture(using opencv)

=head1 SYNOPSIS

  use Image::ObjectDetect;

  my $cascade = 'haarcascade_frontalface_alt2.xml';
  my $file = 'picture.jpg';
  my $detector = Image::ObjectDetect->new($cascade);
  @faces = $detector->detect($file);
  for my $face (@faces) {
      print $face->{x}, "\n";
      print $face->{y}, "\n";
      print $face->{width}, "\n";
      print $face->{height}, "\n";
  }
  # or just like this
  my @faces = detect_objects($cascade, $file);

=head1 DESCRIPTION

Image::ObjectDetect is a simple module to detect objects from picture using opencv.

It is available at: http://sourceforge.net/projects/opencvlibrary/

=head1 METHODS

=over 4

=item new($cascade)

Returns an instance of this module.

=item detect($file)

Detects objects from picture.

=back

=head1 FUNCTIONS

=over 4

=item detect_objects($cascade, $file)

Detects objects from picture.

=back

=head1 EXPORT

detect_objects

=head1 AUTHOR

lib/Image/ObjectDetect.xs  view on Meta::CPAN

#define NEED_sv_2pv_nolen
#include "ppport.h"
#include "cv.h"
#include "highgui.h"

MODULE = Image::ObjectDetect		PACKAGE = Image::ObjectDetect

PROTOTYPES: ENABLE

SV *
new(class, cascade_name)
        SV *class;
        char *cascade_name;
    PREINIT:
        CvHaarClassifierCascade *cascade;
        SV *self;
    CODE:
        cascade = cvLoad(cascade_name, 0, 0, 0);
        if (!cascade)
            croak("Can't load the cascade file");
        self = newSViv(PTR2IV(cascade));
        self = newRV_noinc(self);
        sv_bless(self, gv_stashpv(SvPV_nolen(class), 1));
        RETVAL = self;
    OUTPUT:
        RETVAL

SV *
xs_detect(self, filename)
        SV *self;
        char *filename;
    PREINIT:
        IplImage *img, *gray;
        int i;
        CvMemStorage *storage;
        CvHaarClassifierCascade *cascade;
        CvSeq *objects;
        CvRect *rect;
        AV *retval;
        HV *hash;
    CODE:
        img = cvLoadImage(filename, 1);
        if (!img)
            croak("Can't load the image file");

        gray = cvCreateImage(cvSize(img->width, img->height), 8, 1);
        cvCvtColor(img, gray, CV_BGR2GRAY);
        cvEqualizeHist(gray, gray);

        storage = cvCreateMemStorage(0);
        cascade = INT2PTR(CvHaarClassifierCascade *, SvIV(SvRV(self)));
        objects = cvHaarDetectObjects(gray, cascade, storage,
#if (CV_MAJOR_VERSION < 2 || (CV_MAJOR_VERSION == 2 && CV_MINOR_VERSION < 1))
                1.1, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(0, 0));
#else
                1.1, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(0, 0), cvSize(0, 0));
#endif

        retval = newAV();
        for (i = 0; i < (objects ? objects->total : 0); i++) {
            rect = (CvRect *) cvGetSeqElem(objects, i);
            hash = newHV();

lib/Image/ObjectDetect.xs  view on Meta::CPAN

        cvReleaseImage(&gray);

        RETVAL = newRV_noinc((SV *) retval);
    OUTPUT:
        RETVAL

void
DESTROY(self)
        SV *self;
    PREINIT:
        CvHaarClassifierCascade *cascade;
    CODE:
        cascade = INT2PTR(CvHaarClassifierCascade *, SvIV(SvRV(self)));
        cvReleaseHaarClassifierCascade(&cascade);

t/01_detect.t  view on Meta::CPAN

use strict;
use warnings;

use Test::More;
use Image::ObjectDetect;

my $cascade;
if (exists $ENV{CASCADE_NAME}) {
    $cascade = $ENV{CASCADE_NAME};
} else {
    $cascade = '/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml';
    unless (-f $cascade) {
        Test::More->import(skip_all => "no cascade file found, skipped.");
        exit;
    }
}
plan tests => 7;

my @faces = detect_objects($cascade, 't/test.jpg');
is(scalar @faces, 5); # 5 persons

my $face = shift @faces;
ok(exists $face->{x});
ok(exists $face->{y});
ok(exists $face->{width});
ok(exists $face->{height});

# OO interface
my $detector = Image::ObjectDetect->new($cascade);
isa_ok($detector, 'Image::ObjectDetect');
ok($detector->detect('t/test.jpg'));



( run in 0.998 second using v1.01-cache-2.11-cpan-49f99fa48dc )