forked from os-autoinst/os-autoinst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocr.pm
36 lines (33 loc) · 889 Bytes
/
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
package ocr;
use strict;
use warnings;
use ppm;
our $gocrbin="/usr/bin/gocr";
if(!-x $gocrbin) {$gocrbin=undef}
# input: ref on PPM data
sub get_ocr($$@) {
my $dataref=shift; my $gocrparams=shift||""; my @ocrrect=@{$_[0]};
if(!$gocrbin || !@ocrrect) {return ""}
if(@ocrrect!=4) {return " ocr: bad rect"}
my $ppm=ppm->new($$dataref);
return unless $ppm;
my $ppm2=$ppm->copyrect(@ocrrect);
if(!$ppm2) {return ""}
my $tempname="/dev/shm/$$-".time.rand(10000).".ppm";
open(my $tempfile, ">", $tempname) or return " ocr error writing $tempname";
print $tempfile $ppm2->toppm;
close $tempfile;
# 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;
}
1;