Skip to content

Commit

Permalink
add simple demo
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Mar 26, 2023
1 parent 890f023 commit e71aa89
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
81 changes: 81 additions & 0 deletions Demo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package PDL::Demos::OpenCV;
use PDL::OpenCV;

sub info {('opencv', 'OpenCV')}

sub init {'
use PDL::OpenCV;
'}
my @demo = (
[comment => <<'EOF'
Welcome to a basic demo of the PDL binding of OpenCV.

It will show you some of its capabilities, especially the famous
"Hello world" of computer vision: opening up your webcam, and doing
basic image-processing from it.
EOF
],

[act => q|
# load the modules for this demo
use PDL::LiteF;
use PDL::OpenCV::Videoio;
use PDL::OpenCV::Imgproc;
use PDL::OpenCV::Objdetect;
use PDL::OpenCV::Highgui;
|],

[act => q|
# initialise some objects
$cap = PDL::OpenCV::VideoCapture->new4(0);
$lsd = PDL::OpenCV::LineSegmentDetector->new;
($facedata) = grep -f, map "$_/haarcascades/haarcascade_frontalface_alt.xml",
qw(/usr/share/opencv4 /usr/local/share/opencv4);
$cc = $facedata ? PDL::OpenCV::CascadeClassifier->new2($facedata) : undef;
|],

[act => q|
print <<'EOF';
Toggles: 'l' lines, 'm' mirror, 'f' face-detection (if Haar cascades found)
'q' in the window quits
EOF
($showlines, $mirror, $face) = (1, 1, 1);
while (1) {
($image,$res) = $cap->read;
last if !$res;
$image = $image->slice(',-1:0') if $mirror;
$gray = cvtColor($image, COLOR_BGR2GRAY);
if ($showlines) {
($lines) = $lsd->detect($gray);
$lsd->drawSegments($image, $lines) if !$lines->isempty;
}
if ($cc and $face) {
($objects) = $cc->detectMultiScale(equalizeHist($gray));
rectangle2($image, $objects, [0,255,255,0]); # broadcasting
}
imshow 'frame', $image;
$key = waitKey(1) & 0xFF;
$showlines = !$showlines if $key == ord 'l';
$mirror = !$mirror if $key == ord 'm';
$face = !$face if $key == ord 'f';
last if $key == ord 'q';
}
destroyWindow 'frame';
waitKey(1); # pump event loop so window actually closes
$cap->release;
|],
);

sub demo { @demo }

1;

=head1 NAME

PDL::Demos::OpenCV - demonstrate PDL::OpenCV capabilities

=head1 SYNOPSIS

pdl> demo opencv

=cut
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Changes
classes.pl
constlist.txt
Demo.m
doxyparse.pl
funclist.pl
genpp.pl
Expand Down
2 changes: 1 addition & 1 deletion MANIFEST.SKIP
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
\.pm$
\.sw[op]$
~$
Makefile(\.old)?
Makefile(\.old)?$
\.old$
^blib
pm_to_blib$
Expand Down
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ $hash{clean}{FILES} .= join ' ', '', map "$_.h $_.cpp", qw(opencv_wrapper wraplo
$hash{LIBS}[0] .= $libs;
$hash{INC} .= " $inc";
$hash{OBJECT} .= join ' ', '', map $_.'$(OBJ_EXT)', qw(wraplocal);
$hash{PM}{'Demo.m'} = '$(INST_LIB)/PDL/Demos/OpenCV.pm';

use ExtUtils::CppGuess;
my $guess = ExtUtils::CppGuess->new;
Expand Down

0 comments on commit e71aa89

Please sign in to comment.