#QQ:[email protected]#本截圖適合安康碼截圖,如需其他地區截圖統計,可與我QQ或QQ郵箱聯繫#1、在當前文件夾下創建imgs文件夾用於存放圖片,圖片格式.jpg#2、在當前文件夾下創建“shuju.xlsx”的Excel用於存放統計結果文件夾目錄樣式 統計結果Excel樣式 具體代 ...
#QQ:[email protected]
#本截圖適合安康碼截圖,如需其他地區截圖統計,可與我QQ或QQ郵箱聯繫
#1、在當前文件夾下創建imgs文件夾用於存放圖片,圖片格式.jpg
#2、在當前文件夾下創建“shuju.xlsx”的Excel用於存放統計結果
文件夾目錄樣式
![](https://img2022.cnblogs.com/blog/2013311/202204/2013311-20220419221723362-1192458265.png)
統計結果Excel樣式
![](https://img2022.cnblogs.com/blog/2013311/202204/2013311-20220419221543643-1306039615.png)
具體代碼如下:
# @Time : 2022/4/19 22:00 # @Author : CFang # @File : hesuan_results.py # @Software: PyCharm #QQ:[email protected] #本截圖適合安康碼截圖,如需其他地區截圖統計,可與我QQ或QQ郵箱聯繫 #1、在當前文件夾下創建imgs文件夾用於存放圖片,圖片格式.jpg #2、在當前文件夾下創建“shuju.xlsx”的Excel用於存放統計結果 #獲得截圖結果 def get_hesuan_res(path): #獲得API的access_token import requests AK = '*******'#輸入自己的百度智能雲的AK和SK SK = '*******' # client_id 為官網獲取的AK, client_secret 為官網獲取的SK host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+AK+'&client_secret='+SK response = requests.get(host) if response: print(response.json()) print(response.json()['access_token']) # encoding:utf-8 #文字識別介面,可自己調整不同介面獲得不同精度要求 import requests import base64 ''' 通用文字識別 ''' request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic" # 二進位方式打開圖片文件 f = open(path, 'rb') img = base64.b64encode(f.read()) params = {"image":img} access_token = response.json()['access_token'] request_url = request_url + "?access_token=" + access_token headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(request_url, data=params, headers=headers) if response: # print (response.json()) # print(response.json()['words_result']) all_res = response.json()['words_result'] return all_res # for i in range(len(all_res)): # print(i,all_res[i]) # 對圖片識別結果的數據清洗 all_lists_deals = [] def deal_datas(all_lists): all_lists_deal = [] if all_lists[5]['words'].split(":")[0] == "姓名": for i in range(5, len(all_lists)): print(i, all_lists[i]['words']) # ,all_lists_display[i]['words'] if all_lists[i]['words'] != '>' and all_lists[i]['words'] != '身份證件號碼:': all_lists_deal.append(all_lists[i]['words']) all_lists_deal[0] = all_lists_deal[0].split(":")[1][:-1] # print(all_lists_deal) else: for i in range(6, len(all_lists)): print(i, all_lists[i]['words']) # ,all_lists_display[i]['words'] if all_lists[i]['words'] != '>': all_lists_deal.append(all_lists[i]['words']) all_lists_deal[0] = all_lists_deal[0].split(":")[1] all_lists_deal[1] = all_lists_deal[1].split(":")[1] # print(all_lists_deal) print(all_lists_deal) all_lists_deals.append(all_lists_deal) #獲取文件夾imgs內的所有圖片 import os def get_imlist(path): return [os.path.join(path,f) for f in os.listdir(path) if f.endswith('.jpg')] img_path = get_imlist("imgs") print(img_path) for path in img_path: all_lists = get_hesuan_res(path) deal_datas(all_lists) #保存識別清洗後的數據結果到“shuju.xlsx”表中 # -*- coding: UTF-8 -*- from openpyxl import load_workbook wb = load_workbook('shuju.xlsx') ws = wb['Sheet1'] row = ws.max_row+1 for j in range(len(all_lists_deals)): for i in range(len(all_lists_deals[j])): if len(all_lists_deals[j][i].split(":")) == 1: ws.cell(row+j,i+1).value = all_lists_deals[j][i] elif all_lists_deals[j][i].split(":")[0] == "檢測機構" or all_lists_deals[j][i].split(":")[0] == "身份證件號碼": ws.cell(row+j, i + 1).value = all_lists_deals[j][i].split(":")[1] else: ws.cell(row+j, i + 1).value = all_lists_deals[j][i].split(":")[1][:10] wb.save('shuju.xlsx')