Files
WCX/scripts/ocr.sh
2026-07-17 17:56:23 +02:00

140 lines
4.4 KiB
Bash
Executable File

#!/bin/bash
# -----------------------------------------------------------------------------
# WCX Thumbnail OCR
#
# Extracts text from a local image file or an image URL using the Google Cloud
# Vision API.
#
# The script:
# 1. Accepts either a local image path or an HTTP/HTTPS image URL.
# 2. Reads or downloads the image and encodes it as Base64.
# 3. Sends the image to Google Cloud Vision using TEXT_DETECTION.
# 4. Extracts the first detected text annotation from the API response.
# 5. Writes the OCR text to standard output or to a file specified with -o.
#
# Requirements:
# - GOOGLE_VISION_API_KEY must be set in the environment.
# - curl is required for HTTP requests and remote image downloads.
# - base64 is required for encoding the image.
# - jq is recommended and is required for clean plain-text output.
#
# Usage:
# ocr.sh <image-file-or-url> [-o output-file]
#
# Examples:
# ./ocr.sh thumbnail.jpg
# ./ocr.sh "https://example.com/thumbnail.jpg"
# ./ocr.sh "https://example.com/thumbnail.jpg" -o ocr-result.txt
#
# Output contract:
# - Without -o, the detected OCR text is written to standard output.
# - With -o, the OCR text is written to the specified file and a completion
# message is written to standard output.
# - A non-zero exit status indicates invalid input, missing configuration,
# missing dependencies, an API failure, or that no text was detected.
#
# The output is raw OCR text. Its structure is not guaranteed by Google Vision
# and must be parsed and validated separately before being stored as metadata.
# -----------------------------------------------------------------------------
# --- KONFIGURATION ---
# Skriptet förväntar sig nu miljövariabeln: GOOGLE_VISION_API_KEY
# ---------------------
usage() {
echo "Användning: $0 <bildfil eller URL> [-o utdatafil]"
echo "Exempel 1: $0 kvitto.jpg"
echo "Exempel 2: export GOOGLE_VISION_API_KEY=\"din_nyckel\" && $0 https://example.com/image.png -o text.txt"
exit 1
}
# 1. Kontrollera att miljövariabeln är satt
if [ -z "$GOOGLE_VISION_API_KEY" ]; then
echo "Fel: Miljövariabeln GOOGLE_VISION_API_KEY är inte satt." >&2
echo "Kör detta i terminalen först: export GOOGLE_VISION_API_KEY=\"din_faktiska_nyckel\"" >&2
exit 1
fi
# Kontrollera att vi fick minst ett argument
if [ $# -lt 1 ]; then
usage
fi
INPUT="$1"
OUTPUT_FILE=""
# Hantera flaggor för utdatafil (-o)
if [ "$2" == "-o" ] && [ -n "$3" ]; then
OUTPUT_FILE="$3"
fi
# 2. Hantera bilden (lokal fil vs URL) och koda till Base64
if [[ "$INPUT" =~ ^https?:// ]]; then
# Det är en URL - ladda ner och koda till Base64 i minnet
if ! command -v curl &> /dev/null; then
echo "Fel: curl krävs men är inte installerat." >&2
exit 1
fi
BASE64_IMAGE=$(curl -s "$INPUT" | base64)
else
# Det är en lokal fil
if [ ! -f "$INPUT" ]; then
echo "Fel: Filen '$INPUT' hittades inte." >&2
exit 1
fi
# Hanterar både Mac (base64) och Linux (base64 -w 0) för att undvika radbrytningar i Base64-strängen
if [[ "$OSTYPE" == "darwin"* ]]; then
BASE64_IMAGE=$(base64 "$INPUT")
else
BASE64_IMAGE=$(base64 -w 0 "$INPUT")
fi
fi
# 3. Skapa JSON-payloaden
JSON_PAYLOAD=$(cat <<EOF
{
"requests": [
{
"image": {
"content": "$BASE64_IMAGE"
},
"features": [
{
"type": "TEXT_DETECTION"
}
]
}
]
}
EOF
)
# 4. Skicka till Google Cloud Vision API och filtrera ut texten via 'jq'
if command -v jq &> /dev/null; then
RESULT=$(curl -s -X POST \
-H "Content-Type: application/json; charset=utf-8" \
-d "$JSON_PAYLOAD" \
"https://vision.googleapis.com/v1/images:annotate?key=$GOOGLE_VISION_API_KEY" | jq -r '.responses[0].textAnnotations[0].description')
else
echo "Tips: Installera 'jq' (t.ex. 'brew install jq' eller 'apt install jq') för att få ren text." >&2
RESULT=$(curl -s -X POST \
-H "Content-Type: application/json; charset=utf-8" \
-d "$JSON_PAYLOAD" \
"https://vision.googleapis.com/v1/images:annotate?key=$GOOGLE_VISION_API_KEY")
fi
# Kontrollera om vi fick något svar eller om det blev fel
if [ "$RESULT" == "null" ] || [ -z "$RESULT" ]; then
echo "Ingen text hittades eller så uppstod ett API-fel." >&2
exit 1
fi
# 5. Skriv ut resultatet (till fil eller stdout)
if [ -n "$OUTPUT_FILE" ]; then
echo "$RESULT" > "$OUTPUT_FILE"
echo "Klart! Texten har sparats i $OUTPUT_FILE"
else
echo "$RESULT"
fi