Mencoba Pengenalan Karakter Optik (OCR)

Pengenalan Karakter Optik (OCR) dengan Cloud Vision API memungkinkan Anda mengekstrak teks cetak dan tulisan tangan dari gambar. Dalam tutorial ini, Anda akan mem-container-kan klien Python yang menjalankan OCR pada gambar dan men-deploy-nya sebagai tugas di Google Distributed Cloud (GDC) Sandbox.

Untuk menjalankan workload ini, Anda memerlukan project Google Cloud aktif dengan Cloud Vision API yang diaktifkan, kunci API yang valid, serta akses ke instance GDC Sandbox dan container registry Harbor. Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Python di Panduan memulai Vertex AI menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Menggunakan Vertex AI dan dokumentasi referensi Vertex AI Python API.

  1. Buat file python ocr_test.py. Ganti nilai image_uri_to_test dengan URI image sumber, seperti yang ditunjukkan:

    import os
    import requests
    import json
    
    def detect_text_rest(image_uri):
        """Performs Optical Character Recognition (OCR) on an image by invoking the Vertex AI REST API."""
    
        # Securely fetch the API key from environment variables
        api_key = os.environ.get("GCP_API_KEY")
        if not api_key:
            raise ValueError("GCP_API_KEY environment variable must be defined.")
    
        # Construct the Vision API endpoint URL
        vision_api_url = f"https://vision.googleapis.com/v1/images:annotate?key={api_key}"
    
        print(f"Initiating OCR process for image: {image_uri}")
    
        # Define the request payload for text detection
        request_payload = {
            "requests": [
                {
                    "image": {
                        "source": {
                            "imageUri": image_uri
                        }
                    },
                    "features": [
                        {
                            "type": "TEXT_DETECTION"
                        }
                    ]
                }
            ]
        }
    
        # Send a POST request to the Vision API
        response = requests.post(vision_api_url, json=request_payload)
        response.raise_for_status()  # Check for HTTP errors
    
        response_json = response.json()
    
        print("\n--- OCR Results ---")
    
        # Extract and print the detected text
        if "textAnnotations" in response_json["responses"]:
            full_text = response_json["responses"]["textAnnotations"]["description"]
            print(f"Detected Text:\n{full_text}")
        else:
            print("No text was detected in the image.")
    
        print("--- End of Results ---\n")
    
    if __name__ == "__main__":
        # URI of a publicly available image, or a storage bucket
        image_uri_to_test = "IMAGE_URI"
    
        detect_text_rest(image_uri_to_test)
    

    Ganti kode berikut:

    • IMAGE_URI dengan URI gambar yang tersedia secara publik yang berisi teks, misalnya, "https://cloud.google.com/vision/docs/images/sign.jpg". Atau, Anda dapat menentukan URI Cloud Storage, misalnya, "gs://your-bucket/your-image.png"
  2. Buat Dockerfile:

    ROM python:3.9-slim
    
    WORKDIR /app
    
    COPY ocr_test.py /app/
    
    # Install 'requests' for HTTP calls
    RUN pip install --no-cache-dir requests
    
    CMD ["python", "ocr_test.py"]
    
  3. Bangun image Docker untuk aplikasi terjemahan:

    docker build -t ocr-app .
    
  4. Ikuti petunjuk di Mengonfigurasi Docker untuk:

    1. Konfigurasi Docker,
    2. Membuat secret, dan
    3. Upload gambar ke HaaS.
  5. Login ke cluster pengguna dan buat file kubeconfig-nya dengan identitas pengguna. Pastikan Anda menetapkan jalur kubeconfig sebagai variabel lingkungan:

    export KUBECONFIG=${CLUSTER_KUBECONFIG_PATH}
    
  6. Buat secret Kubernetes dengan menjalankan perintah berikut di terminal Anda, lalu tempelkan kunci API Anda:

    kubectl create secret generic gcp-api-key-secret \
      --from-literal=GCP_API_KEY='PASTE_YOUR_API_KEY_HERE'
    

    Perintah ini akan membuat secret bernama gcp-api-key-secret dengan kunci GCP_API_KEY.

  7. Terapkan manifes kubernetes:

    apiVersion: batch/v1
    kind: Job
    metadata:
      name: ocr-test-job-apikey
    spec:
      template:
        spec:
          containers:
          - name: ocr-test-container
            image: HARBOR_INSTANCE_URL/HARBOR_PROJECT/ocr-app:latest # Your image path
            # Mount the API key from the secret into the container
            # as an environment variable named GCP_API_KEY.
            imagePullSecrets:
            - name: ${SECRET}
            envFrom:
            - secretRef:
                name: gcp-api-key-secret
          restartPolicy: Never
      backoffLimit: 4
    
    

    Ganti kode berikut:

    • HARBOR_INSTANCE_URL: URL instance Harbor.
    • HARBOR_PROJECT: project Harbor.
    • SECRET: nama secret yang dibuat untuk menyimpan kredensial docker.
  8. Periksa status tugas:

    kubectl get jobs/ocr-test-job-apikey
    # It will show 0/1 completions, then 1/1 after it succeeds
    
  9. Setelah tugas selesai, Anda dapat melihat output OCR di log pod:

    kubectl logs -l job-name=ocr-test-job-apikey