About batch processing to convert a large number of image files to text using GoogleDrive’s OCR function

Mac

This post is also available in: 日本語 (Japanese)

Introduction
My environment is Mac OS X 10.13.6 on MacBook Pro. I am uploading image files taken with the scanner to the google document. right click on Google Doc and select “google document" in “Open with application" menu, you can use OCR function. It is a very convenient function, because it has high accuracy of OCR. The problem was that this operation had to be done for each file, so I thought that it would be even more convenient if there was a batch conversion of multiple files.

When I contacted Google Cloud Support, “As I looked it up, the ability to open multiple images at once with the google document app was not implemented at the moment, I’m sorry I can not follow your request. That was the answer. (August 23, 2018)

The following site (in Japanese) was found when searching.

Use GoogleDrive’s OCR function to convert large numbers of image files to text

Although this site was very helpful, I do not know how to make a batch file because I do not do the task of … typing commands at the terminal. However, I understood that the program called gdrive will handle that processus.

“These problems can be solved by using gdrive offered by Petter Rasmussen on github."

After all, I asked my beloved friend G… to write a program to handle using gdrive. I will post that program later, but you need to read gdrive’s readme carefully.

To install on MacOS X Download and install Homebrew.  Download the file adapted to your computer and install it by using Homebrew.

Well, the program to process with gdrive is gdrive-import-export.pl.


#!/usr/bin/perl

# After download: chmod +x gdrive-import-export.pl
#
# Usage examples
# ./gdrive-import-export.pl file1.png file2.png file3.jpg
# ./gdrive-import-export.pl uploads/*.jpg uploads/*.png

my $num_files = 0;
my $FILE = '';
my $IMPORT_TEST = 0; # for debug
my $EXPORT_TEST = 0; # for debug

$num_files = @ARGV;
print "Number of Files: $num_files\n";

if ($num_files < 1) {
print STDERR "give a file name\n";
exit(1);
}

for (my $i=0; $i < $num_files; $i++) {
$FILE = $ARGV[$i];
print STDERR "INPUT $FILE\n";

# gdrive import
my $command="gdrive import $FILE"; # modify this line for command options
my $imported = '';

if (!$IMPORT_TEST) {
$imported=`$command`;
print STDERR "$imported";
} else { print "DEBUG: $command\n"; }

# get FileiD
$imported =~ s/^Imported\s+(\S+)\s+.*/$1/;
chomp($imported);
print STDERR "FileID: $imported\n";

if ($imported ne '') {
my $command ="gdrive export --mime text/plain -f $imported";
# -f is for overwrite. modify mime type as text/rtf or other
#
# "gdrive about export" lists all available types
# From: application/vnd.google-apps.document
# To: text/html, application/rtf, text/plain, application/epub+zip
# and more

if (!$EXPORT_TEST){
my $exported = `$command`;
print STDERR "$exported";
} else { print "DEBUG: $command\n"; }
} else {
print STDERR "import failed. skip export and exit\n";
}

} # end of for

exit(0);
# end of the program


The task of this program:

  1. Upload a jpg or png file as a document to Google Drive. Display the number of files to be uploaded.
  2. Download the text of the converted file to the local folder.

Save the source code above and give an appropriate name and save it in the directory / user / local / bin / of your own Mac.

You only need to run it at the terminal. Terminal of Mac is friendly to people who are not good at commands like me. It supports drag and drop like other applications. Please refer to it as a way to do it (the explanation is Japanese, but you would understand easily, I think).

 

 

MacMac

Posted by hnakata