Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def shrink_poly(poly, r):
try:
area_poly = abs(pyclipper.Area(poly))
perimeter_poly = perimeter(poly)
poly_s = []
pco = pyclipper.PyclipperOffset()
if perimeter_poly:
d=area_poly*(1-r*r)/perimeter_poly
pco.AddPath(poly, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
poly_s = pco.Execute(-d)
return poly_s
except Exception as e:
traceback.print_exc()
raise e
def shrink(bboxes, rate, max_shr=20):
rate = rate * rate
shrinked_bboxes = []
for bbox in bboxes:
area = plg.Polygon(bbox).area()
peri = perimeter(bbox)
pco = pyclipper.PyclipperOffset()
pco.AddPath(bbox, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
offset = min((int)(area * (1 - rate) / (peri + 0.001) + 0.5), max_shr)
shrinked_bbox = pco.Execute(-offset)
if len(shrinked_bbox) == 0:
shrinked_bboxes.append(bbox)
continue
shrinked_bbox = np.array(shrinked_bbox[0])
if shrinked_bbox.shape[0] <= 2:
shrinked_bboxes.append(bbox)
continue
shrinked_bboxes.append(shrinked_bbox)
return np.array(shrinked_bboxes)
def llr_polyscore(cnt, pts, cen, alfa=5, beta=2):
a = cnt[0]; b = cnt[1]
c = cnt[2]; d = cnt[3]
# (1) # za mala powierzchnia
area = cv2.contourArea(cnt)
t2 = area < (4 * alfa * alfa) * 5
if t2: return 0
gamma = alfa/1.5#alfa**(1/2)
#print("ALFA", alfa)
# (2) # za malo punktow
pco = pyclipper.PyclipperOffset()
pco.AddPath(cnt, pyclipper.JT_MITER, pyclipper.ET_CLOSEDPOLYGON)
pcnt = matplotlib.path.Path(pco.Execute(gamma)[0]) # FIXME: alfa/1.5
wtfs = pcnt.contains_points(pts)
pts_in = min(np.count_nonzero(wtfs), 49)
t1 = pts_in < min(len(pts), 49) - 2 * beta - 1
if t1: return 0
A = pts_in
B = area
# (3)
# FIXME: punkty za kwadratowosci? (przypadki z L shape)
nln = lambda l1, x, dx: \
np.linalg.norm(np.cross(na(l1[1])-na(l1[0]),
na(l1[0])-na( x)))/dx
pcnt_in = []; i = 0
def shrink(bboxes, rate, max_shr=20):
rate = rate * rate
shrinked_bboxes = []
for bbox in bboxes:
area = Polygon(bbox).area
peri = perimeter(bbox)
pco = pyclipper.PyclipperOffset()
pco.AddPath(bbox, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
offset = min((int)(area * (1 - rate) / (peri + 0.001) + 0.5), max_shr)
shrinked_bbox = pco.Execute(-offset)
if len(shrinked_bbox) == 0:
shrinked_bboxes.append(bbox)
continue
shrinked_bbox = np.array(shrinked_bbox[0])
if shrinked_bbox.shape[0] <= 2:
shrinked_bboxes.append(bbox)
continue
shrinked_bboxes.append(shrinked_bbox)
return np.array(shrinked_bboxes)
h, w, _ = im.shape
training_mask = np.ones((h, w), dtype=np.uint8)
score_maps = []
for i in (1, shrink_ratio):
score_map, training_mask = generate_rbox((h, w), text_polys, text_tags,training_mask, i)
score_maps.append(score_map)
score_maps = np.array(score_maps, dtype=np.float32)
imgs = data_aug.random_crop([im, score_maps.transpose((1, 2, 0)), training_mask], (input_size, input_size))
return imgs[0], imgs[1].transpose((2, 0, 1)), imgs[2] # im,score_maps,training_mask#
if __name__ == '__main__':
poly = np.array([377,117,463,117,465,130,378,130]).reshape(-1,2)
shrink_ratio = 0.5
d_i = cv2.contourArea(poly) * (1 - shrink_ratio) / cv2.arcLength(poly, True) + 0.5
pco = pyclipper.PyclipperOffset()
pco.AddPath(poly, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
shrinked_poly = np.array(pco.Execute(-d_i))
print(d_i)
print(cv2.contourArea(shrinked_poly.astype(int)) / cv2.contourArea(poly))
print(unshrink_offset(shrinked_poly,shrink_ratio))
def fillet_shape(poly, radius, convex = True):
"""
fillet a polygon
:param poly: list of point tuples describing the polygon
:param radius: radius to fillet by
:param convex: if true fillet the convex corners, if false fillet the concave corners
:return: list of points representing the filleted polygon
"""
scaled_radius = radius * 2 ** 31
pco = pyclipper.PyclipperOffset()
pco.ArcTolerance = arc_tolerance
# shrink
pco.AddPath(pyclipper.scale_to_clipper(poly), pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
expanded = pco.Execute(-scaled_radius if convex else scaled_radius)
# expand
pco.Clear()
pco.AddPath(expanded[0], pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
result = pyclipper.scale_from_clipper(pco.Execute(scaled_radius if convex else -scaled_radius))
return map(lambda point: (point[0], point[1]), result[0])
def offset(paths, amount):
pco = pyclipper.PyclipperOffset()
pco.ArcTolerance = SCALING_FACTOR / 40
paths = pyclipper.scale_to_clipper(paths, SCALING_FACTOR)
pco.AddPaths(paths, pyclipper.JT_SQUARE, pyclipper.ET_CLOSEDPOLYGON)
outpaths = pco.Execute(amount * SCALING_FACTOR)
outpaths = pyclipper.scale_from_clipper(outpaths, SCALING_FACTOR)
return outpaths
def shrink_poly(poly, r):
try:
area_poly = abs(pyclipper.Area(poly))
perimeter_poly = perimeter(poly)
poly_s = []
pco = pyclipper.PyclipperOffset()
if perimeter_poly:
d=area_poly*(1-r*r)/perimeter_poly
pco.AddPath(poly, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
poly_s = pco.Execute(-d)
return poly_s
except Exception as e:
traceback.print_exc()
raise e