/usr/share/perl5/FuzzyOcr/Scanset.pm is in fuzzyocr 3.6.0-9.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | # <@LICENSE>
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to you under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# </@LICENSE>
package FuzzyOcr::Scanset;
use lib qw(..);
use FuzzyOcr::Logging qw(errorlog);
sub new {
my ($class, $label, $preprocessors, $command, $args, $output_in) = @_;
bless {
"label" => $label,
"preprocessors" => $preprocessors,
"command" => $command,
"args" => $args,
"force_output_in" => $output_in,
"hit_counter" => 0
}, $class;
}
sub run {
my ($self, $input) = @_;
my $conf = FuzzyOcr::Config::get_config();
my $tmpdir = FuzzyOcr::Config::get_tmpdir();
my $label = $self->{label};
my $output = "$tmpdir/scanset.$label.out";
my $stderr = ">$tmpdir/scanset.$label.err";
my @result;
my $retcode;
my $stdin = undef;
my $stdout = undef;
my $rcmd = $self->{command};
#Replace supported scanner macros by full path
if ($rcmd =~ m/^\$/) {
my $t = 'focr_bin_'.substr($rcmd,1);
$rcmd = $conf->{$t} if defined $conf->{$t};
}
if (defined $self->{args}) {
$rcmd .= ' ' . $self->{args};
}
# First, run all preprocessors
my $preprocessors = $self->{preprocessors};
if ($preprocessors) {
$preprocessors =~ s/ //g;
my @prep = split(',', $preprocessors);
foreach (@prep) {
my $proc = FuzzyOcr::Config::get_preprocessor($_);
my $plabel = $proc->{label};
my $command = $proc->{command};
if (defined $proc->{args}) {
$command .= ' ' . $proc->{args};
}
$retcode = $proc->run($input);
if ($retcode<0) {
errorlog("Cannot find/execute preprocessor($plabel): $command");
return ($retcode,@result);
} elsif ($retcode>0) {
errorlog("Error running preprocessor($plabel): $command");
open ERR, "<$tmpdir/prep.$plabel.err";
@result = <ERR>;
close ERR;
return ($retcode,@result);
}
# Input of next processor is output of last
# Output name of maketiff is special!
if ($plabel eq "maketiff") {
$input = "$tmpdir/prep.$plabel.tif";
} else {
$input = "$tmpdir/prep.$plabel.out";
}
}
}
# All preprocessors done, filename with result of last is in $input
# Does the scanner expect input from file or from stdin?
if($rcmd =~ /\$input/) {
$rcmd =~ s/\$input/$input/;
} else {
$stdin = "<$input";
}
# Does it output to file or to stdout?
if($rcmd =~ /\$output/) {
$rcmd =~ s/\$output/$output/;
} else {
$stdout = ">$output";
}
# Run scanner
my $out_in = $self->{force_output_in};
# Scanset enforces OCR output in file $out_in (for example TesserAct has multiple files as output)
if ($out_in) {
$out_in =~ s/\$output/$output/;
$out_in =~ s/\$tmpdir/$tmpdir/;
$retcode = FuzzyOcr::Misc::save_execute($rcmd, $stdin, $stdout, $stderr);
unless ( open(INFILE, "<$out_in") ) {
errorlog("Unable to read output from \"$out_in\" for scanset $self->{label}");
$stderr =~ tr/>|</ /;
if (open(INFILE, "<$stderr")) {
@result = <INFILE>;
close(INFILE);
return ($retcode, @result);
}
}
@result = <INFILE>;
close(INFILE);
} else {
($retcode, @result) = FuzzyOcr::Misc::save_execute($rcmd, $stdin, $stdout, $stderr, 1);
}
# If there were errors in the scan, return the errors instead of OCR results
if ($retcode>0) {
$stderr =~ tr/>|</ /;
open(INFILE, "<$stderr");
@result = <INFILE>;
close(INFILE);
}
# Return scanner results and return code
return ($retcode, @result);
}
1;
|