Created
May 24, 2021 14:56
-
-
Save nonoesp/4f2a0277a9f338382da83fc6b977f790 to your computer and use it in GitHub Desktop.
Image transcription with Google Images Annotation API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export GOOGLE_APPLICATION_CREDENTIALS=./path/to/google-key.json | |
# Settings | |
INPUT_IMAGE=~/Desktop/input.jpg | |
# Variables | |
DATA_PATH=~/path/to/save/transcriptions | |
DATE_NOW=$(date '+%y%m%d_%H%M%S') | |
OUTPUT_IMAGE=$(echo $DATA_PATH)/$(echo $DATE_NOW)_image.jpg | |
OUTPUT_IMAGE_FULL=$(echo $DATA_PATH)/$(echo $DATE_NOW)_image-original.jpg | |
OUTPUT_JSON=$(echo $DATA_PATH)/$(echo $DATE_NOW).json | |
OUTPUT_TEXT=$(echo $DATA_PATH)/$(echo $DATE_NOW)_text.md | |
OUTPUT_TEXT_CLEAN=$(echo $DATA_PATH)/$(echo $DATE_NOW)_text-clean.md | |
# Copy image | |
cp $INPUT_IMAGE $OUTPUT_IMAGE_FULL | |
# Reduce image size | |
convert -geometry 800x400 $INPUT_IMAGE $OUTPUT_IMAGE | |
# Request transcription from Google Cloud Vision API | |
curl -X POST \ | |
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \ | |
-H "Content-Type: application/json; charset=utf-8" \ | |
https://vision.googleapis.com/v1/images:annotate -d "{'requests': [{'features': [{ 'type': 'DOCUMENT_TEXT_DETECTION'}],'image': {'content': '$(base64 $OUTPUT_IMAGE)'}}]}" \ | |
> $OUTPUT_JSON | |
# Display annotated text | |
TEXT=$(jq '.["responses"][0]["fullTextAnnotation"]["text"]' $OUTPUT_JSON) | |
echo -e "\033[1;31m $TEXT \033[0m" | |
# Replace line breaks with spaces to make text continuous | |
TEXT_CLEAN=${TEXT//\\n/ } | |
# Save to text file | |
echo -e $TEXT > $OUTPUT_TEXT | |
echo -e $TEXT_CLEAN > $OUTPUT_TEXT_CLEAN | |
# Open Markdown file | |
open $OUTPUT_TEXT_CLEAN |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment