1
1
# -*- coding: utf-8 -*-
2
2
# @Time : 2019/8/23 21:53
3
3
# @Author : zhoujun
4
-
4
+ import math
5
5
import random
6
6
import pyclipper
7
7
import numpy as np
@@ -33,6 +33,25 @@ def check_and_validate_polys(polys, xxx_todo_changeme):
33
33
validated_polys .append (poly )
34
34
return np .array (validated_polys )
35
35
36
+ def unshrink_offset (poly ,ratio ):
37
+ area = cv2 .contourArea (poly )
38
+ peri = cv2 .arcLength (poly , True )
39
+ a = 8
40
+ b = peri - 4
41
+ c = 1 - 0.5 * peri - area / ratio
42
+ return quadratic (a ,b ,c )
43
+
44
+ def quadratic (a , b , c ):
45
+ if (b * b - 4 * a * c ) < 0 :
46
+ return 'None'
47
+ Delte = math .sqrt (b * b - 4 * a * c )
48
+ if Delte > 0 :
49
+ x = (- b + Delte ) / (2 * a )
50
+ y = (- b - Delte ) / (2 * a )
51
+ return x , y
52
+ else :
53
+ x = (- b ) / (2 * a )
54
+ return x
36
55
37
56
def generate_rbox (im_size , text_polys , text_tags ,training_mask , shrink_ratio ):
38
57
"""
@@ -48,7 +67,8 @@ def generate_rbox(im_size, text_polys, text_tags,training_mask, shrink_ratio):
48
67
for i , (poly , tag ) in enumerate (zip (text_polys , text_tags )):
49
68
try :
50
69
poly = poly .astype (np .int )
51
- d_i = cv2 .contourArea (poly ) * (1 - shrink_ratio * shrink_ratio ) / cv2 .arcLength (poly , True )
70
+ # d_i = cv2.contourArea(poly) * (1 - shrink_ratio * shrink_ratio) / cv2.arcLength(poly, True)
71
+ d_i = cv2 .contourArea (poly ) * (1 - shrink_ratio ) / cv2 .arcLength (poly , True ) + 0.5
52
72
pco = pyclipper .PyclipperOffset ()
53
73
pco .AddPath (poly , pyclipper .JT_ROUND , pyclipper .ET_CLOSEDPOLYGON )
54
74
shrinked_poly = np .array (pco .Execute (- d_i ))
@@ -107,3 +127,14 @@ def image_label(im: np.ndarray, text_polys: np.ndarray, text_tags: list, input_s
107
127
score_maps = np .array (score_maps , dtype = np .float32 )
108
128
imgs = data_aug .random_crop ([im , score_maps .transpose ((1 , 2 , 0 )), training_mask ], (input_size , input_size ))
109
129
return imgs [0 ], imgs [1 ].transpose ((2 , 0 , 1 )), imgs [2 ] # im,score_maps,training_mask#
130
+
131
+ if __name__ == '__main__' :
132
+ poly = np .array ([377 ,117 ,463 ,117 ,465 ,130 ,378 ,130 ]).reshape (- 1 ,2 )
133
+ shrink_ratio = 0.5
134
+ d_i = cv2 .contourArea (poly ) * (1 - shrink_ratio ) / cv2 .arcLength (poly , True ) + 0.5
135
+ pco = pyclipper .PyclipperOffset ()
136
+ pco .AddPath (poly , pyclipper .JT_ROUND , pyclipper .ET_CLOSEDPOLYGON )
137
+ shrinked_poly = np .array (pco .Execute (- d_i ))
138
+ print (d_i )
139
+ print (cv2 .contourArea (shrinked_poly .astype (int )) / cv2 .contourArea (poly ))
140
+ print (unshrink_offset (shrinked_poly ,shrink_ratio ))
0 commit comments