[29119] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # AdvImagePlugin.pm -- extends the standard ImagePlugin with feature
|
---|
| 4 | # extraction using the OpenSIFT program. SIFT, in
|
---|
[29120] | 5 | # turn, requires the OpenCV libraries.
|
---|
[29119] | 6 | #
|
---|
| 7 | # A component of the Greenstone digital library software from the New
|
---|
| 8 | # Zealand Digital Library Project at the University of Waikato, New
|
---|
| 9 | # Zealand.
|
---|
| 10 | #
|
---|
| 11 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
| 12 | #
|
---|
| 13 | # This program is free software; you can redistribute it and/or modify
|
---|
| 14 | # it under the terms of the GNU General Public License as published by
|
---|
| 15 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 16 | # (at your option) any later version.
|
---|
| 17 | #
|
---|
| 18 | # This program is distributed in the hope that it will be useful,
|
---|
| 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 21 | # GNU General Public License for more details.
|
---|
| 22 | #
|
---|
| 23 | # You should have received a copy of the GNU General Public License
|
---|
| 24 | # along with this program; if not, write to the Free Software
|
---|
| 25 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 26 | #
|
---|
| 27 | ###########################################################################
|
---|
| 28 |
|
---|
| 29 | package AdvImagePlugin;
|
---|
| 30 |
|
---|
| 31 | use ImagePlugin;
|
---|
| 32 |
|
---|
| 33 | use strict;
|
---|
| 34 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
| 35 | no strict 'subs';
|
---|
| 36 |
|
---|
| 37 | use gsprintf 'gsprintf';
|
---|
| 38 |
|
---|
| 39 | sub BEGIN {
|
---|
| 40 | @AdvImagePlugin::ISA = ('ImagePlugin');
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | my $arguments =
|
---|
| 44 | [ { 'name' => "process_exp",
|
---|
| 45 | 'desc' => "{BasePlugin.process_exp}",
|
---|
| 46 | 'type' => "regexp",
|
---|
| 47 | 'deft' => &get_default_process_exp(),
|
---|
| 48 | 'reqd' => "no" },
|
---|
| 49 | ];
|
---|
| 50 |
|
---|
| 51 | my $options = { 'name' => "AdvImagePlugin",
|
---|
| 52 | 'desc' => "{ImagePlugin.desc}",
|
---|
| 53 | 'abstract' => "no",
|
---|
| 54 | 'inherits' => "yes",
|
---|
| 55 | 'args' => $arguments };
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | ## @function
|
---|
| 59 | #
|
---|
| 60 | sub new {
|
---|
| 61 | my ($class) = shift (@_);
|
---|
| 62 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 63 | push(@$pluginlist, $class);
|
---|
| 64 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 65 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
| 66 | my $self = new ImagePlugin($pluginlist, $inputargs, $hashArgOptLists);
|
---|
| 67 | $self->{'sift_available'} = 0;
|
---|
| 68 | return bless $self, $class;
|
---|
| 69 | }
|
---|
| 70 | ##
|
---|
| 71 |
|
---|
| 72 |
|
---|
| 73 | ## @function
|
---|
| 74 | #
|
---|
| 75 | sub init {
|
---|
| 76 | my $self = shift (@_);
|
---|
| 77 | my ($verbosity, $outhandle, $failhandle) = @_;
|
---|
| 78 | $self->SUPER::init(@_);
|
---|
| 79 | # Test that OpenSIFT is available... otherwise nothing else is going to work
|
---|
| 80 | my $sift_version = `siftfeat -h 2>&1`;
|
---|
| 81 | if ($sift_version =~ /siftfeat version \d+\.\d+\.\d+/)
|
---|
| 82 | {
|
---|
| 83 | $self->{'sift_available'} = 1;
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | ##
|
---|
| 87 |
|
---|
| 88 |
|
---|
| 89 | ## @function
|
---|
| 90 | # - for now we'll restrict to JPGs because I know OpenSIFT likes them
|
---|
| 91 | sub get_default_process_exp {
|
---|
| 92 | my $self = shift (@_);
|
---|
| 93 | return q^(?i)\.(jpe?g)$^;
|
---|
| 94 | }
|
---|
| 95 | ##
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | ## @function
|
---|
| 99 | # do plugin specific processing of doc_obj
|
---|
| 100 | sub process {
|
---|
| 101 | my $self = shift (@_);
|
---|
| 102 | my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
|
---|
| 103 |
|
---|
| 104 | $self->SUPER::process($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli);
|
---|
| 105 |
|
---|
| 106 | my $outhandle = $self->{'outhandle'};
|
---|
| 107 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 | if ($self->{'sift_available'} == 1)
|
---|
| 111 | {
|
---|
| 112 | # we call SIFT on the image, and then read the Oxford formatted features
|
---|
| 113 | # from STDOUT
|
---|
| 114 | # any other messages on STDERR are sent to nullsville
|
---|
| 115 | my $sift_command = 'siftfeat -x "' . $filename_full_path . '" 2>/dev/null';
|
---|
| 116 | my $oxfd_features = `$sift_command`;
|
---|
| 117 | $doc_obj->add_metadata ($doc_obj->get_top_section(), "SIFTOXFDFeatures", $oxfd_features);
|
---|
| 118 | }
|
---|
| 119 | else
|
---|
| 120 | {
|
---|
| 121 | print STDERR 'Warning! SIFT features cannot be extracted: ' . $file . "\n";
|
---|
| 122 | }
|
---|
| 123 | return 1;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | 1;
|
---|
| 127 |
|
---|
| 128 |
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 |
|
---|
| 137 |
|
---|