Python3调用腾讯云OCR接口提取图片文字完整指南

Dylan
Dylan
发布时间:2026-10-13| 7人浏览过
原创

本文详解如何在Python3环境中使用腾讯云OCR服务提取图片文字。内容涵盖百度与腾讯OCR的效果对比、免费额度说明、账号注册与密钥申请流程,以及通过pip安装SDK和具体代码实现示例,帮助用户快速完成接口集成。

百度OCR体验地址:

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

腾讯OCR体验地址:

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

经测试,腾讯OCR的效果优于百度。

腾讯云目前的免费额度为:

每个接口每月提供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)提取图片中的文字内容实例请查看下面的相关链接

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。