OCR在线

光学字符识别是一种识别光栅图像内容的智能方法。当您需要以数字格式保存旧的档案文献时,它甚至会变得更加有用。通过使用 OCR 操作将数千年前的书籍转换为数字图书馆的形式,可以轻松保存它们。而且,多年来,这种需求已经变得无处不在。因此,为了满足这一要求,您要么需要使用一些开箱即用的软件,要么在您需要在没有人工干预的情况下执行批量操作的情况下,最快捷、最简单的方法是使用编程 API。在本文的其余部分,我们将解释如何使用 Python REST API 对图像执行 OCR 的步骤。

OCR 在线 REST API

Aspose.OCR Cloud SDK for Python 在光栅图像(BMPJPEGGIFPNGTIFF)的光学字符识别方面非常出色。执行 OCR 操作时,它使您能够读取字符以及字体信息。您可以在提供 X 和 Y 坐标的同时对整个图像或特定部分执行 OCR。光学字符识别完成后,响应以 XML 或 JSON 格式返回,提取的文本可以保存为 TXT、PDF 和 HOCR 格式。下面指定了一些高级功能。

  • 自动倾斜校正
  • 自动和手动文档布局检测
  • 先进的自动化图像预处理
  • 支持多种国际语言
  • 速度快,不占用硬件资源

支持的语言

除了英语之外,该 API 还能够识别法语、德语、意大利语、葡萄牙语和西班牙语的文本。

支持的文件格式

下面指定的是 REST API 当前支持的 OCR 操作的文件格式的完整列表。

.bmp.dib.jpeg、.jpg、.jpe、.jp2.png.webp、.pbm、.pgm、.ppm、.pxm、.pnm、.pfm、.sr、.ras、.tiff、.tif、.exr、.hdr、.pic

使用 Python 在线进行 OCR

我们的 API 是根据 REST 架构开发的,因此在本节中,我们将探索使用 cURL 命令进行图像到文本的转换。我们知道 cURL 命令是通过控制台访问 REST API 的灵活方式。现在先决条件之一是生成 JWT 令牌。有关更多相关详细信息,请访问 如何使用客户端 ID 和客户端密钥获取 JWT 令牌

curl -v "https://api.aspose.cloud/oauth2/token" \-X POST \-d "grant_type=client_credentials&client_id=xxxxx-xxxx-xxx-xxxx-&client_secret=xxxxxxxxx" \-H "Content-Type: application/x-www-form-urlencoded" \ -H "Accept: application/json"

获得 JWT 令牌后,请尝试使用以下命令对位于云存储上的包含英文文本的图像执行 OCR 操作。

curl -X GET "https://api.aspose.cloud/v3.0/ocr/downsize.jpeg/recognize?language=1" -H "accept: application/json" -H "authorization: Bearer <JWT Token>"

本地图片转文字

在本节中,我们将对从本地驱动器加载的图像执行 OCR 操作

# 完整示例和数据文件请前往https://github.com/aspose-ocr-cloud/aspose-ocr-cloud-python/
import os
import asposeocrcloud.api.storage_api
from asposeocrcloud.configuration import Configuration
from asposeocrcloud.api.ocr_api import OcrApi
from asposeocrcloud.models import OCRRect, OCRRegion, OCRRequestData, OCRRequestDataStorage, LanguageGroup
from asposeocrcloud.rest import ApiException

import json as json

class  RecognizeFromContent(object):

  def __init__(self):

      # 设置 CAD 和存储 API 客户端 
      with open("config.json") as f:
          server_file_info = json.load(f)


      config = Configuration( apiKey=server_file_info['AppKey'],
                              appSid=server_file_info['AppSid'])      
      self.ocr_api = OcrApi(config)

  def recognize_text(self):
      file_name = "5.png"
      src = os.path.join(os.path.abspath("data/"), file_name)
      try:

          res = self.ocr_api.post_recognize_from_content(src)  # type: asposeocrcloud.models.OcrResponse
          return res.text

      except ApiException as ex:
          print("Exception")
          print("Info: " + str(ex))
          raise ex

obj=RecognizeFromContent()
print(obj.recognize_text())

对云存储中的图像进行 Python OCR

我们将学习如何从云存储加载图像并使用 Python 代码片段执行图像 OCR 的详细信息。

# 完整示例和数据文件请前往https://github.com/aspose-ocr-cloud/aspose-ocr-cloud-python/
import asposeocrcloud.api.storage_api
from asposeocrcloud.configuration import Configuration
from asposeocrcloud.api.ocr_api import OcrApi
from asposeocrcloud.models import OCRRect, OCRRegion, OCRRequestData, OCRRequestDataStorage, LanguageGroup

import json as json

class  RecognizeFromStorage(object):

    def __init__(self):

        # 设置 CAD 和存储 API 客户端 
        with open("config.json") as f:
            server_file_info = json.load(f)
        config = Configuration( apiKey=server_file_info['AppKey'],
                                appSid=server_file_info['AppSid'])      
        self.ocr_api = OcrApi(config)
        self.storage_api= asposeocrcloud.api.storage_api.StorageApi(config)

    def recognize_text(self):
        self.storage_api.upload_file("5.png", r"data\5.png")
        res = self.ocr_api.get_recognize_from_storage("5.png")
        return res.text

obj=RecognizeFromStorage()
print(obj.recognize_text())

URL 上的图像 OCR

如果您遇到需要对 Web URL 上可用的图像执行光学图像识别的需求,该 API 完全能够胜任并支持此功能。API 的 postrecognizefromurl 方法可用于满足此需求。

# 完整示例和数据文件请前往https://github.com/aspose-ocr-cloud/aspose-ocr-cloud-python/
import os
import asposeocrcloud.api.storage_api
from asposeocrcloud.configuration import Configuration
from asposeocrcloud.api.ocr_api import OcrApi
from asposeocrcloud.models import OCRRect, OCRRegion, OCRRequestData, OCRRequestDataStorage, LanguageGroup
from asposeocrcloud.rest import ApiException

import json as json

class  RecognizeFromURL(object):

    def __init__(self):
       
        # 设置 CAD 和存储 API 客户端 
        with open("config.json") as f:
            server_file_info = json.load(f)        
        config = Configuration( apiKey=server_file_info['AppKey'],
                                appSid=server_file_info['AppSid'])      
        self.ocr_api = OcrApi(config)  
         
    def recognize_text(self):
        url = "https://upload.wikimedia.org/wikipedia/commons/2/2f/Book_of_Abraham_FirstPage.png"
        try:
            res = self.ocr_api.post_recognize_from_url(url)  # type: asposeocrcloud.models.OcrResponse
            return res.text

        except ApiException as ex:
            print("Exception")
            print("Info: " + str(ex))
            raise ex
                   
obj=RecognizeFromURL()
print(obj.recognize_text())

结论

在本文中,我们了解了如何使用 cURL 命令以及通过 Python 代码片段在线执行 OCR 的详细信息。由于我们的 Cloud SDK 是在 MIT 许可下构建的,因此您可以考虑从 GitHub 存储库 下载完整的源代码。此存储库还附带免费演示,要执行它们,请按照以下步骤操作。

  • 签出 SDK 或从 pip 获取 (pip install aspose-ocr-cloud)
  • 设置您的客户端 ID 和客户端密钥
  • 运行 Python 控制台 DemoUnitTests

相关文章

我们强烈建议您访问以下链接以了解更多信息: