sheet_points_total.py 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. # @Author : fantuan
  2. # @Time : 2019.9.4
  3. import requests
  4. import base64
  5. from urllib import parse, request
  6. import cv2
  7. import re
  8. from segment.sheet_resolve.tools.brain_api import get_ocr_text_and_coordinate_in_google_format
  9. from segment.sheet_resolve.analysis.sheet.ocr_key_words import key_words
  10. OCR_ACCURACY = 'accurate'
  11. # def ocr_login():
  12. # grant_type = 'client_credentials'
  13. # client_id = OCR_CLIENT_ID
  14. # client_secret = OCR_CLIENT_SECRET
  15. #
  16. # textmod = {'grant_type': grant_type, 'client_id': client_id, 'client_secret': client_secret}
  17. # textmod = parse.urlencode(textmod)
  18. #
  19. # # 输出内容:user=admin&password=admin
  20. # header_dict = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko'}
  21. # url = 'https://aip.baidubce.com/oauth/2.0/token'
  22. # req = request.Request(url='{}{}{}'.format(url, '?', textmod), headers=header_dict)
  23. # res = request.urlopen(req).read()
  24. # token = eval(res.decode(encoding='utf-8'))['access_token']
  25. # return token
  26. # def opecv2base64(img):
  27. # image = cv2.imencode('.jpg', img)[1]
  28. # base64_data = str(base64.b64encode(image))[2:-1]
  29. # return base64_data
  30. # def get_ocr_raw_result(img, ocr_accuracy=OCR_ACCURACY, language_type='CHN_ENG'):
  31. # textmod = {'access_token': access_token}
  32. # textmod = parse.urlencode(textmod)
  33. # url = '{}{}{}{}'.format(OCR_BOX_URL, ocr_accuracy, '?', textmod)
  34. # url_general = '{}{}{}{}'.format(OCR_BOX_URL, 'general', '?', textmod)
  35. #
  36. # headers = {'Content-Type': 'application/x-www-form-urlencoded'}
  37. #
  38. # image_type = 'base64'
  39. # group_id = 'group001'
  40. # user_id = 'usr001'
  41. #
  42. # image = opecv2base64(img)
  43. #
  44. # data = {
  45. # 'image_type': image_type,
  46. # 'group_id': group_id,
  47. # 'user_id': user_id,
  48. # 'image': image,
  49. # 'detect_direction': 'true',
  50. # 'recognize_granularity': 'small',
  51. # 'language_type': language_type,
  52. # # 'vertexes_location': 'true',
  53. # # 'probability': 'true'
  54. # }
  55. #
  56. # resp = requests.post(url, data=data, headers=headers).json()
  57. # if resp.get('error_msg'):
  58. # if 'internal error' in resp.get('error_msg'):
  59. # resp = requests.post(url_general, data=data, headers=headers).json()
  60. # if resp.get('error_msg'):
  61. # raise Exception("ocr {}!".format(resp.get('error_msg')))
  62. # else:
  63. # raise Exception("ocr {}!".format(resp.get('error_msg')))
  64. #
  65. # return resp
  66. # def get_ocr_text_and_coordinate(img, ocr_accuracy=OCR_ACCURACY, language_type='CHN_ENG'):
  67. # textmod = {'access_token': access_token}
  68. # textmod = parse.urlencode(textmod)
  69. # url = '{}{}{}{}'.format(OCR_BOX_URL, ocr_accuracy, '?', textmod)
  70. # url_general = '{}{}{}{}'.format(OCR_BOX_URL, 'general', '?', textmod)
  71. #
  72. # headers = {'Content-Type': 'application/x-www-form-urlencoded'}
  73. #
  74. # image_type = 'base64'
  75. # group_id = 'group001'
  76. # user_id = 'usr001'
  77. #
  78. # image = opecv2base64(img)
  79. #
  80. # data = {
  81. # 'image_type': image_type,
  82. # 'group_id': group_id,
  83. # 'user_id': user_id,
  84. # 'image': image,
  85. # 'detect_direction': 'true',
  86. # 'recognize_granularity': 'small',
  87. # 'language_type': language_type,
  88. # # 'vertexes_location': 'true',
  89. # # 'probability': 'true'
  90. # }
  91. #
  92. # resp = requests.post(url, data=data, headers=headers).json()
  93. # if resp.get('error_msg'):
  94. # if 'internal error' in resp.get('error_msg'):
  95. # resp = requests.post(url_general, data=data, headers=headers).json()
  96. # if resp.get('error_msg'):
  97. # raise Exception("ocr {}!".format(resp.get('error_msg')))
  98. # else:
  99. # raise Exception("ocr {}!".format(resp.get('error_msg')))
  100. #
  101. # words_result = resp.get('words_result')
  102. # return words_result
  103. # def get_ocr_text_and_coordinate_in_google_format(img, ocr_accuracy=OCR_ACCURACY, language_type='CHN_ENG'):
  104. # textmod = {'access_token': access_token}
  105. # textmod = parse.urlencode(textmod)
  106. # url = '{}{}{}{}'.format(OCR_BOX_URL, ocr_accuracy, '?', textmod)
  107. # url_general = '{}{}{}{}'.format(OCR_BOX_URL, 'general', '?', textmod)
  108. #
  109. # headers = {'Content-Type': 'application/x-www-form-urlencoded'}
  110. #
  111. # image_type = 'base64'
  112. # group_id = 'group001'
  113. # user_id = 'usr001'
  114. #
  115. # image = opecv2base64(img)
  116. #
  117. # data = {
  118. # 'image_type': image_type,
  119. # 'group_id': group_id,
  120. # 'user_id': user_id,
  121. # 'image': image,
  122. # 'detect_direction': 'true',
  123. # 'recognize_granularity': 'small',
  124. # 'language_type': language_type,
  125. # # 'vertexes_location': 'true',
  126. # # 'probability': 'true'
  127. # }
  128. #
  129. # resp = requests.post(url, data=data, headers=headers).json()
  130. # if resp.get('error_msg'):
  131. # if 'internal error' in resp.get('error_msg'):
  132. # resp = requests.post(url_general, data=data, headers=headers).json()
  133. # if resp.get('error_msg'):
  134. # raise Exception("ocr {}!".format(resp.get('error_msg')))
  135. # else:
  136. # raise Exception("ocr {}!".format(resp.get('error_msg')))
  137. #
  138. # words_result = resp.get('words_result')
  139. # dict_list = [item2.get('location') for item in words_result for item2 in item['chars']]
  140. # char_list = [item2.get('char') for item in words_result for item2 in item['chars']]
  141. # words = [item.get('words') for item in words_result]
  142. # matrix = []
  143. # for adict in dict_list:
  144. # xmin = adict['left']
  145. # ymin = adict['top']
  146. # xmax = adict['width'] + adict['left']
  147. # ymax = adict['top'] + adict['height']
  148. # item0 = (xmin, ymin, xmax, ymax)
  149. # matrix.append(item0)
  150. #
  151. # res_dict = {'chars': char_list, 'coordinates': matrix, 'words': words}
  152. # return res_dict
  153. def model_type_score(all_type_score_one, choice_box, cloze_box, solve_box,composition_box): # 与模型得到边框与type_score对应
  154. '''
  155. :param all_type_score_one: 模型得到的单个type_score的坐标位置
  156. :param choice_box: 模型得到的选择题坐标位置
  157. :param cloze_box: 模型得到的填空题坐标位置
  158. :param solve_box: 模型得到的解答题坐标位置
  159. :return:
  160. '''
  161. N_choice = len(choice_box)
  162. N_cloze = len(cloze_box)
  163. N_solve_box = len(solve_box)
  164. N_composition = len(composition_box)
  165. min_choice_dict = {}
  166. min_cloze_dict = {}
  167. min_solve_dict = {}
  168. min_composition_dict = {}
  169. test_result1 = 0
  170. min_choice = 100000000
  171. min_cloze = 100000000
  172. min_solve = 100000000
  173. min_composition = 100000000
  174. for j in range(N_choice):
  175. choice_distance_x = abs(list(all_type_score_one)[0] - choice_box[j][0])
  176. choice_distance_y = abs(list(all_type_score_one)[1] - choice_box[j][1])
  177. choice_distance = choice_distance_x + choice_distance_y
  178. if choice_distance < min_choice:
  179. min_choice = choice_distance
  180. min_choice_dict = {'min_choice': min_choice,
  181. 'bounding_box': choice_box[j],
  182. 'label': 'choice',
  183. 'type_box': all_type_score_one}
  184. for k in range(N_cloze):
  185. cloze_distance_x = abs(list(all_type_score_one)[0] - cloze_box[k][0])
  186. cloze_distance_y = abs(list(all_type_score_one)[1] - cloze_box[k][1])
  187. cloze_distance = cloze_distance_x + cloze_distance_y
  188. if cloze_distance < min_cloze:
  189. min_cloze = cloze_distance
  190. min_cloze_dict = {'min_cloze': min_cloze,
  191. 'bounding_box': cloze_box[k],
  192. 'label': 'cloze',
  193. 'type_box': all_type_score_one}
  194. for l in range(N_solve_box):
  195. solve_distance_x = abs(list(all_type_score_one)[0] - solve_box[l][0])
  196. solve_distance_y = abs(list(all_type_score_one)[1] - solve_box[l][1])
  197. solve_distance = solve_distance_x + solve_distance_y
  198. if solve_distance < min_solve:
  199. min_solve = solve_distance
  200. min_solve_dict = {'min_solve': min_solve,
  201. 'bounding_box': solve_box[l],
  202. 'label': 'solve',
  203. 'type_box': all_type_score_one}
  204. for m in range(N_composition):
  205. composition_distance_x = abs(list(all_type_score_one)[0] - composition_box[m][0])
  206. composition_distance_y = abs(list(all_type_score_one)[1] - composition_box[m][1])
  207. composition_distance = composition_distance_x + composition_distance_y
  208. if composition_distance < min_composition:
  209. min_composition = composition_distance
  210. min_composition_dict = {'min_cloze': min_composition,
  211. 'bounding_box': composition_box[m],
  212. 'label': 'composition',
  213. 'type_box': all_type_score_one}
  214. if min_choice < min_cloze and min_choice < min_solve and min_choice < min_composition:
  215. # 建立相互关联的关系。 即表示该type_score对应于选择题
  216. if test_result1 < 400:
  217. test_result1 = min_choice_dict
  218. else:
  219. test_result1 = -1
  220. elif min_cloze < min_choice and min_cloze < min_solve and min_cloze < min_composition:
  221. # 建立相互关联的关系。 即表示该type_score对应于填空题
  222. if test_result1 < 400:
  223. test_result1 = min_cloze_dict
  224. else:
  225. test_result1 = -1
  226. elif min_solve < min_cloze and min_solve < min_choice and min_solve < min_composition:
  227. # 建立相互关联的关系。 即表示该type_score对应于解答题
  228. if test_result1 < 300:
  229. test_result1 = min_solve_dict
  230. else:
  231. test_result1 = -1
  232. elif min_composition < min_cloze and min_composition < min_choice and min_composition < min_solve:
  233. # 建立相互关联的关系。 即表示该type_score对应于解答题
  234. if test_result1 < 300:
  235. test_result1 = min_composition_dict
  236. else:
  237. test_result1 = -1
  238. return test_result1
  239. def ocr_key_words(rect, type_score_dict): # 将ocr识别得到的文字与模型得到的type_score对应
  240. '''
  241. :param rect: OCR识别结果数组,格式:res = {'chars': [},'coordinates': [(),()},'words': []}
  242. :param type_score_dict: 模型得到的type_score(与模型得到的边框相对应)
  243. :return: 字典中添加word
  244. '''
  245. len_ocr = len(rect['chars'])
  246. xmin = type_score_dict['type_box'][0]
  247. ymin = type_score_dict['type_box'][1]
  248. xmax = type_score_dict['type_box'][2]
  249. ymax = type_score_dict['type_box'][3]
  250. words = []
  251. for j in range(len_ocr):
  252. if rect['coordinates'][j][0] - xmin > -30 and rect['coordinates'][j][1] - ymin > -30 and rect['coordinates'][j][
  253. 2] - xmax < 30 and rect['coordinates'][j][3] - ymax < 30:
  254. word = rect['chars'][j]
  255. words.append(word)
  256. type_score_dict['words'] = words
  257. type_score_dict_ocr = type_score_dict
  258. return type_score_dict_ocr
  259. def get_sheet_number_total(answer_sheet, res, img0):
  260. # img_path0 = r'C:\Users\Administrator\Desktop\score_test\image\dili\000003.jpg'
  261. # img0 = cv2.imread(img_path0)
  262. # access_token = ocr_login()
  263. # res = get_ocr_text_and_coordinate_in_google_format(img0, ocr_accuracy=OCR_ACCURACY, language_type='CHN_ENG') # 整张OCR
  264. # answer_sheet = {"img_name": "000003.jpg", "analysis_type": "geography", "regions": [{"class_name": "cloze_s", "bounding_box": {"xmin": 57, "ymin": 412, "xmax": 1554, "ymax": 514}, "score": "1.0000"}, {"class_name": "cloze_s", "bounding_box": {"xmin": 82, "ymin": 988, "xmax": 1496, "ymax": 1073}, "score": "1.0000"}, {"class_name": "cloze_s", "bounding_box": {"xmin": 101, "ymin": 508, "xmax": 1540, "ymax": 602}, "score": "0.9999"}, {"class_name": "cloze_s", "bounding_box": {"xmin": 170, "ymin": 1485, "xmax": 1532, "ymax": 1556}, "score": "0.9999"}, {"class_name": "cloze_s", "bounding_box": {"xmin": 49, "ymin": 1298, "xmax": 1516, "ymax": 1383}, "score": "0.9994"}, {"class_name": "cloze_s", "bounding_box": {"xmin": 179, "ymin": 1067, "xmax": 1502, "ymax": 1164}, "score": "0.9994"}, {"class_name": "cloze_s", "bounding_box": {"xmin": 118, "ymin": 1529, "xmax": 1532, "ymax": 1635}, "score": "0.9983"}, {"class_name": "cloze_s", "bounding_box": {"xmin": 74, "ymin": 1395, "xmax": 923, "ymax": 1456}, "score": "0.9976"}, {"class_name": "cloze_s", "bounding_box": {"xmin": 52, "ymin": 830, "xmax": 1466, "ymax": 903}, "score": "0.9960"}, {"class_name": "cloze_s", "bounding_box": {"xmin": 79, "ymin": 1594, "xmax": 1540, "ymax": 1737}, "score": "0.9365"}, {"class_name": "cloze_s", "bounding_box": {"xmin": 85, "ymin": 266, "xmax": 940, "ymax": 342}, "score": "0.7881"}, {"class_name": "cloze_s", "bounding_box": {"xmin": 99, "ymin": 900, "xmax": 956, "ymax": 974}, "score": "0.7662"}, {"class_name": "page", "bounding_box": {"xmin": 716, "ymin": 2220, "xmax": 995, "ymax": 2284}, "score": "0.8190"}, {"class_name": "type_score", "bounding_box": {"xmin": 63, "ymin": 757, "xmax": 242, "ymax": 821}, "score": "0.9989"}, {"class_name": "type_score", "bounding_box": {"xmin": 66, "ymin": 105, "xmax": 250, "ymax": 163}, "score": "0.9613"}, {"class_name": "type_score", "bounding_box": {"xmin": 55, "ymin": 1228, "xmax": 239, "ymax": 1287}, "score": "0.9506"}, {"class_name": "cloze", "bounding_box": {"xmin": 49, "ymin": 93, "xmax": 1518, "ymax": 754}, "score": "0.9954"}, {"class_name": "cloze", "bounding_box": {"xmin": 88, "ymin": 780, "xmax": 1524, "ymax": 1579}, "score": "0.9208"}], "series_number": "20190912000015", "is_success": 1}
  265. choice_boxs = []
  266. choice_m_boxs = []
  267. cloze_boxs = []
  268. solve_boxs = []
  269. type_score_boxs = []
  270. select_boxs = []
  271. all_test = []
  272. num_choice = 0
  273. num_choice_m = 0
  274. num_cloze = 0
  275. num_slove = 0
  276. num_select = 0
  277. num_type_score = 0
  278. volume_last = []
  279. Score_last = []
  280. score_last_one = 0
  281. volume_last_one = 0
  282. model_box2 = []
  283. composition_boxs = []
  284. score2 = []
  285. num_redundance = 0
  286. num_composition = 0
  287. j_temp = []
  288. jj_temp =[]
  289. for ele in answer_sheet["regions"]: # 从模型输出获取对应标签的边框信息
  290. if ele["class_name"] == 'choice':
  291. choice_box = ele['bounding_box']
  292. choice_one = (
  293. int(choice_box['xmin']), int(choice_box['ymin']), int(choice_box['xmax']), int(choice_box['ymax']))
  294. choice_boxs.append(choice_one)
  295. num_choice = num_choice + 1
  296. elif ele["class_name"] == 'choice_m':
  297. choice_m_boxs.append(ele)
  298. num_choice_m = num_choice_m + 1
  299. elif ele["class_name"] == 'cloze':
  300. cloze_box = ele['bounding_box']
  301. cloze_one = (int(cloze_box['xmin']), int(cloze_box['ymin']), int(cloze_box['xmax']), int(cloze_box['ymax']))
  302. cloze_boxs.append(cloze_one)
  303. num_cloze = num_cloze + 1
  304. elif ele["class_name"] == 'composition0' or ele["class_name"] == 'composition' or ele[
  305. "class_name"] == 'correction':
  306. composition_box = ele['bounding_box']
  307. composition_one = (int(composition_box['xmin']), int(composition_box['ymin']), int(composition_box['xmax']),
  308. int(composition_box['ymax']))
  309. composition_boxs.append(composition_one)
  310. num_composition = num_composition + 1
  311. elif ele["class_name"] == 'solve' or ele["class_name"] == 'solve0':
  312. solve_box = ele['bounding_box']
  313. solve_one = (int(solve_box['xmin']), int(solve_box['ymin']), int(solve_box['xmax']), int(solve_box['ymax']))
  314. solve_boxs.append(solve_one)
  315. num_slove = num_slove + 1
  316. elif ele["class_name"] == 'select_s' or ele["class_name"] == 'select_b':
  317. select_box = ele['bounding_box']
  318. select_one = (
  319. int(select_box['xmin']), int(select_box['ymin']), int(select_box['xmax']), int(select_box['ymax']))
  320. select_boxs.append(select_one)
  321. num_select = num_select + 1
  322. elif ele["class_name"] == 'type_score':
  323. type_score_box = ele['bounding_box']
  324. type_score_one = (int(type_score_box['xmin']), int(type_score_box['ymin']), int(type_score_box['xmax']),
  325. int(type_score_box['ymax']))
  326. type_score_boxs.append(type_score_one)
  327. num_type_score = num_type_score + 1
  328. for i in range(len(type_score_boxs)):
  329. test_result1 = model_type_score(type_score_boxs[i], choice_boxs, cloze_boxs, solve_boxs, composition_boxs)
  330. if test_result1 != -1 and test_result1 != 0:
  331. type_score_dict_ocr = ocr_key_words(res, test_result1)
  332. if type_score_dict_ocr['words'] == []: # type_score没有ocr结果,暂定可能ocr漏识别到分数,再过一遍ocr
  333. type_score_dict_ocr['words'] = get_ocr_text_and_coordinate_in_google_format(img0[type_score_boxs[i][1]:type_score_boxs[i][3],type_score_boxs[i][0]:type_score_boxs[i][2]],ocr_accuracy=OCR_ACCURACY,language_type='CHN_ENG')['words']
  334. test = key_words(type_score_dict_ocr)
  335. if test == {} or test['Score_structure'] == -1: # 如果存在type_score未识别到分数,暂定可能ocr漏识别到分数,再过一遍ocr
  336. image = img0[test_result1['bounding_box'][1]: test_result1['bounding_box'][3],
  337. test_result1['bounding_box'][0]: test_result1['bounding_box'][2]]
  338. res1 = get_ocr_text_and_coordinate_in_google_format(image,ocr_accuracy=OCR_ACCURACY,language_type='CHN_ENG')
  339. aa = []
  340. for ii in range(len(res1['coordinates'])):
  341. xmin11 = res1['coordinates'][ii][0] + test_result1['bounding_box'][0]
  342. ymin11 = res1['coordinates'][ii][1] + test_result1['bounding_box'][1]
  343. xmax11 = res1['coordinates'][ii][2] + test_result1['bounding_box'][0]
  344. ymax11 = res1['coordinates'][ii][3] + test_result1['bounding_box'][1]
  345. aaa = (xmin11, ymin11, xmax11, ymax11)
  346. aa.append(aaa)
  347. res1['coordinates'] = aa
  348. if len(res1['words']) > 0:
  349. type_score_dict_ocr['words'] = res1['words'][0]
  350. new_test = key_words(type_score_dict_ocr)
  351. if new_test == {} or new_test['Score_structure'] == -1:
  352. if len(res1['words']) > 1:
  353. type_score_dict_ocr['words'] = res1['words'][1]
  354. new_test = key_words(type_score_dict_ocr)
  355. if new_test == {} or new_test['Score_structure'] == -1:
  356. if len(res1['words']) > 2:
  357. type_score_dict_ocr['words'] = res1['words'][2]
  358. new_test = key_words(type_score_dict_ocr)
  359. if new_test != {}:
  360. if new_test['volume_structure'] != -1 and int(new_test['volume_structure'][0][
  361. 'volume_total_score']) > 200: # 暂定试卷分数都在200以内,超过200的表示识别错误
  362. new_test['volume_structure'][0]['volume_total_score'] = int(
  363. new_test['volume_structure'][0]['volume_total_score']) % 100
  364. elif new_test['volume_structure'] == -1 and new_test['Score_structure'] != -1 and int(
  365. new_test['Score_structure'][0]['item_total_score']) > 200: # 暂定试卷分数都在200以内,超过200的表示识别错误
  366. new_test['Score_structure'][0]['item_total_score'] = int(
  367. new_test['Score_structure'][0]['item_total_score']) % 100
  368. all_test.append(new_test)
  369. else:
  370. if test['volume_structure'] != -1 and int(
  371. test['volume_structure'][0]['volume_total_score']) > 200: # 暂定试卷分数都在200以内,超过200的表示识别错误
  372. test['volume_structure'][0]['volume_total_score'] = int(
  373. test['volume_structure'][0]['volume_total_score']) % 100
  374. elif test['volume_structure'] == -1 and test['Score_structure'] != -1 and int(
  375. test['Score_structure'][0]['item_total_score']) > 200: # 暂定试卷分数都在200以内,超过200的表示识别错误
  376. test['Score_structure'][0]['item_total_score'] = int(
  377. test['Score_structure'][0]['item_total_score']) % 100
  378. all_test.append(test)
  379. for jjjj in range(len(all_test)):
  380. if all_test[jjjj]['Score_structure'] != -1:
  381. label_1 = all_test[jjjj]['Score_structure'][0]['label']
  382. if label_1 == 'choice':
  383. if choice_boxs.count(all_test[jjjj]['Score_structure'][0]['bounding_box']):
  384. choice_boxs.remove(all_test[jjjj]['Score_structure'][0]['bounding_box'])
  385. elif label_1 == 'cloze':
  386. if cloze_boxs.count(all_test[jjjj]['Score_structure'][0]['bounding_box']):
  387. cloze_boxs.remove(all_test[jjjj]['Score_structure'][0]['bounding_box'])
  388. elif label_1 == 'solve':
  389. if solve_boxs.count(all_test[jjjj]['Score_structure'][0]['bounding_box']):
  390. solve_boxs.remove(all_test[jjjj]['Score_structure'][0]['bounding_box'])
  391. elif label_1 == 'composition':
  392. if solve_boxs.count(all_test[jjjj]['Score_structure'][0]['bounding_box']):
  393. solve_boxs.remove(all_test[jjjj]['Score_structure'][0]['bounding_box'])
  394. if choice_boxs != []: # 9月16号修改
  395. for ij in range(len(choice_boxs)):
  396. if choice_boxs[ij][1] - 150 > 0:
  397. yminss = choice_boxs[ij][1] - 150
  398. else:
  399. yminss = choice_boxs[ij][1]
  400. if choice_boxs[ij][0] - 100 > 0:
  401. xminss = choice_boxs[ij][0] - 100
  402. else:
  403. xminss = choice_boxs[ij][0]
  404. res1 = get_ocr_text_and_coordinate_in_google_format(
  405. img0[yminss:choice_boxs[ij][3], xminss:choice_boxs[ij][2]], ocr_accuracy=OCR_ACCURACY,
  406. language_type='CHN_ENG')
  407. aa = []
  408. type_score_dict_ocrs = {}
  409. for ii in range(len(res1['coordinates'])):
  410. xmin11 = res1['coordinates'][ii][0] + choice_boxs[ij][0]
  411. ymin11 = res1['coordinates'][ii][1] + choice_boxs[ij][1]
  412. xmax11 = res1['coordinates'][ii][2] + choice_boxs[ij][0]
  413. ymax11 = res1['coordinates'][ii][3] + choice_boxs[ij][1]
  414. aaa = (xmin11, ymin11, xmax11, ymax11)
  415. aa.append(aaa)
  416. res1['coordinates'] = aa
  417. # print(res1)
  418. if len(res1['words']) > 0:
  419. type_score_dict_ocrs['words'] = res1['words'][0]
  420. new_test = key_words(type_score_dict_ocrs)
  421. if new_test == {} or new_test['Score_structure'] == -1:
  422. if len(res1['words']) > 1:
  423. type_score_dict_ocrs['words'] = res1['words'][1]
  424. new_test = key_words(type_score_dict_ocrs)
  425. if new_test == {} or new_test['Score_structure'] == -1:
  426. if len(res1['words']) > 2:
  427. type_score_dict_ocrs['words'] = res1['words'][2]
  428. new_test = key_words(type_score_dict_ocrs)
  429. if new_test == {} or new_test['Score_structure'] == -1:
  430. if len(res1['words']) > 3:
  431. type_score_dict_ocrs['words'] = res1['words'][3]
  432. new_test = key_words(type_score_dict_ocrs)
  433. if new_test == {} or new_test['Score_structure'] == -1:
  434. if len(res1['words']) > 4:
  435. type_score_dict_ocrs['words'] = res1['words'][4]
  436. new_test = key_words(type_score_dict_ocrs)
  437. if new_test != {} and new_test['volume_structure'] != -1 and (int(new_test['volume_structure'][0]['volume_total_score']) > 4 or int(new_test['volume_structure'][0]['volume_score']) > 4): # 如果识别到分数,添加到输出信息;如果还没有识别到分数,默认没有分数
  438. if int(new_test['volume_structure'][0]['volume_total_score']) > 200: # 暂定试卷分数都在200以内,超过200的表示识别错误
  439. new_test['volume_structure'][0]['volume_total_score'] = int(
  440. new_test['volume_structure'][0]['volume_total_score']) % 100
  441. new_test['volume_structure'][0]['bounding_box'] = choice_boxs[ij]
  442. new_test['volume_structure'][0]['label'] = 'choice'
  443. all_test.append(new_test)
  444. elif new_test != {} and new_test['volume_structure'] == -1 and new_test['Score_structure'] != -1 and (int(new_test['Score_structure'][0]['item_total_score']) >4 or int(new_test['Score_structure'][0]['item_score']) > 4): # 如果识别到分数,添加到输出信息;如果还没有识别到分数,默认没有分数
  445. if int(new_test['Score_structure'][0]['item_total_score']) > 200: # 暂定试卷分数都在200以内,超过200的表示识别错误
  446. new_test['Score_structure'][0]['item_total_score'] = int(
  447. new_test['Score_structure'][0]['item_total_score']) % 100
  448. new_test['Score_structure'][0]['bounding_box'] = choice_boxs[ij]
  449. new_test['Score_structure'][0]['label'] = 'choice'
  450. all_test.append(new_test)
  451. if cloze_boxs != []:
  452. for ij in range(len(cloze_boxs)):
  453. if cloze_boxs[ij][1] - 100 > 0:
  454. yminss = cloze_boxs[ij][1] - 100
  455. else:
  456. yminss = cloze_boxs[ij][1]
  457. if cloze_boxs[ij][0] - 100 > 0:
  458. xminss = cloze_boxs[ij][0] - 100
  459. else:
  460. xminss = cloze_boxs[ij][0]
  461. res1 = get_ocr_text_and_coordinate_in_google_format(
  462. img0[yminss:cloze_boxs[ij][3], xminss:cloze_boxs[ij][2]], ocr_accuracy=OCR_ACCURACY,
  463. language_type='CHN_ENG')
  464. aa = []
  465. type_score_dict_ocrs = {}
  466. for ii in range(len(res1['coordinates'])):
  467. xmin11 = res1['coordinates'][ii][0] + cloze_boxs[ij][0]
  468. ymin11 = res1['coordinates'][ii][1] + cloze_boxs[ij][1]
  469. xmax11 = res1['coordinates'][ii][2] + cloze_boxs[ij][0]
  470. ymax11 = res1['coordinates'][ii][3] + cloze_boxs[ij][1]
  471. aaa = (xmin11, ymin11, xmax11, ymax11)
  472. aa.append(aaa)
  473. res1['coordinates'] = aa
  474. if len(res1['words']) > 0:
  475. type_score_dict_ocrs['words'] = res1['words'][0]
  476. new_test = key_words(type_score_dict_ocrs)
  477. if new_test == {} or new_test['Score_structure'] == -1:
  478. if len(res1['words']) > 1:
  479. type_score_dict_ocrs['words'] = res1['words'][1]
  480. new_test = key_words(type_score_dict_ocrs)
  481. if new_test == {} or new_test['Score_structure'] == -1:
  482. if len(res1['words']) > 2:
  483. type_score_dict_ocrs['words'] = res1['words'][2]
  484. new_test = key_words(type_score_dict_ocrs)
  485. if new_test == {} or new_test['Score_structure'] == -1:
  486. if len(res1['words']) > 3:
  487. type_score_dict_ocrs['words'] = res1['words'][3]
  488. new_test = key_words(type_score_dict_ocrs)
  489. if new_test == {} or new_test['Score_structure'] == -1:
  490. if len(res1['words']) > 4:
  491. type_score_dict_ocrs['words'] = res1['words'][4]
  492. new_test = key_words(type_score_dict_ocrs)
  493. if new_test != {} and new_test['volume_structure'] != -1 and (int(new_test['volume_structure'][0]['volume_total_score']) > 4 or int(new_test['volume_structure'][0]['volume_score']) > 4): # 如果识别到分数,添加到输出信息;如果还没有识别到分数,默认没有分数
  494. if int(new_test['volume_structure'][0]['volume_total_score']) > 200: # 暂定试卷分数都在200以内,超过200的表示识别错误
  495. new_test['volume_structure'][0]['volume_total_score'] = int(
  496. new_test['volume_structure'][0]['volume_total_score']) % 100
  497. new_test['volume_structure'][0]['bounding_box'] = cloze_boxs[ij]
  498. new_test['volume_structure'][0]['label'] = 'cloze'
  499. all_test.append(new_test)
  500. elif new_test != {} and new_test['volume_structure'] == -1 and new_test['Score_structure'] != -1 and (int(new_test['Score_structure'][0]['item_total_score']) > 4 or int(new_test['Score_structure'][0]['item_score']) > 4): # 如果识别到分数,添加到输出信息;如果还没有识别到分数,默认没有分数
  501. if int(new_test['Score_structure'][0]['item_total_score']) > 200: # 暂定试卷分数都在200以内,超过200的表示识别错误
  502. new_test['Score_structure'][0]['item_total_score'] = int(
  503. new_test['Score_structure'][0]['item_total_score']) % 100
  504. new_test['Score_structure'][0]['bounding_box'] = cloze_boxs[ij]
  505. new_test['Score_structure'][0]['label'] = 'cloze'
  506. all_test.append(new_test)
  507. if solve_boxs != []:
  508. for ij in range(len(solve_boxs)):
  509. yminss = solve_boxs[ij][1]
  510. xminss = solve_boxs[ij][0]
  511. res1 = get_ocr_text_and_coordinate_in_google_format(
  512. img0[yminss:solve_boxs[ij][3], xminss:solve_boxs[ij][2]], ocr_accuracy=OCR_ACCURACY,
  513. language_type='CHN_ENG')
  514. aa = []
  515. type_score_dict_ocrs = {}
  516. for ii in range(len(res1['coordinates'])):
  517. xmin11 = res1['coordinates'][ii][0] + solve_boxs[ij][0]
  518. ymin11 = res1['coordinates'][ii][1] + solve_boxs[ij][1]
  519. xmax11 = res1['coordinates'][ii][2] + solve_boxs[ij][0]
  520. ymax11 = res1['coordinates'][ii][3] + solve_boxs[ij][1]
  521. aaa = (xmin11, ymin11, xmax11, ymax11)
  522. aa.append(aaa)
  523. res1['coordinates'] = aa
  524. if len(res1['words']) > 0:
  525. type_score_dict_ocrs['words'] = res1['words'][0]
  526. new_test = key_words(type_score_dict_ocrs)
  527. if new_test == {} or new_test['Score_structure'] == -1:
  528. if len(res1['words']) > 1:
  529. type_score_dict_ocrs['words'] = res1['words'][1]
  530. new_test = key_words(type_score_dict_ocrs)
  531. if new_test == {} or new_test['Score_structure'] == -1:
  532. if len(res1['words']) > 2:
  533. type_score_dict_ocrs['words'] = res1['words'][2]
  534. new_test = key_words(type_score_dict_ocrs)
  535. if new_test == {} or new_test['Score_structure'] == -1:
  536. if len(res1['words']) > 3:
  537. type_score_dict_ocrs['words'] = res1['words'][3]
  538. new_test = key_words(type_score_dict_ocrs)
  539. if new_test == {} or new_test['Score_structure'] == -1:
  540. if len(res1['words']) > 4:
  541. type_score_dict_ocrs['words'] = res1['words'][4]
  542. new_test = key_words(type_score_dict_ocrs)
  543. if new_test != {} and new_test['volume_structure'] != -1 and int(new_test['volume_structure'][0]['volume_total_score']) > 5: # 如果识别到分数,添加到输出信息;如果还没有识别到分数,默认没有分数
  544. if int(new_test['volume_structure'][0]['volume_total_score']) > 200: # 暂定试卷分数都在200以内,超过200的表示识别错误
  545. new_test['volume_structure'][0]['volume_total_score'] = int(
  546. new_test['volume_structure'][0]['volume_total_score']) % 100
  547. new_test['volume_structure'][0]['bounding_box'] = solve_boxs[ij]
  548. new_test['volume_structure'][0]['label'] = 'solve'
  549. all_test.append(new_test)
  550. elif new_test != {} and new_test['volume_structure'] == -1 and new_test['Score_structure'] != -1 and (int(new_test['Score_structure'][0]['item_total_score']) > 5 or int(new_test['Score_structure'][0]['item_total_score']) == -1): # 如果识别到分数,添加到输出信息;如果还没有识别到分数,默认没有分数
  551. if int(new_test['Score_structure'][0]['item_total_score']) > 200: # 暂定试卷分数都在200以内,超过200的表示识别错误
  552. new_test['Score_structure'][0]['item_total_score'] = int(
  553. new_test['Score_structure'][0]['item_total_score']) % 100
  554. new_test['Score_structure'][0]['bounding_box'] = solve_boxs[ij]
  555. new_test['Score_structure'][0]['label'] = 'solve'
  556. all_test.append(new_test)
  557. if composition_boxs != []:
  558. for ij in range(len(composition_boxs)):
  559. if composition_boxs[ij][1] - 250 > 0:
  560. yminss = composition_boxs[ij][1] - 250
  561. else:
  562. yminss = composition_boxs[ij][1]
  563. if composition_boxs[ij][0] - 100 > 0:
  564. xminss = composition_boxs[ij][0] - 100
  565. else:
  566. xminss = composition_boxs[ij][0]
  567. res1 = get_ocr_text_and_coordinate_in_google_format(
  568. img0[yminss:composition_boxs[ij][3], xminss:composition_boxs[ij][2]], ocr_accuracy=OCR_ACCURACY,
  569. language_type='CHN_ENG')
  570. aa = []
  571. type_score_dict_ocrs = {}
  572. for ii in range(len(res1['coordinates'])):
  573. xmin11 = res1['coordinates'][ii][0] + composition_boxs[ij][0]
  574. ymin11 = res1['coordinates'][ii][1] + composition_boxs[ij][1]
  575. xmax11 = res1['coordinates'][ii][2] + composition_boxs[ij][0]
  576. ymax11 = res1['coordinates'][ii][3] + composition_boxs[ij][1]
  577. aaa = (xmin11, ymin11, xmax11, ymax11)
  578. aa.append(aaa)
  579. res1['coordinates'] = aa
  580. if len(res1['words']) > 0:
  581. type_score_dict_ocrs['words'] = res1['words'][0]
  582. new_test = key_words(type_score_dict_ocrs)
  583. if new_test == {} or new_test['Score_structure'] == -1:
  584. if len(res1['words']) > 1:
  585. type_score_dict_ocrs['words'] = res1['words'][1]
  586. new_test = key_words(type_score_dict_ocrs)
  587. if new_test == {} or new_test['Score_structure'] == -1:
  588. if len(res1['words']) > 2:
  589. type_score_dict_ocrs['words'] = res1['words'][2]
  590. new_test = key_words(type_score_dict_ocrs)
  591. if new_test == {} or new_test['Score_structure'] == -1:
  592. if len(res1['words']) > 3:
  593. type_score_dict_ocrs['words'] = res1['words'][3]
  594. new_test = key_words(type_score_dict_ocrs)
  595. if new_test == {} or new_test['Score_structure'] == -1:
  596. if len(res1['words']) > 4:
  597. type_score_dict_ocrs['words'] = res1['words'][4]
  598. new_test = key_words(type_score_dict_ocrs)
  599. if new_test != {} and new_test['volume_structure'] != -1 and int(
  600. new_test['volume_structure'][0]['volume_total_score']) > 4: # 如果识别到分数,添加到输出信息;如果还没有识别到分数,默认没有分数
  601. if int(new_test['volume_structure'][0]['volume_total_score']) > 200: # 暂定试卷分数都在200以内,超过200的表示识别错误
  602. new_test['volume_structure'][0]['volume_total_score'] = int(
  603. new_test['volume_structure'][0]['volume_total_score']) % 100
  604. new_test['volume_structure'][0]['bounding_box'] = composition_boxs[ij]
  605. new_test['volume_structure'][0]['label'] = 'composition'
  606. all_test.append(new_test)
  607. elif new_test != {} and new_test['volume_structure'] == -1 and new_test[
  608. 'Score_structure'] != -1 and int(
  609. new_test['Score_structure'][0]['item_total_score']) > 4: # 如果识别到分数,添加到输出信息;如果还没有识别到分数,默认没有分数
  610. if int(new_test['Score_structure'][0]['item_total_score']) > 200: # 暂定试卷分数都在200以内,超过200的表示识别错误
  611. new_test['Score_structure'][0]['item_total_score'] = int(
  612. new_test['Score_structure'][0]['item_total_score']) % 100
  613. new_test['Score_structure'][0]['bounding_box'] = composition_boxs[ij]
  614. new_test['Score_structure'][0]['label'] = 'composition'
  615. all_test.append(new_test)
  616. for aaa in range(len(all_test)):
  617. if all_test[aaa]['Score_structure'] != -1 and all_test[aaa]['volume_structure'] == -1:
  618. score_last_one = {'model_box': dict(all_test[aaa])['Score_structure'][0]['bounding_box'],
  619. 'label': dict(all_test[aaa])['Score_structure'][0]['label'],
  620. 'number': dict(all_test[aaa])['Score_structure'][0]['item_N'],
  621. 'score': dict(all_test[aaa])['Score_structure'][0]['item_total_score'],
  622. 'number_score': dict(all_test[aaa])['Score_structure'][0]['item_score'],
  623. 'counts': dict(all_test[aaa])['Score_structure'][0]['item_count']}
  624. Score_last.append(score_last_one)
  625. continue
  626. elif all_test[aaa]['Score_structure'] != -1 and all_test[aaa]['volume_structure'] != -1:
  627. score_last_one = {'model_box': dict(all_test[aaa])['Score_structure'][0]['bounding_box'],
  628. 'label': dict(all_test[aaa])['Score_structure'][0]['label'],
  629. 'number': -1,
  630. 'score': dict(all_test[aaa])['Score_structure'][0]['volume_total_score'],
  631. 'number_score': dict(all_test[aaa])['Score_structure'][0]['volume_score'],
  632. 'counts': dict(all_test[aaa])['Score_structure'][0]['volume_count']}
  633. Score_last.append(score_last_one)
  634. volume_last_one = {'volume_N': dict(all_test[aaa])['volume_structure'][0]['volume_N'],
  635. 'volume_total_score': dict(all_test[aaa])['volume_structure'][0]['volume_total_score'],
  636. 'volume_count': dict(all_test[aaa])['volume_structure'][0]['volume_count'],
  637. 'volume_score': dict(all_test[aaa])['volume_structure'][0]['volume_score'],
  638. 'keyword_type': dict(all_test[aaa])['volume_structure'][0]['keyword_type']}
  639. volume_last.append(volume_last_one)
  640. continue
  641. elif all_test[aaa]['volume_structure'] != -1:
  642. volume_last_one = {'volume_N': dict(all_test[aaa])['volume_structure'][0]['volume_N'],
  643. 'volume_total_score': dict(all_test[aaa])['volume_structure'][0]['volume_total_score'],
  644. 'volume_count': dict(all_test[aaa])['volume_structure'][0]['volume_count'],
  645. 'volume_score': dict(all_test[aaa])['volume_structure'][0]['volume_score'],
  646. 'keyword_type': dict(all_test[aaa])['volume_structure'][0]['keyword_type']}
  647. volume_last.append(volume_last_one)
  648. continue
  649. # Score_last = sorted(Score_last, key=lambda x: (
  650. # x['model_box'][0], x['model_box'][0] + x['model_box'][1], -x['score'])) # 按答题卡顺序输出
  651. len_Score_last = len(Score_last)
  652. for i in range(len_Score_last): # 去重一个边框可能对应多个type_score的情况
  653. if Score_last[i]['model_box'] in model_box2:
  654. index2 = model_box2.index(Score_last[i]['model_box'])
  655. score = Score_last[i]['score']
  656. if score < score2[index2] and score2[index2] < 30: # 去重,type_score多余的包含小题分数
  657. Score_last[i] = -1
  658. elif score < score2[index2] and score2[index2] > 30: # 去重,type_score多余的包含分卷分数
  659. Score_last[index2] = -1
  660. elif score > score2[index2] and score < 30: # 去重,type_score在不大于30分的情况下,暂定保留更大的分数
  661. Score_last[index2] = -1
  662. elif score > score2[index2] and score > 30: # 去重,type_score去除大于30分的重复分数
  663. Score_last[i] = -1
  664. else:
  665. Score_last[i] = -1
  666. else:
  667. model_box2.append(Score_last[i]['model_box'])
  668. score2.append(Score_last[i]['score'])
  669. while num_redundance < len_Score_last: # 去重一个边框可能对应多个type_score的情况
  670. if Score_last[num_redundance] == -1:
  671. del Score_last[num_redundance]
  672. len_Score_last = len_Score_last - 1
  673. else:
  674. num_redundance = num_redundance + 1
  675. # print(Score_last)
  676. # print(volume_last) # 分卷信息,暂不输出
  677. # print(answer_sheet['regions'])
  678. if Score_last != []:
  679. for i in range(len(Score_last)):
  680. if type(Score_last[i]['number']) is list:
  681. C_q_s = len(Score_last[i]['number']) * [Score_last[i]['score']]
  682. Score_last[i]['score'] = C_q_s
  683. if Score_last != []:
  684. if num_choice == 1 or num_cloze == 1: # 对应choice_m分数
  685. for i in range(len(Score_last)):
  686. if Score_last[i]['label'] == 'choice':
  687. count_choice_m = 0
  688. for j in range(len(answer_sheet['regions'])):
  689. if answer_sheet['regions'][j]['class_name'] == 'choice_m':
  690. if Score_last[i]['number_score'] != -1 and ('number' in answer_sheet['regions'][j].keys()):
  691. answer_sheet['regions'][j]['default_points'] = len(answer_sheet['regions'][j]['number']) * [float(Score_last[i]['number_score'])]
  692. elif Score_last[i]['number_score'] == -1 and Score_last[i]['score'] != -1 and 'number' in answer_sheet['regions'][j].keys():
  693. count_choice_m = count_choice_m + len(answer_sheet['regions'][j]['number'])
  694. j_temp.append(j)
  695. if j == len(answer_sheet['regions']) - 1 and j_temp !=[]:
  696. for index, jj in enumerate(j_temp):
  697. num_score_m = round(float(Score_last[i]['score'] / count_choice_m),1)
  698. answer_sheet['regions'][jj]['default_points'] = len(answer_sheet['regions'][jj]['number']) * [num_score_m]
  699. break
  700. elif Score_last[i]['label'] == 'cloze':
  701. count_cloze_s = 0
  702. for j in range(len(answer_sheet['regions'])):
  703. if answer_sheet['regions'][j]['class_name'] == 'cloze_s':
  704. if Score_last[i]['number_score'] != -1:
  705. answer_sheet['regions'][j]['default_points'] = Score_last[i]['number_score']
  706. else:
  707. count_cloze_s = count_cloze_s + 1
  708. jj_temp.append(j)
  709. if j == len(answer_sheet['regions']) - 1 and jj_temp !=[] and Score_last[i]['score']!=-1:
  710. for index, jj in enumerate(jj_temp):
  711. num_score_m = round(float(Score_last[i]['score'] / count_cloze_s),1)
  712. answer_sheet['regions'][jj]['default_points'] = num_score_m
  713. break
  714. elif num_choice > 1 or num_cloze >1:
  715. for i in range(len(Score_last)):
  716. if Score_last[i]['label'] == 'choice':
  717. count_choice_m = 0
  718. for j in range(len(answer_sheet['regions'])):
  719. if answer_sheet['regions'][j]['class_name'] == 'choice_m':
  720. xmin_dis = answer_sheet['regions'][j]['bounding_box']['xmin'] - \
  721. Score_last[i]['model_box'][0]
  722. ymin_dis = answer_sheet['regions'][j]['bounding_box']['ymin'] - \
  723. Score_last[i]['model_box'][1]
  724. xmax_dis = answer_sheet['regions'][j]['bounding_box']['xmax'] - \
  725. Score_last[i]['model_box'][2]
  726. ymax_dis = answer_sheet['regions'][j]['bounding_box']['ymax'] - \
  727. Score_last[i]['model_box'][3]
  728. if xmin_dis > -30 and ymin_dis > -30 and xmax_dis < 30 and ymax_dis < 30:
  729. if Score_last[i]['number_score'] != -1 and 'number' in answer_sheet['regions'][j].keys():
  730. answer_sheet['regions'][j]['default_points'] = len(answer_sheet['regions'][j]['number']) * [float(Score_last[i]['number_score'])]
  731. elif Score_last[i]['number_score'] == -1 and Score_last[i]['score'] != -1 and 'number' in answer_sheet['regions'][j].keys():
  732. count_choice_m = count_choice_m + len(answer_sheet['regions'][j]['number'])
  733. j_temp.append(j)
  734. if j == len(answer_sheet['regions']) - 1 and j_temp !=[]:
  735. for index ,jj in enumerate(j_temp):
  736. num_score_m = round(float(Score_last[i]['score'] / count_choice_m),1)
  737. answer_sheet['regions'][jj]['default_points'] = len(answer_sheet['regions'][jj]['number']) * [num_score_m]
  738. break
  739. elif Score_last[i]['label'] == 'cloze':
  740. count_cloze_s = 0
  741. for j in range(len(answer_sheet['regions'])):
  742. if answer_sheet['regions'][j]['class_name'] == 'cloze_s':
  743. xmin_dis = answer_sheet['regions'][j]['bounding_box']['xmin'] - \
  744. Score_last[i]['model_box'][0]
  745. ymin_dis = answer_sheet['regions'][j]['bounding_box']['ymin'] - \
  746. Score_last[i]['model_box'][1]
  747. xmax_dis = answer_sheet['regions'][j]['bounding_box']['xmax'] - \
  748. Score_last[i]['model_box'][2]
  749. ymax_dis = answer_sheet['regions'][j]['bounding_box']['ymax'] - \
  750. Score_last[i]['model_box'][3]
  751. if xmin_dis > -30 and ymin_dis > -30 and xmax_dis < 30 and ymax_dis < 30:
  752. if Score_last[i]['number_score'] != -1 :
  753. answer_sheet['regions'][j]['default_points'] = Score_last[i]['number_score']
  754. elif Score_last[i]['number_score'] == -1 and Score_last[i]['score'] != -1 :
  755. count_cloze_s = count_cloze_s + 1
  756. jj_temp.append(j)
  757. if j == len(answer_sheet['regions']) - 1 and jj_temp !=[]:
  758. for index ,jj in enumerate(jj_temp):
  759. num_score_m = round(float(Score_last[i]['score'] / count_cloze_s),1)
  760. answer_sheet['regions'][jj]['default_points'] = len(answer_sheet['regions'][jj]['number']) * [num_score_m]
  761. break
  762. if Score_last != []:
  763. for i in range(len(answer_sheet['regions'])):
  764. for j in range(len(Score_last)):
  765. if (Score_last[j]['model_box'][0] == answer_sheet['regions'][i]['bounding_box']['xmin']
  766. and Score_last[j]['model_box'][1] == answer_sheet['regions'][i]['bounding_box']['ymin']
  767. and Score_last[j]['model_box'][2] == answer_sheet['regions'][i]['bounding_box']['xmax']
  768. and Score_last[j]['model_box'][3] == answer_sheet['regions'][i]['bounding_box']['ymax']):
  769. if Score_last[j]['number'] != -1:
  770. answer_sheet['regions'][i]['number'] = Score_last[j]['number'] # 题号
  771. if Score_last[j]['score'] != -1:
  772. # score = Score_last[j]['score']
  773. # try:
  774. # length = len(answer_sheet['regions'][i]['number'])
  775. # answer_sheet['regions'][i]['default_points'] = length * [score]
  776. # except Exception:
  777. # answer_sheet['regions'][i]['default_points'] = score
  778. answer_sheet['regions'][i]['default_points'] = Score_last[j]['score']
  779. if type(answer_sheet['regions'][i]['default_points']) is list and (
  780. answer_sheet['regions'][i]['class_name'] == 'solve' or answer_sheet['regions'][i][
  781. 'class_name'] == 'solve0'):
  782. answer_sheet['regions'][i]['class_name'] = 'optional_solve'
  783. # answer_sheet['regions'][i]['number_score'] = Score_last[j]['number_score'] # 小题分数
  784. # answer_sheet['regions'][i]['counts'] = Score_last[j]['counts'] # 小题个数
  785. return answer_sheet