Python3使用腾讯云文字识别(腾讯OCR)提取图片中的文字内容实例详解
作者:彭世瑜 时间:2023-11-16 22:45:05
百度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()
来源:https://blog.csdn.net/mouday/article/details/104346484
标签:Python,OCR
0
投稿
猜你喜欢
python中web框架的自定义创建
2023-09-18 14:54:24
numpy中的meshgrid函数的使用
2021-10-07 06:11:39
详解如何通过Mysql的二进制日志恢复数据库数据
2024-01-19 11:33:43
详解Python魔法方法之描述符类
2023-12-17 04:59:59
PHP获取类中常量,属性,及方法列表的方法
2023-11-19 19:57:58
标签水平右对齐更适合中文网站
2009-05-01 11:54:00
如何调用SQL Server的存储过程?
2009-11-15 20:15:00
MySQL查询倒数第二条记录实现方法
2024-01-26 07:15:50
SQL Server 2000 作数据库服务器的优点
2009-01-23 13:47:00
MySQL按小时查询数据,没有的补0
2024-01-18 00:55:30
oracle初始化参数设置
2010-07-31 12:47:00
Python open读写文件实现脚本
2022-08-23 00:26:46
Python 实现数据库(SQL)更新脚本的生成方法
2024-01-16 21:01:18
python中numpy包使用教程之数组和相关操作详解
2022-10-26 19:18:40
ES6正则表达式的一些新功能总结
2024-05-09 10:35:05
Python3.6中Twisted模块安装的问题与解决
2022-05-29 15:45:02
对比分析BN和dropout在预测和训练时区别
2022-09-05 11:46:55
python使用urllib2提交http post请求的方法
2023-11-24 19:35:53
利用Python编写个有趣的记仇本
2022-08-25 19:20:20
linux centos7安装mysql8的教程
2024-01-20 21:52:11