#QQ:@qq.com
#本截圖適合安康碼截圖,如需其他地區(qū)截圖統(tǒng)計,可與我QQ或QQ郵箱聯(lián)系
#1、在當(dāng)前文件夾下創(chuàng)建imgs文件夾用于存放圖片,圖片格式.jpg
#2、在當(dāng)前文件夾下創(chuàng)建“shuju.xlsx”的Excel用于存放統(tǒng)計結(jié)果
文件夾目錄樣式
黃浦網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,黃浦網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為黃浦上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的黃浦做網(wǎng)站的公司定做!
統(tǒng)計結(jié)果Excel樣式
具體代碼如下:
# @Time : 2022/4/19 22:00 # @Author : CFang # @File : hesuan_results.py # @Software: PyCharm #QQ:@qq.com #本截圖適合安康碼截圖,如需其他地區(qū)截圖統(tǒng)計,可與我QQ或QQ郵箱聯(lián)系 #1、在當(dāng)前文件夾下創(chuàng)建imgs文件夾用于存放圖片,圖片格式.jpg #2、在當(dāng)前文件夾下創(chuàng)建“shuju.xlsx”的Excel用于存放統(tǒng)計結(jié)果 #獲得截圖結(jié)果 def get_hesuan_res(path): #獲得API的access_token import requests AK= '*******'#輸入自己的百度智能云的AK和SK SK = '*******' # client_id 為官網(wǎng)獲取的AK, client_secret 為官網(wǎng)獲取的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 #文字識別接口,可自己調(diào)整不同接口獲得不同精度要求 import requests import base64 ''' 通用文字識別 ''' request_url= "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic" # 二進(jìn)制方式打開圖片文件 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]) # 對圖片識別結(jié)果的數(shù)據(jù)清洗 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內(nèi)的所有圖片 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) #保存識別清洗后的數(shù)據(jù)結(jié)果到“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] == "檢測機構(gòu)" 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')