RPA中房產證的 OCR 識別

  客戶需求,識別一些證件內容,包括身份證、戶口本、營業執照、銀行卡以及房產證,前四個比較容易實現,無論是藝賽旗的 RPA 仍是百度的 OCR 都有接口,直接調用便可,可是都沒有房產證的 OCR 識別,只能本身使用其餘 OCR 接口來進行相關操做了。
  房產證以下圖所示:
  json

 

 


  若是使用通用文字識別全部的文字均可以識別出來,可是順序是亂的,因此我選擇了使用通用文字識別(高精度含位置版),這樣的話,我不只能夠識別到文字,還能知道文字所在的位置,而後根據文字位置進行區域劃分,劃分後的效果以下:
app

 

  


  代碼以下:
  import base64
  import copy
  import re
  import requests
  class OCR(object):
  # client_id 爲官網獲取的AK, client_secret 爲官網獲取的SK
  client_id = ""
  client_secret = ""
  def get_token(self):
  """獲取 access_token"""
  host = 'https://aip.baidubce.com/oauth/2.0/token'
  # 請求頭
  headers = {'Content-Type': 'application/json; charset=UTF-8'}
  # 請求參數
  params = {
  "grant_type": "client_credentials",
  "client_id": self.client_id,
  "client_secret": self.client_secret
  }
  # get 請求
  response = requests.get(host, headers=headers, params=params)
  # 獲取 json 內容
  content = response.json()
  # 獲取 access_token
  access_token = content["access_token"]
  return access_token
  def encode_img(self, img_path):
  """對圖片進行編碼"""
  with open(img_path, "rb") as f:
  img_content = f.read()
  # 對圖片進行 base64 編碼
  img_content = base64.b64encode(img_content)
  return img_content
  def img_to_str(self, img_path):
  """對圖片文字進行識別"""
  access_token = self.get_token()
  # 請求 URL
  URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate?access_token=" + access_token
  # post 請求頭
  headers = {"Content-Type": "application/x-www-form-urlencoded"}
  # post 參數
  data = {
  "image": self.encode_img(img_path),
  "recognize_granularity": "big"
  }
  # post 請求
  response = requests.post(URL, headers=headers, data=data)
  return response.json()
  if __name__ == '__main__':
  ocr = OCR()
  img = "./imgs/fcz_01.jpg"
  str_json = ocr.img_to_str(img)
  my_str_list = str_json["words_result"]
  my_title = ["證實權利或事項", "權利人(申請人)", "義務人", "坐落", "不動產單元號", "其餘", "附記"]
  column_line = 999999999
  my_word_list = []
  for data in my_str_list:
  my_list = []
  words = data["words"]
  width = data["location"]["width"]
  top = data["location"]["top"]
  left = data["location"]["left"]
  height = data["location"]["height"]
  my_list.append(words)
  my_list.append(width)
  my_list.append(top)
  my_list.append(left)
  my_list.append(height)
  my_word_list.append(my_list)
  if "不動產證實" in words:
  top_01 = top
  height_01 = height
  if "證實權利或事項" in words:
  top_02 = top
  if words in my_title:
  column_line = left + width if left + width < column_line else column_line
  row_line = (top_01 + top_02 + height) / 2
  head_list = []
  left_list = []
  right_list = []
  for data in my_word_list:
  if data[0] in my_title:
  continue
  if data[2] < row_line:
  head_list.append(data)
  elif data[3] < column_line:
  for i in my_title:
  data[0] = re.sub(i, "", data[0])
  right_list.append(data)
  else:
  right_list.append(data)
  # head 處理
  head_dict = {}
  left = 0
  for data in head_list:
  head_dict[data[3]] = data[0]
  # 排序
  head_dict = sorted(head_dict.items(), key=lambda x: x[0])
  head_str = ""
  for data in head_dict:
  head_str += data[1]
  print(head_str)
  # right 處理
  right_dict = {}
  other_list = copy.deepcopy(right_list)
  for i in range(len(my_title) - 2):
  right_dict[my_title[i]] = right_list[i][0]
  other_list.remove(right_list[i])
  right_dict[my_title[-1]] = right_list[-1][0]
  other_list.remove(right_list[-1])
  # 其餘處理
  other_str = ""
  for data in other_list:
  if ":" in data[0]:
  other_str += ";"
  other_str += data[0]
  right_dict[my_title[-2]] = other_str[1:]
  print(right_dict)
  運行效果以下:
  冀(2019)**市不動產證實第00***19號
  {'證實權利或事項': '抵押權', '權利人(申請人)': '中國**************分行', '義務人': '***', '坐落': '路南區*************號', '不動產單元號': '130202*************0118', '附記': '業務編號:20190**20', '其餘': '產權證書號:冀(2019)**市不動產權第00****2號;抵押物類型:土地和房屋;抵押方式:通常抵押;擔保債權數額:60.00萬元;債權起止時間:2019年10月24日起2049年10月24日止'}post

相關文章
相關標籤/搜索