1. 程式人生 > 程式設計 >Python3使用騰訊雲文字識別(騰訊OCR)提取圖片中的文字內容例項詳解

Python3使用騰訊雲文字識別(騰訊OCR)提取圖片中的文字內容例項詳解

百度OCR體驗地址:

https://ai.baidu.com/tech/imagerecognition/general

騰訊OCR體驗地址:

https://cloud.tencent.com/act/event/ocrdemo

測試結果是:騰訊的效果要比百度的好

騰訊雲目前額度是:

每個介面 1,000次/月免費,有6個文字識別的介面,一共是6,000次/月

百度介面呼叫之前寫過文章

python實現百度OCR圖片識別過程解析

使用步驟

1、註冊賬號: https://cloud.tencent.com/

2、開通服務:https://console.cloud.tencent.com/ocr/general

3、申請訪問祕鑰:https://console.cloud.tencent.com/cam/capi

4、通過 API 或 SDK 或命令列來使用服務

具體參考《操作指南》:https://cloud.tencent.com/document/product/866/17622

介面使用

1、安裝SDK

https://github.com/TencentCloud/tencentcloud-sdk-python

pip3 install tencentcloud-sdk-python

2、程式碼例項

# -*- coding: utf-8 -*-

import json

from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.ocr.v20181119 import ocr_client
from tencentcloud.ocr.v20181119.models import (
  GeneralAccurateOCRRequest,EnglishOCRRequest,GeneralBasicOCRRequest,GeneralEfficientOCRRequest,GeneralFastOCRRequest,GeneralHandwritingOCRRequest
)


class TencentOcr(object):
  """
  計費說明:1,000次/月免費
  https://cloud.tencent.com/document/product/866/17619
  """
  SECRET_ID = "你的祕鑰 SECRET_ID"

  SECRET_KEY = "你的祕鑰 SECRET_KEY"
	
	# 地域列表
	# https://cloud.tencent.com/document/api/866/33518#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8
  Region = "ap-beijing"

  endpoint = "ocr.tencentcloudapi.com"

  # 通用文字識別相關介面
  # https://cloud.tencent.com/document/api/866/37173
  mapping = {
    # 通用印刷體識別(高精度版) ok
    "GeneralAccurateOCR": GeneralAccurateOCRRequest,# 英文識別 ok
    "EnglishOCR": EnglishOCRRequest,# 通用印刷體識別 一般
    "GeneralBasicOCR": GeneralBasicOCRRequest,# 通用印刷體識別(精簡版)(免費公測版)no
    "GeneralEfficientOCR": GeneralEfficientOCRRequest,# 通用印刷體識別(高速版)一般
    "GeneralFastOCR": GeneralFastOCRRequest,# 通用手寫體識別 ok
    "GeneralHandwritingOCR": GeneralHandwritingOCRRequest,}

  def __init__(self):
    cred = credential.Credential(self.SECRET_ID,self.SECRET_KEY)

    httpProfile = HttpProfile()
    httpProfile.endpoint = self.endpoint

    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    self.client = ocr_client.OcrClient(cred,self.Region,clientProfile)

  def get_image_text(self,image_url,ocr="GeneralAccurateOCR"):
    req = self.mapping[ocr]()
    req.ImageUrl = image_url
    resp = getattr(self.client,ocr)(req)
    return json.loads(resp.to_json_string())['TextDetections'][0]['DetectedText']


def main():
  tencentOcr = TencentOcr()
  url = "https://ocr-demo-1254418846.cos.ap-guangzhou.myqcloud.com/general/GeneralBasicOCR/GeneralBasicOCR3.jpg"
  print(tencentOcr.get_image_text(url,ocr="GeneralHandwritingOCR"))


if __name__ == '__main__':
  main()

更多關於Python3使用騰訊雲文字識別(騰訊OCR)提取圖片中的文字內容例項請檢視下面的相關連結