forked from os-autoinst/os-autoinst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocr.pm
65 lines (57 loc) · 1.6 KB
/
ocr.pm
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
package ocr;
use strict;
use warnings;
our $gocrbin = "/usr/bin/gocr";
if (!-x $gocrbin) { $gocrbin = undef }
# input: image ref
sub get_ocr($$@) {
my $ppm = shift;
my $gocrparams = shift || "";
my @ocrrect = @{$_[0]};
if (!$gocrbin || !@ocrrect) { return "" }
if (@ocrrect != 4) { return " ocr: bad rect" }
return unless $ppm;
my $ppm2 = $ppm->copyrect(@ocrrect);
if (!$ppm2) { return "" }
my $tempname = "ocr.$$-" . time . rand(10000) . ".ppm";
$ppm2->write($tempname) or return " ocr error writing $tempname";
# init DB file:
if (!-e "db/db.lst") {
mkdir "db";
open(my $fd, ">db/db.lst");
close $fd;
}
open(my $pipe, "$gocrbin -l 128 -d 0 $gocrparams $tempname |") or return "failed to exec $gocrbin: $!";
local $/;
my $ocr = <$pipe>;
close($pipe);
unlink $tempname;
return $ocr;
}
# input: image ref, area
# FIXME: pass options
# FIXME: write C library bindings instead of system()
sub tesseract($;$$) {
my $img = shift;
my $area = shift;
my $imgfn = 'ocr.png';
my $txtfn = 'ocr'; # tesseract appends .txt automatically o_O
my $txt;
if ($area) {
$img = $img->copyrect($area->{'xpos'}, $area->{'ypos'}, $area->{'width'}, $area->{'height'});
}
$img->write($imgfn);
if (system('tesseract', $imgfn, $txtfn) == 0) {
$txtfn .= '.txt';
if (open(my $fh, '<:encoding(UTF-8)', $txtfn)) {
local $/;
$txt = <$fh>;
close $fh;
}
}
unlink $imgfn;
unlink $txtfn;
return $txt;
}
1;
# vim: set sw=4 et: