Skip to content

Instantly share code, notes, and snippets.

@UBISOFT-1
Created December 18, 2021 16:36
Show Gist options
  • Save UBISOFT-1/4017d641c329159f8de3d203efc919e1 to your computer and use it in GitHub Desktop.
Save UBISOFT-1/4017d641c329159f8de3d203efc919e1 to your computer and use it in GitHub Desktop.
Google Cloud Vision OCR JSON Response Converter
# this script is updated in order to support for multi-threaded applications.
# $3 is a random md5 hash or a uuid.uuid4() object that enables gcvocr.sh to be used without having two overlapping files named temp.json
#!/bin/bash
#cd ~
echo '{"requests":[{"image":{"content":"' > ./$3.json
openssl base64 -in $1 | cat >> ./$3.json
echo '"},
"features":{"type":"TEXT_DETECTION","maxResults":2048}}]}' >> ./$3.json
curl -k -s -H "Content-Type: application/json" https://vision.googleapis.com/v1/images:annotate?key=$2 --data-binary @./$3.json > "$1.json"
rm ./$3.json
@UBISOFT-1
Copy link
Author

import uuid
import os
random_number = uuid.uuid4()
IMAGE_PATH = "path/to/image"
api_key = ""
os.system('./gcvocr.sh {IMAGE_PATH} "{api_key}" {random_number}')

@UBISOFT-1
Copy link
Author

Single Threaded Mode

In single threaded mode as shown above we can go ahead and convert the file with little to no problem.

Multi-Threaded Mode

In order to go ahead and run the script in the Multi-Threaded Mode we can go ahead and do the following bash command to generate a hash that is random in order to go ahead and run the script from the terminal.

./gcvocr.sh ./program_work/27b6db7ac84649e57ed059d0c5b8ebd7/final_images/1.jpeg "xxxxxxxxxxxxxxxxxxxxx" "$RANDOM"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment