快速入門:Azure AI 視覺 v3.2 GA 讀取

OCR (閱讀) 版本

重要

選取最符合您需求的讀取版本。

輸入 範例 讀取版本 優點
影像:一般、野生影像 標籤、街道標誌和海報 適用於映像的 OCR (4.0 版) 已針對具有效能增強的同步 API 進行一般非檔影像優化,可讓您更輕鬆地在用戶體驗案例中內嵌 OCR。
:數位和掃描,包括影像 書籍、文章和報表 文件智慧讀取模型 使用異步 API 針對大量文字掃描和數位文件進行優化,可協助大規模自動化智慧型手機文件處理。

關於 Azure AI 視覺 v3.2 GA 讀取

尋找最新的 Azure AI 視覺 v3.2 GA 讀取? 所有未來的讀取 OCR 增強功能都屬於先前所列的兩項服務。 Azure AI 視覺 v3.2 沒有進一步的更新。 如需詳細資訊,請參閱呼叫 Azure AI 視覺 3.2 GA 讀取 API快速入門:Azure AI 視覺 v3.2 GA 讀取

開始使用 Azure AI 視覺讀取 REST API 或用戶端程式庫。 讀取 API 提供 AI 演算法,讓您從影像擷取文字,並將其傳回為結構化字串。 請遵循下列步驟,將套件安裝到您的應用程式,並試用基本工作的範例程序代碼。

使用光學字元辨識 (OCR) 用戶端程式庫,讀取影像中的印刷和手寫文字。 OCR 服務可以讀取影像中的可見文字,並將其轉換成字元數據流。 如需文字辨識的詳細資訊,請參閱 OCR 概觀。 本節中的程序代碼會使用最新的 Azure AI 視覺 套件。

提示

您也可以從本機影像擷取文字。 請參閱 ComputerVisionClient 方法,例如 ReadInStreamAsync。 或者,如需涉及本機映射的案例,請參閱 GitHub 上的範例程序代碼。

參考文檔 | 庫原始程式碼 | 套件 (NuGet)範例 |

必要條件

  • Azure 訂用帳戶 - 免費建立一個訂用帳戶。

  • Visual Studio IDE 或 .NET Core目前版本。

  • Azure AI 視覺資源。 您可以使用免費定價層 (F0) 來試用服務,稍後再升級至生產環境的付費層。

  • 來自所建立資源的金鑰和端點,可將應用程式連線至 Azure AI 視覺服務。

    1. 部署 Azure 視覺資源之後,請選取 [前往資源]。
    2. 在左側導覽功能表中,選取 [金鑰和端點]。
    3. 複製其中一個金鑰和 [端點],以供稍後在快速入門中使用。

建立環境變數

在此範例中,在執行應用程式的本機電腦上將認證寫入環境變數。

前往 Azure 入口網站。 如果已成功部署您在 [必要條件] 區段中建立的資源,請選取 [後續步驟] 下的 [前往資源] 按鈕。 您可以在 [金鑰和端點] 頁面中 [資源管理] 底下找到金鑰和端點。 您的資源金鑰與您的 Azure 訂用帳戶識別碼不同。

提示

請勿將金鑰直接包含在您的程式代碼中,且絕不會公開發佈。 如需更多驗證選項 (例如 Azure Key Vault),請參閱 Azure AI 服務安全性文章。

若要設定金鑰和端點的環境變數,請開啟主控台視窗,然後遵循作業系統和開發環境的指示進行。

  1. 若要設定 VISION_KEY 環境變數,請將 取代 your-key 為您資源的其中一個密鑰。
  2. 若要設定 VISION_ENDPOINT 環境變數,請將 取代 your-endpoint 為您資源的端點。
setx VISION_KEY your-key
setx VISION_ENDPOINT your-endpoint

新增環境變數之後,您可能需要重新啟動任何將讀取環境變數的執行中程式,包括主控台視窗。

讀取印刷和手寫文字

  1. 建立新的 C# 應用程式。

    使用 Visual Studio,針對 C#、Windows、主控台建立 [主控台應用程式 (.NET Framework)] 專案。

    建立新專案之後,請安裝用戶端程式庫:

    1. 在 [方案總管] 中以滑鼠右鍵按一下專案解決方案,然後選取 [管理解決方案的 NuGet 套件]。
    2. 在開啟的套件管理員中,選取 [瀏覽]。 選取 [包含發行前版本]
    3. 搜尋並選取 Microsoft.Azure.CognitiveServices.Vision.ComputerVision
    4. 在詳細資料對話方塊中,選取您的專案,然後選取最新的穩定版本。 然後選取 [安裝]。
  2. 從項目目錄中,在慣用的編輯器或 IDE 中開啟 Program.cs 檔案。 將 Program.cs 的內容取代為下列程式碼。

    using System;
    using System.Collections.Generic;
    using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
    using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
    using System.Threading.Tasks;
    using System.IO;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System.Threading;
    using System.Linq;
    
    namespace ComputerVisionQuickstart
    {
        class Program
        {
            // Add your Computer Vision key and endpoint
            static string key = Environment.GetEnvironmentVariable("VISION_KEY");
            static string endpoint = Environment.GetEnvironmentVariable("VISION_ENDPOINT");
    
            private const string READ_TEXT_URL_IMAGE = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/printed_text.jpg";
    
            static void Main(string[] args)
            {
                Console.WriteLine("Azure Cognitive Services Computer Vision - .NET quickstart example");
                Console.WriteLine();
    
                ComputerVisionClient client = Authenticate(endpoint, key);
    
                // Extract text (OCR) from a URL image using the Read API
                ReadFileUrl(client, READ_TEXT_URL_IMAGE).Wait();
            }
    
            public static ComputerVisionClient Authenticate(string endpoint, string key)
            {
                ComputerVisionClient client =
                  new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
                  { Endpoint = endpoint };
                return client;
            }
    
            public static async Task ReadFileUrl(ComputerVisionClient client, string urlFile)
            {
                Console.WriteLine("----------------------------------------------------------");
                Console.WriteLine("READ FILE FROM URL");
                Console.WriteLine();
    
                // Read text from URL
                var textHeaders = await client.ReadAsync(urlFile);
                // After the request, get the operation location (operation ID)
                string operationLocation = textHeaders.OperationLocation;
                Thread.Sleep(2000);
    
                // Retrieve the URI where the extracted text will be stored from the Operation-Location header.
                // We only need the ID and not the full URL
                const int numberOfCharsInOperationId = 36;
                string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);
    
                // Extract the text
                ReadOperationResult results;
                Console.WriteLine($"Extracting text from URL file {Path.GetFileName(urlFile)}...");
                Console.WriteLine();
                do
                {
                    results = await client.GetReadResultAsync(Guid.Parse(operationId));
                }
                while ((results.Status == OperationStatusCodes.Running ||
                    results.Status == OperationStatusCodes.NotStarted));
    
                // Display the found text.
                Console.WriteLine();
                var textUrlFileResults = results.AnalyzeResult.ReadResults;
                foreach (ReadResult page in textUrlFileResults)
                {
                    foreach (Line line in page.Lines)
                    {
                        Console.WriteLine(line.Text);
                    }
                }
                Console.WriteLine();
            }
    
        }
    }
    
  3. 如需選擇性步驟,請參閱決定如何處理資料。 例如,若要明確指定最新的 GA 模型,請編輯呼叫, ReadAsync 如下所示。 略過參數或使用 "latest" 來使用最新的 GA 模型。

      // Read text from URL with a specific model version
      var textHeaders = await client.ReadAsync(urlFile,null,null,"2022-04-30");
    
  4. 執行應用程式。

    • 從 [偵錯] 功能表中,選取 [開始偵錯]

輸出

Azure AI Vision - .NET quickstart example

----------------------------------------------------------
READ FILE FROM URL

Extracting text from URL file printed_text.jpg...


Nutrition Facts Amount Per Serving
Serving size: 1 bar (40g)
Serving Per Package: 4
Total Fat 13g
Saturated Fat 1.5g
Amount Per Serving
Trans Fat 0g
Calories 190
Cholesterol 0mg
ories from Fat 110
Sodium 20mg
nt Daily Values are based on Vitamin A 50%
calorie diet.

清除資源

如果您想要清除和移除 Azure AI 服務訂用帳戶,則可以刪除資源或資源群組。 刪除資源群組也會刪除與其相關聯的任何其他資源。

下一步

在本快速入門中,您已瞭解如何安裝 OCR 用戶端連結庫,並使用讀取 API。 接下來,深入了解讀取 API 功能。

  • OCR 概觀
  • 您可以在 GitHub找到此範例的原始程式碼。

使用光學字元辨識 (OCR) 用戶端程式庫,讀取遠端影像中的印刷和手寫文字。 OCR 服務可以讀取影像中的可見文字,並將其轉換成字元數據流。 如需文字辨識的詳細資訊,請參閱 OCR 概觀

提示

您也可以從本機影像讀取文字。 請參閱 ComputerVisionClientOperationsMixin 方法,例如read_in_stream。 或者,如需涉及本機映射的案例,請參閱 GitHub 上的範例程序代碼。

參考文檔 | 庫原始程式碼 | 套件 (PiPy)範例 |

必要條件

  • Azure 訂用帳戶 - 免費建立一個訂用帳戶。

  • Python 3.x

  • 您的 Python 安裝應該包含 pip。 您可以在命令列上執行 pip --version,檢查是否已安裝 pip。 安裝最新版本的 Python 以取得 pip。

  • Azure AI 視覺資源。 您可以使用免費定價層 (F0) 來試用服務,稍後再升級至生產環境的付費層。

  • 來自所建立資源的金鑰和端點,可將應用程式連線至 Azure AI 視覺服務。

    1. 部署 Azure 視覺資源之後,請選取 [前往資源]。
    2. 在左側導覽功能表中,選取 [金鑰和端點]。
    3. 複製其中一個金鑰和 [端點],以供稍後在快速入門中使用。

建立環境變數

在此範例中,在執行應用程式的本機電腦上將認證寫入環境變數。

前往 Azure 入口網站。 如果已成功部署您在 [必要條件] 區段中建立的資源,請選取 [後續步驟] 下的 [前往資源] 按鈕。 您可以在 [金鑰和端點] 頁面中 [資源管理] 底下找到金鑰和端點。 您的資源金鑰與您的 Azure 訂用帳戶識別碼不同。

提示

請勿將金鑰直接包含在您的程式代碼中,且絕不會公開發佈。 如需更多驗證選項 (例如 Azure Key Vault),請參閱 Azure AI 服務安全性文章。

若要設定金鑰和端點的環境變數,請開啟主控台視窗,然後遵循作業系統和開發環境的指示進行。

  1. 若要設定 VISION_KEY 環境變數,請將 取代 your-key 為您資源的其中一個密鑰。
  2. 若要設定 VISION_ENDPOINT 環境變數,請將 取代 your-endpoint 為您資源的端點。
setx VISION_KEY your-key
setx VISION_ENDPOINT your-endpoint

新增環境變數之後,您可能需要重新啟動任何將讀取環境變數的執行中程式,包括主控台視窗。

讀取印刷和手寫文字

  1. 安裝客戶端連結庫。

    在主控台視窗中,執行下列命令:

    pip install --upgrade azure-cognitiveservices-vision-computervision
    
  2. 安裝 Pillow 程式庫。

    pip install pillow
    
  3. 建立新的 Python 應用程式檔案 quickstart-file.py。 然後在您慣用的編輯器或 IDE 中開啟它。

  4. 以下列程式代碼取代 quickstart-file.py 的內容

    from azure.cognitiveservices.vision.computervision import ComputerVisionClient
    from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
    from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
    from msrest.authentication import CognitiveServicesCredentials
    
    from array import array
    import os
    from PIL import Image
    import sys
    import time
    
    '''
    Authenticate
    Authenticates your credentials and creates a client.
    '''
    subscription_key = os.environ["VISION_KEY"]
    endpoint = os.environ["VISION_ENDPOINT"]
    
    computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))
    '''
    END - Authenticate
    '''
    
    '''
    OCR: Read File using the Read API, extract text - remote
    This example will extract text in an image, then print results, line by line.
    This API call can also extract handwriting style text (not shown).
    '''
    print("===== Read File - remote =====")
    # Get an image with text
    read_image_url = "https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png"
    
    # Call API with URL and raw response (allows you to get the operation location)
    read_response = computervision_client.read(read_image_url,  raw=True)
    
    # Get the operation location (URL with an ID at the end) from the response
    read_operation_location = read_response.headers["Operation-Location"]
    # Grab the ID from the URL
    operation_id = read_operation_location.split("/")[-1]
    
    # Call the "GET" API and wait for it to retrieve the results 
    while True:
        read_result = computervision_client.get_read_result(operation_id)
        if read_result.status not in ['notStarted', 'running']:
            break
        time.sleep(1)
    
    # Print the detected text, line by line
    if read_result.status == OperationStatusCodes.succeeded:
        for text_result in read_result.analyze_result.read_results:
            for line in text_result.lines:
                print(line.text)
                print(line.bounding_box)
    print()
    '''
    END - Read File - remote
    '''
    
    print("End of Computer Vision quickstart.")
    
    
  5. 如需選擇性步驟,請參閱決定如何處理資料。 例如,若要明確指定最新的 GA 模型,請編輯 read 語句,如下所示。 略過 參數或使用 "latest" 會自動使用最新的 GA 模型。

       # Call API with URL and raw response (allows you to get the operation location)
       read_response = computervision_client.read(read_image_url,  raw=True, model_version="2022-04-30")
    
  6. python 快速入門檔案上使用 命令執行應用程式。

    python quickstart-file.py
    

輸出

===== Read File - remote =====
The quick brown fox jumps
[38.0, 650.0, 2572.0, 699.0, 2570.0, 854.0, 37.0, 815.0]
Over
[184.0, 1053.0, 508.0, 1044.0, 510.0, 1123.0, 184.0, 1128.0]
the lazy dog!
[639.0, 1011.0, 1976.0, 1026.0, 1974.0, 1158.0, 637.0, 1141.0]

End of Azure AI Vision quickstart.

清除資源

如果您想要清除和移除 Azure AI 服務訂用帳戶,則可以刪除資源或資源群組。 刪除資源群組也會刪除與其相關聯的任何其他資源。

下一步

在本快速入門中,您已瞭解如何安裝 OCR 用戶端連結庫,並使用讀取 API。 接下來,深入了解讀取 API 功能。

  • OCR 概觀
  • 您可以在 GitHub找到此範例的原始程式碼。

使用光學字元辨識 (OCR) 用戶端程式庫,以讀取 API 來讀取印刷和手寫文字。 OCR 服務可以讀取影像中的可見文字,並將其轉換成字元數據流。 如需文字辨識的詳細資訊,請參閱 OCR 概觀

提示

您也可以從本機影像讀取文字。 請參閱 ComputerVisionClient 方法,例如 readInStream。 或者,如需涉及本機映射的案例,請參閱 GitHub 上的範例程序代碼。

參考文檔 | 庫原始程式碼 | 套件 (npm)範例 |

必要條件

  • Azure 訂用帳戶 - 免費建立一個訂用帳戶。

  • 目前版本的 Node.js

  • Azure AI 視覺資源。 您可以使用免費定價層 (F0) 來試用服務,稍後再升級至生產環境的付費層。

  • 來自所建立資源的金鑰和端點,可將應用程式連線至 Azure AI 視覺服務。

    1. 部署 Azure 視覺資源之後,請選取 [前往資源]。
    2. 在左側導覽功能表中,選取 [金鑰和端點]。
    3. 複製其中一個金鑰和 [端點],以供稍後在快速入門中使用。

建立環境變數

在此範例中,在執行應用程式的本機電腦上將認證寫入環境變數。

前往 Azure 入口網站。 如果已成功部署您在 [必要條件] 區段中建立的資源,請選取 [後續步驟] 下的 [前往資源] 按鈕。 您可以在 [金鑰和端點] 頁面中 [資源管理] 底下找到金鑰和端點。 您的資源金鑰與您的 Azure 訂用帳戶識別碼不同。

提示

請勿將金鑰直接包含在您的程式代碼中,且絕不會公開發佈。 如需更多驗證選項 (例如 Azure Key Vault),請參閱 Azure AI 服務安全性文章。

若要設定金鑰和端點的環境變數,請開啟主控台視窗,然後遵循作業系統和開發環境的指示進行。

  1. 若要設定 VISION_KEY 環境變數,請將 取代 your-key 為您資源的其中一個密鑰。
  2. 若要設定 VISION_ENDPOINT 環境變數,請將 取代 your-endpoint 為您資源的端點。
setx VISION_KEY your-key
setx VISION_ENDPOINT your-endpoint

新增環境變數之後,您可能需要重新啟動任何將讀取環境變數的執行中程式,包括主控台視窗。

讀取印刷和手寫文字

建立新的Node.js應用程式。

  1. 在主控台視窗中,為您的應用程式建立新目錄,並瀏覽至該目錄。

    mkdir myapp
    cd myapp
    
  2. npm init執行 命令以使用 package.json 檔案建立節點應用程式。 針對任何提示選取 Enter

    npm init
    
  3. 若要安裝用戶端程式庫,請安裝 ms-rest-azure@azure/cognitiveservices-computervision npm 套件:

    npm install ms-rest-azure
    npm install @azure/cognitiveservices-computervision
    
  4. 安裝非同步模組:

    npm install async
    

    您的應用程式檔案 package.json 會隨著相依性更新。

  5. 建立新的檔案, index.js,然後在文本編輯器中開啟它。

  6. 將下列程式代碼貼到index.js檔案中。

    'use strict';
    
    const async = require('async');
    const fs = require('fs');
    const https = require('https');
    const path = require("path");
    const createReadStream = require('fs').createReadStream
    const sleep = require('util').promisify(setTimeout);
    const ComputerVisionClient = require('@azure/cognitiveservices-computervision').ComputerVisionClient;
    const ApiKeyCredentials = require('@azure/ms-rest-js').ApiKeyCredentials;
    /**
     * AUTHENTICATE
     * This single client is used for all examples.
     */
    const key = process.env.VISION_KEY;
    const endpoint = process.env.VISION_ENDPOINT;
    
    const computerVisionClient = new ComputerVisionClient(
      new ApiKeyCredentials({ inHeader: { 'Ocp-Apim-Subscription-Key': key } }), endpoint);
    /**
     * END - Authenticate
     */
    
    function computerVision() {
      async.series([
        async function () {
    
          /**
           * OCR: READ PRINTED & HANDWRITTEN TEXT WITH THE READ API
           * Extracts text from images using OCR (optical character recognition).
           */
          console.log('-------------------------------------------------');
          console.log('READ PRINTED, HANDWRITTEN TEXT AND PDF');
          console.log();
    
          // URL images containing printed and/or handwritten text. 
          // The URL can point to image files (.jpg/.png/.bmp) or multi-page files (.pdf, .tiff).
          const printedTextSampleURL = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/printed_text.jpg';
    
          // Recognize text in printed image from a URL
          console.log('Read printed text from URL...', printedTextSampleURL.split('/').pop());
          const printedResult = await readTextFromURL(computerVisionClient, printedTextSampleURL);
          printRecText(printedResult);
    
          // Perform read and await the result from URL
          async function readTextFromURL(client, url) {
            // To recognize text in a local image, replace client.read() with readTextInStream() as shown:
            let result = await client.read(url);
            // Operation ID is last path segment of operationLocation (a URL)
            let operation = result.operationLocation.split('/').slice(-1)[0];
    
            // Wait for read recognition to complete
            // result.status is initially undefined, since it's the result of read
            while (result.status !== "succeeded") { await sleep(1000); result = await client.getReadResult(operation); }
            return result.analyzeResult.readResults; // Return the first page of result. Replace [0] with the desired page if this is a multi-page file such as .pdf or .tiff.
          }
    
          // Prints all text from Read result
          function printRecText(readResults) {
            console.log('Recognized text:');
            for (const page in readResults) {
              if (readResults.length > 1) {
                console.log(`==== Page: ${page}`);
              }
              const result = readResults[page];
              if (result.lines.length) {
                for (const line of result.lines) {
                  console.log(line.words.map(w => w.text).join(' '));
                }
              }
              else { console.log('No recognized text.'); }
            }
          }
    
          /**
           * 
           * Download the specified file in the URL to the current local folder
           * 
           */
          function downloadFilesToLocal(url, localFileName) {
            return new Promise((resolve, reject) => {
              console.log('--- Downloading file to local directory from: ' + url);
              const request = https.request(url, (res) => {
                if (res.statusCode !== 200) {
                  console.log(`Download sample file failed. Status code: ${res.statusCode}, Message: ${res.statusMessage}`);
                  reject();
                }
                var data = [];
                res.on('data', (chunk) => {
                  data.push(chunk);
                });
                res.on('end', () => {
                  console.log('   ... Downloaded successfully');
                  fs.writeFileSync(localFileName, Buffer.concat(data));
                  resolve();
                });
              });
              request.on('error', function (e) {
                console.log(e.message);
                reject();
              });
              request.end();
            });
          }
    
          /**
           * END - Recognize Printed & Handwritten Text
           */
          console.log();
          console.log('-------------------------------------------------');
          console.log('End of quickstart.');
    
        },
        function () {
          return new Promise((resolve) => {
            resolve();
          })
        }
      ], (err) => {
        throw (err);
      });
    }
    
    computerVision();
    
  7. 如需選擇性步驟,請參閱決定如何處理資料。 例如,若要明確指定最新的 GA 模型,請編輯 read 語句,如下所示。 略過 參數或使用 "latest" 會自動使用最新的 GA 模型。

      let result = await client.read(url,{modelVersion:"2022-04-30"});
    
  8. node 快速入門檔案上使用 命令執行應用程式。

    node index.js
    

輸出

-------------------------------------------------
READ PRINTED, HANDWRITTEN TEXT AND PDF

Read printed text from URL... printed_text.jpg
Recognized text:
Nutrition Facts Amount Per Serving
Serving size: 1 bar (40g)
Serving Per Package: 4
Total Fat 13g
Saturated Fat 1.5g
Amount Per Serving
Trans Fat 0g
Calories 190
Cholesterol 0mg
ories from Fat 110
Sodium 20mg
nt Daily Values are based on Vitamin A 50%
calorie diet.

-------------------------------------------------
End of quickstart.

清除資源

如果您想要清除和移除 Azure AI 服務訂用帳戶,則可以刪除資源或資源群組。 刪除資源群組也會刪除與其相關聯的任何其他資源。

下一步

在本快速入門中,您已瞭解如何安裝 OCR 用戶端連結庫,並使用讀取 API。 接下來,深入了解讀取 API 功能。

  • OCR 概觀
  • 您可以在 GitHub找到此範例的原始程式碼。

使用光學字元辨識 (OCR) REST API 來讀取印刷和手寫文字。

注意

本快速入門會使用 cURL 命令來呼叫 REST API。 您也可以使用程式設計語言呼叫 REST API。 如需 C#、PythonJavaJavaScript 中的範例,請參閱 GitHub 範例。

必要條件

  • Azure 訂用帳戶 - 免費建立一個訂用帳戶。

  • 已安裝 cURL

  • Azure AI 視覺資源。 您可以使用免費定價層 (F0) 來試用服務,稍後再升級至生產環境的付費層。

  • 來自所建立資源的金鑰和端點,可將應用程式連線至 Azure AI 視覺服務。

    1. 部署 Azure 視覺資源之後,請選取 [前往資源]。
    2. 在左側導覽功能表中,選取 [金鑰和端點]。
    3. 複製其中一個金鑰和 [端點],以供稍後在快速入門中使用。

讀取印刷和手寫文字

光學字元辨識 (OCR) 服務可擷取影像或文件中的可見文字,並將其轉換成字元資料流。 如需文字擷取的詳細資訊,請參閱 OCR 概觀

呼叫讀取 API

若要建立並執行範例,請執行下列步驟:

  1. 將下列 命令複製到文字編輯器。

  2. 視需要在命令中進行下列變更:

    1. 將的值 <key> 取代為您的金鑰。
    2. 將要求 URL 的第一個部分取代https://westcentralus.api.cognitive.microsoft.com/為您自己的端點 URL 中的文字。

      注意

      在 2019 年 7 月 1 日之後建立的新資源將會使用自定義子域名稱。 如需詳細資訊和完整的區域端點清單,請參閱 Azure AI 服務的自訂子網域名稱

    3. 或者,將要求本文https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png中的影像 URL 變更為要分析之不同影像的 URL。
  3. 開啟 [命令提示字元] 視窗。

  4. 將命令從文字編輯器貼到命令提示字元視窗中,然後執行命令。

curl -v -X POST "https://westcentralus.api.cognitive.microsoft.com/vision/v3.2/read/analyze" -H "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key: <subscription key>" --data-ascii "{'url':'https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png'}"

回應包含 Operation-Location 標頭,其值是唯一的 URL。 您可以使用此網址來查詢讀取作業的結果。 URL 會在 48 小時內到期。

選擇性地指定模型版本

如需選擇性步驟,請參閱決定如何處理資料。 例如,若要明確指定最新的 GA 模型,請使用 model-version=2022-04-30 作為 參數。 略過 參數或使用 model-version=latest 會自動使用最新的 GA 模型。

curl -v -X POST "https://westcentralus.api.cognitive.microsoft.com/vision/v3.2/read/analyze?model-version=2022-04-30" -H "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key: <subscription key>" --data-ascii "{'url':'https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png'}"

取得讀取結果

  1. 將下列命令複製到文字編輯器中。

  2. 將 URL 取代為您在上一個程序中複製的 Operation-Location 值。

  3. 將的值 <key> 取代為您的金鑰。

  4. 開啟主控台視窗。

  5. 將文字編輯器中的命令貼到主控台視窗中,然後執行該命令。

    curl -v -X GET "https://westcentralus.api.cognitive.microsoft.com/vision/v3.2/read/analyzeResults/{operationId}" -H "Ocp-Apim-Subscription-Key: {key}" --data-ascii "{body}" 
    

檢查回應

JSON 中會傳回成功的回應。 範例應用程式會在主控台視窗中剖析並顯示成功的回應,類似於下列範例:

{
  "status": "succeeded",
  "createdDateTime": "2021-04-08T21:56:17.6819115+00:00",
  "lastUpdatedDateTime": "2021-04-08T21:56:18.4161316+00:00",
  "analyzeResult": {
    "version": "3.2",
    "readResults": [
      {
        "page": 1,
        "angle": 0,
        "width": 338,
        "height": 479,
        "unit": "pixel",
        "lines": [
          {
            "boundingBox": [
              25,
              14,
              318,
              14,
              318,
              59,
              25,
              59
            ],
            "text": "NOTHING",
            "appearance": {
              "style": {
                "name": "other",
                "confidence": 0.971
              }
            },
            "words": [
              {
                "boundingBox": [
                  27,
                  15,
                  294,
                  15,
                  294,
                  60,
                  27,
                  60
                ],
                "text": "NOTHING",
                "confidence": 0.994
              }
            ]
          }
        ]
      }
    ]
  }
}

清除資源

如果您想要清除和移除 Azure AI 服務訂用帳戶,則可以刪除資源或資源群組。 刪除資源群組也會刪除與其相關聯的任何其他資源。

下一步

在本快速入門中,您已瞭解如何呼叫讀取 REST API。 接下來,深入了解讀取 API 功能。

必要條件

  • Azure 訂用帳戶 - 免費建立一個訂用帳戶。

  • Azure AI 視覺資源。 您可以使用免費定價層 (F0) 來試用服務,稍後再升級至生產環境的付費層。

  • 連線至 Vision Studio

    • 您可能需要登入。
    • 登入之後,請選取 [檢視所有資源]。 如有必要,請選取 [重新整理]。 確認您的資源可以使用。

    如需詳細資訊,請參閱開始使用 Vision Studio

讀取印刷和手寫文字

  1. 在 [光學字元辨識] 底下,選取 [從影像擷取文字]。

  2. 在 [試用] 底下,確認此示範招致使用您的 Azure 帳戶。 如需詳細資訊,請參閱 Azure AI 視覺定價

  3. 從可用的集合中選取映射,或上傳您自己的映像。

  4. 如有必要,請選取 [請選取資源] 以選取您的資源。

    選取影像之後,擷取的文字會出現在輸出視窗中。 您也可以選取 [JSON] 索引標籤,以查看 API 呼叫傳回的 JSON 輸出。

在試用體驗下方是後續步驟,以在您自己的應用程式中開始使用此功能。

下一步

在本快速入門中,您已使用 Vision Studio 來存取讀取 API。 接下來,深入了解讀取 API 功能。