sheet_points_total.py 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2020/6/15 0015 10:10
  3. # @Author : LF
  4. # @FileName: sheet_point_total.py
  5. # @Software: PyCharm
  6. import requests
  7. import base64
  8. from urllib import parse, request
  9. import cv2
  10. import re
  11. from threading import Thread
  12. import copy
  13. from collections import OrderedDict
  14. from PIL import Image
  15. from segment.sheet_resolve.tools.brain_api import get_ocr_text_and_coordinate_in_google_format
  16. from segment.sheet_resolve.analysis.sheet.ocr_key_words import key_words
  17. try:
  18. import tr.tr as tr
  19. except Exception:
  20. pass
  21. OCR_ACCURACY = 'accurate'
  22. # def ocr_login():
  23. # grant_type = 'client_credentials'
  24. # client_id = OCR_CLIENT_ID
  25. # client_secret = OCR_CLIENT_SECRET
  26. #
  27. # textmod = {'grant_type': grant_type, 'client_id': client_id, 'client_secret': client_secret}
  28. # textmod = parse.urlencode(textmod)
  29. #
  30. # # 输出内容:user=admin&password=admin
  31. # header_dict = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko'}
  32. # url = 'https://aip.baidubce.com/oauth/2.0/token'
  33. # req = request.Request(url='{}{}{}'.format(url, '?', textmod), headers=header_dict)
  34. # res = request.urlopen(req).read()
  35. # token = eval(res.decode(encoding='utf-8'))['access_token']
  36. # return token
  37. # def opecv2base64(img):
  38. # image = cv2.imencode('.jpg', img)[1]
  39. # base64_data = str(base64.b64encode(image))[2:-1]
  40. # return base64_data
  41. # def get_ocr_raw_result(img, ocr_accuracy=OCR_ACCURACY, language_type='CHN_ENG'):
  42. # textmod = {'access_token': access_token}
  43. # textmod = parse.urlencode(textmod)
  44. # url = '{}{}{}{}'.format(OCR_BOX_URL, ocr_accuracy, '?', textmod)
  45. # url_general = '{}{}{}{}'.format(OCR_BOX_URL, 'general', '?', textmod)
  46. #
  47. # headers = {'Content-Type': 'application/x-www-form-urlencoded'}
  48. #
  49. # image_type = 'base64'
  50. # group_id = 'group001'
  51. # user_id = 'usr001'
  52. #
  53. # image = opecv2base64(img)
  54. #
  55. # data = {
  56. # 'image_type': image_type,
  57. # 'group_id': group_id,
  58. # 'user_id': user_id,
  59. # 'image': image,
  60. # 'detect_direction': 'true',
  61. # 'recognize_granularity': 'small',
  62. # 'language_type': language_type,
  63. # # 'vertexes_location': 'true',
  64. # # 'probability': 'true'
  65. # }
  66. #
  67. # resp = requests.post(url, data=data, headers=headers).json()
  68. # if resp.get('error_msg'):
  69. # if 'internal error' in resp.get('error_msg'):
  70. # resp = requests.post(url_general, data=data, headers=headers).json()
  71. # if resp.get('error_msg'):
  72. # raise Exception("ocr {}!".format(resp.get('error_msg')))
  73. # else:
  74. # raise Exception("ocr {}!".format(resp.get('error_msg')))
  75. #
  76. # return resp
  77. # def get_ocr_text_and_coordinate(img, ocr_accuracy=OCR_ACCURACY, language_type='CHN_ENG'):
  78. # textmod = {'access_token': access_token}
  79. # textmod = parse.urlencode(textmod)
  80. # url = '{}{}{}{}'.format(OCR_BOX_URL, ocr_accuracy, '?', textmod)
  81. # url_general = '{}{}{}{}'.format(OCR_BOX_URL, 'general', '?', textmod)
  82. #
  83. # headers = {'Content-Type': 'application/x-www-form-urlencoded'}
  84. #
  85. # image_type = 'base64'
  86. # group_id = 'group001'
  87. # user_id = 'usr001'
  88. #
  89. # image = opecv2base64(img)
  90. #
  91. # data = {
  92. # 'image_type': image_type,
  93. # 'group_id': group_id,
  94. # 'user_id': user_id,
  95. # 'image': image,
  96. # 'detect_direction': 'true',
  97. # 'recognize_granularity': 'small',
  98. # 'language_type': language_type,
  99. # # 'vertexes_location': 'true',
  100. # # 'probability': 'true'
  101. # }
  102. #
  103. # resp = requests.post(url, data=data, headers=headers).json()
  104. # if resp.get('error_msg'):
  105. # if 'internal error' in resp.get('error_msg'):
  106. # resp = requests.post(url_general, data=data, headers=headers).json()
  107. # if resp.get('error_msg'):
  108. # raise Exception("ocr {}!".format(resp.get('error_msg')))
  109. # else:
  110. # raise Exception("ocr {}!".format(resp.get('error_msg')))
  111. #
  112. # words_result = resp.get('words_result')
  113. # return words_result
  114. # def get_ocr_text_and_coordinate_in_google_format(img, ocr_accuracy=OCR_ACCURACY, language_type='CHN_ENG'):
  115. # textmod = {'access_token': access_token}
  116. # textmod = parse.urlencode(textmod)
  117. # url = '{}{}{}{}'.format(OCR_BOX_URL, ocr_accuracy, '?', textmod)
  118. # url_general = '{}{}{}{}'.format(OCR_BOX_URL, 'general', '?', textmod)
  119. #
  120. # headers = {'Content-Type': 'application/x-www-form-urlencoded'}
  121. #
  122. # image_type = 'base64'
  123. # group_id = 'group001'
  124. # user_id = 'usr001'
  125. #
  126. # image = opecv2base64(img)
  127. #
  128. # data = {
  129. # 'image_type': image_type,
  130. # 'group_id': group_id,
  131. # 'user_id': user_id,
  132. # 'image': image,
  133. # 'detect_direction': 'true',
  134. # 'recognize_granularity': 'small',
  135. # 'language_type': language_type,
  136. # # 'vertexes_location': 'true',
  137. # # 'probability': 'true'
  138. # }
  139. #
  140. # resp = requests.post(url, data=data, headers=headers).json()
  141. # if resp.get('error_msg'):
  142. # if 'internal error' in resp.get('error_msg'):
  143. # resp = requests.post(url_general, data=data, headers=headers).json()
  144. # if resp.get('error_msg'):
  145. # raise Exception("ocr {}!".format(resp.get('error_msg')))
  146. # else:
  147. # raise Exception("ocr {}!".format(resp.get('error_msg')))
  148. #
  149. # words_result = resp.get('words_result')
  150. # dict_list = [item2.get('location') for item in words_result for item2 in item['chars']]
  151. # char_list = [item2.get('char') for item in words_result for item2 in item['chars']]
  152. # words = [item.get('words') for item in words_result]
  153. # matrix = []
  154. # for adict in dict_list:
  155. # xmin = adict['left']
  156. # ymin = adict['top']
  157. # xmax = adict['width'] + adict['left']
  158. # ymax = adict['top'] + adict['height']
  159. # item0 = (xmin, ymin, xmax, ymax)
  160. # matrix.append(item0)
  161. #
  162. # res_dict = {'chars': char_list, 'coordinates': matrix, 'words': words}
  163. # return res_dict
  164. def model_type_score(all_type_score_one, choice_box, cloze_box, solve_box,composition_box): # 与模型得到边框与type_score对应
  165. '''
  166. :param all_type_score_one: 模型得到的单个type_score的坐标位置
  167. :param choice_box: 模型得到的选择题坐标位置
  168. :param cloze_box: 模型得到的填空题坐标位置
  169. :param solve_box: 模型得到的解答题坐标位置
  170. :return:
  171. '''
  172. N_choice = len(choice_box)
  173. N_cloze = len(cloze_box)
  174. N_solve_box = len(solve_box)
  175. N_composition = len(composition_box)
  176. min_choice_dict = {}
  177. min_cloze_dict = {}
  178. min_solve_dict = {}
  179. min_composition_dict = {}
  180. test_result1 = 0
  181. min_choice = 100000000
  182. min_cloze = 100000000
  183. min_solve = 100000000
  184. min_composition = 100000000
  185. for j in range(N_choice):
  186. choice_distance_x = abs(list(all_type_score_one)[0] - choice_box[j][0])
  187. choice_distance_y = abs(list(all_type_score_one)[1] - choice_box[j][1])
  188. choice_distance = choice_distance_x + choice_distance_y
  189. if choice_distance < min_choice:
  190. min_choice = choice_distance
  191. min_choice_dict = {'min_choice': min_choice,
  192. 'bounding_box': choice_box[j],
  193. 'label': 'choice',
  194. 'type_box': all_type_score_one}
  195. for k in range(N_cloze):
  196. cloze_distance_x = abs(list(all_type_score_one)[0] - cloze_box[k][0])
  197. cloze_distance_y = abs(list(all_type_score_one)[1] - cloze_box[k][1])
  198. cloze_distance = cloze_distance_x + cloze_distance_y
  199. if cloze_distance < min_cloze:
  200. min_cloze = cloze_distance
  201. min_cloze_dict = {'min_cloze': min_cloze,
  202. 'bounding_box': cloze_box[k],
  203. 'label': 'cloze',
  204. 'type_box': all_type_score_one}
  205. for l in range(N_solve_box):
  206. solve_distance_x = abs(list(all_type_score_one)[0] - solve_box[l][0])
  207. solve_distance_y = abs(list(all_type_score_one)[1] - solve_box[l][1])
  208. solve_distance = solve_distance_x + solve_distance_y
  209. if solve_distance < min_solve:
  210. min_solve = solve_distance
  211. min_solve_dict = {'min_solve': min_solve,
  212. 'bounding_box': solve_box[l],
  213. 'label': 'solve',
  214. 'type_box': all_type_score_one}
  215. for m in range(N_composition):
  216. composition_distance_x = abs(list(all_type_score_one)[0] - composition_box[m][0])
  217. composition_distance_y = abs(list(all_type_score_one)[1] - composition_box[m][1])
  218. composition_distance = composition_distance_x + composition_distance_y
  219. if composition_distance < min_composition:
  220. min_composition = composition_distance
  221. min_composition_dict = {'min_cloze': min_composition,
  222. 'bounding_box': composition_box[m],
  223. 'label': 'composition',
  224. 'type_box': all_type_score_one}
  225. if min_choice < min_cloze and min_choice < min_solve and min_choice < min_composition:
  226. # 建立相互关联的关系。 即表示该type_score对应于选择题
  227. if min_choice < 600:
  228. test_result1 = min_choice_dict
  229. else:
  230. test_result1 = -1
  231. elif min_cloze < min_choice and min_cloze < min_solve and min_cloze < min_composition:
  232. # 建立相互关联的关系。 即表示该type_score对应于填空题
  233. if min_cloze < 600:
  234. test_result1 = min_cloze_dict
  235. else:
  236. test_result1 = -1
  237. elif min_solve < min_cloze and min_solve < min_choice and min_solve < min_composition:
  238. # 建立相互关联的关系。 即表示该type_score对应于解答题
  239. if min_solve < 400:
  240. test_result1 = min_solve_dict
  241. else:
  242. test_result1 = -1
  243. elif min_composition < min_cloze and min_composition < min_choice and min_composition < min_solve:
  244. # 建立相互关联的关系。 即表示该type_score对应于解答题
  245. if min_composition < 400:
  246. test_result1 = min_composition_dict
  247. else:
  248. test_result1 = -1
  249. return test_result1
  250. def module_type_score(all_type_score_one, choice_box, cloze_box, solve_box, composition_box): # 每个模块内包含的type_score
  251. '''
  252. :param all_type_score_one: 模型得到的单个type_score的坐标位置
  253. :param choice_box: 模型得到的选择题坐标位置
  254. :param cloze_box: 模型得到的填空题坐标位置
  255. :param solve_box: 模型得到的解答题坐标位置
  256. :return:
  257. '''
  258. N_choice = len(choice_box)
  259. N_cloze = len(cloze_box)
  260. N_solve = len(solve_box)
  261. N_composition = len(composition_box)
  262. choice_type_score = {}
  263. cloze_type_score = {}
  264. solve_type_score = {}
  265. composition_type_score = {}
  266. test_result1 = {}
  267. temp_dis = 100000
  268. for j in range(N_choice):
  269. if (list(all_type_score_one)[0] and list(all_type_score_one)[2]) in range(choice_box[j][0] - 100, choice_box[j][2] + 50) and (list(all_type_score_one)[1] and list(all_type_score_one)[3]) in range(choice_box[j][1] - 150, choice_box[j][3] - 50):
  270. choice_type_score = {'bounding_box': choice_box[j],
  271. 'label': 'choice',
  272. 'type_box': all_type_score_one}
  273. break
  274. for j in range(N_cloze):
  275. if (list(all_type_score_one)[0] and list(all_type_score_one)[2]) in range(cloze_box[j][0] - 100, cloze_box[j][2] + 50) and (list(all_type_score_one)[1] and list(all_type_score_one)[3]) in range(cloze_box[j][1] - 100, cloze_box[j][3] - 50):
  276. cloze_type_score = {'bounding_box': cloze_box[j],
  277. 'label': 'cloze',
  278. 'type_box': all_type_score_one}
  279. break
  280. for j in range(N_solve):
  281. if (list(all_type_score_one)[0] and list(all_type_score_one)[2]) in range(solve_box[j][0] - 50, solve_box[j][2] + 50) and (list(all_type_score_one)[1] and list(all_type_score_one)[3]) in range(solve_box[j][1]-50, solve_box[j][3]):
  282. solve_type_score = {'bounding_box': solve_box[j],
  283. 'label': 'solve',
  284. 'type_box': all_type_score_one}
  285. break
  286. for j in range(N_composition):
  287. if (list(all_type_score_one)[0] and list(all_type_score_one)[2]) in range(composition_box[j][0] - 100, composition_box[j][2] + 50) and (list(all_type_score_one)[1] and list(all_type_score_one)[3]) in range(composition_box[j][1] - 200, composition_box[j][3] - 50):
  288. composition_type_score = {'bounding_box': composition_box[j],
  289. 'label': 'composition',
  290. 'type_box': all_type_score_one}
  291. break
  292. if choice_type_score != {}:
  293. # 建立相互关联的关系。 即表示该type_score对应于选择题
  294. test_result1 = choice_type_score
  295. elif cloze_type_score != {}:
  296. # 建立相互关联的关系。 即表示该type_score对应于填空题
  297. test_result1 = cloze_type_score
  298. elif solve_type_score != {}:
  299. # 建立相互关联的关系。 即表示该type_score对应于解答题
  300. test_result1 = solve_type_score
  301. elif composition_type_score != {}:
  302. test_result1 = composition_type_score
  303. else:
  304. test_result1 = -1
  305. return test_result1
  306. def ocr_key_words(rect, type_score_dict): # 将ocr识别得到的文字与模型得到的type_score对应
  307. '''
  308. :param rect: OCR识别结果数组,格式:res = {'chars': [},'coordinates': [(),()},'words': []}
  309. :param type_score_dict: 模型得到的type_score(与模型得到的边框相对应)
  310. :return: 字典中添加word
  311. '''
  312. len_ocr = len(rect['chars'])
  313. xmin = type_score_dict['type_box'][0]
  314. ymin = type_score_dict['type_box'][1]
  315. xmax = type_score_dict['type_box'][2]
  316. ymax = type_score_dict['type_box'][3]
  317. words = []
  318. for j in range(len_ocr):
  319. if rect['coordinates'][j][0] - xmin > -30 and rect['coordinates'][j][1] - ymin > -30 and rect['coordinates'][j][2] - xmax < 30 and rect['coordinates'][j][3] - ymax < 30:
  320. word = rect['chars'][j]
  321. words.append(word)
  322. type_score_dict['words'] = words
  323. type_score_dict_ocr = type_score_dict
  324. return type_score_dict_ocr
  325. def big_block_score(img0,xmins_b,ymins_b,xmaxs_b,ymaxs_b):
  326. res1 = get_ocr_text_and_coordinate_in_google_format(img0[ymins_b:ymaxs_b, xmins_b:xmaxs_b],ocr_accuracy=OCR_ACCURACY, language_type='CHN_ENG')
  327. aa = []
  328. type_score_dict_ocrs = {}
  329. for ii in range(len(res1['coordinates'])):
  330. xmin11 = res1['coordinates'][ii][0] + xmins_b
  331. ymin11 = res1['coordinates'][ii][1] + ymins_b
  332. xmax11 = res1['coordinates'][ii][2] + xmins_b
  333. ymax11 = res1['coordinates'][ii][3] + ymins_b
  334. aaa = (xmin11, ymin11, xmax11, ymax11)
  335. aa.append(aaa)
  336. res1['coordinates'] = aa
  337. new_test = {}
  338. coordinates = 0
  339. if len(res1['words']) > 0:
  340. type_score_dict_ocrs['words'] = res1['words'][0]
  341. coordinates = res1['coordinates'][0]
  342. new_test = key_words(type_score_dict_ocrs)
  343. if new_test == {} or new_test['Score_structure'] == -1:
  344. if len(res1['words']) > 1:
  345. type_score_dict_ocrs['words'] = res1['words'][1]
  346. coordinates = res1['coordinates'][1]
  347. new_test = key_words(type_score_dict_ocrs)
  348. if new_test == {} or new_test['Score_structure'] == -1:
  349. if len(res1['words']) > 2:
  350. type_score_dict_ocrs['words'] = res1['words'][2]
  351. coordinates = res1['coordinates'][2]
  352. new_test = key_words(type_score_dict_ocrs)
  353. if new_test == {} or new_test['Score_structure'] == -1:
  354. if len(res1['words']) > 3:
  355. type_score_dict_ocrs['words'] = res1['words'][3]
  356. coordinates = res1['coordinates'][3]
  357. new_test = key_words(type_score_dict_ocrs)
  358. if new_test == {} or new_test['Score_structure'] == -1:
  359. if len(res1['words']) > 4:
  360. type_score_dict_ocrs['words'] = res1['words'][4]
  361. coordinates = res1['coordinates'][4]
  362. new_test = key_words(type_score_dict_ocrs)
  363. if new_test != {} and new_test['volume_structure'] != -1 and new_test['volume_structure'] != 1: # 如果识别到分数,添加到输出信息;如果还没有识别到分数,默认没有分数
  364. if int(new_test['volume_structure'][0]['volume_total_score']) > 200: # 暂定试卷分数都在200以内,超过200的表示识别错误
  365. new_test['volume_structure'][0]['volume_total_score'] = int(
  366. new_test['volume_structure'][0]['volume_total_score']) % 100
  367. return new_test
  368. elif new_test != {} and (new_test['volume_structure'] == -1 or new_test['volume_structure'] == 1) and new_test['Score_structure'] != -1: # 如果识别到分数,添加到输出信息;如果还没有识别到分数,默认没有分数
  369. if int(new_test['Score_structure'][0]['item_total_score']) > 200: # 暂定试卷分数都在200以内,超过200的表示识别错误
  370. new_test['Score_structure'][0]['item_total_score'] = int(
  371. new_test['Score_structure'][0]['item_total_score']) % 100
  372. new_test['Score_structure'][0]['type_box'] = coordinates
  373. return new_test
  374. else:
  375. return -1
  376. def get_sheet_number_total(answer_sheet, res, img0):
  377. # img0 = cv2.imread(img_path0)
  378. image_src = Image.fromarray(cv2.cvtColor(img0, cv2.COLOR_BGR2RGB))
  379. img_w, img_h = image_src.size
  380. if (image_src.mode == 'RGB'):
  381. image_src = image_src.convert("L")
  382. choice_boxs = []
  383. choice_m_boxs = []
  384. cloze_boxs = []
  385. solve_boxs = []
  386. type_score_boxs = []
  387. select_boxs = []
  388. all_test = []
  389. num_choice = 0
  390. num_choice_m = 0
  391. num_cloze = 0
  392. num_slove = 0
  393. num_select = 0
  394. num_type_score = 0
  395. volume_last = []
  396. Score_last = []
  397. score_last_one = 0
  398. volume_last_one = 0
  399. composition_boxs = []
  400. num_composition = 0
  401. j_temp = []
  402. jj_temp =[]
  403. eles = []
  404. yy_max = []
  405. score_del = []
  406. key_modules_classes = ['choice', 'cloze', 'solve', 'solve0', 'composition0', 'composition', 'correction', 'type_score']
  407. for ele in answer_sheet["regions"]: # 从模型输出获取对应标签的边框信息
  408. if ele["class_name"] == 'choice':
  409. choice_box = ele['bounding_box']
  410. choice_one = (
  411. int(choice_box['xmin']), int(choice_box['ymin']), int(choice_box['xmax']), int(choice_box['ymax']))
  412. choice_boxs.append(choice_one)
  413. num_choice = num_choice + 1
  414. elif ele["class_name"] == 'choice_m':
  415. choice_m_boxs.append(ele['bounding_box'])
  416. num_choice_m = num_choice_m + 1
  417. elif ele["class_name"] == 'cloze':
  418. cloze_box = ele['bounding_box']
  419. cloze_one = (int(cloze_box['xmin']), int(cloze_box['ymin']), int(cloze_box['xmax']), int(cloze_box['ymax']))
  420. cloze_boxs.append(cloze_one)
  421. num_cloze = num_cloze + 1
  422. elif ele["class_name"] == 'composition0' or ele["class_name"] == 'composition' or ele[
  423. "class_name"] == 'correction':
  424. composition_box = ele['bounding_box']
  425. composition_one = (int(composition_box['xmin']), int(composition_box['ymin']), int(composition_box['xmax']),
  426. int(composition_box['ymax']))
  427. composition_boxs.append(composition_one)
  428. num_composition = num_composition + 1
  429. elif ele["class_name"] == 'solve' or ele["class_name"] == 'solve0':
  430. solve_box = ele['bounding_box']
  431. solve_one = (int(solve_box['xmin']), int(solve_box['ymin']), int(solve_box['xmax']), int(solve_box['ymax']))
  432. solve_boxs.append(solve_one)
  433. num_slove = num_slove + 1
  434. elif ele["class_name"] == 'select_s' or ele["class_name"] == 'select_b':
  435. select_box = ele['bounding_box']
  436. select_one = (
  437. int(select_box['xmin']), int(select_box['ymin']), int(select_box['xmax']), int(select_box['ymax']))
  438. select_boxs.append(select_one)
  439. num_select = num_select + 1
  440. elif ele["class_name"] == 'type_score':
  441. type_score_box = ele['bounding_box']
  442. type_score_one = (int(type_score_box['xmin']), int(type_score_box['ymin']), int(type_score_box['xmax']),
  443. int(type_score_box['ymax']))
  444. type_score_boxs.append(type_score_one)
  445. num_type_score = num_type_score + 1
  446. '''解析type_score与对应分割模块的分数'''
  447. for i in range(len(type_score_boxs)):
  448. test_result1 = module_type_score(type_score_boxs[i], choice_boxs, cloze_boxs, solve_boxs, composition_boxs)
  449. if test_result1 != -1 and test_result1 != 0:
  450. xminss = (type_score_boxs[i][0] - 5) if type_score_boxs[i][0] - 5 > 0 else type_score_boxs[i][0]
  451. yminss = (type_score_boxs[i][1] - 5) if type_score_boxs[i][1] - 5 > 0 else type_score_boxs[i][1]
  452. if type_score_boxs[i][2] + 5 < img_w:
  453. xmaxss = type_score_boxs[i][2] + 5
  454. else:
  455. xmaxss = type_score_boxs[i][2]
  456. if type_score_boxs[i][3] + 5 < img_h:
  457. ymaxss = type_score_boxs[i][3] + 5
  458. else:
  459. ymaxss = type_score_boxs[i][3]
  460. test_result1['words'] = str()
  461. try: # tr_OCR
  462. image_src_type_score = image_src.crop((xminss, yminss, xmaxss, ymaxss))
  463. w_small = xmaxss - xminss
  464. h_small = ymaxss - yminss
  465. if h_small < 100 and w_small > 100:
  466. image_src_type_score = Image.new(image_src.mode, (w_small, 100), (255))
  467. image_src_type_score.paste(image_src, [0, 0, w_small, h_small])
  468. elif w_small < 100 and h_small > 100:
  469. image_src_type_score = Image.new(image_src.mode, (100, h_small), (255))
  470. image_src_type_score.paste(image_src, [0, 0, w_small, h_small])
  471. elif w_small < 100 and h_small < 100:
  472. image_src_type_score = Image.new(image_src.mode, (100, 100), (255))
  473. image_src_type_score.paste(image_src, [0, 0, w_small, h_small])
  474. type_score_dict_ocr = tr.run(image_src_type_score)
  475. print('tr_OCR')
  476. for t in range(len(type_score_dict_ocr)):
  477. test_result1['words'] = test_result1['words'] + type_score_dict_ocr[t][1]
  478. except Exception as e: # baidu_OCR
  479. print('baidu_OCR')
  480. type_score_dict_ocr = get_ocr_text_and_coordinate_in_google_format(img0[yminss:ymaxss, xminss:xmaxss], ocr_accuracy=OCR_ACCURACY,language_type='CHN_ENG')
  481. for t in range(len(type_score_dict_ocr['words'])):
  482. test_result1['words'] = test_result1['words'] + type_score_dict_ocr['words'][t]
  483. test = key_words(test_result1)
  484. if test == {}:
  485. ### 添加返回值OCR结果
  486. add_ocr = {}
  487. add_ocr['model_box'] = test_result1['bounding_box']
  488. add_ocr['label'] = test_result1['label']
  489. add_ocr['number'] = -1
  490. add_ocr['score'] = -1
  491. add_ocr['number_score'] = -1
  492. add_ocr['counts'] = -1
  493. add_ocr['type_score_box'] = type_score_boxs[i]
  494. add_ocr['ocr'] = test_result1['words']
  495. Score_last.append(add_ocr)
  496. elif test['volume_structure'] == -1 and test['Score_structure'] == -1:
  497. ### 添加返回值OCR结果
  498. add_ocr = {}
  499. add_ocr['model_box'] = test_result1['bounding_box']
  500. add_ocr['label'] = test_result1['label']
  501. add_ocr['number'] = -1
  502. add_ocr['score'] = -1
  503. add_ocr['number_score'] = -1
  504. add_ocr['counts'] = -1
  505. add_ocr['type_score_box'] = type_score_boxs[i]
  506. add_ocr['ocr'] = test_result1['words']
  507. Score_last.append(add_ocr)
  508. elif test != {}:
  509. if test['volume_structure'] != -1 and test['volume_structure'] != 1 and int(
  510. test['volume_structure'][0]['volume_total_score']) > 200: # 暂定试卷分数都在200以内,超过200的表示识别错误
  511. test['volume_structure'][0]['volume_total_score'] = int(
  512. test['volume_structure'][0]['volume_total_score']) % 100
  513. elif (test['volume_structure'] == -1 or test['volume_structure'] == 1) and test['Score_structure'] != -1 and int(
  514. test['Score_structure'][0]['item_total_score']) > 200: # 暂定试卷分数都在200以内,超过200的表示识别错误
  515. test['Score_structure'][0]['item_total_score'] = int(
  516. test['Score_structure'][0]['item_total_score']) % 100
  517. all_test.append(test)
  518. else:
  519. ### 添加返回值OCR结果
  520. add_ocr = {}
  521. add_ocr['model_box'] = test_result1['bounding_box']
  522. add_ocr['label'] = test_result1['label']
  523. add_ocr['number'] = -1
  524. add_ocr['score'] = -1
  525. add_ocr['number_score'] = -1
  526. add_ocr['counts'] = -1
  527. add_ocr['type_score_box'] = type_score_boxs[i]
  528. add_ocr['ocr'] = test_result1['words']
  529. Score_last.append(add_ocr)
  530. ''' 解析模型分割模块没有对应的type_score时的分数'''
  531. for jjjj in range(len(all_test)):
  532. if all_test[jjjj]['Score_structure'] != -1:
  533. label_1 = all_test[jjjj]['Score_structure'][0]['label']
  534. num_1 = all_test[jjjj]['Score_structure'][0]['item_N']
  535. if label_1 == 'choice' :
  536. if choice_boxs.count(all_test[jjjj]['Score_structure'][0]['bounding_box']):
  537. choice_boxs.remove(all_test[jjjj]['Score_structure'][0]['bounding_box'])
  538. elif label_1 == 'cloze':
  539. if cloze_boxs.count(all_test[jjjj]['Score_structure'][0]['bounding_box']):
  540. cloze_boxs.remove(all_test[jjjj]['Score_structure'][0]['bounding_box'])
  541. elif label_1 == 'solve' and num_1 != 10000 and num_1 != -1:
  542. if solve_boxs.count(all_test[jjjj]['Score_structure'][0]['bounding_box']):
  543. solve_boxs.remove(all_test[jjjj]['Score_structure'][0]['bounding_box'])
  544. elif label_1 == 'composition':
  545. if solve_boxs.count(all_test[jjjj]['Score_structure'][0]['bounding_box']):
  546. solve_boxs.remove(all_test[jjjj]['Score_structure'][0]['bounding_box'])
  547. if choice_boxs != []: # 9月16号修改
  548. for ij in range(len(choice_boxs)):
  549. yminss = choice_boxs[ij][1] - 150 if choice_boxs[ij][1] - 150 > 0 else choice_boxs[ij][1]
  550. xminss = choice_boxs[ij][0] - 100 if choice_boxs[ij][0] - 100 > 0 else choice_boxs[ij][0]
  551. try:
  552. new_test = big_block_score(img0, xminss, yminss, choice_boxs[ij][2], choice_boxs[ij][3])
  553. if new_test != -1:
  554. new_test['Score_structure'][0]['bounding_box'] = choice_boxs[ij]
  555. new_test['Score_structure'][0]['label'] = 'choice'
  556. all_test.append(new_test)
  557. except Exception:
  558. print('choice_boxs_score_NULL_or_error')
  559. if cloze_boxs != []:
  560. for ij in range(len(cloze_boxs)):
  561. yminss = cloze_boxs[ij][1] - 100 if cloze_boxs[ij][1] - 100 > 0 else cloze_boxs[ij][1]
  562. xminss = cloze_boxs[ij][0] - 100 if cloze_boxs[ij][0] - 100 > 0 else cloze_boxs[ij][0]
  563. try:
  564. new_test = big_block_score(img0, xminss, yminss, cloze_boxs[ij][2], cloze_boxs[ij][3])
  565. if new_test != -1:
  566. new_test['Score_structure'][0]['bounding_box'] = cloze_boxs[ij]
  567. new_test['Score_structure'][0]['label'] = 'cloze'
  568. all_test.append(new_test)
  569. except Exception:
  570. print('cloze_boxs_score_NULL_or_error')
  571. if solve_boxs != []:
  572. for ij in range(len(solve_boxs)):
  573. yminss = solve_boxs[ij][1] - 50 if solve_boxs[ij][1] - 50 > 0 else solve_boxs[ij][1]
  574. xminss = solve_boxs[ij][0] - 50 if solve_boxs[ij][0] - 50 > 0 else solve_boxs[ij][0]
  575. try:
  576. new_test = big_block_score(img0, xminss, yminss, solve_boxs[ij][2], solve_boxs[ij][3])
  577. if new_test != -1:
  578. new_test['Score_structure'][0]['bounding_box'] = solve_boxs[ij]
  579. new_test['Score_structure'][0]['label'] = 'solve'
  580. all_test.append(new_test)
  581. except Exception:
  582. print('solve_boxs_score_NULL_or_error')
  583. if composition_boxs != []:
  584. for ij in range(len(composition_boxs)):
  585. yminss = composition_boxs[ij][1] - 240 if composition_boxs[ij][1] - 240 > 0 else composition_boxs[ij][1]
  586. xminss = composition_boxs[ij][0] - 100 if composition_boxs[ij][0] - 100 > 0 else composition_boxs[ij][0]
  587. try:
  588. new_test = big_block_score(img0, xminss, yminss, composition_boxs[ij][2], composition_boxs[ij][3])
  589. if new_test != -1:
  590. new_test['Score_structure'][0]['bounding_box'] = composition_boxs[ij]
  591. new_test['Score_structure'][0]['label'] = 'composition'
  592. all_test.append(new_test)
  593. except Exception:
  594. print('composition_boxs_score_NULL_or_error')
  595. for aaa in range(len(all_test)):
  596. if all_test[aaa]['Score_structure'] != -1 and all_test[aaa]['volume_structure'] == -1:
  597. score_last_one = {'model_box': dict(all_test[aaa])['Score_structure'][0]['bounding_box'],
  598. 'label': dict(all_test[aaa])['Score_structure'][0]['label'],
  599. 'number': dict(all_test[aaa])['Score_structure'][0]['item_N'],
  600. 'score': dict(all_test[aaa])['Score_structure'][0]['item_total_score'],
  601. 'number_score': dict(all_test[aaa])['Score_structure'][0]['item_score'],
  602. 'counts': dict(all_test[aaa])['Score_structure'][0]['item_count'],
  603. 'type_score_box': dict(all_test[aaa])['Score_structure'][0]['type_box']}
  604. Score_last.append(score_last_one)
  605. continue
  606. elif all_test[aaa]['Score_structure'] != -1 and all_test[aaa]['volume_structure'] == 1:
  607. score_last_one = {'model_box': dict(all_test[aaa])['Score_structure'][0]['bounding_box'],
  608. 'label': dict(all_test[aaa])['Score_structure'][0]['label'],
  609. 'number': 10000,
  610. 'score': dict(all_test[aaa])['Score_structure'][0]['item_total_score'],
  611. 'number_score': dict(all_test[aaa])['Score_structure'][0]['item_score'],
  612. 'counts': dict(all_test[aaa])['Score_structure'][0]['item_count'],
  613. 'type_score_box': dict(all_test[aaa])['Score_structure'][0]['type_box']}
  614. Score_last.append(score_last_one)
  615. continue
  616. elif all_test[aaa]['Score_structure'] != -1 and all_test[aaa]['volume_structure'] != -1:
  617. score_last_one = {'model_box': dict(all_test[aaa])['Score_structure'][0]['bounding_box'],
  618. 'label': dict(all_test[aaa])['Score_structure'][0]['label'],
  619. 'number': -1,
  620. 'score': dict(all_test[aaa])['Score_structure'][0]['volume_total_score'],
  621. 'number_score': dict(all_test[aaa])['Score_structure'][0]['volume_score'],
  622. 'counts': dict(all_test[aaa])['Score_structure'][0]['volume_count'],
  623. 'type_score_box': dict(all_test[aaa])['Score_structure'][0]['type_box']}
  624. Score_last.append(score_last_one)
  625. volume_last_one = {'volume_N': dict(all_test[aaa])['volume_structure'][0]['volume_N'],
  626. 'volume_total_score': dict(all_test[aaa])['volume_structure'][0]['volume_total_score'],
  627. 'volume_count': dict(all_test[aaa])['volume_structure'][0]['volume_count'],
  628. 'volume_score': dict(all_test[aaa])['volume_structure'][0]['volume_score'],
  629. 'keyword_type': dict(all_test[aaa])['volume_structure'][0]['keyword_type']}
  630. volume_last.append(volume_last_one)
  631. continue
  632. elif all_test[aaa]['volume_structure'] != -1:
  633. volume_last_one = {'volume_N': dict(all_test[aaa])['volume_structure'][0]['volume_N'],
  634. 'volume_total_score': dict(all_test[aaa])['volume_structure'][0]['volume_total_score'],
  635. 'volume_count': dict(all_test[aaa])['volume_structure'][0]['volume_count'],
  636. 'volume_score': dict(all_test[aaa])['volume_structure'][0]['volume_score'],
  637. 'keyword_type': dict(all_test[aaa])['volume_structure'][0]['keyword_type']}
  638. volume_last.append(volume_last_one)
  639. continue
  640. '''去重一个边框可能对应多个type_score的情况,英语单独解析'''
  641. Score_last_Remove_Duplicates = OrderedDict()
  642. for item in Score_last:
  643. Score_last_Remove_Duplicates.setdefault(item['model_box'], {**item, })
  644. Score_last_Remove_Duplicates = list(Score_last_Remove_Duplicates.values())
  645. if len(Score_last_Remove_Duplicates) != len(Score_last):
  646. len_Score_last = len(Score_last)
  647. Score_last = sorted(Score_last, key=lambda x: (x['model_box'][0] + x['model_box'][1] + x['type_score_box'][0] + x['type_score_box'][1]),reverse=True)
  648. if answer_sheet['subject'] == 'english': # 暂定英语只去除重复分数,不修改主观题边框
  649. for i in range(len_Score_last-1, -1, -1):
  650. if Score_last[i]['label'] == 'cloze':
  651. model_box2 = []; score2 = []; num2 = []; type_score2 = []; temp22 = []
  652. if Score_last[i]['model_box'] in model_box2:
  653. index21 = model_box2.index(Score_last[i]['model_box'])
  654. index2 = temp22[index21]
  655. score = Score_last[i]['score']
  656. if score < score2[index21] and score2[index21] < 20: # 去重,type_score多余的包含小题分数
  657. Score_last[i] = -1
  658. elif score < score2[index21] and score2[index21] > 20: # 去重,type_score多余的包含分卷分数
  659. Score_last[index2] = -1
  660. temp22[index21] = i
  661. score2[index21] = Score_last[i]['score']
  662. num2[index21] = Score_last[i]['number']
  663. type_score2[index21] = Score_last[i]['type_score_box']
  664. elif score > score2[index21] and score < 20: # 去重,type_score在不大于20分的情况下,暂定保留更大的分数
  665. Score_last[index2] = -1
  666. temp22[index21] = i
  667. score2[index21] = Score_last[i]['score']
  668. num2[index21] = Score_last[i]['number']
  669. type_score2[index21] = Score_last[i]['type_score_box']
  670. elif score > score2[index21] and score > 20: # 去重,type_score去除大于20分的重复分数
  671. Score_last[i] = -1
  672. else:
  673. Score_last[i] = -1
  674. else:
  675. model_box2.append(Score_last[i]['model_box'])
  676. score2.append(Score_last[i]['score'])
  677. num2.append(Score_last[i]['number'])
  678. type_score2.append(Score_last[i]['type_score_box'])
  679. temp22.append(i)
  680. else:
  681. model_box2 = []; score2 = []; num2 = []; type_score2 = []; temp22 = []
  682. if Score_last[i]['model_box'] in model_box2:
  683. index21 = model_box2.index(Score_last[i]['model_box'])
  684. index2 = temp22[index21]
  685. score = Score_last[i]['score']
  686. if score < score2[index21]: # 去重,暂定保留更大的分数
  687. Score_last[i] = -1
  688. elif score > score2[index21]: # 去重,暂定保留更大的分数
  689. Score_last[index2] = -1
  690. temp22[index21] = i
  691. score2[index21] = Score_last[i]['score']
  692. num2[index21] = Score_last[i]['number']
  693. type_score2[index21] = Score_last[i]['type_score_box']
  694. else:
  695. Score_last[i] = -1
  696. else:
  697. model_box2.append(Score_last[i]['model_box'])
  698. score2.append(Score_last[i]['score'])
  699. num2.append(Score_last[i]['number'])
  700. type_score2.append(Score_last[i]['type_score_box'])
  701. temp22.append(i)
  702. else: # 除去英语外的主观题边框修正
  703. model_box2 = []; score2 = []; num2 = []; type_score2 = []; temp22 = []
  704. for i in range(len_Score_last - 1, -1, -1): # 根据type_score切分
  705. if Score_last[i]['label'] == 'solve' or Score_last[i]['label'] == 'solve0':
  706. if Score_last[i]['model_box'] in model_box2 and type(Score_last[i]['number']) is not list:
  707. index21 = model_box2.index(Score_last[i]['model_box'])
  708. index2 = temp22[index21]
  709. score = Score_last[i]['score']
  710. num = Score_last[i]['number']
  711. type_score = Score_last[i]['type_score_box']
  712. del_box = 0
  713. if num == -1: # 去除同一主观题内对应的多个边框内的小项分数
  714. if 'ocr' in Score_last[index2]:
  715. Score_last[index2] = -1
  716. temp22[index21] = i
  717. score2[index21] = Score_last[i]['score']
  718. num2[index21] = Score_last[i]['number']
  719. type_score2[index21] = Score_last[i]['type_score_box']
  720. elif 'ocr' in Score_last[i]:
  721. Score_last[i] = -1
  722. else:
  723. Score_last[i] = -1
  724. elif num == 10000:
  725. Score_last[i] = -1
  726. elif num2[index21] == 10000 or num2[index21] == -1 and type_score[1] - type_score2[index21][1] > 100: # 同一主观题包含大题分数和小项分数,且大题分数位于边框中间,切分为两个主观题
  727. yy_max.append(Score_last[i]['model_box'][3])
  728. del_box = copy.deepcopy(Score_last[index2]['model_box'])
  729. score_del.append(del_box)
  730. Score_last[index2]['model_box'] = (
  731. Score_last[index2]['model_box'][0], Score_last[index2]['model_box'][1],
  732. Score_last[index2]['model_box'][2], type_score[1])
  733. Score_last[i]['model_box'] = (
  734. Score_last[i]['model_box'][0], type_score[1], Score_last[i]['model_box'][2],
  735. Score_last[i]['model_box'][3])
  736. score2[index21] = Score_last[i]['score']
  737. num2[index21] = Score_last[i]['number']
  738. type_score2[index21] = Score_last[i]['type_score_box']
  739. temp22[index21] = i
  740. elif score < score2[index21] and score2[index21] > 30: # 默认有效的分数值小于30分
  741. Score_last[index2] = -1
  742. temp22[index21] = i
  743. score2[index21] = Score_last[i]['score']
  744. num2[index21] = Score_last[i]['number']
  745. type_score2[index21] = Score_last[i]['type_score_box']
  746. elif score < 30 and score2[index21] < 30 and type_score[1] - type_score2[index21][1] > 100: # 同一主观题包含两个大题分数,且距离 >00 的情况下切分为两个主观题
  747. yy_max.append(Score_last[i]['model_box'][3])
  748. del_box = copy.deepcopy(Score_last[index2]['model_box'])
  749. score_del.append(del_box)
  750. Score_last[index2]['model_box'] = (
  751. Score_last[index2]['model_box'][0], Score_last[index2]['model_box'][1],
  752. Score_last[index2]['model_box'][2], type_score[1])
  753. Score_last[i]['model_box'] = (
  754. Score_last[i]['model_box'][0], type_score[1], Score_last[i]['model_box'][2],
  755. Score_last[i]['model_box'][3])
  756. score2[index21] = Score_last[i]['score']
  757. num2[index21] = Score_last[i]['number']
  758. type_score2[index21] = Score_last[i]['type_score_box']
  759. temp22[index21] = i
  760. elif score > 30 and score2[index21] > 30: # 默认有效的分数值小于30分
  761. temp_del = i if score > score2[index21] else index2
  762. Score_last[temp_del] = -1
  763. if temp_del == index2:
  764. temp22[index21] = i
  765. score2[index21] = Score_last[i]['score']
  766. num2[index21] = Score_last[i]['number']
  767. type_score2[index21] = Score_last[i]['type_score_box']
  768. elif score < 30 and score2[index21] < 30: # 多个分数小于30,且距离 <100 的情况下,删除
  769. temp_del = i if score < score2[index21] else index2
  770. Score_last[temp_del] = -1
  771. if temp_del == index2:
  772. temp22[index21] = i
  773. score2[index21] = Score_last[i]['score']
  774. num2[index21] = Score_last[i]['number']
  775. type_score2[index21] = Score_last[i]['type_score_box']
  776. else:
  777. Score_last[i] = -1
  778. else:
  779. model_box2.append(Score_last[i]['model_box'])
  780. score2.append(Score_last[i]['score'])
  781. num2.append(Score_last[i]['number'])
  782. type_score2.append(Score_last[i]['type_score_box'])
  783. temp22.append(i)
  784. for del_i in range(len_Score_last - 1, -1, -1):
  785. if Score_last[del_i] == -1:
  786. del Score_last[del_i]
  787. if answer_sheet['subject'] != 'english':
  788. Score_last = sorted(Score_last, key=lambda x: (x['model_box'][0], x['model_box'][0] + x['model_box'][1]),reverse=True)
  789. temp33 = 0
  790. len3 = len(Score_last)
  791. for i in range(len3 - 1, -1, -1): # 根据type_score合并
  792. if Score_last[i]['label'] == 'solve' or Score_last[i]['label'] == 'solve0':
  793. num = Score_last[i]['number']
  794. type_score = Score_last[i]['type_score_box']
  795. if type(Score_last[i]['number']) is not list:
  796. if num == 10000 or num == -1 or num < 4 and (Score_last[i]['label'] == 'solve' or Score_last[i]['label'] == 'solve0'): # 小题题号
  797. temp3 = 100000
  798. for indexi, model_box31 in enumerate(Score_last):
  799. if indexi != i and ((type_score[0] and type_score[2]) in range(model_box31['model_box'][0] - 30,model_box31['model_box'][2])) and ((type_score[1] and type_score[3]) in range(model_box31['model_box'][1],model_box31['model_box'][3]+30)): # 根据小题边框的xmin与主观题xmin的差值判断为一栏,以及纵坐标差值判断条件
  800. del_box = copy.deepcopy(Score_last[i]['model_box'])
  801. score_del.append(del_box)
  802. score_del.append(Score_last[indexi]['model_box'])
  803. Score_last[indexi]['model_box'] = (
  804. Score_last[indexi]['model_box'][0], Score_last[indexi]['model_box'][1],
  805. Score_last[indexi]['model_box'][2], Score_last[i]['model_box'][3])
  806. del Score_last[i]
  807. break
  808. temp31 = int(type_score[1] - model_box31['model_box'][3]) # 计算小题边框的ymin与主观题边框ymax的距离
  809. if (type_score[0] in range(model_box31['model_box'][0] - 30,model_box31['model_box'][2])) and temp31 > -20 and temp31 < temp3: # 根据小题边框的xmin与主观题xmin的差值判断为一栏,以及纵坐标差值判断条件
  810. temp3 = temp31
  811. temp33 = indexi
  812. yy_max.append(Score_last[i]['model_box'][3])
  813. if indexi == len(Score_last) - 1:
  814. del_box = copy.deepcopy(Score_last[temp33]['model_box'])
  815. score_del.append(del_box)
  816. score_del.append(Score_last[i]['model_box'])
  817. Score_last[temp33]['model_box'] = (
  818. Score_last[temp33]['model_box'][0], Score_last[temp33]['model_box'][1],
  819. Score_last[temp33]['model_box'][2], Score_last[i]['model_box'][3])
  820. del Score_last[i]
  821. break
  822. elif indexi == len(Score_last) - 1 and temp3 != 100000:
  823. del_box = copy.deepcopy(Score_last[temp33]['model_box'])
  824. score_del.append(del_box)
  825. score_del.append(Score_last[i]['model_box'])
  826. Score_last[temp33]['model_box'] = (
  827. Score_last[temp33]['model_box'][0], Score_last[temp33]['model_box'][1],
  828. Score_last[temp33]['model_box'][2], Score_last[i]['model_box'][3])
  829. del Score_last[i]
  830. break
  831. if Score_last != []:
  832. for i in range(len(Score_last)): # 多选题题号和分数逐个显示
  833. if type(Score_last[i]['number']) is list:
  834. C_q_s = len(Score_last[i]['number']) * [Score_last[i]['score']]
  835. Score_last[i]['score'] = C_q_s
  836. '''choice_m/cloze_s分数解析'''
  837. if num_choice == 1 or num_cloze == 1: # 对应choice_m分数
  838. for i in range(len(Score_last)):
  839. if Score_last[i]['label'] == 'choice':
  840. count_choice_m = 0
  841. for j in range(len(answer_sheet['regions'])):
  842. if answer_sheet['regions'][j]['class_name'] == 'choice_m':
  843. if Score_last[i]['number_score'] != -1 and ('number' in answer_sheet['regions'][j].keys()):
  844. answer_sheet['regions'][j]['default_points'] = len(answer_sheet['regions'][j]['number']) * [float(Score_last[i]['number_score'])]
  845. elif Score_last[i]['number_score'] == -1 and Score_last[i]['score'] != -1 and 'number' in answer_sheet['regions'][j].keys():
  846. count_choice_m = count_choice_m + len(answer_sheet['regions'][j]['number'])
  847. j_temp.append(j)
  848. if j == len(answer_sheet['regions']) - 1 and j_temp !=[]:
  849. try:
  850. for index, jj in enumerate(j_temp):
  851. num_score_m = round(float(Score_last[i]['score'] / count_choice_m), 2)
  852. answer_sheet['regions'][jj]['default_points'] = len(answer_sheet['regions'][jj]['number']) * [num_score_m]
  853. break
  854. except Exception:
  855. print('choice_m_error')
  856. elif Score_last[i]['label'] == 'cloze':
  857. for j in range(len(answer_sheet['regions'])):
  858. if answer_sheet['regions'][j]['class_name'] == 'cloze_s':
  859. if Score_last[i]['number_score'] != -1:
  860. answer_sheet['regions'][j]['default_points'] = Score_last[i]['number_score']
  861. elif num_choice > 1 or num_cloze > 1:
  862. for i in range(len(Score_last)):
  863. if Score_last[i]['label'] == 'choice':
  864. count_choice_m = 0
  865. for j in range(len(answer_sheet['regions'])):
  866. if answer_sheet['regions'][j]['class_name'] == 'choice_m':
  867. xmin_dis = answer_sheet['regions'][j]['bounding_box']['xmin'] - \
  868. Score_last[i]['model_box'][0]
  869. ymin_dis = answer_sheet['regions'][j]['bounding_box']['ymin'] - \
  870. Score_last[i]['model_box'][1]
  871. xmax_dis = answer_sheet['regions'][j]['bounding_box']['xmax'] - \
  872. Score_last[i]['model_box'][2]
  873. ymax_dis = answer_sheet['regions'][j]['bounding_box']['ymax'] - \
  874. Score_last[i]['model_box'][3]
  875. if xmin_dis > -30 and ymin_dis > -30 and xmax_dis < 30 and ymax_dis < 30:
  876. if Score_last[i]['number_score'] != -1 and 'number' in answer_sheet['regions'][j].keys():
  877. answer_sheet['regions'][j]['default_points'] = len(answer_sheet['regions'][j]['number']) * [float(Score_last[i]['number_score'])]
  878. elif Score_last[i]['number_score'] == -1 and Score_last[i]['score'] != -1 and 'number' in answer_sheet['regions'][j].keys():
  879. count_choice_m = count_choice_m + len(answer_sheet['regions'][j]['number'])
  880. j_temp.append(j)
  881. if j == len(answer_sheet['regions']) - 1 and j_temp !=[]:
  882. try:
  883. for index, jj in enumerate(j_temp):
  884. num_score_m = round(float(Score_last[i]['score'] / count_choice_m), 2)
  885. answer_sheet['regions'][jj]['default_points'] = len(answer_sheet['regions'][jj]['number']) * [num_score_m]
  886. break
  887. except Exception:
  888. print('choice_m_error')
  889. elif Score_last[i]['label'] == 'cloze':
  890. for j in range(len(answer_sheet['regions'])):
  891. if answer_sheet['regions'][j]['class_name'] == 'cloze_s':
  892. xmin_dis = answer_sheet['regions'][j]['bounding_box']['xmin'] - \
  893. Score_last[i]['model_box'][0]
  894. ymin_dis = answer_sheet['regions'][j]['bounding_box']['ymin'] - \
  895. Score_last[i]['model_box'][1]
  896. xmax_dis = answer_sheet['regions'][j]['bounding_box']['xmax'] - \
  897. Score_last[i]['model_box'][2]
  898. ymax_dis = answer_sheet['regions'][j]['bounding_box']['ymax'] - \
  899. Score_last[i]['model_box'][3]
  900. if xmin_dis > -30 and ymin_dis > -30 and xmax_dis < 30 and ymax_dis < 30:
  901. if Score_last[i]['number_score'] != -1 :
  902. answer_sheet['regions'][j]['default_points'] = Score_last[i]['number_score']
  903. elif choice_m_boxs !=[]:
  904. x_choice_m_min = 10000
  905. y_choice_m_min = 10000
  906. x_choice_m_max = 0
  907. y_choice_m_max = 0
  908. test = {}
  909. for index_m, choice_m_s in enumerate(choice_m_boxs):
  910. if int(choice_m_s['xmin']) < x_choice_m_min:
  911. x_choice_m_min = int(choice_m_s['xmin'])
  912. if int(choice_m_s['ymin']) < y_choice_m_min:
  913. y_choice_m_min = int(choice_m_s['ymin'])
  914. if int(choice_m_s['xmax']) > x_choice_m_max:
  915. x_choice_m_max = int(choice_m_s['xmax'])
  916. if int(choice_m_s['ymax']) > y_choice_m_max:
  917. y_choice_m_max = int(choice_m_s['ymax'])
  918. # 模型输出没有choice的情况下暂时假定只有一个choice
  919. type_score_choice_m = -1
  920. for index_t1, type_score_s1 in enumerate(type_score_boxs):
  921. if type_score_s1[0] < x_choice_m_max and type_score_s1[3] < y_choice_m_min:
  922. type_score_choice_m = type_score_s1
  923. break
  924. if type_score_choice_m != -1:
  925. test_result1 = {'min_choice_m': -1,
  926. 'bounding_box': choice_m_boxs,
  927. 'label': 'choice_m',
  928. 'type_box': type_score_choice_m}
  929. test_result1['words'] = str()
  930. try: # tr_OCR
  931. image_choice = image_src.crop((type_score_choice_m[0], type_score_choice_m[1], type_score_choice_m[2], type_score_choice_m[3]))
  932. res1 = tr.run(image_choice)
  933. print('tr_OCR')
  934. for t in range(len(res1)):
  935. test_result1['words'] = test_result1['words'] + res1[t][1]
  936. except Exception as e: # baidu_OCR
  937. print('baidu_OCR')
  938. res1 = get_ocr_text_and_coordinate_in_google_format(
  939. img0[type_score_choice_m[1]:type_score_choice_m[3], type_score_choice_m[0]:type_score_choice_m[2]], ocr_accuracy=OCR_ACCURACY, language_type='CHN_ENG')
  940. for t in range(len(res1['words'])):
  941. test_result1['words'] = test_result1['words'] + res1['words'][t]
  942. if test_result1['words'] != {}:
  943. test = key_words(test_result1)
  944. choice_m_score = -1
  945. if test == {}:
  946. choice_m_type_score_ocr = test_result1['words']
  947. elif test['volume_structure'] == -1 and test['Score_structure'] == -1:
  948. choice_m_type_score_ocr = test_result1['words']
  949. else:
  950. if test['volume_structure'] != -1 and test['volume_structure'][0]['volume_score'] != -1:
  951. choice_m_score = test['volume_structure'][0]['volume_score']
  952. elif test['volume_structure'] == -1 and test['Score_structure'] != -1 and test['Score_structure'][0]['item_score'] != -1:
  953. choice_m_score = test['Score_structure'][0]['item_score']
  954. if choice_m_score != -1:
  955. for j in range(len(answer_sheet['regions'])):
  956. if answer_sheet['regions'][j]['class_name'] == 'choice_m':
  957. answer_sheet['regions'][j]['default_points'] = len(
  958. answer_sheet['regions'][j]['number']) * [float(choice_m_score)]
  959. elif test_result1['words'] != {}:
  960. for j in range(len(answer_sheet['regions'])):
  961. if answer_sheet['regions'][j]['class_name'] == 'choice_m':
  962. answer_sheet['regions'][j]['type_score_ocr'] = choice_m_type_score_ocr
  963. '''分数与模型对应'''
  964. ocr_flag = 0
  965. for i in range(len(answer_sheet['regions'])-1, -1, -1):
  966. for j in range(len(Score_last)-1, -1, -1):
  967. if (int(Score_last[j]['model_box'][0]) == int(answer_sheet['regions'][i]['bounding_box']['xmin']) and
  968. int(Score_last[j]['model_box'][1]) == int(answer_sheet['regions'][i]['bounding_box']['ymin']) and
  969. int(Score_last[j]['model_box'][2]) == int(answer_sheet['regions'][i]['bounding_box']['xmax']) and
  970. int(Score_last[j]['model_box'][3]) != int(answer_sheet['regions'][i]['bounding_box']['ymax'])) and (answer_sheet['regions'][i]['class_name'] == 'solve' or answer_sheet['regions'][i]['class_name'] == 'solve0'):
  971. answer_sheet['regions'][i]['bounding_box']['ymax'] = int(Score_last[j]['model_box'][3])
  972. if Score_last[j]['number'] == 10000:
  973. answer_sheet['regions'][i]['number'] = -1 # 题号
  974. elif Score_last[j]['number'] != -1 and Score_last[j]['number'] != 10000:
  975. answer_sheet['regions'][i]['number'] = Score_last[j]['number'] # 题号
  976. else:
  977. answer_sheet['regions'][i]['number'] = -1 # 题号
  978. if Score_last[j]['score'] != -1:
  979. answer_sheet['regions'][i]['default_points'] = Score_last[j]['score']
  980. if type(answer_sheet['regions'][i]['default_points']) is list and (
  981. answer_sheet['regions'][i]['class_name'] == 'solve' or answer_sheet['regions'][i][
  982. 'class_name'] == 'solve0'):
  983. answer_sheet['regions'][i]['class_name'] = 'optional_solve'
  984. elif 'default_points' not in answer_sheet['regions'][i].keys():
  985. answer_sheet['regions'][i]['default_points'] = -1
  986. del Score_last[j]
  987. elif (int(Score_last[j]['model_box'][0]) == int(answer_sheet['regions'][i]['bounding_box']['xmin']) and
  988. int(Score_last[j]['model_box'][1]) == int(answer_sheet['regions'][i]['bounding_box']['ymin']) and
  989. int(Score_last[j]['model_box'][2]) == int(answer_sheet['regions'][i]['bounding_box']['xmax']) and
  990. int(Score_last[j]['model_box'][3]) == int(answer_sheet['regions'][i]['bounding_box']['ymax'])):
  991. if Score_last[j]['number'] == 10000:
  992. answer_sheet['regions'][i]['number'] = -1 # 题号
  993. elif Score_last[j]['number'] != -1 and Score_last[j]['number'] != 10000:
  994. answer_sheet['regions'][i]['number'] = Score_last[j]['number'] # 题号
  995. else:
  996. answer_sheet['regions'][i]['number'] = -1 # 题号
  997. if Score_last[j]['score'] != -1:
  998. answer_sheet['regions'][i]['default_points'] = Score_last[j]['score']
  999. if type(answer_sheet['regions'][i]['default_points']) is list and (
  1000. answer_sheet['regions'][i]['class_name'] == 'solve' or answer_sheet['regions'][i][
  1001. 'class_name'] == 'solve0'):
  1002. answer_sheet['regions'][i]['class_name'] = 'optional_solve'
  1003. ocr_flag = 1
  1004. if 'type_score_ocr' in answer_sheet['regions'][i].keys():
  1005. del answer_sheet['regions'][i]['type_score_ocr']
  1006. elif 'default_points' not in answer_sheet['regions'][i].keys():
  1007. answer_sheet['regions'][i]['default_points'] = -1
  1008. if ocr_flag == 0 and 'ocr' in Score_last[j]: # 没有识别到分数的模块添加type_score_ocr结果
  1009. answer_sheet['regions'][i]['type_score_ocr'] = Score_last[j]['ocr']
  1010. del Score_last[j]
  1011. if score_del != []: # del_model_boxs
  1012. for j in range(len(score_del)):
  1013. if score_del != [] and (score_del[j][0] == answer_sheet['regions'][i]['bounding_box']['xmin'] and score_del[j][1] ==
  1014. answer_sheet['regions'][i]['bounding_box']['ymin'] and score_del[j][2] ==
  1015. answer_sheet['regions'][i]['bounding_box']['xmax'] and score_del[j][3] ==
  1016. answer_sheet['regions'][i]['bounding_box']['ymax']):
  1017. del answer_sheet['regions'][i]
  1018. for jj in range(len(Score_last)): # add_model_boxs
  1019. answer_sheet_one = {}
  1020. answer_sheet_one['class_name'] = Score_last[jj]['label']
  1021. box_one = {}
  1022. box_one['xmin'] = int(Score_last[jj]['model_box'][0])
  1023. box_one['ymin'] = int(Score_last[jj]['model_box'][1])
  1024. box_one['xmax'] = int(Score_last[jj]['model_box'][2])
  1025. box_one['ymax'] = int(Score_last[jj]['model_box'][3])
  1026. answer_sheet_one['bounding_box'] = box_one
  1027. if Score_last[jj]['number'] == 10000:
  1028. answer_sheet_one['number'] = -1 # 题号
  1029. elif Score_last[jj]['number'] != -1 and Score_last[jj]['number'] != 10000:
  1030. answer_sheet_one['number'] = Score_last[jj]['number'] # 题号
  1031. else:
  1032. answer_sheet_one['number'] = -1 # 题号
  1033. if Score_last[jj]['score'] != -1:
  1034. answer_sheet_one['default_points'] = Score_last[jj]['score']
  1035. else:
  1036. answer_sheet_one['default_points'] = -1
  1037. answer_sheet['regions'].append(answer_sheet_one)
  1038. return answer_sheet