def char2wordBB(self, charBB, text):
"""
Converts character bounding-boxes to word-level
bounding-boxes.
charBB : 2x4xn matrix of BB coordinates
text : the text string
output : 2x4xm matrix of BB coordinates,
where, m == number of words.
"""
wrds = text.split()
bb_idx = np.r_[0, np.cumsum([len(w) for w in wrds])]
wordBB = np.zeros((2,4,len(wrds)), 'float32')
for i in xrange(len(wrds)):
cc = charBB[:,:,bb_idx[i]:bb_idx[i+1]]
# fit a rotated-rectangle:
# change shape from 2x4xn_i -> (4*n_i)x2
cc = np.squeeze(np.concatenate(np.dsplit(cc,cc.shape[-1]),axis=1)).T.astype('float32')
rect = cv2.minAreaRect(cc.copy())
box = np.array(cv2.cv.BoxPoints(rect))
# find the permutation of box-coordinates which
# are "aligned" appropriately with the character-bb.
# (exhaustive search over all possible assignments):
cc_tblr = np.c_[cc[0,:],
cc[-3,:],
cc[-2,:],
cc[3,:]].T
perm4 = np.array(list(itertools.permutations(np.arange(4))))
dists = []
for pidx in xrange(perm4.shape[0]):
d = np.sum(np.linalg.norm(box[perm4[pidx],:]-cc_tblr,axis=1))
dists.append(d)
wordBB[:,:,i] = box[perm4[np.argmin(dists)],:].T
return wordBB
评论列表
文章目录