Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Auf dieser Seite erfahren Sie, wie Sie Text in Dateien mit der OCR API (Optical Character Recognition) auf einer Air-Gap-Appliance von Google Distributed Cloud (GDC) erkennen.
Der OCR-Dienst von Vertex AI auf der GDC-Air-Gap-Appliance erkennt Text in PDF- und TIFF-Dateien mit der API-Methode BatchAnnotateFiles.
Hinweise
Für die Verwendung der OCR API benötigen Sie ein Projekt, für das die OCR API aktiviert ist. Außerdem benötigen Sie die entsprechenden Anmeldedaten.
Sie können auch Clientbibliotheken installieren, um Aufrufe an die API zu ermöglichen. Weitere Informationen finden Sie unter Projekt zur Zeichenerkennung einrichten.
Text mit Inline-Anfragen erkennen
Mit der Methode BatchAnnotateFiles wird Text aus einer Batch-Datei mit PDF- oder TIFF-Dateien erkannt.
Sie senden die Datei, aus der Sie Text erkennen möchten, direkt als Inhalt in der API-Anfrage. Das System gibt den erkannten Text im JSON-Format in der API-Antwort zurück.
Sie müssen Werte für die Felder im JSON-Text Ihrer API-Anfrage angeben. In der folgenden Tabelle finden Sie eine Beschreibung der Felder im Anfragetext, die Sie angeben müssen, wenn Sie die API-Methode BatchAnnotateFiles für Ihre Anfragen zur Texterkennung verwenden:
Felder im Anfragetext
Feldbeschreibung
content
Die Dateien mit dem zu erkennenden Text. Sie stellen die Base64-Darstellung (ASCII-String) des Inhalts Ihrer Binärdatei bereit.
mime_type
Der Quelldateityp. Sie müssen einen der folgenden Werte festlegen:
application/pdf für PDF-Dateien
image/tiff für TIFF-Dateien
type
Die Art der Texterkennung, die Sie für die Datei benötigen.
Geben Sie eines der beiden Annotations-Features an:
TEXT_DETECTION erkennt und extrahiert Text aus beliebigen Dateien. Die JSON-Antwort enthält den extrahierten String, einzelne Wörter und deren Begrenzungsrahmen.
DOCUMENT_TEXT_DETECTION extrahiert auch Text aus einer Datei, wobei die Antwort für Fließtext und Dokumente optimiert ist. Die JSON-Datei enthält Informationen zu Seite, Block, Absatz, Wort und Worttrennung.
Optional. Liste der Sprachen, die für die Texterkennung verwendet werden sollen
Ein leerer Wert für dieses Feld wird vom System als automatische Spracherkennung interpretiert.
Bei Sprachen, die auf dem lateinischen Alphabet basieren, müssen Sie das Feld language_hints nicht festlegen.
Wenn Sie die Sprache des Texts in der Datei kennen, können Sie durch Festlegen eines Hinweises die Ergebnisse verbessern.
pages
Optional. Die Anzahl der Seiten der Datei, die für die Texterkennung verarbeitet werden sollen.
Sie können maximal fünf Seiten angeben. Wenn Sie die Anzahl der Seiten nicht angeben, verarbeitet der Dienst die ersten fünf Seiten der Datei.
Inline-API-Anfrage stellen
Stellen Sie eine Anfrage an die vortrainierte OCR-API mit der REST API-Methode. Andernfalls können Sie über ein Python-Skript mit der vortrainierten OCR-API interagieren, um Text aus PDF- oder TIFF-Dateien zu erkennen.
Die folgenden Beispiele zeigen, wie Sie mit OCR Text in einer Datei erkennen:
REST
So erkennen Sie Text in Dateien mit der REST API-Methode:
Speichern Sie die folgende request.json-Datei für den Anfragetext:
BASE64_ENCODED_FILE: die Base64-Darstellung (ASCII-String) des Inhalts Ihrer Binärdatei. Dieser String beginnt mit Zeichen, die /9j/4QAYRXhpZgAA...9tAVx/zDQDlGxn//2Q== ähneln.
FEATURE_TYPE: Der Typ der Texterkennung, die Sie für die Datei benötigen. Zulässige Werte sind TEXT_DETECTION oder DOCUMENT_TEXT_DETECTION.
LANGUAGE_HINT: Die BCP 47-Sprachtags, die als Sprachhinweise für die Texterkennung verwendet werden sollen, z. B. en-t-i0-handwrit. Dieses Feld ist optional. Ein leerer Wert wird vom System als automatische Spracherkennung interpretiert.
Fügen Sie dem von Ihnen erstellten Python-Skript den folgenden Code hinzu:
fromgoogle.cloudimportvisionimportgoogle.authfromgoogle.auth.transportimportrequestsfromgoogle.api_core.client_optionsimportClientOptionsaudience="https://ENDPOINT:443"api_endpoint="ENDPOINT:443"defvision_client(creds):opts=ClientOptions(api_endpoint=api_endpoint)returnvision.ImageAnnotatorClient(credentials=creds,client_options=opts)defmain():creds=Nonetry:creds,project_id=google.auth.default()creds=creds.with_gdch_audience(audience)req=requests.Request()creds.refresh(req)print("Got token: ")print(creds.token)exceptExceptionase:print("Caught exception"+str(e))raiseereturncredsdefvision_func(creds):vc=vision_client(creds)input_config={"content":"BASE64_ENCODED_FILE"}features=[{"type_":vision.Feature.Type.FEATURE_TYPE}]# Each requests element corresponds to a single file. To annotate more# files, create a request element for each file and add it to# the array of requestsreq={"input_config":input_config,"features":features}metadata=[("x-goog-user-project","projects/PROJECT_ID")]resp=vc.annotate_file(req,metadata=metadata)print(resp)if__name__=="__main__":creds=main()vision_func(creds)
Ersetzen Sie Folgendes:
ENDPOINT: Der OCR-Endpunkt, den Sie für Ihre Organisation verwenden. Weitere Informationen
BASE64_ENCODED_FILE: die Base64-Darstellung (ASCII-String) des Dateiinhalts. Dieser String beginnt mit Zeichen, die /9j/4QAYRXhpZgAA...9tAVx/zDQDlGxn//2Q== ähneln.
FEATURE_TYPE: Der Typ der Texterkennung, die Sie für die Datei benötigen. Zulässige Werte sind TEXT_DETECTION oder DOCUMENT_TEXT_DETECTION.
PROJECT_ID: Ihre Projekt-ID.
Speichern Sie das Python-Skript.
Führen Sie das Python-Skript aus, um Text in der Datei zu erkennen:
pythonSCRIPT_NAME
Ersetzen Sie SCRIPT_NAME durch den Namen, den Sie Ihrem Python-Skript gegeben haben, z. B. vision.py.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-04 (UTC)."],[],[],null,["# Detect text in files\n\nThis page shows you how to detect text in files using the\nOptical Character Recognition (OCR) API on Google Distributed Cloud (GDC) air-gapped appliance.\n\nThe OCR service of Vertex AI on\nGDC air-gapped appliance detects text in PDF and TIFF files using the\n`BatchAnnotateFiles` API method.\n| **Note:** The `BatchAnnotateFiles` API method only supports a single request per batch call.\n\nBefore you begin\n----------------\n\nBefore you can start using the OCR API, you must have a project\nwith the OCR API enabled and have the appropriate credentials.\nYou can also install client libraries to help you make calls to the API. For\nmore information, see [Set up a character recognition project](/distributed-cloud/hosted/docs/latest/appliance/application/ao-user/vai-set-up-ocr).\n\nDetect text with inline requests\n--------------------------------\n\nThe `BatchAnnotateFiles` method detects text from a batch of PDF or TIFF files.\nYou send the file from which you want to detect text directly as content in the\nAPI request. The system returns the resulting detected text in JSON format in\nthe API response.\n\nYou must specify values for the fields in the JSON body of your API request. The\nfollowing table contains a description of the request body fields you must\nprovide when you use the `BatchAnnotateFiles` API method for your text\ndetection requests:\n\n### Make an inline API request\n\nMake a request to the OCR pre-trained API using the REST API\nmethod. Otherwise, interact with the OCR pre-trained API from a\nPython script to detect text from PDF or TIFF files.\n| **Note:** The `BatchAnnotateFiles` API method only supports a single request per batch call.\n\nThe following examples show how to detect text in a file using\nOCR: \n\n### REST\n\nFollow these steps to detect text in files using the REST API method:\n\n1. Save the following `request.json` file for your request body:\n\n cat \u003c\u003c- EOF \u003e request.json\n {\n \"requests\": [\n {\n \"input_config\": {\n \"content\": \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-err\"\u003eBASE\u003c/span\u003e\u003cspan class=\"devsite-syntax-mi\"\u003e64\u003c/span\u003e\u003cspan class=\"devsite-syntax-err\"\u003e_ENCODED_FILE\u003c/span\u003e\u003c/var\u003e,\n \"mime_type\": \"application/pdf\"\n },\n \"features\": [\n {\n \"type\": \"\u003cvar translate=\"no\"\u003eFEATURE_TYPE\u003c/var\u003e\"\n }\n ],\n \"image_context\": {\n \"language_hints\": [\n \"\u003cvar translate=\"no\"\u003eLANGUAGE_HINT_1\u003c/var\u003e\",\n \"\u003cvar translate=\"no\"\u003eLANGUAGE_HINT_2\u003c/var\u003e\",\n ...\n ]\n },\n \"pages\": []\n }\n ]\n }\n EOF\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eBASE64_ENCODED_FILE\u003c/var\u003e: the Base64 representation (ASCII string) of your binary file content. This string begins with characters that look similar to `/9j/4QAYRXhpZgAA...9tAVx/zDQDlGxn//2Q==`.\n - \u003cvar translate=\"no\"\u003eFEATURE_TYPE\u003c/var\u003e: the type of text detection you need from the file. Allowed values are `TEXT_DETECTION` or `DOCUMENT_TEXT_DETECTION`.\n - \u003cvar translate=\"no\"\u003eLANGUAGE_HINT\u003c/var\u003e: the BCP 47 language tags to use as language hints for text detection, such as `en-t-i0-handwrit`. This field is optional and the system interprets an empty value as automatic language detection.\n2. [Get an authentication token](/distributed-cloud/hosted/docs/latest/appliance/application/ao-user/vertex-ai-api-auth).\n\n3. Make the request:\n\n ### curl\n\n curl -X POST \\\n -H \"Authorization: Bearer \u003cvar translate=\"no\"\u003eTOKEN\u003c/var\u003e\" \\\n -H \"x-goog-user-project: projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e\" \\\n -H \"Content-Type: application/json; charset=utf-8\" \\\n -d @request.json \\\n https://\u003cvar translate=\"no\"\u003eENDPOINT\u003c/var\u003e/v1/files:annotate\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eTOKEN\u003c/var\u003e: [the authentication token](/distributed-cloud/hosted/docs/latest/appliance/application/ao-user/vertex-ai-api-auth) you obtained.\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: your project ID.\n - \u003cvar translate=\"no\"\u003eENDPOINT\u003c/var\u003e: the OCR endpoint that you use for your organization. For more information, [view service status and endpoints](/distributed-cloud/hosted/docs/latest/appliance/application/ao-user/vertex-ai-api-status).\n\n ### PowerShell\n\n $headers = @{\n \"Authorization\" = \"Bearer \u003cvar translate=\"no\"\u003eTOKEN\u003c/var\u003e\"\n \"x-goog-user-project\" = \"projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e\"\n }\n\n Invoke-WebRequest\n -Method POST\n -Headers $headers\n -ContentType: \"application/json; charset=utf-8\"\n -InFile request.json\n -Uri \"\u003cvar translate=\"no\"\u003eENDPOINT\u003c/var\u003e/v1/files:annotate\" | Select-Object -Expand Content\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eTOKEN\u003c/var\u003e: [the authentication token](/distributed-cloud/hosted/docs/latest/appliance/application/ao-user/vertex-ai-api-auth) you obtained.\n - \u003cvar translate=\"no\"\u003eENDPOINT\u003c/var\u003e: the OCR endpoint that you use for your organization. For more information, [view service status and endpoints](/distributed-cloud/hosted/docs/latest/appliance/application/ao-user/vertex-ai-api-status).\n\n### Python\n\nFollow these steps to use the OCR service from a Python\nscript to detect text in a file:\n\n1. [Install the latest version of the OCR client library](/distributed-cloud/hosted/docs/latest/appliance/application/ao-user/vertex-ai-install-libraries).\n\n2. [Set the required environment variables on a Python script](/distributed-cloud/hosted/docs/latest/appliance/application/ao-user/vai-set-up-ocr#set-env-var).\n\n3. [Authenticate your API request](/distributed-cloud/hosted/docs/latest/appliance/application/ao-user/vertex-ai-api-auth).\n\n4. Add the following code to the Python script you created:\n\n from google.cloud import vision\n import google.auth\n from google.auth.transport import requests\n from google.api_core.client_options import ClientOptions\n\n audience = \"https://\u003cvar translate=\"no\"\u003eENDPOINT\u003c/var\u003e:443\"\n api_endpoint=\"\u003cvar translate=\"no\"\u003eENDPOINT\u003c/var\u003e:443\"\n\n def vision_client(creds):\n opts = ClientOptions(api_endpoint=api_endpoint)\n return vision.https://cloud.google.com/python/docs/reference/vision/latest/google.cloud.vision_v1.services.image_annotator.ImageAnnotatorClient.html(credentials=creds, client_options=opts)\n\n def main():\n creds = None\n try:\n creds, project_id = google.auth.default()\n creds = creds.with_gdch_audience(audience)\n req = requests.Request()\n creds.refresh(req)\n print(\"Got token: \")\n print(creds.token)\n except Exception as e:\n print(\"Caught exception\" + str(e))\n raise e\n return creds\n\n def vision_func(creds):\n vc = vision_client(creds)\n input_config = {\"content\": \"\u003cvar translate=\"no\"\u003eBASE64_ENCODED_FILE\u003c/var\u003e\"}\n features = [{\"type_\": vision.https://cloud.google.com/python/docs/reference/vision/latest/google.cloud.vision_v1.types.Feature.html.Type.\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eFEATURE_TYPE\u003c/span\u003e\u003c/var\u003e}]\n # Each requests element corresponds to a single file. To annotate more\n # files, create a request element for each file and add it to\n # the array of requests\n req = {\"input_config\": input_config, \"features\": features}\n\n metadata = [(\"x-goog-user-project\", \"projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e\")]\n\n resp = vc.annotate_file(req,metadata=metadata)\n\n print(resp)\n\n if __name__==\"__main__\":\n creds = main()\n vision_func(creds)\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eENDPOINT\u003c/var\u003e: the OCR endpoint that you use for your organization. For more information, [view service status and endpoints](/distributed-cloud/hosted/docs/latest/appliance/application/ao-user/vertex-ai-api-status).\n - \u003cvar translate=\"no\"\u003eBASE64_ENCODED_FILE\u003c/var\u003e: the Base64 representation (ASCII string) of your file content. This string begins with characters that look similar to `/9j/4QAYRXhpZgAA...9tAVx/zDQDlGxn//2Q==`.\n - \u003cvar translate=\"no\"\u003eFEATURE_TYPE\u003c/var\u003e: the type of text detection you need from the file. Allowed values are `TEXT_DETECTION` or `DOCUMENT_TEXT_DETECTION`.\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: your project ID.\n5. Save the Python script.\n\n6. Run the Python script to detect text in the file:\n\n python \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eSCRIPT_NAME\u003c/span\u003e\u003c/var\u003e\n\n Replace \u003cvar translate=\"no\"\u003eSCRIPT_NAME\u003c/var\u003e with the name you gave to your\n Python script, such as `vision.py`."]]