use v5.12; use utf8; use lib qw{./lib}; use Image::Detect::Model; use Image::Detect::Model::Schema; use Image::Characteristics; use File::Find; use JSON; use Path::Class; use Encode; use Config::Any; if ( !@ARGV ) { print "$0 \n"; exit 0; } my $cfg = Config::Any->load_files({ files => [ qw/config.yml/ ], use_ext => 1, })->[0]{q/config.yml/}; my $model = Image::Detect::Model->new( connect_info => [ @{$cfg->{database}}{qw/dsn user pass/}, $cfg->{database}{opts} ], ); my @algorithms = $model->search('algorithms'); my $jsonp = JSON->new->utf8(1); find( { wanted => \&wanted, no_chdir => 1, }, @ARGV); sub wanted { my $path = $File::Find::name; return if !-f $path || $path !~ m{\.(?:jpe?g|png|gif)$}i; print "processing $path...\n"; $path = file($path)->absolute->stringify; my $imager; my $imgc; eval { $imager = Imager->new(file => $path); $imgc = Image::Characteristics->new(imager => $imager); }; if ( $@ ) { say STDERR $@; next; } $path = decode('cp932', $path); return if $model->search_by_sql('select count(*) as count from files where path=?', [$path ] )->next->count; my $txn = $model->txn_scope; my $file = $model->insert('files', { path => $path, aspect_ratio => $imgc->aspect_ratio, img_width => $imager->getwidth, img_height => $imager->getheight, }); for my $algorithm ( @algorithms ) { my $c; eval { $c = $imgc->calculate( rows => 5, cols => 5, algorithm => $algorithm->name, ); }; if ( $@ ) { print STDERR $@; next; } my @c = flat(@$c); if ( $algorithm->name eq "rgb_average" ) { @c = map { $_ / 255.0 } @c; } $model->insert('characteristics', { file_id => $file->id, algorithm_id => $algorithm->id, characteristics => $jsonp->encode(\@c), }); } $txn->commit; } sub flat { map { 'ARRAY' eq ref $_ ? flat(@$_) : $_ } @_ }