choice_line_box.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. # @Author : lightXu
  2. # @File : choice_line_box.py
  3. # @Time : 2018/11/22 0022 下午 16:01
  4. import time
  5. import re
  6. import cv2, os
  7. import numpy as np
  8. import xml.etree.cElementTree as ET
  9. from segment.sheet_resolve.tools.brain_api import get_ocr_text_and_coordinate
  10. from segment.sheet_resolve.tools import tf_settings, utils
  11. from segment.sheet_resolve.analysis.choice.choice_m_row_column import get_choice_m_row_and_col
  12. from . import get_title_number_by_choice_m
  13. def get_interval(word_result_list):
  14. all_char_str = ''
  15. location = []
  16. for i, chars_dict in enumerate(word_result_list):
  17. chars_list = chars_dict['chars']
  18. for ele in chars_list:
  19. all_char_str = all_char_str + ele['char']
  20. location.append(ele['location'])
  21. pattern1 = re.compile(r"\]\[")
  22. pattern2 = re.compile(r"\[[ABCD]")
  23. def intervel(pattern):
  24. group_list = []
  25. for i in pattern.finditer(all_char_str):
  26. # print(i.group() + str(i.span()))
  27. group_list.append(list(i.span()))
  28. # print(group_list)
  29. sum_intervel = 0
  30. size = 0
  31. for group in group_list:
  32. left_x, right_x = location[group[0]]['left'] \
  33. + location[group[0]]['width'], location[group[1] - 1]['left']
  34. if abs(location[group[0]]['top'] - location[group[1]]['top']) < location[group[0]]['height']:
  35. if right_x - left_x > 0:
  36. sum_intervel = sum_intervel + right_x - left_x
  37. size += 1
  38. # print(sum_intervel // size)
  39. return sum_intervel // size
  40. intervel_width1 = intervel(pattern1)
  41. intervel_width2 = intervel(pattern2)
  42. return (intervel_width1 + intervel_width2) * 2 // 3
  43. def preprocess(image0, xe, ye):
  44. scale = 0
  45. dilate = 1
  46. blur = 5
  47. # 预处理图像
  48. img = image0
  49. # rescale the image
  50. if scale != 0:
  51. img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
  52. # Convert to gray
  53. img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  54. # # Apply dilation and erosion to remove some noise
  55. # if dilate != 0:
  56. # kernel = np.ones((dilate, dilate), np.uint8)
  57. # img = cv2.dilate(img, kernel, iterations=1)
  58. # img = cv2.erode(img, kernel, iterations=1)
  59. # Apply blur to smooth out the edges
  60. # if blur != 0:
  61. # img = cv2.GaussianBlur(img, (blur, blur), 0)
  62. # Apply threshold to get image with only b&w (binarization)
  63. img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
  64. # cv2.namedWindow('image', cv2.WINDOW_NORMAL)
  65. # cv2.imshow('image', img)
  66. # if cv2.waitKey(0) == 27:
  67. # cv2.destroyAllWindows()
  68. # cv2.imwrite('otsu.jpg', img)
  69. kernel = np.ones((ye, xe), np.uint8) # y轴膨胀, x轴膨胀
  70. dst = cv2.dilate(img, kernel, iterations=1)
  71. # cv2.imshow('dilate', dst)
  72. # if cv2.waitKey(0) == 27:
  73. # cv2.destroyAllWindows()
  74. return dst
  75. def contours(image):
  76. _, cnts, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  77. bboxes = []
  78. for cnt_id, cnt in enumerate(reversed(cnts)):
  79. x, y, w, h = cv2.boundingRect(cnt)
  80. bboxes.append((x, y, x + w, y + h))
  81. return bboxes
  82. def box_coordinates(img):
  83. img_arr = np.asarray(img)
  84. def axis_break_point(img, tolerance_number, axis):
  85. sum_x_axis = img.sum(axis=axis)
  86. sum_x_axis[sum_x_axis > 255 * tolerance_number] = 1 # 白色有字
  87. sum_x_axis[sum_x_axis != 1] = 0 # 黑色无字
  88. sum_x_axis_list = list(sum_x_axis)
  89. sum_x_axis_list.append(0) # 最后几行到结束有字时,使索引值增加最后一位
  90. split_x_index = []
  91. num = 1
  92. for index, ele in enumerate(sum_x_axis_list):
  93. num = num % 2
  94. if ele == num:
  95. # print(i)
  96. num = num + 1
  97. split_x_index.append(index)
  98. # print('length: ', len(split_x_index), split_x_index)
  99. return split_x_index
  100. y_break_points_list = axis_break_point(img_arr, 1, axis=1)
  101. x_break_points_list = axis_break_point(img_arr, 1, axis=0)
  102. all_coordinates = []
  103. for i in range(0, len(y_break_points_list), 2): # y轴分组
  104. ymin = y_break_points_list[i]
  105. ymax = y_break_points_list[i + 1]
  106. for j in range(0, len(x_break_points_list), 2):
  107. xmin = x_break_points_list[j]
  108. xmax = x_break_points_list[j + 1]
  109. all_coordinates.append([xmin, ymin, xmax, ymax])
  110. return all_coordinates
  111. def get_choice_box_coordinate(left, top, word_result_list, choice_img, cv_box_list, choice_bbox_list):
  112. shape = choice_img.shape
  113. y, x = shape[0], shape[1]
  114. # cv2.imshow('ocr_region', ocr_region)
  115. # if cv2.waitKey(0) == 27:
  116. # cv2.destroyAllWindows()
  117. all_digital_list = []
  118. digital_model = re.compile(r'\d')
  119. for i, chars_dict in enumerate(word_result_list):
  120. chars_list = chars_dict['chars']
  121. for ele in chars_list:
  122. if digital_model.search(ele['char']):
  123. all_digital_list.append(ele)
  124. new_all_digital_list = []
  125. i = 1
  126. while i <= len(all_digital_list):
  127. pre_one = all_digital_list[i - 1]
  128. if i == len(all_digital_list):
  129. new_all_digital_list.append(pre_one)
  130. break
  131. rear_one = all_digital_list[i]
  132. condition1 = abs(pre_one['location']['top'] - rear_one['location']['top']) < pre_one['location'][
  133. 'height'] # 两字高度差小于一字高度
  134. condition2 = pre_one['location']['left'] + 2 * pre_one['location']['width'] > rear_one['location'][
  135. 'left'] # 某字宽度的2倍大于两字间间隔
  136. if condition1:
  137. if condition2:
  138. new_char = pre_one['char'] + rear_one['char']
  139. new_location = {'left': pre_one['location']['left'],
  140. 'top': min(pre_one['location']['top'], rear_one['location']['top']),
  141. 'width': rear_one['location']['left'] + rear_one['location']['width'] -
  142. pre_one['location']['left'],
  143. 'height': max(pre_one['location']['height'], rear_one['location']['height'])}
  144. new_all_digital_list.append({'char': new_char, 'location': new_location})
  145. i = i + 1 + 1
  146. else:
  147. new_all_digital_list.append(pre_one)
  148. i = i + 1
  149. else:
  150. new_all_digital_list.append(pre_one) # 遇到字符y轴相差过大就结束
  151. i = i + 1
  152. content_list = list()
  153. for index, box in enumerate(choice_bbox_list['regions']): # rcnn识别的框匹配题号
  154. box = box['bounding_box']
  155. box_coordiante = (box['xmin'], box['ymin'], box['xmax'], box['ymax'])
  156. horizontal = box['xmax'] - box['xmin'] >= box['ymax'] - box['ymin']
  157. vertical = box['xmax'] - box['xmin'] < box['ymax'] - box['ymin']
  158. choice_number = {'number': 999, 'location': box_coordiante}
  159. content_list.insert(index, choice_number)
  160. for digital in new_all_digital_list:
  161. digital_coordiante = (digital['location']['left'], digital['location']['top'],
  162. digital['location']['left'] + digital['location']['width'],
  163. digital['location']['top'] + digital['location']['height'])
  164. if utils.decide_coordinate_contains(digital_coordiante, box_coordiante):
  165. if horizontal:
  166. box['xmin'] = digital['location']['left'] + digital['location']['width'] + 1 # 从数字处截取
  167. if vertical:
  168. box['ymin'] = digital['location']['top'] + digital['location']['height'] + 1
  169. box_coordiante = (box['xmin'], box['ymin'], box['xmax'], box['ymax'])
  170. content_list[index]['number'] = int(digital['char'])
  171. content_list[index]['location'] = box_coordiante
  172. break
  173. a_z = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  174. for box in content_list:
  175. box_coordiante = (box['location'][0], box['location'][1], box['location'][2], box['location'][3])
  176. mtx = []
  177. for cv_box in cv_box_list:
  178. if utils.decide_coordinate_contains(cv_box, box_coordiante): # 若fasterrcnn未识别到选项框,单独的ABCD也舍去
  179. mtx.append(cv_box)
  180. matrix = np.asarray(sorted(mtx))
  181. dif = matrix[1:, 0] - matrix[:-1, 2] # 后一个char的left与前一个char的right的差
  182. dif[dif < 0] = 0
  183. dif_length = np.mean(dif) # 小于平均间隔的合并
  184. block_list = utils.box_by_x_intervel(matrix, dif_length)
  185. # block_list = utils.box_by_x_intervel(matrix, 5)
  186. choice_count = len(block_list)
  187. choice_option = ','.join(list(a_z[:choice_count]))
  188. combine = np.asarray(sorted(block_list))
  189. min_temp = np.min(combine, axis=0)
  190. max_temp = np.max(combine, axis=0)
  191. abcd_coordinate = {'xmin': int(min_temp[0] + left), 'ymin': int(min_temp[1] + top),
  192. 'xmax': int(max_temp[2] + left), 'ymax': int(max_temp[3] + top)}
  193. single_height = np.mean(combine[:, 3] - combine[:, 1])
  194. single_width = np.mean(combine[:, 2] - combine[:, 0])
  195. box['location'] = abcd_coordinate
  196. box['single_height'] = int(single_height)
  197. box['single_width'] = int(single_width)
  198. box['default_points'] = 5
  199. # box['choice_option'] = choice_option
  200. box['choice_option'] = 'A,B,C,D'
  201. if max_temp[2] - min_temp[0] >= max_temp[3] - min_temp[1]: # x > y 横向
  202. box['row'] = 1
  203. box['column'] = choice_count
  204. else:
  205. box['row'] = choice_count
  206. box['column'] = 1
  207. return content_list
  208. def choice_line(left, top, image, choice_bbox_list, xml_path):
  209. a_z = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  210. t1 = time.time()
  211. word_result_list0 = get_ocr_text_and_coordinate(image, ocr_accuracy='accurate', language_type='ENG')
  212. # word_result_list0 = [{'chars': [{'char': '1', 'location': {'width': 25, 'top': 11, 'left': 63, 'height': 53}}, {'char': 'A', 'location': {'width': 23, 'top': 11, 'left': 142, 'height': 53}}, {'char': '.', 'location': {'width': 35, 'top': 11, 'left': 162, 'height': 53}}, {'char': '[', 'location': {'width': 17, 'top': 11, 'left': 197, 'height': 53}}, {'char': 'B', 'location': {'width': 28, 'top': 11, 'left': 213, 'height': 53}}, {'char': ']', 'location': {'width': 18, 'top': 11, 'left': 241, 'height': 53}}, {'char': 'C', 'location': {'width': 50, 'top': 11, 'left': 290, 'height': 53}}, {'char': 'D', 'location': {'width': 59, 'top': 11, 'left': 333, 'height': 53}}, {'char': ']', 'location': {'width': 34, 'top': 11, 'left': 389, 'height': 53}}, {'char': '5', 'location': {'width': 18, 'top': 11, 'left': 519, 'height': 53}}, {'char': 'A', 'location': {'width': 24, 'top': 11, 'left': 586, 'height': 53}}, {'char': 'T', 'location': {'width': 33, 'top': 11, 'left': 603, 'height': 53}}, {'char': '[', 'location': {'width': 17, 'top': 11, 'left': 650, 'height': 53}}, {'char': 'D', 'location': {'width': 25, 'top': 11, 'left': 812, 'height': 53}}, {'char': ']', 'location': {'width': 34, 'top': 11, 'left': 842, 'height': 53}}, {'char': '9', 'location': {'width': 28, 'top': 11, 'left': 963, 'height': 53}}, {'char': '[', 'location': {'width': 24, 'top': 11, 'left': 1022, 'height': 53}}, {'char': 'A', 'location': {'width': 26, 'top': 11, 'left': 1042, 'height': 53}}, {'char': '[', 'location': {'width': 25, 'top': 11, 'left': 1091, 'height': 53}}, {'char': 'B', 'location': {'width': 18, 'top': 11, 'left': 1121, 'height': 53}}, {'char': ']', 'location': {'width': 18, 'top': 11, 'left': 1139, 'height': 53}}, {'char': '[', 'location': {'width': 24, 'top': 11, 'left': 1171, 'height': 53}}, {'char': 'C', 'location': {'width': 25, 'top': 11, 'left': 1188, 'height': 53}}, {'char': 'H', 'location': {'width': 33, 'top': 11, 'left': 1206, 'height': 53}}, {'char': ']', 'location': {'width': 26, 'top': 11, 'left': 1288, 'height': 53}}], 'location': {'width': 1256, 'top': 9, 'left': 58, 'height': 57}, 'words': ' 1 A.[B] CD]5 AT[ D]9 [A [B] [CH]'}, {'chars': [{'char': '2', 'location': {'width': 25, 'top': 96, 'left': 61, 'height': 51}}, {'char': 'A', 'location': {'width': 24, 'top': 96, 'left': 139, 'height': 51}}, {'char': '[', 'location': {'width': 16, 'top': 96, 'left': 202, 'height': 51}}, {'char': 'B', 'location': {'width': 24, 'top': 96, 'left': 215, 'height': 51}}, {'char': '3', 'location': {'width': 33, 'top': 96, 'left': 233, 'height': 51}}, {'char': 'C', 'location': {'width': 25, 'top': 96, 'left': 292, 'height': 51}}, {'char': '[', 'location': {'width': 17, 'top': 96, 'left': 348, 'height': 51}}, {'char': 'D', 'location': {'width': 26, 'top': 96, 'left': 365, 'height': 51}}, {'char': ']', 'location': {'width': 35, 'top': 96, 'left': 390, 'height': 51}}, {'char': '6', 'location': {'width': 27, 'top': 96, 'left': 519, 'height': 51}}, {'char': 'A', 'location': {'width': 23, 'top': 96, 'left': 594, 'height': 51}}, {'char': ']', 'location': {'width': 35, 'top': 96, 'left': 614, 'height': 51}}, {'char': '[', 'location': {'width': 17, 'top': 96, 'left': 649, 'height': 51}}, {'char': 'B', 'location': {'width': 25, 'top': 96, 'left': 662, 'height': 51}}, {'char': 'I', 'location': {'width': 33, 'top': 96, 'left': 680, 'height': 51}}, {'char': '[', 'location': {'width': 16, 'top': 96, 'left': 727, 'height': 51}}, {'char': '[', 'location': {'width': 23, 'top': 96, 'left': 801, 'height': 51}}, {'char': 'D', 'location': {'width': 25, 'top': 96, 'left': 821, 'height': 51}}, {'char': ']', 'location': {'width': 17, 'top': 96, 'left': 847, 'height': 51}}, {'char': '1', 'location': {'width': 24, 'top': 96, 'left': 956, 'height': 51}}, {'char': '0', 'location': {'width': 33, 'top': 96, 'left': 973, 'height': 51}}, {'char': 'L', 'location': {'width': 20, 'top': 96, 'left': 1025, 'height': 51}}, {'char': 'A', 'location': {'width': 28, 'top': 96, 'left': 1038, 'height': 51}}, {'char': '[', 'location': {'width': 23, 'top': 96, 'left': 1103, 'height': 51}}, {'char': 'B', 'location': {'width': 26, 'top': 96, 'left': 1122, 'height': 51}}, {'char': ']', 'location': {'width': 10, 'top': 96, 'left': 1148, 'height': 51}}, {'char': 'I', 'location': {'width': 20, 'top': 96, 'left': 1180, 'height': 51}}, {'char': 'C', 'location': {'width': 24, 'top': 96, 'left': 1193, 'height': 51}}, {'char': 'I', 'location': {'width': 28, 'top': 96, 'left': 1211, 'height': 51}}, {'char': '[', 'location': {'width': 23, 'top': 96, 'left': 1248, 'height': 51}}, {'char': 'D', 'location': {'width': 27, 'top': 96, 'left': 1268, 'height': 51}}, {'char': ']', 'location': {'width': 17, 'top': 96, 'left': 1295, 'height': 51}}], 'location': {'width': 1255, 'top': 94, 'left': 57, 'height': 56}, 'words': ' 2 A[B3 C[D]6 A][BI[ [D]10 LA [B] ICI [D]'}, {'chars': [{'char': '3', 'location': {'width': 25, 'top': 178, 'left': 60, 'height': 53}}, {'char': '[', 'location': {'width': 25, 'top': 178, 'left': 122, 'height': 53}}, {'char': 'A', 'location': {'width': 18, 'top': 178, 'left': 143, 'height': 53}}, {'char': ']', 'location': {'width': 18, 'top': 178, 'left': 160, 'height': 53}}, {'char': '[', 'location': {'width': 17, 'top': 178, 'left': 197, 'height': 53}}, {'char': 'B', 'location': {'width': 27, 'top': 178, 'left': 214, 'height': 53}}, {'char': ']', 'location': {'width': 10, 'top': 178, 'left': 241, 'height': 53}}, {'char': 'C', 'location': {'width': 48, 'top': 178, 'left': 293, 'height': 53}}, {'char': 'D', 'location': {'width': 118, 'top': 178, 'left': 334, 'height': 53}}, {'char': '7', 'location': {'width': 97, 'top': 178, 'left': 445, 'height': 53}}, {'char': 'I', 'location': {'width': 21, 'top': 178, 'left': 572, 'height': 53}}, {'char': 'A', 'location': {'width': 30, 'top': 178, 'left': 585, 'height': 53}}, {'char': ']', 'location': {'width': 27, 'top': 178, 'left': 620, 'height': 53}}, {'char': '[', 'location': {'width': 18, 'top': 178, 'left': 647, 'height': 53}}, {'char': 'B', 'location': {'width': 25, 'top': 178, 'left': 662, 'height': 53}}, {'char': 'I', 'location': {'width': 39, 'top': 178, 'left': 680, 'height': 53}}, {'char': 'E', 'location': {'width': 34, 'top': 178, 'left': 711, 'height': 53}}, {'char': 'C', 'location': {'width': 25, 'top': 178, 'left': 738, 'height': 53}}, {'char': 'H', 'location': {'width': 29, 'top': 178, 'left': 756, 'height': 53}}, {'char': 'E', 'location': {'width': 21, 'top': 178, 'left': 795, 'height': 53}}, {'char': 'D', 'location': {'width': 29, 'top': 178, 'left': 809, 'height': 53}}, {'char': ']', 'location': {'width': 18, 'top': 178, 'left': 845, 'height': 53}}, {'char': '1', 'location': {'width': 20, 'top': 178, 'left': 958, 'height': 53}}, {'char': '1', 'location': {'width': 31, 'top': 178, 'left': 971, 'height': 53}}, {'char': '[', 'location': {'width': 25, 'top': 178, 'left': 1020, 'height': 53}}, {'char': 'A', 'location': {'width': 27, 'top': 178, 'left': 1041, 'height': 53}}, {'char': ']', 'location': {'width': 10, 'top': 178, 'left': 1068, 'height': 53}}, {'char': '[', 'location': {'width': 25, 'top': 178, 'left': 1101, 'height': 53}}, {'char': 'B', 'location': {'width': 18, 'top': 178, 'left': 1122, 'height': 53}}, {'char': ']', 'location': {'width': 18, 'top': 178, 'left': 1141, 'height': 53}}, {'char': '[', 'location': {'width': 24, 'top': 178, 'left': 1174, 'height': 53}}, {'char': 'C', 'location': {'width': 26, 'top': 178, 'left': 1191, 'height': 53}}, {'char': 'H', 'location': {'width': 35, 'top': 178, 'left': 1209, 'height': 53}}, {'char': 'L', 'location': {'width': 30, 'top': 178, 'left': 1236, 'height': 53}}, {'char': 'D', 'location': {'width': 29, 'top': 178, 'left': 1259, 'height': 53}}, {'char': ']', 'location': {'width': 18, 'top': 178, 'left': 1294, 'height': 53}}], 'location': {'width': 1257, 'top': 175, 'left': 55, 'height': 59}, 'words': ' 3 [A][B] CD7 IA][BIECH ED]11 [A] [B] [CHLD]'}, {'chars': [{'char': '4', 'location': {'width': 50, 'top': 262, 'left': 61, 'height': 53}}, {'char': 'A', 'location': {'width': 83, 'top': 262, 'left': 104, 'height': 53}}, {'char': 'B', 'location': {'width': 83, 'top': 262, 'left': 179, 'height': 53}}, {'char': 'C', 'location': {'width': 82, 'top': 262, 'left': 255, 'height': 53}}, {'char': 'D', 'location': {'width': 56, 'top': 262, 'left': 330, 'height': 53}}, {'char': ']', 'location': {'width': 36, 'top': 262, 'left': 391, 'height': 53}}, {'char': '8', 'location': {'width': 26, 'top': 262, 'left': 516, 'height': 53}}, {'char': 'A', 'location': {'width': 25, 'top': 262, 'left': 592, 'height': 53}}, {'char': '.', 'location': {'width': 35, 'top': 262, 'left': 693, 'height': 53}}, {'char': ']', 'location': {'width': 35, 'top': 262, 'left': 843, 'height': 53}}, {'char': '1', 'location': {'width': 20, 'top': 262, 'left': 955, 'height': 53}}, {'char': '2', 'location': {'width': 30, 'top': 262, 'left': 968, 'height': 53}}, {'char': 'L', 'location': {'width': 20, 'top': 262, 'left': 1018, 'height': 53}}, {'char': 'A', 'location': {'width': 29, 'top': 262, 'left': 1031, 'height': 53}}, {'char': '[', 'location': {'width': 18, 'top': 262, 'left': 1101, 'height': 53}}, {'char': 'I', 'location': {'width': 25, 'top': 262, 'left': 1239, 'height': 53}}, {'char': 'D', 'location': {'width': 34, 'top': 262, 'left': 1257, 'height': 53}}, {'char': 'T', 'location': {'width': 30, 'top': 262, 'left': 1284, 'height': 53}}], 'location': {'width': 1258, 'top': 259, 'left': 56, 'height': 58}, 'words': ' 4ABCD]8 A.]12 LA[ IDT'}]
  213. t2 = time.time()
  214. print('choice ocr time cost: ', t2 - t1)
  215. # print(word_result_list0)
  216. # try:
  217. # intervel_x = get_interval(word_result_list0)
  218. # except Exception:
  219. # intervel_x = 15
  220. intervel_x = 3
  221. img = preprocess(image, intervel_x, 3)
  222. cv_box_list0 = box_coordinates(img)
  223. content_list = get_choice_box_coordinate(left, top, word_result_list0, image, cv_box_list0, choice_bbox_list)
  224. tree = ET.parse(xml_path) # xml tree
  225. for index_num, choice_box in enumerate(content_list):
  226. abcd = choice_box['location']
  227. number = str(choice_box['number'])
  228. tree = utils.create_xml(number, tree, abcd['xmin'], abcd['ymin'], abcd['xmax'], abcd['ymax'])
  229. tree.write(xml_path)
  230. return content_list
  231. def choice_m_row_col0(left, top, image, choice_bbox_list, xml_path, img, choice_box):
  232. a_z = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  233. t1 = time.time()
  234. tree = ET.parse(xml_path) # xml tree
  235. choice_m_dict_list = []
  236. for index0, box in enumerate(choice_bbox_list['regions']): # rcnn识别的框匹配题号
  237. box = box['bounding_box']
  238. m_left, m_top = box['xmin']+left, box['ymin']+top,
  239. box_coordiante = (m_left, m_top, box['xmax']+left, box['ymax']+top)
  240. tree = utils.create_xml('choice_m', tree,
  241. box_coordiante[0], box_coordiante[1],
  242. box_coordiante[2], box_coordiante[3])
  243. single_choice_m = utils.crop_region(image, box)
  244. row_col_dict = get_choice_m_row_and_col(m_left, m_top, single_choice_m) # 所有的小框, 行列等
  245. if len(row_col_dict) > 0:
  246. # single choice_m ocr result
  247. res_dict = get_ocr_text_and_coordinate(single_choice_m, ocr_accuracy='accurate', language_type='CHN_ENG')
  248. choice_m_cal_num = [0]
  249. for index, ele in enumerate(res_dict):
  250. words = ele['words']
  251. location = ele['location']
  252. width = int(location['width'])
  253. height = int(location['height'])
  254. if width > height:
  255. abcd_str = 'ABD'
  256. cal_num = max([words.count(char) for char in abcd_str])
  257. choice_m_cal_num.append(cal_num)
  258. max_num = max(choice_m_cal_num)
  259. if max_num < 2:
  260. direction = 180
  261. choice_option = a_z[:row_col_dict['cols']]
  262. else:
  263. direction = 90
  264. choice_option = a_z[:row_col_dict['rows']]
  265. row_col_dict.update({'direction': direction, 'option': choice_option})
  266. choice_m_dict_list.append(row_col_dict)
  267. title_number_by_choice_m_list = get_title_number_by_choice_m.get_title_number(row_col_dict, choice_bbox_list, img, choice_box) # choice_m相对坐标 list
  268. for element in title_number_by_choice_m_list:
  269. if row_col_dict['bounding_box'] == element['bounding_box']:
  270. row_col_dict.update({'title_number': element['title_number']})
  271. for index_num, choice_box in enumerate(choice_m_dict_list):
  272. if len(choice_box['bounding_box']) > 0:
  273. abcd = choice_box['bounding_box']
  274. name = '{}_{}*{}_{}'.format('choice_m', choice_box['rows'], choice_box['cols'], choice_box['direction'])
  275. tree = utils.create_xml(name, tree,
  276. abcd['xmin'], abcd['ymin'],
  277. abcd['xmax'], abcd['ymax'])
  278. tree.write(xml_path)
  279. return choice_m_dict_list
  280. def choice_bbox_vague(choice_m_list0, x_y_interval_ave, singe_box_width_height_ave, direction, image_size):
  281. img_width = image_size[0]
  282. choice_list_temp1 = []
  283. choice_list_temp2 = []
  284. index_list1 = []
  285. index_list2 = []
  286. for index, ele in enumerate(choice_m_list0):
  287. if ele[0] < img_width // 2:
  288. choice_list_temp1.append(ele)
  289. index_list1.append(index)
  290. else:
  291. choice_list_temp2.append(ele)
  292. index_list2.append(index)
  293. if index_list2 == []:
  294. choice_list = [[choice_list_temp1, index_list1]]
  295. else:
  296. choice_list = [[choice_list_temp1, index_list1]]
  297. choice_list.append([choice_list_temp2, index_list2])
  298. choice_bbox_all = []
  299. for choice_m_list1 in choice_list:
  300. choice_m_list = sorted(choice_m_list1[0], key=lambda k: k[0])
  301. xmin0 = [ele[0] for ele in choice_m_list]
  302. ymin0 = [ele[1] for ele in choice_m_list]
  303. xmax0 = [ele[2] for ele in choice_m_list]
  304. ymax0 = [ele[3] for ele in choice_m_list]
  305. if direction == 180: # vertical
  306. x_diff = x_y_interval_ave[0]
  307. s_width = singe_box_width_height_ave[1]
  308. choice_bbox = (np.hstack((np.array([min(xmin0) - x_diff - 3 * s_width, min(ymin0)]), np.array([max(xmax0), max(ymax0)])))).tolist()
  309. choice_bbox_with_index_list = (choice_bbox, choice_m_list1[1])
  310. choice_bbox_all.append(choice_bbox_with_index_list)
  311. elif direction == 90:
  312. y_diff = x_y_interval_ave[1]
  313. s_height = singe_box_width_height_ave[1]
  314. choice_bbox = (np.hstack((np.array([min(xmin0), min(ymin0) - y_diff - 3 * s_height]), np.array([max(xmax0), max(ymax0)])))).tolist()
  315. choice_bbox_with_index_list = (choice_bbox, choice_m_list[1])
  316. choice_bbox_all.append(choice_bbox_with_index_list)
  317. return choice_bbox_all
  318. def get_direction(words_res_dict, choice_m_mum=1):
  319. choice_m_cal_num = [0]
  320. for index, ele in enumerate(words_res_dict):
  321. words = ele['words']
  322. location = ele['location']
  323. width = int(location['width'])
  324. height = int(location['height'])
  325. if width > height:
  326. abcd_str = 'ABD'
  327. cal_num = max([words.count(char) for char in abcd_str])
  328. choice_m_cal_num.append(cal_num)
  329. # max_num = max(choice_m_cal_num)
  330. counts = np.bincount(np.array(choice_m_cal_num))
  331. mode = int(counts.argmax())
  332. # if max_num/choice_m_mum < 2:
  333. if mode < 2:
  334. direction = 180
  335. else:
  336. direction = 90
  337. return direction
  338. def choice_m_adjust(image, choice_m_bbox_list):
  339. a_z = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  340. for index0, choice_m in enumerate(choice_m_bbox_list): # rcnn识别的框匹配题号
  341. box = choice_m['bounding_box']
  342. m_left, m_top = box['xmin'], box['ymin'],
  343. # box_coordiante = (m_left, m_top, box['xmax'], box['ymax'])
  344. single_choice_m = utils.crop_region(image, box)
  345. row_col_dict = get_choice_m_row_and_col(m_left, m_top, single_choice_m) # 所有的小框, 行列等
  346. if len(row_col_dict) > 0:
  347. if choice_m['direction'] == 90:
  348. choice_option = a_z[:row_col_dict['rows']].replace('', ',')[1:-1]
  349. else:
  350. choice_option = a_z[:row_col_dict['cols']].replace('', ',')[1:-1]
  351. choice_m.update(row_col_dict) # 更新 bounding_box
  352. choice_m.update({'choice_option': choice_option})
  353. return choice_m_bbox_list
  354. def choice_m_row_col(image, choice_m_bbox_list, xml_path):
  355. a_z = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  356. choice_m_dict_list = []
  357. choice_m_for_dircetion = utils.crop_region(image, choice_m_bbox_list[0]['bounding_box'])
  358. res_dict = get_ocr_text_and_coordinate(choice_m_for_dircetion, ocr_accuracy='accurate', language_type='ENG')
  359. direction = get_direction(res_dict)
  360. for index0, box in enumerate(choice_m_bbox_list): # rcnn识别的框匹配题号
  361. box = box['bounding_box']
  362. m_left, m_top = box['xmin'], box['ymin'],
  363. # box_coordiante = (m_left, m_top, box['xmax'], box['ymax'])
  364. single_choice_m = utils.crop_region(image, box)
  365. try:
  366. row_col_dict = get_choice_m_row_and_col(m_left, m_top, single_choice_m) # 所有的小框, 行列等
  367. if len(row_col_dict) > 0:
  368. if direction == 90:
  369. choice_option = a_z[:row_col_dict['rows']].replace('', ',')[1:-1]
  370. if 'number' in box.keys():
  371. title_number = box['number']
  372. else:
  373. title_number = [-1] * row_col_dict['cols']
  374. default_points = [-1] * row_col_dict['cols']
  375. else:
  376. choice_option = a_z[:row_col_dict['cols']].replace('', ',')[1:-1]
  377. if 'number' in box.keys():
  378. title_number = box['number']
  379. else:
  380. title_number = [-1] * row_col_dict['rows']
  381. default_points = [-1] * row_col_dict['rows']
  382. row_col_dict.update({'direction': direction, 'option': choice_option,
  383. 'number': title_number, 'default_points': default_points})
  384. choice_m_dict_list.append(row_col_dict)
  385. except Exception:
  386. pass
  387. try:
  388. choice_m_box_dict = get_title_number_by_choice_m.analysis_s_box(choice_m_dict_list)
  389. choice_m_list = [[ele['bounding_box']['xmin'], ele['bounding_box']['ymin'],
  390. ele['bounding_box']['xmax'], ele['bounding_box']['ymax']] for ele in choice_m_box_dict]
  391. x_y_interval_all = []
  392. s_box_w_h = []
  393. for index, s_box in enumerate(choice_m_box_dict):
  394. all_small_coordinate_dict = s_box['all_small_coordinate']
  395. all_small_coordinate_list = [[ele['xmin'], ele['ymin'], ele['xmax'], ele['ymax']] for ele in all_small_coordinate_dict]
  396. col = s_box['cols']
  397. x_y_interval = utils.get_x_diff_and_y_diff1(all_small_coordinate_list, col)
  398. x_y_interval_all.append(x_y_interval)
  399. all_small_coordinate_list = sorted(all_small_coordinate_list, key=lambda k: k[1])
  400. s_box_array = np.array(all_small_coordinate_list)
  401. s_box_wid_hei = (
  402. int(np.mean(s_box_array[:, 2])) - int(np.mean(s_box_array[:, 0])),
  403. int(np.mean(s_box_array[:, 3])) - int(np.mean(s_box_array[:, 1])))
  404. s_box_w_h.append(s_box_wid_hei)
  405. x_y_interval_arr = np.array(x_y_interval_all)
  406. x_y_interval_ave = (int(np.mean(x_y_interval_arr[:, 0])), int(np.mean(x_y_interval_arr[:, 1])))
  407. s_box_w_h_arr = np.array(s_box_w_h)
  408. singe_box_width_height_ave = (int(np.mean(s_box_w_h_arr[:, 0])), int(np.mean(s_box_w_h_arr[:, 1])))
  409. image_height, image_width, _ = image.shape
  410. image_size = (image_width, image_height)
  411. choice_bbox = choice_bbox_vague(choice_m_list, x_y_interval_ave, singe_box_width_height_ave, direction, image_size)
  412. choice_m_dict_list_all = []
  413. choice_m_dict_list_part1 = []
  414. for index, choice_box_ele in enumerate(choice_bbox):
  415. choice_region = utils.crop_region_direct(image, choice_box_ele[0])
  416. # choice_path = xml_path[: xml_path.rfind('\\')]
  417. # cv2.imwrite(os.path.join(choice_path, 'choice_region_' + str(index) + '.jpg'), choice_region)
  418. choice_m_box_dict_new = [choice_m_box_dict[i] for i in choice_box_ele[1]]
  419. choice_m_dict_list_part = get_title_number_by_choice_m.get_title_number(choice_box_ele[0], choice_region,
  420. choice_m_box_dict_new, direction)
  421. choice_m_dict_list_part1.append(choice_m_dict_list_part)
  422. choice_m_dict_list_all = choice_m_dict_list_part1[0] + choice_m_dict_list_part1[1]
  423. except Exception as e:
  424. choice_m_dict_list_all = choice_m_dict_list
  425. return choice_m_dict_list_all
  426. def choice_line_with_number(left, top, image, choice_bbox_list, xml_path):
  427. t1 = time.time()
  428. word_result_list0 = get_ocr_text_and_coordinate(image, ocr_accuracy='accurate', language_type='ENG')
  429. t2 = time.time()
  430. print('choice ocr time cost: ', t2 - t1)
  431. # print(word_result_list0)
  432. intervel_x = 3
  433. img = preprocess(image, intervel_x, 3)
  434. cv_box_list = box_coordinates(img)
  435. content_list = get_choice_line_box_coordinate(left, top, word_result_list0, cv_box_list, choice_bbox_list)
  436. return content_list
  437. def get_choice_line_box_coordinate(left, top, word_result_list, cv_box_list, choice_bbox_list):
  438. all_digital_list = []
  439. digital_model = re.compile(r'\d')
  440. for i, chars_dict in enumerate(word_result_list):
  441. chars_list = chars_dict['chars']
  442. for ele in chars_list:
  443. if digital_model.search(ele['char']):
  444. all_digital_list.append(ele)
  445. new_all_digital_list = []
  446. i = 1
  447. while i <= len(all_digital_list):
  448. pre_one = all_digital_list[i - 1]
  449. if i == len(all_digital_list):
  450. new_all_digital_list.append(pre_one)
  451. break
  452. rear_one = all_digital_list[i]
  453. condition1 = abs(pre_one['location']['top'] - rear_one['location']['top']) < pre_one['location'][
  454. 'height'] # 两字高度差小于一字高度
  455. condition2 = pre_one['location']['left'] + 2 * pre_one['location']['width'] > rear_one['location'][
  456. 'left'] # 某字宽度的2倍大于两字间间隔
  457. if condition1:
  458. if condition2:
  459. new_char = pre_one['char'] + rear_one['char']
  460. new_location = {'left': pre_one['location']['left'],
  461. 'top': min(pre_one['location']['top'], rear_one['location']['top']),
  462. 'width': rear_one['location']['left'] + rear_one['location']['width'] -
  463. pre_one['location']['left'],
  464. 'height': max(pre_one['location']['height'], rear_one['location']['height'])}
  465. new_all_digital_list.append({'char': new_char, 'location': new_location})
  466. i = i + 1 + 1
  467. else:
  468. new_all_digital_list.append(pre_one)
  469. i = i + 1
  470. else:
  471. new_all_digital_list.append(pre_one) # 遇到字符y轴相差过大就结束
  472. i = i + 1
  473. content_list = list()
  474. new_all_digital_list = sorted(new_all_digital_list, key=lambda k: k.get('location').get('top'))
  475. for index, box in enumerate(
  476. sorted(choice_bbox_list['regions'], key=lambda k: k.get('bounding_box').get('ymin'))): # rcnn识别的框匹配题号
  477. box = box['bounding_box']
  478. box_coordinate = (box['xmin'] + left, box['ymin'] + top, box['xmax'] + left, box['ymax'] + top)
  479. choice_number = {'number': 999, 'location': box_coordinate}
  480. content_list.insert(index, choice_number)
  481. for digital in new_all_digital_list:
  482. digital_coordinate = (digital['location']['left'] + left, digital['location']['top'] + top,
  483. digital['location']['left'] + digital['location']['width'] + left,
  484. digital['location']['top'] + digital['location']['height'] + top)
  485. if utils.decide_coordinate_contains(digital_coordinate, box_coordinate):
  486. content_list[index]['number'] = digital['char']
  487. new_all_digital_list.remove(digital)
  488. break
  489. for box in content_list: # 计算间距
  490. box_coordiante = (box['location'][0], box['location'][1], box['location'][2], box['location'][3])
  491. mtx = []
  492. for cv_box in cv_box_list:
  493. if utils.decide_coordinate_contains(cv_box, box_coordiante): # 若fasterrcnn未识别到选项框,单独的ABCD也舍去
  494. mtx.append(cv_box)
  495. matrix = np.asarray(sorted(mtx))
  496. dif = matrix[1:, 0] - matrix[:-1, 2] # 后一个char的left与起一个char的right的差
  497. dif[dif < 0] = 0
  498. dif_length = np.mean(dif) # 小于平均间隔的合并
  499. block_list = utils.box_by_x_intervel(matrix, dif_length)
  500. # block_list = utils.box_by_x_intervel(matrix, 5)
  501. box['abcd'] = block_list
  502. matrix_distance = np.asarray(sorted(block_list))
  503. dif = matrix_distance[1:, 0] - matrix_distance[:-1, 2] # 后一个char的left与起一个char的right的差
  504. distance = np.mean(dif)
  505. return content_list