不是广告哈,就是记录一下操作,因为这些页面真的很难找,即便知道缺什么也找不到对应位置在哪…
调用说明:(以Python为例)
整个流程:
- 腾讯开服务
- 图片转base64编码
- 把base64编码转腾讯OCR
- 腾讯OCR返回的
json ,转Pandas Dict 类型 - 整理dict的数据为
Pandas DataFrame 类型
步骤1:腾讯开服务
-
进入控制台:https://console.cloud.tencent.com/cam/capi,新建一个密钥: -
在控制台开启资源包:https://console.cloud.tencent.com/ocr/overview
步骤2:图片转Base64使用
def change_img_to_base64(image_path):
"""base64编码图片"""
with open(image_path, 'rb') as f:
image_data = f.read()
base64_data: bytes = base64.b64encode(image_data)
return base64_data
步骤3:把Base64编码转腾讯OCR
def tencent_ocr(suffix, image_based_64):
"""腾讯OCR
:param suffix:图片的后缀,比如png,jpg
:param image_based_64:图片的base64编码
"""
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.ocr.v20181119 import ocr_client, models
try:
cred = credential.Credential("自己的SecretId(看步骤1)", "自己的SecretKey(看步骤1)")
httpProfile = HttpProfile()
httpProfile.endpoint = "ocr.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = ocr_client.OcrClient(cred, "ap-beijing", clientProfile)
req = models.RecognizeTableOCRRequest()
params = {
"ImageBase64": "data:image/{suffix};base64,{image_based_64}".format(
suffix=suffix, image_based_64=image_based_64.decode("utf8"))
}
req.from_json_string(json.dumps(params))
resp = client.RecognizeTableOCR(req)
return resp.to_json_string()
except TencentCloudSDKException as err:
print(err)
步骤4:Json转Dict
def json_to_dict(my_json: str) -> dict:
"""json转dict类型"""
return json.loads(my_json)
步骤5:Dict数据转Pandas.DataFrame格式:
def formation(json_data):
"""根据腾讯ocr识别结果整理格式并输出"""
if json_data is not None:
dict_data = json_to_dict(json_data)
for table_index, table_data in enumerate(dict_data['TableDetections']):
table_data_list = []
for each_table_data in table_data['Cells']:
content: str = each_table_data['Text']
x_y: list = list(each_table_data['Polygon'][0].values())
x_y.append(content)
table_data_list.append(x_y)
table_df = pd.DataFrame(table_data_list)
table_df.columns = ['x', 'y', 'content']
table_df.sort_values(by=['y', 'x'], ascending=True, inplace=True)
for index, line_df in table_df.groupby("y"):
line_df.sort_values(by=['x'], ascending=True, inplace=True)
line_values = line_df['content'].values
print(",".join(line_values))
步骤6:整体依次调用
if __name__ == '__main__':
image = "mypic.png"
image_base64 = change_img_to_base64(image)
suffix = image.split('.')[-1]
tencent_result: json = tencent_ocr(suffix, image_base64)
formation(tencent_result)
完整代码
import base64
import json
import pandas as pd
def change_img_to_base64(image_path):
"""base64编码图片"""
with open(image_path, 'rb') as f:
image_data = f.read()
base64_data: bytes = base64.b64encode(image_data)
return base64_data
def json_to_dict(my_json: str) -> dict:
"""json转dict类型"""
return json.loads(my_json)
def tencent_ocr(suffix, image_based_64):
"""腾讯OCR
:param suffix:图片的后缀,比如png,jpg
:param image_based_64:图片的base64编码
"""
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.ocr.v20181119 import ocr_client, models
try:
cred = credential.Credential("自己的SecretId(看步骤1)", "自己的SecretKey(看步骤1)")
httpProfile = HttpProfile()
httpProfile.endpoint = "ocr.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = ocr_client.OcrClient(cred, "ap-beijing", clientProfile)
req = models.RecognizeTableOCRRequest()
params = {
"ImageBase64": "data:image/{suffix};base64,{image_based_64}".format(
suffix=suffix, image_based_64=image_based_64.decode("utf8"))
}
req.from_json_string(json.dumps(params))
resp = client.RecognizeTableOCR(req)
return resp.to_json_string()
except TencentCloudSDKException as err:
print(err)
def formation(json_data):
"""根据腾讯ocr识别结果整理格式并输出"""
if json_data is not None:
dict_data = json_to_dict(json_data)
for table_index, table_data in enumerate(dict_data['TableDetections']):
table_data_list = []
for each_table_data in table_data['Cells']:
content: str = each_table_data['Text']
x_y: list = list(each_table_data['Polygon'][0].values())
x_y.append(content)
table_data_list.append(x_y)
table_df = pd.DataFrame(table_data_list)
table_df.columns = ['x', 'y', 'content']
table_df.sort_values(by=['y', 'x'], ascending=True, inplace=True)
for index, line_df in table_df.groupby("y"):
line_df.sort_values(by=['x'], ascending=True, inplace=True)
line_values = line_df['content'].values
print(",".join(line_values))
if __name__ == '__main__':
image = "mypic.png"
image_base64 = change_img_to_base64(image)
suffix = image.split('.')[-1]
tencent_result: json = tencent_ocr(suffix, image_base64)
formation(tencent_result)
资料整理
|