Arcade Adventure: Google Cloud ML APIs | GSP075

Solution for Arcade Adventure: Google Cloud ML APIs | GSP075. 1 lab: GSP075. Fast copy-paste commands for Google Cloud.

GSP075 — Extract, Analyze, and Translate Text from Images with the Cloud ML APIs

Estimated time: 15 minutes

# 🚀 Extract, Analyze, and Translate Text from Images with the Cloud ML APIs | GSP075 > ⚠️ **Disclaimer:** This is an independent, community-made walkthrough created for educational purposes, hands-on practice, and Google Cloud certification preparation. This guide is designed to help learners understand Google Cloud Machine Learning APIs, including Vision, Translation, and Natural Language, through practical exercises. Always attempt the lab yourself first and follow Google Cloud Skills Boost

clear
# Professional ANSI Color Variables
CYAN='\033[1;36m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
RESET='\033[0m'
BOLD='\033[1m'

echo -e "${CYAN}${BOLD}"
echo "   ____       _    _ _             __   ___             "
echo "  / __ \     | |  (_) |           / _| / _ \            "
echo " | |  | |_ __| |__  _| |_  ___   | |_ | | | |_ __ ___  "
echo " | |  | | '__| '_ \| | __| / _ \ |  _|| | | | '_ \ / __| "
echo " | |__| | |  | |_) | | |_ | (_) || |  | |_| | |_) \__ \ "
echo "  \____/|_|  |_.__/|_|\__| \___/ |_|   \___/| .__/|___/ "
echo "                                            | |         "
echo "                                            |_|         "
echo -e "${RESET}"

export PROJECT_ID=$(gcloud config get-value project)
export BUCKET_NAME="${PROJECT_ID}-bucket"

echo -e "${YELLOW}▶ Creating API Key with required service restrictions...${RESET}"
gcloud services api-keys create --display-name="Lab-Key" \
    --api-target=service=vision.googleapis.com \
    --api-target=service=translate.googleapis.com \
    --api-target=service=language.googleapis.com

# Give the API key a few seconds to propagate in the backend
sleep 5

export KEY_NAME=$(gcloud services api-keys list --filter="displayName=Lab-Key" --format="value(name)" | head -n 1)
export API_KEY=$(gcloud services api-keys get-key-string $KEY_NAME --format="value(keyString)")

echo -e "${BLUE}▶ Creating Cloud Storage bucket (gs://${BUCKET_NAME})...${RESET}"
gcloud storage buckets create gs://$BUCKET_NAME
gcloud storage buckets update gs://$BUCKET_NAME --no-uniform-bucket-level-access

echo -e "${YELLOW}▶ Downloading and uploading sample image 'sign.jpg'...${RESET}"
curl -L -o sign.jpg https://raw.githubusercontent.com/Orbit-of-Ops/Labs-Solutions/main/Extract%2C%20Analyze%2C%20and%20Translate%20Text%20from%20Images%20with%20the%20Cloud%20ML%20APIs/sign.jpg
gcloud storage cp sign.jpg gs://$BUCKET_NAME/sign.jpg

echo -e "${CYAN}▶ Making image publicly accessible...${RESET}"
gsutil acl ch -u allUsers:R gs://$BUCKET_NAME/sign.jpg

echo -e "${BLUE}▶ Task 3 & 4: Querying Cloud Vision API (OCR)...${RESET}"
cat > ocr-request.json <<EOF
{
  "requests": [
      {
        "image": {
          "source": {
              "gcsImageUri": "gs://$BUCKET_NAME/sign.jpg"
          }
        },
        "features": [
          {
            "type": "TEXT_DETECTION",
            "maxResults": 10
          }
        ]
      }
  ]
}
EOF

curl -s -X POST -H "Content-Type: application/json" --data-binary @ocr-request.json "https://vision.googleapis.com/v1/images:annotate?key=${API_KEY}" -o ocr-response.json

echo -e "${YELLOW}▶ Task 5: Querying Cloud Translation API...${RESET}"
cat > translation-request.json <<EOF
{
  "q": "your_text_here",
  "target": "en"
}
EOF

STR=$(jq .responses[0].textAnnotations[0].description ocr-response.json) && STR="${STR//\"}" && sed -i "s|your_text_here|$STR|g" translation-request.json
curl -s -X POST -H "Content-Type: application/json" --data-binary @translation-request.json "https://translation.googleapis.com/language/translate/v2?key=${API_KEY}" -o translation-response.json

echo -e "${BLUE}▶ Task 6: Querying Cloud Natural Language API...${RESET}"
cat > nl-request.json <<EOF
{
  "document":{
    "type":"PLAIN_TEXT",
    "content":"your_text_here"
  },
  "encodingType":"UTF8"
}
EOF

STR=$(jq .data.translations[0].translatedText translation-response.json) && STR="${STR//\"}" && sed -i "s|your_text_here|$STR|g" nl-request.json
curl -s -X POST -H "Content-Type: application/json" --data-binary @nl-request.json "https://language.googleapis.com/v1/documents:analyzeEntities?key=${API_KEY}"

echo -e "\n${GREEN}✓ All ML API requests processed successfully! You may now click 'Check my progress' on all tasks.${RESET}"