(boxes, labels, mapper, linkmap)
| 89 | return det, labels, mapper |
| 90 | |
| 91 | def getPoly_core(boxes, labels, mapper, linkmap): |
| 92 | # configs |
| 93 | num_cp = 5 |
| 94 | max_len_ratio = 0.7 |
| 95 | expand_ratio = 1.45 |
| 96 | max_r = 2.0 |
| 97 | step_r = 0.2 |
| 98 | |
| 99 | polys = [] |
| 100 | for k, box in enumerate(boxes): |
| 101 | # size filter for small instance |
| 102 | w, h = int(np.linalg.norm(box[0] - box[1]) + 1), int(np.linalg.norm(box[1] - box[2]) + 1) |
| 103 | if w < 30 or h < 30: |
| 104 | polys.append(None); continue |
| 105 | |
| 106 | # warp image |
| 107 | tar = np.float32([[0,0],[w,0],[w,h],[0,h]]) |
| 108 | M = cv2.getPerspectiveTransform(box, tar) |
| 109 | word_label = cv2.warpPerspective(labels, M, (w, h), flags=cv2.INTER_NEAREST) |
| 110 | try: |
| 111 | Minv = np.linalg.inv(M) |
| 112 | except: |
| 113 | polys.append(None); continue |
| 114 | |
| 115 | # binarization for selected label |
| 116 | cur_label = mapper[k] |
| 117 | word_label[word_label != cur_label] = 0 |
| 118 | word_label[word_label > 0] = 1 |
| 119 | |
| 120 | """ Polygon generation """ |
| 121 | # find top/bottom contours |
| 122 | cp = [] |
| 123 | max_len = -1 |
| 124 | for i in range(w): |
| 125 | region = np.where(word_label[:,i] != 0)[0] |
| 126 | if len(region) < 2 : continue |
| 127 | cp.append((i, region[0], region[-1])) |
| 128 | length = region[-1] - region[0] + 1 |
| 129 | if length > max_len: max_len = length |
| 130 | |
| 131 | # pass if max_len is similar to h |
| 132 | if h * max_len_ratio < max_len: |
| 133 | polys.append(None); continue |
| 134 | |
| 135 | # get pivot points with fixed length |
| 136 | tot_seg = num_cp * 2 + 1 |
| 137 | seg_w = w / tot_seg # segment width |
| 138 | pp = [None] * num_cp # init pivot points |
| 139 | cp_section = [[0, 0]] * tot_seg |
| 140 | seg_height = [0] * num_cp |
| 141 | seg_num = 0 |
| 142 | num_sec = 0 |
| 143 | prev_h = -1 |
| 144 | for i in range(0,len(cp)): |
| 145 | (x, sy, ey) = cp[i] |
| 146 | if (seg_num + 1) * seg_w <= x and seg_num <= tot_seg: |
| 147 | # average previous segment |
| 148 | if num_sec == 0: break |
no test coverage detected
searching dependent graphs…