老王

使用Python+Webhook整合百度名片OCR API

老王Lv.2入道 发布于 分享
0 回复2045 浏览
注册百度云 注册百度云帐户,并申请 OCR 相关权限。 地址 创建应用,并获取 Client ID 和 Secret Key [图片] 名片扫描接口百度提供每天 500 个免费的额度 创建定时任务,更新 access_token 创建一个配置表,包含 access_token 字段和 expired_datetime 字段 创建按 expired_date 字段定时触发的工作流 工作流主要构成 webhook 获取 access_token 和 expires_in # webhook 自定义方式 POST https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=# 百度云里的API KEY# &client_secret=# 百度云里的Secret Key# 当前时间 +expires_in 获得 expired_datetime [图片] access_token 和 expired_datetime 写入配置表 调用名片 OCR 扫描接口 在 API 开发文档中找到存储图片表的相关配置 创建 webhook,传入 rowid 和配置表中的 access_token [图片] import requests import json ''' 获取图片地址 ''' AppKey = "" SecretKey = "" Sign = "==" request_url_ECSP2 = "" worksheetId = "" rowId = input [ "rowId" ] req = { "appKey" : AppKey , "sign" : Sign , "worksheetId" : worksheetId , "rowId" : rowId } data_to_post = json . dumps ( req , ensure_ascii = False , separators = ( ',' , ':' )) print ( data_to_post ) headers = { 'content-type' : 'application/json' } response = requests . post ( request_url_ECSP2 , data = data_to_post , headers = headers ) if response : response_json = json . loads ( response . text ) namecard_img = json . loads ( response_json [ "data" ][ "NamecardIMG" ]) namecard_img_url = namecard_img [ 0 ][ "original_file_full_path" ] ''' 名片识别 ''' request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/business_card" access_token = input [ "access_token" ] data = { "access_token" : access_token , "url" : namecard_img_url } headers = { 'content-type' : 'application/x-www-form-urlencoded' } response = requests . post ( request_url , data = data , headers = headers ) if response : result = json . loads ( response . text )[ 'words_result' ] print ( result ) output = {} output [ "PC" ] = result [ 'PC' ][ 0 ] output [ "TEL" ] = result [ 'TEL' ][ 0 ] TITLE = '' for i in range ( 0 , len ( result [ 'TITLE' ])): TITLE = TITLE + result [ 'TITLE' ][ i ] output [ "TITLE" ] = TITLE output [ "EMAIL" ] = result [ 'EMAIL' ][ 0 ] output [ "FAX" ] = result [ 'FAX' ][ 0 ] output [ "URL" ] = result [ 'URL' ][ 0 ] output [ "ADDR" ] = result [ 'ADDR' ][ 0 ] output [ "COMPANY" ] = result [ 'COMPANY' ][ 0 ] output [ "MOBILE" ] = result [ 'MOBILE' ][ 0 ] output [ "NAME" ] = result [ 'NAME' ][ 0 ]