/usr/share/perl5/FuzzyOcr/Hashing.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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | # <@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>
use strict;
package FuzzyOcr::Hashing;
use base 'Exporter';
our @EXPORT_OK = qw(check_image_hash_db
add_image_hash_db
calc_image_hash);
use lib qw(..);
use FuzzyOcr::Config qw(get_thresholds get_config set_config get_tmpdir get_mysql_ddb);
use FuzzyOcr::Misc qw(save_execute);
use FuzzyOcr::Logging qw(debuglog errorlog warnlog infolog);
use Fcntl;
use Fcntl ':flock';
#Implements all functions related to Image Hashing
sub within_threshold {
my $thresref = get_thresholds();
my %Threshold = %$thresref;
my $digest = shift;
my $record = shift;
my ($dimg,$dkey) = split('::',$digest);
my ($rimg,$rkey) = split('::',$record);
my ($ds, $dh, $dw, $dcn) = split(':',$dimg);
my ($rs, $rh, $rw, $rcn) = split(':',$rimg);
return(0) unless $rs;
return(0) unless $rh;
return(0) unless $rw;
return(0) unless $rcn;
return(0) unless $rkey;
return(0) if ((abs($ds - $rs ) / $rs ) > $Threshold{s});
return(0) if ((abs($dh - $rh ) / $rh ) > $Threshold{h});
return(0) if ((abs($dw - $rw ) / $rw ) > $Threshold{w});
return(0) if ((abs($dcn - $rcn) / $rcn) > $Threshold{cn});
my @rcf = split('::',$rkey);
my @dcf = split('::',$dkey);
my (@dcfs, @rcfs);
foreach (@dcf) { push @dcfs,split(':',$_); }
foreach (@rcf) { push @rcfs,split(':',$_); }
my $total = scalar(@rcfs);
if ($total == scalar(@dcfs)) {
my $match = 0;
foreach (0 .. ($total-1)) {
$match++ if (abs($dcfs[$_] - $rcfs[$_]) <= $Threshold{c});
}
infolog("image matched <$match> of <$total> colors");
return(1) if ($match == $total);
}
return(0);
}
sub check_image_hash_db {
my $conf = get_config();
if ($conf->{focr_enable_image_hashing} == 0) {
warnlog("Image Hashing is disabled");
return (0,'');
}
my $digest = $_[0];
my $dbfile = $_[1] || $conf->{focr_db_hash};
my $fname = $_[2];
my $ctype = $_[3];
my $ftype = $_[4] || 0;
my ($img, $key) = split('::', $digest,2);
return (0,'') unless defined $key;
my $now = time;
my $hash = $digest;
my $ret = 0; my $txt = 'Exact';
my $dinfo;
if ($conf->{focr_enable_image_hashing} == 3) {
unless (defined $conf->{focr_mysql_ddb}) {
warnlog("Connection to MySQL server unavailable");
return (0,'');
}
my $ddb = $conf->{focr_mysql_ddb};
my $db = $conf->{focr_mysql_db};
my $sql = qq(select * from $db.$dbfile where $dbfile.key='$key');
my @data = $ddb->selectrow_array($sql);
my $next = 0;
my $when = 0;
my $match = 0;
if ((scalar(@data)>0) and ($img eq $data[1])) {
$match++;
infolog("Found[$dbfile]: Score='$data[8]' Info: '$data[9]'");
$next = $data[5]; $next++;
$when = $data[7]; $data[8] += 0;
$ret = $data[8] == 0 ? 0.001 : $data[8];
$dinfo = $data[9] || '';
if ($data[2] eq '') {
infolog("Updating $txt info File-Name:'$fname'");
$ddb->do(qq(update $db.$dbfile set $dbfile.fname=? where $dbfile.key='$key'),undef,$fname);
}
if ($data[3] eq '') {
infolog("Updating $txt info Content-Type:'$ctype'");
$ddb->do(qq(update $db.$dbfile set $dbfile.ctype=? where $dbfile.key='$key'),undef,$ctype);
}
if ($data[4] != $ftype) {
infolog("Updating $txt info File-Type:'$ftype'");
$ddb->do(qq(update $db.$dbfile set $dbfile.ftype=? where $dbfile.key='$key'),undef,$ftype);
}
}
unless ($match) {
my $then = time - ($conf->{focr_db_max_days}*86400);
$sql = qq(select * from $db.$dbfile order by $dbfile.check);
my $sth = $ddb->prepare($sql); $sth->execute;
while (my @row = $sth->fetchrow_array) {
my $hash2 = $row[1] || "0:0:0:0";
$hash2 .= "::$row[0]";
if (within_threshold($digest,$hash2)) {
$txt = 'Approx';
$key = $row[0];
$next = $row[5] + 1;
$when = $row[7] || $now;
$ret = $dbfile eq $conf->{focr_mysql_hash} ? $row[8] : $row[5];
$dinfo = $row[9] || '';
infolog("Found[$dbfile]: Score='$row[8]' Info: '$row[9]'");
last;
}
}
# Expire old records...
$sql = qq(delete from $db.$dbfile where $dbfile.check < $then);
debuglog($sql,2);
$ddb->do($sql);
}
if ($ret > 0) {
if ($dbfile eq $conf->{focr_mysql_hash}) {
infolog("Found Score <$ret> for $txt Image Hash");
}
infolog("Matched [$next] time(s). Prev match: ".fmt_time($now - $when));
$sql = qq(update $db.$dbfile set $dbfile.match='$next',$dbfile.check='$now' where $dbfile.key='$key');
$ddb->do($sql);
debuglog($sql);
}
return ($ret,$dinfo);
}
elsif ($conf->{focr_enable_image_hashing} == 2) {
import MLDBM qw(DB_File Storable);
my %DB = (); my $dbm; my $sdbm;
$sdbm = tie %DB, 'MLDBM::Sync', $dbfile, O_CREAT|O_RDWR or $ret++;
if ($ret>0) {
warnlog("No Image Hash database found at \"$dbfile\", or permissions wrong.");
return (0,'');
}
$sdbm->Lock;
if (defined $DB{$key}) {
$dbm = $DB{$key};
if ($img eq $dbm->{basic}) {
$ret = $dbm->{score} || 0.001;
$dinfo = $dbm->{dinfo} || '';
$dbm->{fname} = $fname;
$dbm->{ctype} = $ctype;
infolog("Updating $txt info File:'$fname' Type:'$ctype'");
$DB{$key} = $dbm;
}
}
if ($ret == 0) {
my $then = time - ($conf->{focr_db_max_days}*86400);
foreach my $k (keys %DB) {
$dbm = $DB{$k};
$hash = $dbm->{basic} ? $dbm->{basic} : "0:0:0:0::$k";
if (within_threshold($digest,$hash)) {
$ret = $dbfile eq $conf->{focr_db_hash} ? $dbm->{score} : $dbm->{match};
$txt = 'Approx'; $dinfo = $dbm->{dinfo} || '';
infolog("Found in: <$dbfile>");
last;
}
# Has the record expired??
$dbm->{check} = $now - 1 unless defined $dbm->{check};
if ($dbm->{check} < $then) {
infolog("Expiring <$k> older than $conf->{focr_db_max_days} days");
delete $DB{$k};
}
}
}
if ($ret>0) {
$dbm->{match}++;
if ($dbfile eq $conf->{focr_db_hash}) {
$ret = sprintf("%0.3f",$dbm->{score});
infolog("Found Score <$ret> for $txt Image Hash");
}
infolog("Matched [$dbm->{match}] time(s). Prev match: ".fmt_time(time - $dbm->{check}));
$dbm->{check} = time;
$DB{$key} = $dbm;
}
$sdbm->UnLock;
undef $sdbm;
untie %DB;
return ($ret,$dinfo);
}
elsif ($conf->{focr_enable_image_hashing} == 1) {
$ret = open HASH, $conf->{focr_digest_db};
unless($ret) {
warnlog("No Image Hash database found at \"$conf->{focr_digest_db}\", or permissions wrong.");
return (0,'');
}
while (<HASH>) {
chomp;
($ret,$hash) = split('::',$_,2);
if (within_threshold($digest,$hash)) {
infolog("Found Score <$ret> for Hash <$hash>");
return ($ret,'');
}
}
close HASH;
return (0,'');
}
}
sub add_image_hash_db {
my $conf = get_config();
return if ($conf->{focr_enable_image_hashing} == 0);
my $digest = $_[0];
my $score = $_[1];
my $ret = 0;
if ($conf->{focr_enable_image_hashing} == 3) {
unless (defined $conf->{focr_mysql_ddb}) {
warnlog("Connection to MySQL server unavailable");
return;
}
my $db = $conf->{focr_mysql_db};
my $ddb = $conf->{focr_mysql_ddb};
my $table = $_[2] || $conf->{focr_mysql_hash};
my $fname = $_[3] || '';
my $ctype = $_[4] || 'image';
my $ftype = $_[5] || 0;
my $dinfo = $_[6] || '';
infolog("Adding Hash to table: \"$db.$table\" with score \"$score\"");
my $sql;
my ($img,$key) = split('::',$digest,2);
if (defined $key) {
$sql = "select basic from $db.$table where $table.key='$key'";
my @data = $ddb->selectrow_array($sql);
if (scalar(@data)>0) {
if ($conf->{focr_mysql_update_hash}) {
infolog("Hash already in $db.$table updating...");
$sql = "update $db.$table set ";
my @params;
unless ($data[1] eq $img) {
$sql .= "basic=?,"; push @params,$img;
}
unless ($data[2] eq $fname) {
$sql .= "fname=?,"; push @params,$fname;
}
unless ($data[3] eq $ctype) {
$sql .= "ctype=?,"; push @params,$ctype;
}
unless ($data[4] == $ftype) {
$sql .= "ftype=?,"; push @params,$ftype;
}
unless ($data[8] == $score) {
$sql .= "score=?,"; push @params,$score;
}
unless ($data[9] == $dinfo) {
$sql .= "dinfo=?,"; push @params,$dinfo;
}
$sql =~ s/,$//;
$sql .= " where $table.key='$key'";
$ddb->do($sql,undef,@params);
foreach my $p (@params) { $sql =~ s/\?/$p/; }
debuglog($sql);
} else {
infolog("Hash already in $db.$table skipping...");
}
} else {
my @params = (
$key, $img, $fname, $ctype, $ftype,
($table eq $conf->{focr_mysql_hash} ? 0 : 1),
time, time, $score, $dinfo);
$sql = "insert into $db.$table values (?,?,?,?,?,?,?,?,?,?)";
$ddb->do($sql,undef,@params);
foreach my $p (@params) { $sql =~ s/\?/$p/; }
debuglog($sql);
}
}
}
elsif ($conf->{focr_enable_image_hashing} == 2) {
import MLDBM qw(DB_File Storable);
my $dbfile = $_[2] || $conf->{focr_db_hash};
my %DB = (); my $sdbm;
$sdbm = tie %DB, 'MLDBM::Sync', $dbfile, O_CREAT|O_RDWR or $ret++;
if ($ret>0) {
warnlog("Unable to open/create Image Hash database at \"$dbfile\", check permissions.");
return;
}
$sdbm->Lock;
infolog("Adding Hash to \"$dbfile\" with score \"$score\"");
my ($img,$key) = split('::',$digest,2);
if (defined $key) {
my $dbm = $DB{$key};
$dbm->{fname} = $_[3];
$dbm->{ctype} = $_[4];
$dbm->{ftype} = $_[5];
$dbm->{dinfo} = $_[6];
$dbm->{basic} = $img;
$dbm->{score} = $score;
$dbm->{input} =
$dbm->{check} = time;
$dbm->{match} = $dbfile eq $conf->{focr_db_hash} ? 0 : 1;
$DB{$key} = $dbm;
}
$sdbm->UnLock;
undef $sdbm;
untie %DB;
}
elsif ($conf->{focr_enable_image_hashing} == 1) {
if (-e $conf->{focr_digest_db}) {
$ret = open DB, ">>$conf->{focr_digest_db}";
} else {
$ret = open DB, ">$conf->{focr_digest_db}";
}
unless ($ret) {
warnlog("Unable to open/create Image Hash database at \"$conf->{focr_digest_db}\", check permissions.");
return;
}
infolog("Adding Hash to \"$conf->{focr_digest_db}\"");
flock( DB, LOCK_EX );
seek( DB, 0, 2 );
print DB "${score}::${digest}\n";
close(DB);
}
debuglog("Digest: $digest");
}
sub calc_image_hash {
my $conf = get_config();
my $imgdir = get_tmpdir();
my $thresref = get_thresholds();
my %Threshold = %$thresref;
my $pfile = $_[0];
my $pic = $_[1];
my $hash;
foreach my $a (qw/ppmhist/) { #pamfile
unless (defined $conf->{"focr_bin_$a"}) {
errorlog("calc_image_hash cannot exec $a");
return (1, '');
}
}
unless (-r $pfile) {
errorlog("Cannot read $pfile");
return(1, '');
}
my ($r, @stdout_data) = save_execute(
"$conf->{focr_bin_ppmhist} -noheader $pfile", undef,
">$imgdir/ppmhist.info",
">/dev/null", 1);
if ($r) {
chomp $r;
errorlog("$conf->{focr_bin_ppmhist}: ".
($r<0) ? 'Timed out' : 'Error'
." [$r], skipping...");
return (1, '');
}
my $cnt = 0;
my $c = scalar(@stdout_data);
my $s = (stat($pfile))[7] || 0;
$hash = sprintf "%d:%d:%d:%d",$s,
defined $pic->{height} ? $pic->{height} : 0,
defined $pic->{width} ? $pic->{width} : 0,
$c;
if ($Threshold{max_hash}) {
foreach (@stdout_data) {
$_ =~ s/ +/ /g;
my(@d) = split(' ', $_);
$hash .= sprintf("::%d:%d:%d:%d:%d",@d);
if ($cnt++ ge $Threshold{max_hash}) {
last;
}
}
}
debuglog("Got: <$hash>");
return(0, $hash);
}
sub fmt_time {
my $when = $_[0];
my $ret;
if ($when>86400) {
my $d = int($when/86400);
$when -= $d*86400;
$ret = "$d days,";
}
if ($when>3600) {
my $h = int($when/3600);
$when -= $h*3600;
$ret .= " $h hrs.";
}
if ($when>60) {
my $m = int($when/60);
$when -= $m*60;
$ret .= " $m min.";
}
if ($when>0) {
$ret .= " $when sec.";
}
$ret .= " ago";
return $ret;
}
1;
|