source: gs2-extensions/ocr/trunk/perllib/plugins/OCRImageConverter.pm@ 30110

Last change on this file since 30110 was 30110, checked in by davidb, 6 years ago

Zach's initial auto-loading support module for OCRing using tesseract

File size: 6.9 KB
Line 
1package OCRImageConverter;
2
3use BaseMediaConverter;
4
5use strict;
6no strict 'refs'; # allow filehandles to be variables and viceversa
7no strict 'subs'; # allow barewords (eg STDERR) as function arguments
8
9use gsprintf 'gsprintf';
10
11# these two variables mustn't be initialised here or they will get stuck
12# at those values.
13our $ocrimage_conversion_available;
14our $no_ocrimage_conversion_reason;
15
16BEGIN {
17 @OCRImageConverter::ISA = ('BaseMediaConverter');
18
19 # Check that PDFBox is installed and available on the path
20 $ocrimage_conversion_available = 1;
21
22 if (!defined $ENV{'GEXTOCR'}) {
23 $ocrimage_conversion_available = 0;
24 $no_ocrimage_conversion_reason = "gextocrnotinstalled";
25 } else {
26 my $gextocr_home = $ENV{'GEXTOCR'};
27 #my $pbajar = &FileUtils::filenameConcatenate($gextpb_home,"lib","java","pdfbox-app.jar");
28 my $tesseract = &FileUtils::filenameConcatenate($gextocr_home, "installed", "cmdline", "bin", "tesseract");
29
30 if (!-e $tesseract) {
31 &gsprintf(STDERR, "**** Failed to find $tesseract\n");
32 $ocrimage_conversion_available = 0;
33 $no_ocrimage_conversion_reason = "gexttesseractnotinstalled";
34 } else {
35 my $cmd = "\"$tesseract\" -v ";
36 if ($ENV{'GSDLOS'} =~ /^windows/i) {
37 $cmd .= " >nul 2>&1"; # java 2>&1 >null or java >null 2>&1 both work (%ERRORLEVEL% is 0)
38 } else {
39 # On Ubuntu, java >/dev/null 2>&1 works,
40 # but java 2>&1 >/dev/null doesn't work: output goes to screen anyway
41 $cmd .= " >/dev/null 2>&1"; # " >/dev/null 2>&1 &" - don't need & at end for Linux Centos anymore (Ubuntu was already fine without it)
42 }
43
44 my $status = system($cmd);
45
46 if ($status != 0) {
47 my $error_message = "**** Testing for Tesseract\n";
48 $error_message .= "Failed to run: $cmd\n";
49 $error_message .= "Error variable: |$!| and status: $status\n";
50
51 &gsprintf(STDERR, "OCRImageConverter: $error_message");
52
53 $ocrimage_conversion_available = 0;
54 $no_ocrimage_conversion_reason = "couldnotruntesseract";
55 }
56 }
57 }
58
59}
60
61my $arguments = [ ];
62
63my $options = {
64 'name' => "OCRImageConverter",
65 'desc' => "{OCRImageConverter.desc}",
66 'abstract' => "yes",
67 'inherits' => "yes",
68 'args' => $arguments
69};
70
71sub new {
72 my ($class) = shift (@_);
73 my ($pluginlist, $inputargs, $hashArgOptLists, $auxilary) = @_;
74 push(@$pluginlist, $class);
75
76 push(@{$hashArgOptLists->{"ArgList"}}, @{$arguments});
77 push(@{$hashArgOptLists->{"OptList"}}, $options);
78
79
80 my $self = new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists, $auxilary);
81
82 if ($self->{'info_only'}) {
83 # don't worry about any options etc
84 return bless $self, $class;
85 }
86
87 if ($ocrimage_conversion_available) {
88 my $gextocr_home = $ENV{'GEXTOCR'};
89 my $tesseract = &FileUtils::filenameConcatenate($gextocr_home, "installed", "cmdline", "bin", "tesseract");
90 my $lang = "eng"; # TODO
91 my $launch_cmd = "\"$tesseract\" -l $lang ";
92
93 $self->{'ocrimage_launch_cmd'} = $launch_cmd;
94 } else {
95 $self->{'no_ocrimage_conversion_reason'} = $no_ocrimage_conversion_reason;
96
97 my $outhandle = $self->{'outhandle'};
98 &gsprintf($outhandle, "OCRImageConverter: {OCRImageConverter.noconversionavailable} ({OCRImageConverter.$no_ocrimage_conversion_reason})\n");
99 }
100
101 $self->{'ocrimage_conversion_available'} = $ocrimage_conversion_available;
102
103 return bless $self, $class;
104}
105
106sub init {
107 my $self = shift(@_);
108 my ($verbosity, $outhandle, $failhandle) = @_;
109
110 $self->{'ocrtmp_file_paths'} = ();
111}
112
113sub deinit {
114 my $self = shift(@_);
115
116 $self->clean_up_temporary_files();
117}
118
119sub convert {
120 my $self = shift(@_);
121 my ($source_file_full_path) = @_;
122
123 print STDERR "***** WE ARE WORKING *****\n";
124
125 # TODO cache
126 if (lc(substr($source_file_full_path, length($source_file_full_path) - 4)) eq ".gif") {
127 # need to convert to another format first
128 #my $target_source = substr($source_file_full_path, length($source_file_full_path) - 4) . ".tif";
129 my $converted_file = &util::get_tmp_filename("tif");
130 push(@{$self->{'ocrtmp_file_paths'}}, $converted_file);
131
132 my $imagick_cmd = "\"" . &util::get_perl_exec() . "\" -S gs-magick.pl convert \"$source_file_full_path\" \"$converted_file\"";
133 system($imagick_cmd);
134
135 print STDERR "*** Magick command: $imagick_cmd\n";
136
137 $source_file_full_path = $converted_file;
138 print STDERR "*** Converted file: $converted_file\n";
139 }
140
141 my $source_file_no_path = &File::Basename::basename($source_file_full_path);
142
143 return 0 unless $ocrimage_conversion_available;
144 return 0 if (!-f $source_file_full_path);
145
146 my $outhandle = $self->{'outhandle'};
147 my $verbosity = $self->{'verbosity'};
148
149 my $target_file_path;
150
151 if ($self->{'enable_cache'}) {
152 $self->init_cache_for_file($source_file_full_path);
153 my $cache_dir = $self->{'cached_dir'};
154 my $file_root = $self->{'cached_file_root'};
155
156 $target_file_path = &FileUtils::filenameConcatenate($cache_dir, $file_root);
157 } else {
158 $target_file_path = &util::get_tmp_filename("txt");
159 push(@{$self->{'ocrtmp_file_paths'}}, $target_file_path);
160 }
161
162 my $convert_cmd = "";
163 my ($tailname, $dirname, $suffix) = &File::Basename::fileparse($source_file_full_path, "\\.[^\\.]+\$");
164
165 $convert_cmd = $self->{'ocrimage_launch_cmd'};
166 $convert_cmd .= " \"$source_file_full_path\" \"" . substr($target_file_path, 0, length($target_file_path) - 4) . "\"";
167
168 #&gsprintf(STDERR, "OCRImageConverter convert command: $convert_cmd\n");
169 print STDERR "OCRImageConverter convert command: $convert_cmd\n";
170
171 my $print_info = {
172 'message_prefix' => "OCR Conversion",
173 'message' => "Converting $source_file_no_path."
174 };
175
176 my ($regenerated, $result, $had_error) = $self->autorun_general_cmd($convert_cmd, $source_file_full_path, $target_file_path, $print_info);
177
178 if ($had_error) {
179 return (0, $result, $target_file_path);
180 } else {
181 return (1, $result, $target_file_path);
182 }
183}
184
185sub convert_without_result {
186 my $self = shift(@_);
187
188 my $source_file_path = shift(@_);
189 my $target_file_type = shift(@_);
190 my $convert_options = shift(@_) || "";
191 my $convert_id = shift(@_) || "";
192
193 return $self->convert($source_file_path,$target_file_type,
194 $convert_options,$convert_id,"without_result");
195}
196
197sub clean_up_temporary_files {
198 my $self = shift(@_);
199
200 foreach my $ocrtmp_file_path (@{$self->{'ocrtmp_file_paths'}}) {
201 if (-d $ocrtmp_file_path) {
202 #print STDERR "@@@@@@ cleanup called on $pbtmp_file_path\n";
203 &FileUtils::removeFilesRecursive($ocrtmp_file_path);
204 } elsif (-e $ocrtmp_file_path) {
205 &FileUtils::removeFiles($ocrtmp_file_path);
206 }
207 }
208
209 $self->{'ocrtmp_file_paths'} = ();
210}
211
2121;
Note: See TracBrowser for help on using the repository browser.