378 lines
14 KiB
Plaintext
378 lines
14 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from aip import AipOcr\n",
|
||
"\n",
|
||
"\"\"\" 你的 APPID AK SK \"\"\"\n",
|
||
"APP_ID = '17553946'\n",
|
||
"API_KEY = 'IMC1ss3Tyo3vEAdVH6jgcdv2'\n",
|
||
"SECRET_KEY = 'Iua6KdhuDtz159rjzGeFGpqgjhnoU0zZ'\n",
|
||
"\n",
|
||
"client = AipOcr(APP_ID, API_KEY, SECRET_KEY)\n",
|
||
"def get_file_content(filePath):\n",
|
||
" with open(filePath, 'rb') as fp:\n",
|
||
" return fp.read()\n",
|
||
"\n",
|
||
"image = get_file_content('1.jpg')\n",
|
||
"\n",
|
||
"\"\"\" 调用通用文字识别, 图片参数为本地图片 \"\"\"\n",
|
||
"#client.basicGeneral(image);\n",
|
||
"\n",
|
||
"\"\"\" 如果有可选参数 \"\"\"\n",
|
||
"options = {}\n",
|
||
"options[\"language_type\"] = \"CHN_ENG\"\n",
|
||
"options[\"detect_direction\"] = \"true\"\n",
|
||
"options[\"detect_language\"] = \"true\"\n",
|
||
"options[\"probability\"] = \"true\"\n",
|
||
"\n",
|
||
"\"\"\" 带参数调用通用文字识别, 图片参数为本地图片 \"\"\"\n",
|
||
"result= client.basicGeneral(image, options)\n",
|
||
"if 'words_result' in result:\n",
|
||
" print('\\n'.join([w['words'] for w in result['words_result']]))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"#将指定目录下图片文件进行文字识别\n",
|
||
"import os,sys\n",
|
||
"from aip import AipOcr\n",
|
||
"\n",
|
||
"\"\"\" 你的 APPID AK SK \"\"\"\n",
|
||
"APP_ID = '17553946'\n",
|
||
"API_KEY = 'IMC1ss3Tyo3vEAdVH6jgcdv2'\n",
|
||
"SECRET_KEY = 'Iua6KdhuDtz159rjzGeFGpqgjhnoU0zZ'\n",
|
||
"\n",
|
||
"client = AipOcr(APP_ID, API_KEY, SECRET_KEY)\n",
|
||
"def get_file_content(filePath):\n",
|
||
" with open(filePath, 'rb') as fp:\n",
|
||
" return fp.read()\n",
|
||
"\n",
|
||
"options = {}\n",
|
||
"options[\"language_type\"] = \"CHN_ENG\"\n",
|
||
"options[\"detect_direction\"] = \"true\"\n",
|
||
"options[\"detect_language\"] = \"true\"\n",
|
||
"options[\"probability\"] = \"true\"\n",
|
||
"\n",
|
||
"fi_path = os.getcwd()+'/data'\n",
|
||
"fl = os.listdir(fi_path)\n",
|
||
"fl.sort()\n",
|
||
"for fl1 in fl:\n",
|
||
" file_name = fi_path+'/' + fl1\n",
|
||
" image = get_file_content(file_name)\n",
|
||
" result= client.basicGeneral(image, options)\n",
|
||
" if 'words_result' in result:\n",
|
||
" print('\\n'.join([w['words'] for w in result['words_result']]))\n",
|
||
" print('\\n')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 识别图片中表格"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"metadata": {
|
||
"execution": {
|
||
"iopub.execute_input": "2020-11-26T08:59:12.967127Z",
|
||
"iopub.status.busy": "2020-11-26T08:59:12.966172Z",
|
||
"iopub.status.idle": "2020-11-26T08:59:28.010488Z",
|
||
"shell.execute_reply": "2020-11-26T08:59:28.006947Z",
|
||
"shell.execute_reply.started": "2020-11-26T08:59:12.967019Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"22917135_2274033\n",
|
||
"已完成\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import requests\n",
|
||
"import json\n",
|
||
"import base64\n",
|
||
"import time\n",
|
||
"\n",
|
||
"def get_access_token():\n",
|
||
" client_id = 'KwXkGawxh0sjOQdF9Ae9LeLb'\n",
|
||
" client_secret = 'siprEKMp5UcRTOAngEfIOOe9x6xkqGXq' \n",
|
||
" # client_id 为官网获取的AK, client_secret 为官网获取的SK\n",
|
||
" host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'.format(\n",
|
||
" client_id, client_secret)\n",
|
||
" response = requests.get(host).text\n",
|
||
" data = json.loads(response)\n",
|
||
" access_token = data['access_token']\n",
|
||
" return access_token\n",
|
||
"\n",
|
||
"def get_excel(requests_id, access_token):\n",
|
||
" headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
|
||
" pargams = {\n",
|
||
" 'request_id': requests_id,\n",
|
||
" 'result_type': 'excel'\n",
|
||
" }\n",
|
||
" url = 'https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/get_request_result'\n",
|
||
" url_all = url + \"?access_token=\" + access_token\n",
|
||
" res = requests.post(url_all, headers=headers, params=pargams)#访问链接获取excel下载页\n",
|
||
" info_1 = res.json()['result']['ret_msg']\n",
|
||
" excel_url=res.json()['result']['result_data']\n",
|
||
" excel_1=requests.get(excel_url).content\n",
|
||
" with open('识别结果11.xls','wb+') as f:\n",
|
||
" f.write(excel_1)\n",
|
||
" print(info_1)\n",
|
||
"\n",
|
||
"\n",
|
||
"request_url = \"https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/request\"\n",
|
||
"# 二进制方式打开图片文件\n",
|
||
"f = open('山东大学强基计划(2020).jpg', 'rb')\n",
|
||
"img = base64.b64encode(f.read())\n",
|
||
"\n",
|
||
"params = {\"image\":img}\n",
|
||
"access_token = get_access_token()\n",
|
||
"request_url = request_url + \"?access_token=\" + access_token\n",
|
||
"headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
|
||
"response = requests.post(request_url, data=params, headers=headers)\n",
|
||
"if response:\n",
|
||
" m_xx = response.json()\n",
|
||
"requests_id = m_xx['result'][0]['request_id'] \n",
|
||
"print(requests_id)\n",
|
||
"time.sleep(10)\n",
|
||
"get_excel(requests_id, access_token)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"metadata": {
|
||
"execution": {
|
||
"iopub.execute_input": "2020-11-03T02:06:23.901979Z",
|
||
"iopub.status.busy": "2020-11-03T02:06:23.900925Z",
|
||
"iopub.status.idle": "2020-11-03T02:06:24.312339Z",
|
||
"shell.execute_reply": "2020-11-03T02:06:24.309116Z",
|
||
"shell.execute_reply.started": "2020-11-03T02:06:23.901714Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"{'refresh_token': '25.1dfb7a14cd15d03051976c8db246fa92.315360000.1919729184.282335-22917135', 'expires_in': 2592000, 'session_key': '9mzdCSFczT8Mv7ZN07SjsPo0dZr0AvxAeDt6Kjn1Z9jiiotA6kW2TZYzslnuYOFd1ZCx75mmzW0TRF+nxYAHzPOb5fmjlw==', 'access_token': '24.277af0e5357bc3958c5b3cac49662a80.2592000.1606961184.282335-22917135', 'scope': 'public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_台湾通行证 vis-ocr_港澳通行证 vis-ocr_机动车购车发票识别 vis-ocr_机动车检验合格证识别 vis-ocr_车辆vin码识别 vis-ocr_定额发票识别 vis-ocr_保单识别 vis-ocr_机打发票识别 vis-ocr_行程单识别 brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_invoice brain_ocr_vehicle_certificate brain_ocr_air_ticket brain_ocr_invoice brain_ocr_insurance_doc brain_formula brain_ocr_meter brain_doc_analysis brain_ocr_webimage_loc wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component', 'session_secret': '6ac230d6705697808e228241c519f303'}\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import requests \n",
|
||
"\n",
|
||
"# client_id 为官网获取的AK, client_secret 为官网获取的SK\n",
|
||
"host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=KwXkGawxh0sjOQdF9Ae9LeLb&client_secret=siprEKMp5UcRTOAngEfIOOe9x6xkqGXq'\n",
|
||
"response = requests.get(host)\n",
|
||
"if response:\n",
|
||
" print(response.json())"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 14,
|
||
"metadata": {
|
||
"execution": {
|
||
"iopub.execute_input": "2020-11-03T03:24:08.412670Z",
|
||
"iopub.status.busy": "2020-11-03T03:24:08.411768Z",
|
||
"iopub.status.idle": "2020-11-03T03:24:08.813580Z",
|
||
"shell.execute_reply": "2020-11-03T03:24:08.810985Z",
|
||
"shell.execute_reply.started": "2020-11-03T03:24:08.412567Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"已完成\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import requests\n",
|
||
"import json\n",
|
||
"import base64\n",
|
||
"access_token = '24.277af0e5357bc3958c5b3cac49662a80.2592000.1606961184.282335-22917135'\n",
|
||
"headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
|
||
"pargams = {\n",
|
||
" 'request_id': '22917135_2227436',\n",
|
||
" 'result_type': 'excel'\n",
|
||
"}\n",
|
||
"\n",
|
||
"\n",
|
||
"url = 'https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/get_request_result'\n",
|
||
"url_all = url + \"?access_token=\" + access_token\n",
|
||
"res = requests.post(url_all, headers=headers, params=pargams)#访问链接获取excel下载页\n",
|
||
"info_1 = res.json()['result']['ret_msg']\n",
|
||
"excel_url=res.json()['result']['result_data']\n",
|
||
"excel_1=requests.get(excel_url).content\n",
|
||
"with open('识别结果12.xls','wb+') as f:\n",
|
||
" f.write(excel_1)\n",
|
||
"print(info_1)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 80,
|
||
"metadata": {
|
||
"execution": {
|
||
"iopub.execute_input": "2020-11-03T08:43:37.180693Z",
|
||
"iopub.status.busy": "2020-11-03T08:43:37.179800Z",
|
||
"iopub.status.idle": "2020-11-03T08:43:37.656055Z",
|
||
"shell.execute_reply": "2020-11-03T08:43:37.653668Z",
|
||
"shell.execute_reply.started": "2020-11-03T08:43:37.180589Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"list"
|
||
]
|
||
},
|
||
"execution_count": 80,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"import requests\n",
|
||
"import json\n",
|
||
"import base64\n",
|
||
"import demjson\n",
|
||
"access_token = '24.277af0e5357bc3958c5b3cac49662a80.2592000.1606961184.282335-22917135'\n",
|
||
"headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
|
||
"pargams = {\n",
|
||
" 'request_id': '22917135_2227436',\n",
|
||
" 'result_type': 'json'\n",
|
||
"}\n",
|
||
"\n",
|
||
"\n",
|
||
"url = 'https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/get_request_result'\n",
|
||
"url_all = url + \"?access_token=\" + access_token\n",
|
||
"res = requests.post(url_all, headers=headers, params=pargams)#访问链接获取excel下载页\n",
|
||
"#info_1 = res.json()['result']['ret_msg']\n",
|
||
"excel_1=res.json()['result']['result_data']#['forms'][0]['body']\n",
|
||
"type(excel_1)\n",
|
||
"#excel_new = demjson.decode(excel_1)\n",
|
||
"#for m_col in excel_new['forms'][0]['body']:\n",
|
||
"# print(m_col)\n",
|
||
"m_xx =json.loads(excel_1)\n",
|
||
"\n",
|
||
"#with open('识别结果12.json','w') as fl:\n",
|
||
"# json.dump(m_xx['forms'][0],fl,ensure_ascii=False)\n",
|
||
"#print(info_1)\n",
|
||
"#print(json.dumps(m_xx['forms'][0],ensure_ascii=False))\n",
|
||
"print(m_xx['forms'][0]['body'])"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 识别保存为json文件"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 81,
|
||
"metadata": {
|
||
"execution": {
|
||
"iopub.execute_input": "2020-11-03T08:48:26.082167Z",
|
||
"iopub.status.busy": "2020-11-03T08:48:26.081266Z",
|
||
"iopub.status.idle": "2020-11-03T08:48:26.430039Z",
|
||
"shell.execute_reply": "2020-11-03T08:48:26.428236Z",
|
||
"shell.execute_reply.started": "2020-11-03T08:48:26.082060Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"已完成\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import requests\n",
|
||
"import json\n",
|
||
"import base64\n",
|
||
"import demjson\n",
|
||
"access_token = '24.277af0e5357bc3958c5b3cac49662a80.2592000.1606961184.282335-22917135'\n",
|
||
"headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
|
||
"pargams = {\n",
|
||
" 'request_id': '22917135_2227436',\n",
|
||
" 'result_type': 'json'\n",
|
||
"}\n",
|
||
"\n",
|
||
"\n",
|
||
"url = 'https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/get_request_result'\n",
|
||
"url_all = url + \"?access_token=\" + access_token\n",
|
||
"res = requests.post(url_all, headers=headers, params=pargams)#访问链接获取excel下载页\n",
|
||
"#info_1 = res.json()['result']['ret_msg']\n",
|
||
"excel_1=res.json()['result']['result_data']#['forms'][0]['body']\n",
|
||
"type(excel_1)\n",
|
||
"#excel_new = demjson.decode(excel_1)\n",
|
||
"#for m_col in excel_new['forms'][0]['body']:\n",
|
||
"# print(m_col)\n",
|
||
"m_xx =json.loads(excel_1)\n",
|
||
"\n",
|
||
"with open('识别结果12.json','w') as fl:\n",
|
||
" json.dump(m_xx['forms'][0],fl,ensure_ascii=False)\n",
|
||
"print(info_1)\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": []
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "Python 3",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.8.10"
|
||
},
|
||
"toc-autonumbering": true,
|
||
"toc-showmarkdowntxt": false,
|
||
"toc-showtags": false
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 4
|
||
}
|