12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- #-*-coding:utf-8 -*-
- import json,re
- from aip import AipOcr
- APP_ID = '11225532'
- API_KEY = 'iGhZP96p570OG62oHPRExG09'
- SECRET_KEY = 'XGTCHsTf7sWkVmBNKNoGIcWfGXrGHYHI'
- client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
- def get_file_content(filePath):
- with open(filePath, 'rb') as fp:
- return fp.read()
- def rec_basicgeneral(imgpath):
- """
- 通用文字识别
- """
- words = []
- image = get_file_content(imgpath)
- #res_image = client.basicGeneral(image)
- res_image = client.general(image)
- for item in res_image["words_result"]:
- print(item)
- if item["words"]:
- words.append(item)
- return words
- def rec_general_num(imgpath):
- """
- 识别出数字(题号)
- """
- nums = []
- nums_dct = {}
- choices = set()
- words = rec_basicgeneral(imgpath)
- for word in words:
- location = word["location"]
- word = word["words"]
- res = re.match(r"^(\d+).*",word)
- if res:
- nums.append(int(res.group(1)))
- nums_dct[int(res.group(1))] = location
- res = re.findall(r"(A|B|C|D|E|F|G)",word)
- if res:
- choices.update(set(res))
- print(nums)
- if (1 in nums) and (2 in nums) and (nums.index(2)-nums.index(1) > 1):
- rank_order = 1
- else:
- rank_order = 2
- nums.sort()
- return nums,nums_dct,list(choices),rank_order
- if __name__ == "__main__":
- imgpath = "/tmp/src4.png"
- imgpath = "/tmp/test_0_crop.png"
- nums,nums_dct,choices,rank_order = rec_general_num(imgpath)
- print(nums,nums_dct,choices,rank_order,222222222222222222)
|