Skip to content

Instantly share code, notes, and snippets.

@lpar
Created November 11, 2019 17:17
Show Gist options
  • Save lpar/e4d421d8b17510dd933ff77c52ccd368 to your computer and use it in GitHub Desktop.
Save lpar/e4d421d8b17510dd933ff77c52ccd368 to your computer and use it in GitHub Desktop.
OCR random images using Google Cloud Vision and put the results in the Finder metadata for macOS Spotlight search
# Ruby script to OCR random JPEG and PNG files using Google Cloud Vision,
# then add the results to the macOS Finder metadata for Spotlight search purposes.
# Before you can use this you need to set up billing for Google Cloud Vision, and
# download the public/private key credentials as a JSON file. See:
# https://cloud.google.com/vision/docs/setup
ENV["GOOGLE_APPLICATION_CREDENTIALS"] = "path-to-google-cloud-vision-credentials-file.json"
require "shellwords"
require "google/cloud/vision"
require "cfpropertylist"
require "find"
require "pp"
PROJECT_ID = "ocr-random-images"
def ocrfile(image_path)
vision = Google::Cloud::Vision.new project: PROJECT_ID
image = vision.image image_path
document = image.document
return document.text.gsub(/[\r\n]+/, " ").gsub(/ +/, " ")
end
def plistize(str)
plist = CFPropertyList::List.new
plist.value = CFPropertyList.guess(str)
return plist.to_str
end
# This doesn't seem to work for some unknown reason?
def setcomment(fspc, txt)
comment = txt.gsub(/[\r\n]/, ' ').gsub(/\s\s*/, " ")
plist = plistize(comment).unpack("H*")[0]
system("xattr", "-wx", "com.apple.metadata:kMDItemFinderComment", plist, fspc)
end
def findersetcomment(fspc, txt)
deq = txt.gsub(/[\r\n]+/, ' ').gsub(/\s\s*/, " ").gsub(/["']/, "")
system("osascript",
"-e",
"tell app \"Finder\" to set comment of (POSIX file \"#{fspc}\" as alias) to \"#{deq}\"")
end
def ocrimg(fspc)
txt = ocrfile(fspc)
findersetcomment(fspc, txt)
puts "#{fspc}: #{txt}"
end
def ocrimgs(folder)
Find.find(folder) do |path|
if path.match(/\.(jpg|jpeg|png)$/i)
ocrimg(path)
end
end
end
# Now call on a folder full of images
ocrimgs("/Users/meta/Pictures/Cartoons/Scenes From A Multiverse")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment