7
7
import numpy as np
8
8
import cv2
9
9
import math
10
+ from scipy .ndimage import label
10
11
11
- """ auxilary functions """
12
+ """ auxiliary functions """
12
13
# unwarp corodinates
13
14
def warpCoord (Minv , pt ):
14
15
out = np .matmul (Minv , (pt [0 ], pt [1 ], 1 ))
15
16
return np .array ([out [0 ]/ out [2 ], out [1 ]/ out [2 ]])
16
- """ end of auxilary functions """
17
+ """ end of auxiliary functions """
17
18
18
19
19
- def getDetBoxes_core (textmap , linkmap , text_threshold , link_threshold , low_text ):
20
+ def getDetBoxes_core (textmap , linkmap , text_threshold , link_threshold , low_text , estimate_num_chars = False ):
20
21
# prepare data
21
22
linkmap = linkmap .copy ()
22
23
textmap = textmap .copy ()
@@ -42,6 +43,12 @@ def getDetBoxes_core(textmap, linkmap, text_threshold, link_threshold, low_text)
42
43
# make segmentation map
43
44
segmap = np .zeros (textmap .shape , dtype = np .uint8 )
44
45
segmap [labels == k ] = 255
46
+ if estimate_num_chars :
47
+ _ , character_locs = cv2 .threshold ((textmap - linkmap ) * segmap / 255. , text_threshold , 1 , 0 )
48
+ _ , n_chars = label (character_locs )
49
+ mapper .append (n_chars )
50
+ else :
51
+ mapper .append (k )
45
52
segmap [np .logical_and (link_score == 1 , text_score == 0 )] = 0 # remove link area
46
53
x , y = stats [k , cv2 .CC_STAT_LEFT ], stats [k , cv2 .CC_STAT_TOP ]
47
54
w , h = stats [k , cv2 .CC_STAT_WIDTH ], stats [k , cv2 .CC_STAT_HEIGHT ]
@@ -74,7 +81,6 @@ def getDetBoxes_core(textmap, linkmap, text_threshold, link_threshold, low_text)
74
81
box = np .array (box )
75
82
76
83
det .append (box )
77
- mapper .append (k )
78
84
79
85
return det , labels , mapper
80
86
@@ -160,7 +166,7 @@ def getPoly_core(boxes, labels, mapper, linkmap):
160
166
if num_sec != 0 :
161
167
cp_section [- 1 ] = [cp_section [- 1 ][0 ] / num_sec , cp_section [- 1 ][1 ] / num_sec ]
162
168
163
- # pass if num of pivots is not sufficient or segment widh is smaller than character height
169
+ # pass if num of pivots is not sufficient or segment width is smaller than character height
164
170
if None in pp or seg_w < np .max (seg_height ) * 0.25 :
165
171
polys .append (None ); continue
166
172
@@ -224,15 +230,17 @@ def getPoly_core(boxes, labels, mapper, linkmap):
224
230
225
231
return polys
226
232
227
- def getDetBoxes (textmap , linkmap , text_threshold , link_threshold , low_text , poly = False ):
228
- boxes , labels , mapper = getDetBoxes_core (textmap , linkmap , text_threshold , link_threshold , low_text )
233
+ def getDetBoxes (textmap , linkmap , text_threshold , link_threshold , low_text , poly = False , estimate_num_chars = False ):
234
+ if poly and estimate_num_chars :
235
+ raise Exception ("Estimating the number of characters not currently supported with poly." )
236
+ boxes , labels , mapper = getDetBoxes_core (textmap , linkmap , text_threshold , link_threshold , low_text , estimate_num_chars )
229
237
230
238
if poly :
231
239
polys = getPoly_core (boxes , labels , mapper , linkmap )
232
240
else :
233
241
polys = [None ] * len (boxes )
234
242
235
- return boxes , polys
243
+ return boxes , polys , mapper
236
244
237
245
def adjustResultCoordinates (polys , ratio_w , ratio_h , ratio_net = 2 ):
238
246
if len (polys ) > 0 :
0 commit comments