Changeset 40

Show
Ignore:
Timestamp:
17.11.2006 23:13:02 (2 years ago)
Author:
decoder
Message:

Added:

- Option --learn-ham, adds given picture file into ham db
- Option --learn-spam, adds given picture file into spam db

Fixed:

Script now recognizes file type of given file correctly according to the extension

Todo:

Parse config also for db file locations

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/devel/Utils/fuzzy-find

    r4 r40  
    1 #!/usr/local/bin/perl 
     1#!/usr/bin/perl 
    22use Getopt::Long; 
    33use MLDBM qw(DB_File Storable); 
     
    77    );   
    88 
    9 my %App = ( 
    10     identify  => '/usr/bin/identify', 
    11     ppmhist   => '/usr/local/netpbm/bin/ppmhist', 
    12     jpegtopnm => '/usr/local/netpbm/bin/jpegtopnm', 
    13     giftopnm => '/usr/local/netpbm/bin/giftopnm', 
    14     pngtopnm => '/usr/local/netpbm/bin/pngtopnm', 
    15     bmptopnm => '/usr/local/netpbm/bin/bmptopnm', 
    16     ); 
     9# defaults 
     10my $cfgfile = "FuzzyOcr.cf"; 
     11my %App; 
     12my @bin_utils = qw/pamfile ppmhist jpegtopnm giftopnm pngtopnm bmptopnm/; 
     13foreach (@bin_utils) { 
     14    $App{$_} = "/usr/bin/$_"; 
     15
    1716 
    1817my $delete = 0; 
    1918my $verbose = 0; 
     19my $learn_ham = 0; 
     20my $learn_spam = 0; 
    2021GetOptions( 
    2122    'verbose' => \$verbose, 
    2223    'delete'  => \$delete, 
     24    'learn-ham' => \$learn_ham, 
     25    'learn-spam' => \$learn_spam, 
    2326); 
     27 
     28unless (@ARGV) { 
     29    print "Usage: fuzzy-find.pl [Options] (imagehash|imagefile) \n"; 
     30    print "\n"; 
     31    print "Available options:\n"; 
     32    print "--delete     Removes the hash from the database\n"; 
     33    print "--learn-ham  Add the hash as ham to the database\n"; 
     34    print "--learn-spam Add the hash as spam to the database\n"; 
     35    print "--verbose    Show more informations\n"; 
     36    print "\n"; 
     37    exit 1; 
     38} 
     39 
     40# Read custom paths from FuzzyOcr.cf 
     41open CONFIG, "< $cfgfile" or warn "Can't read configuration file, using defaults...\n"; 
     42 
     43while (<CONFIG>) { 
     44    chomp; 
     45    if ($_ =~ m/^focr_bin_(\w+) (.+)/) { 
     46        $App{$1} = $2; 
     47        printf "Found custom path \"$2\" for application \"$1\"\n" if $verbose 
     48    } 
     49} 
     50 
     51close CONFIG; 
    2452 
    2553while (@ARGV) { 
     
    3563    } 
    3664    my $key = ''; 
     65    my $ctype = ''; 
    3766    unless (@data) { 
    38         my $res = `/usr/bin/identify $file`; 
     67        my $app; 
     68        if (($file =~ m/\.jpg$/i) or ($file =~ m/\.jpeg$/i)) { 
     69            $app = $App{jpegtopnm}; 
     70            $ctype = "image/jpeg"; 
     71        } elsif ($file =~ m/\.png$/i) { 
     72            $app = $App{pngtopnm}; 
     73            $ctype = "image/png"; 
     74        } elsif ($file =~ m/\.bmp$/i) { 
     75            $app = $App{bmptopnm}; 
     76            $ctype = "image/bmp"; 
     77        } elsif ($file =~ m/\.pnm$/i) { 
     78            $ctype = "image/pnm"; 
     79            $app = '/bin/cat'; 
     80        } elsif ($file =~ m/\.gif$/i) { 
     81            $ctype = "image/gif"; 
     82            $app = $App{giftopnm}; 
     83        } else { 
     84            print "Unknown extension given in \"$file\", aborting...\n"; 
     85            exit 1; 
     86        } 
     87        my @hist = `$app $file |$App{ppmhist} -noheader -`; 
     88        my @res = `$app $file |$App{pamfile} -`; 
    3989        my ($h,$w) = (0,0); 
    40         if ($res =~ /(\d+)x(\d+)/) { 
    41             $h=$1;$w=$2; 
     90        if ($res[0] =~ m/(\d+) by (\d+)/) { 
     91            $w = $1; $h = $2; 
    4292            printf "Found ($h,$w)\n" if $verbose 
    4393        } 
    44         my $app; 
    45         if ($res =~ m/JPEG/) { 
    46             $app = $App{jpegtopnm}; 
    47         } elsif ($res =~ m/PNG/) { 
    48             $app = $App{pngtopnm}; 
    49         } elsif ($res =~ m/BMP/) { 
    50             $app = $App{bmptopnm}; 
    51         } elsif ($res =~ m/PNM/) { 
    52             $app = '/bin/cat'; 
    53         } else { 
    54             $app = $App{giftopnm}; 
    55         } 
    56         my @hist = `$app $file |$App{ppmhist} -noheader -`; 
    5794        my $c = scalar(@hist); my $cnt = 0; 
    5895        printf "Colors: %d\n",$c if $verbose; 
     
    68105    printf "Img = %9d %dx%dx%d\n",@data; 
    69106    printf "key = <$key>\n" if ($key); 
     107    if ($learn_spam || $learn_ham) { 
     108        my %DB; 
     109        my $ff = $learn_spam ? 'db_hash' : 'db_safe'; 
     110        tie %DB, 'MLDBM', $Files{$ff} or die "Can't open $ff"; 
     111        print "Adding key to database...\n"; 
     112        if (defined $key) { 
     113            my $dbm = $DB{$key}; 
     114            $dbm->{fname} = $file; 
     115            $dbm->{ctype} = $ctype; 
     116            $dbm->{dinfo} = "Manually added to the database\n"; 
     117            $dbm->{basic} = join(':', @data); 
     118            $dbm->{score} = $learn_spam ? 10 : 0; 
     119            $dbm->{input} = 
     120            $dbm->{check} = time; 
     121            $dbm->{match} = $learn_spam ? 0 : 1; 
     122            $DB{$key} = $dbm; 
     123        } 
     124        untie %DB; 
     125        exit 0; 
     126    } else { 
    70127    foreach my $ff (keys %Files) { 
    71128        my %DB; 
     
    95152        untie %DB; 
    96153    } 
     154    } 
    97155}