line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package PDF::OCR; |
2
|
1
|
|
|
1
|
|
21009
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
3
|
1
|
|
|
1
|
|
5
|
use vars qw($VERSION $DEBUG); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
44
|
|
4
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
692
|
|
5
|
|
|
|
|
|
|
$VERSION = sprintf "%d.%02d", q$Revision: 1.11 $ =~ /(\d+)/g; |
6
|
|
|
|
|
|
|
|
7
|
0
|
|
|
0
|
0
|
0
|
sub DEBUG : lvalue { $DEBUG } |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
1
|
|
|
1
|
1
|
447
|
my($class, $arg) = @_; |
11
|
1
|
50
|
|
|
|
6
|
$arg or croak("missing argument to constructor"); |
12
|
1
|
|
|
|
|
2
|
my $self = {}; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
|
|
10
|
require Cwd; |
16
|
1
|
50
|
|
|
|
62
|
my $abs = Cwd::abs_path($arg) |
17
|
|
|
|
|
|
|
or croak("[$arg] not resolving with Cwd::abs_path()"); |
18
|
|
|
|
|
|
|
|
19
|
1
|
50
|
|
|
|
8
|
$abs=~/\.pdf$/i |
20
|
|
|
|
|
|
|
or croak("[$arg] cant match pdf ext in filename"); |
21
|
|
|
|
|
|
|
|
22
|
1
|
50
|
|
|
|
19
|
-f $abs |
23
|
|
|
|
|
|
|
or croak("[$abs] is not file."); |
24
|
|
|
|
|
|
|
|
25
|
1
|
|
|
|
|
2
|
$self->{abs_pdf} = $abs; |
26
|
|
|
|
|
|
|
|
27
|
1
|
|
|
|
|
3
|
bless $self,$class; |
28
|
1
|
|
|
|
|
6
|
return $self; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub abs_pdf { |
32
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
33
|
0
|
|
|
|
|
0
|
return $self->{abs_pdf}; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub abs_tmp { |
37
|
0
|
|
|
0
|
0
|
0
|
my $self= shift; |
38
|
0
|
|
|
|
|
0
|
return $self->{abs_tmp}; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub abs_images { |
43
|
2
|
|
|
2
|
1
|
3
|
my $self = shift; |
44
|
|
|
|
|
|
|
|
45
|
2
|
50
|
|
|
|
7
|
unless( defined $self->{abs_images}){ |
46
|
2
|
|
|
|
|
851
|
require PDF::GetImages; |
47
|
0
|
|
|
|
|
0
|
my $images = PDF::GetImages::pdfimages($self->abs_pdf); |
48
|
0
|
0
|
|
|
|
0
|
unless( scalar @$images ){ |
49
|
0
|
|
|
|
|
0
|
warn("no images in ".$self->abs_pdf); |
50
|
0
|
|
|
|
|
0
|
$self->{abs_images} = []; |
51
|
0
|
|
|
|
|
0
|
return []; |
52
|
|
|
|
|
|
|
} |
53
|
0
|
|
|
|
|
0
|
$self->{abs_images} = $images; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
0
|
return $self->{abs_images}; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub abs_images_count { |
60
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
61
|
0
|
|
|
|
|
0
|
return scalar @{$self->abs_images}; |
|
0
|
|
|
|
|
0
|
|
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub get_ocr { |
68
|
0
|
|
|
0
|
1
|
0
|
my ($self,$abs)= @_; |
69
|
|
|
|
|
|
|
|
70
|
0
|
0
|
|
|
|
0
|
if(defined $abs){ |
71
|
0
|
|
|
|
|
0
|
return $self->_get_ocr($abs); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
0
|
my $ocr = join( "\f", @{$self->get_ocr_arrayref} ); |
|
0
|
|
|
|
|
0
|
|
75
|
0
|
|
|
|
|
0
|
return $ocr; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub _get_ocr { |
83
|
0
|
|
|
0
|
|
0
|
my ($self,$abs)= @_; |
84
|
0
|
0
|
|
|
|
0
|
defined $abs or confess('missing arg'); |
85
|
0
|
|
0
|
|
|
0
|
$self->{ocr} ||={}; |
86
|
0
|
0
|
|
|
|
0
|
unless( defined $self->{ocr}->{$abs} ){ |
87
|
|
|
|
|
|
|
# TODO could check that this *IS* one of the files extracted |
88
|
0
|
|
|
|
|
0
|
require Image::OCR::Tesseract; |
89
|
0
|
|
|
|
|
0
|
my $content = Image::OCR::Tesseract::get_ocr($abs); |
90
|
0
|
|
|
|
|
0
|
$self->{ocr}->{$abs} = $content; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
0
|
return $self->{ocr}->{$abs}; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub get_ocr_arrayref { |
100
|
0
|
|
|
0
|
1
|
0
|
my ($self)= @_; |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
0
|
my $ocr = []; |
103
|
0
|
|
|
|
|
0
|
for( @{$self->abs_images} ){ |
|
0
|
|
|
|
|
0
|
|
104
|
0
|
|
|
|
|
0
|
push @$ocr, $self->get_ocr($_); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
0
|
return $ocr; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub cleanup { |
112
|
1
|
|
|
1
|
1
|
1
|
my $self = shift; |
113
|
|
|
|
|
|
|
|
114
|
1
|
|
|
|
|
3
|
for(@{$self->abs_images}){ |
|
1
|
|
|
|
|
5
|
|
115
|
0
|
|
|
|
|
0
|
unlink $_; |
116
|
|
|
|
|
|
|
} |
117
|
0
|
|
|
|
|
0
|
return 1; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub DESTROY { |
121
|
1
|
|
|
1
|
|
3
|
my $self = shift; |
122
|
1
|
|
|
|
|
4
|
$self->cleanup; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
1; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
__END__ |