Changeset 36988


Ignore:
Timestamp:
2022-12-07T22:45:39+13:00 (17 months ago)
Author:
davidb
Message:

Code refactor to GoogleVisionAPI in a separate Perl module so it can be used in another Plugin, such as the to-be-written GoogleVisionPagedImagePlugin

Location:
gs3-extensions/structured-image/trunk/perllib/plugins
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • gs3-extensions/structured-image/trunk/perllib/plugins/GoogleVisionImagePlugin.pm

    r36247 r36988  
    2929
    3030use strict;
    31 no strict 'refs'; # allow filehandles to be variables and viceversa
    32 no strict 'subs';
     31no  strict 'refs'; # allow filehandles to be variables and viceversa
     32no  strict 'subs';
     33
     34use utf8;
     35use JSON qw( from_json );
     36
     37#use Data::Dumper;
    3338
    3439use gsprintf;
     
    3641
    3742use ImagePlugin;
    38 use BaseMediaConverter;
     43use GoogleVisionAPIConverter;
    3944
    40 use utf8;
    41 use JSON qw( from_json );
    4245
    4346sub BEGIN {
    44     @GoogleVisionImagePlugin::ISA = ('ImagePlugin', 'BaseMediaConverter');
     47    @GoogleVisionImagePlugin::ISA = ('ImagePlugin', 'GoogleVisionAPIConverter');
    4548}
    4649
    47 my $arguments =
    48     [ { 'name' => "enable_image_labelling",
    49     'desc' => "{GoogleVisionImagePlugin.enable_image_labelling}",
    50     'type' => "flag",
    51     'reqd' => "no" },
    52     { 'name' => "enable_image_ocr",
    53     'desc' => "{GoogleVisionImagePlugin.enable_image_ocr}",
    54     'type' => "flag",
    55     'reqd' => "no" },
    56     { 'name' => "enable_document_ocr",
    57     'desc' => "{GoogleVisionImagePlugin.enable_document_ocr}",
    58     'type' => "flag",
    59     'reqd' => "no" }
    60 ];
     50my $arguments = [];
     51
    6152
    6253my $options = { 'name'     => "GoogleVisionImagePlugin",
     
    7465    push(@{$hashArgOptLists->{"OptList"}},$options);
    7566
    76     new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
     67    new GoogleVisionAPIConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
    7768    my $self = new ImagePlugin($pluginlist, $inputargs, $hashArgOptLists);
    7869
     
    8071}
    8172
    82 sub begin {
    83     my $self = shift (@_);
    84     my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
    85 
    86     if ($self->{'enable_image_ocr'} && $self->{'enable_document_ocr'}) {
    87         print STDERR "Please use the following command syntax for vision types: (--enable_image_ocr | --enable_document_ocr) [--enable_image_labelling]\n";
    88         print STDERR "\t\t --enable_image_ocr : optical character recognition for text within images\n";
    89         print STDERR "\t\t --enable_document_ocr : optical character recognition for text within documents\n";
    90         print STDERR "\t\t --enable_image_labelling : annotation labeling for objects within images\n";
    91         exit(2);
    92     }
    93 
    94     $self->SUPER::begin(@_);
    95 }
    96 
    97 sub vision_monitor_line {
    98     my ($line) = @_;
    99    
    100     my $had_error = 0;
    101     my $generate_dot = 0;
    102        
    103     if ($line =~ m/^.*$/)
    104     {
    105         $generate_dot = 1;
    106     }
    107 
    108     return ($had_error,$generate_dot);
    109 }
    110 
    111 sub run_convert {
    112     my $self = shift (@_);
    113     my ($base_dir,$filename,$file,$doc_obj) = @_;
    114    
    115     my $section = $doc_obj->get_top_section();
    116    
    117     my $verbosity = $self->{'verbosity'};
    118     my $outhandle = $self->{'outhandle'};
    119     print $outhandle "----- GoogleVisionImagePlugin run_convert -----\n";
    120     # print STDERR "*****", $self->{'enable_document_ocr'}, "\n";
    121 
    122     my @vision_type = (); # array containing target ocr / labelling type(s)
    123 
    124     if ($self->{'enable_image_labelling'}) { push(@vision_type, "enable_image_labelling"); }
    125     if ($self->{'enable_image_ocr'}) { push(@vision_type, "enable_image_ocr"); }
    126     if ($self->{'enable_document_ocr'}) { push(@vision_type, "enable_document_ocr"); }
    127 
    128     my $vision_type_length = @vision_type; # assigning scalar var to array returns length
    129 
    130     if ($vision_type_length != 0) {
    131        
    132         $self->init_cache_for_file($filename);
    133         my $cached_image_dir = $self->{'cached_dir'};
    134         # my $audio_root = $self->{'cached_file_root'};
    135 
    136         # my $filename_no_path = &File::Basename::basename($filename);
    137 
    138         my $ofile = "google-vision-output.json";
    139         my $ofilename = &FileUtils::filenameConcatenate($cached_image_dir,"google-vision-output.json");
    140         my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
    141         my $credentials_filename = &FileUtils::filenameConcatenate($collect_dir, "etc", "atea-storage-cd63a39dfeb5.json");
    142 
    143         if ($vision_type_length == 1) {
    144             my $vision_type_first = $vision_type[0];
    145             my $vision_cmd = "vision.py --$vision_type_first --credentials \"$credentials_filename\" \"$filename\" \"$ofilename\"";
    146 
    147             $self->run_vision($file, $filename, $ofile, $ofilename, $vision_cmd, $vision_type_first, $section, $doc_obj);
    148         } elsif ($vision_type_length == 2) {
    149             my $vision_type_first = $vision_type[0];
    150             my $vision_type_second = $vision_type[1];
    151 
    152             my $ofilename_first = &FileUtils::filenameConcatenate($cached_image_dir, $vision_type_first . "-google-vision-output.json");
    153             my $ofilename_second = &FileUtils::filenameConcatenate($cached_image_dir, $vision_type_second . "-google-vision-output.json");
    154 
    155             my $vision_cmd_1 = "vision.py --$vision_type_first --credentials \"$credentials_filename\" \"$filename\" \"$ofilename_first\"";
    156             my $vision_cmd_2 = "vision.py --$vision_type_second --credentials \"$credentials_filename\" \"$filename\" \"$ofilename_second\"";
    157 
    158             $self->run_vision($file, $filename, $ofile, $ofilename_first, $vision_cmd_1, $vision_type_first, $section, $doc_obj);
    159             $self->run_vision($file, $filename, $ofile, $ofilename_second, $vision_cmd_2, $vision_type_second, $section, $doc_obj);
    160         }
    161     }
    162 
    163     return "json";
    164 }
    165 
    166 sub run_vision {
    167     use Data::Dumper;
    168     my $self = shift (@_);
    169     my ($file, $filename, $ofile, $ofilename, $vision_cmd, $vision_type, $section, $doc_obj) = @_;
    170 
    171     my $vision_regenerated;
    172     my $vision_result;
    173     my $vision_error;
    174 
    175     my $print_info = { 'message_prefix' => "GoogleVisionAPI",
    176         'message' => "Sending $file to GoogleVisionAPI using vision.py" };
    177 
    178     ($vision_regenerated,$vision_result,$vision_error)
    179         = $self->run_cached_general_cmd($vision_cmd,$filename,$ofilename,$print_info);
    180 
    181     $doc_obj->associate_file($ofilename,$vision_type . $ofile,"application/json",$section);
    182 
    183     my $json_text = do { # read in json file
    184         open(my $json_fh, "<:encoding(UTF-8)", $ofilename)
    185             or die("Can't open \"$ofilename\": $!\n");
    186         local $/;
    187         <$json_fh>
    188     };
    189 
    190     my $cursection = $doc_obj->get_top_section(); # get top section for text append
    191    
    192     my $decoded_json = from_json($json_text);
    193     my $ocr_text;
    194     if ($vision_type eq "enable_document_ocr" || $vision_type eq "enable_image_ocr") {
    195         $ocr_text = $decoded_json->{textAnnotations}[0]{description}; # access full ocr content
    196         $doc_obj->add_utf8_text( $cursection, $ocr_text); # append text to section
    197 
    198         my $blocks = $decoded_json->{fullTextAnnotation}{pages}[0]{blocks};
    199         my %text_and_language;
    200 
    201         foreach my $block (@{ $blocks }) {
    202             foreach my $paragraph (@{ $block->{paragraphs} }) {
    203                 foreach my $word (@{ $paragraph->{words} }) {
    204                     my $detected_language = $word->{property}{detectedLanguages}[0]{languageCode} || "no_lang";
    205                     my $word_text = "";
    206                     foreach my $letter (@{ $word->{symbols} }) {
    207                         $word_text .= $letter->{text};
    208                     }
    209                     $text_and_language{$detected_language} .= $word_text . " ";
    210                 }
    211             }
    212         }
    213         for (keys %text_and_language) {
    214             $doc_obj->add_utf8_metadata ($cursection, "z_" . $_, $text_and_language{$_});
    215         }
    216 
    217 
    218     } elsif ($vision_type eq "enable_image_labelling") {
    219         $ocr_text = $decoded_json->{labelAnnotations};
    220         foreach my $label (@{ $ocr_text }) {
    221             # write to metadata : 'description'='Book' 'score'='0.9' 'topicality'='0.9' 'mid'='/m/0123'
    222             $doc_obj->add_utf8_metadata ($cursection, "description", $label->{description});
    223             $doc_obj->add_utf8_metadata ($cursection, "score", $label->{score});
    224             $doc_obj->add_utf8_metadata ($cursection, "topicality", $label->{topicality});
    225             $doc_obj->add_utf8_metadata ($cursection, "mid", $label->{mid});
    226             # write to metadata : 'descriptions'='Book' 'Book_score'='0.9' 'Book_topicality'='0.9' 'Book_mid'='/m/0123'
    227             $doc_obj->add_utf8_metadata ($cursection, "descriptions", $label->{description});
    228             $doc_obj->add_utf8_metadata ($cursection, $label->{description} . "_score", $label->{score});
    229             $doc_obj->add_utf8_metadata ($cursection, $label->{description} . "_topicality", $label->{topicality});
    230             $doc_obj->add_utf8_metadata ($cursection, $label->{description} . "_mid", $label->{mid});
    231 
    232         }
    233     }
    234 }
    23573
    23674# do plugin specific processing of doc_obj
Note: See TracChangeset for help on using the changeset viewer.