Photograph a receipt, get the text. Upload a product shot, get keywords for search. Cloud Vision does the hard part; Deluge only has to build a small JSON body and read the answer back.
Still current, with one meaningful improvement. The endpoint vision.googleapis.com/v1/images:annotate is unchanged and — contrary to a lot of advice online — API-key authentication still works. Google's documentation now showcases OAuth, but a bogus key returns API_KEY_INVALID, meaning the method is accepted and only the key was wrong. Keys remain the pragmatic choice from Deluge. What has been changed here is the original's use of a third-party PHP relay to base64-encode the image before sending it. That relay is gone: Vision accepts an imageUri and fetches the picture itself.
gs:// URI.// ============================================
// GOOGLE CLOUD VISION — OCR AND LABELLING
// Uses imageUri so nothing has to be base64-encoded.
// ============================================
string Vision.readImage(string imageUrl, string featureType)
{
API_KEY = "YOUR_GOOGLE_CLOUD_API_KEY";
source = Map();
source.put("imageUri",imageUrl);
image = Map();
image.put("source",source);
feature = Map();
feature.put("type",featureType); // TEXT_DETECTION or LABEL_DETECTION
feature.put("maxResults",10);
features = List();
features.add(feature);
request = Map();
request.put("image",image);
request.put("features",features);
requests = List();
requests.add(request);
payload = Map();
payload.put("requests",requests);
response = invokeurl
[
url : "https://vision.googleapis.com/v1/images:annotate?key=" + API_KEY
type : POST
parameters : payload.toString()
headers: {"Content-Type":"application/json"}
];
responses = response.getJSON("responses");
if(responses == null || responses.size() == 0)
{
info "Vision error: " + response.toString();
return "";
}
first = responses.get(0);
// Vision reports per-image failures inside a 200 response
if(first.getJSON("error") != null)
{
info "Vision rejected the image: " + first.getJSON("error").toString();
return "";
}
if(featureType == "TEXT_DETECTION")
{
full = first.getJSON("fullTextAnnotation");
if(full != null)
{
return full.getJSON("text");
}
return "";
}
else
{
labels = first.getJSON("labelAnnotations");
out = "";
if(labels != null)
{
for each l in labels
{
if(out != "")
{
out = out + ", ";
}
out = out + l.getJSON("description");
}
}
return out;
}
}imageUri, not content. Base64-encoding a photo in Deluge produces a huge string, risks the script timeout, and was the only reason the original needed an external helper. Let Google fetch the image.error object when it cannot reach or decode the picture. Without the check above, an unreachable image looks exactly like an image containing no text.fullTextAnnotation for documents, textAnnotations for words. The first gives you the whole block with layout preserved, which is what you want for a receipt. The second gives every word with bounding boxes, which is what you want when position matters.This script is part of the free Creator Scripts Deluge Library.
All 39 Deluge scripts, the full Zoho Creator course, and every downloadable asset are now free. Get free access →