Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 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 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)
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
生成mask图,白色部分是文本,黑色是北京
:param im_size: 图像的h,w
:param text_polys: 框的坐标
:param text_tags: 标注文本框是否参与训练
:param training_mask: 忽略标注为 DO NOT CARE 的矩阵
:return: 生成的mask图
"""
h, w = im_size
score_map = np.zeros((h, w), dtype=np.uint8)
for i, (poly, tag) in enumerate(zip(text_polys, text_tags)):
try:
poly = poly.astype(np.int)
# d_i = cv2.contourArea(poly) * (1 - shrink_ratio * shrink_ratio) / cv2.arcLength(poly, True)
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))
cv2.fillPoly(score_map, shrinked_poly, i + 1)
if not tag:
cv2.fillPoly(training_mask, shrinked_poly, 0)
except:
print(poly)
return score_map, training_mask
def poly_offset(img, poly, dis):
subj_poly = np.array(poly)
# Polygon(subj_poly).area, Polygon(subj_poly).length
pco = pyclipper.PyclipperOffset()
pco.AddPath(subj_poly, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
solution = pco.Execute(-1.0 * dis)
ss = np.array(solution)
cv2.fillPoly(img, ss.astype(np.int32), 1)
return img
def unclip(self, box, unclip_ratio=1.5):
poly = Polygon(box)
distance = poly.area * unclip_ratio / poly.length
offset = pyclipper.PyclipperOffset()
offset.AddPath(box, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
expanded = np.array(offset.Execute(distance))
return expanded
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
preds[:2, :, :] = torch.sigmoid(preds[:2, :, :])
preds = preds.detach().cpu().numpy()
text = preds[0] > threshold # text
kernel = (preds[1] > threshold) * text # kernel
label_num, label = cv2.connectedComponents(kernel.astype(np.uint8), connectivity=4)
bbox_list = []
for label_idx in range(1, label_num):
points = np.array(np.where(label_num == label_idx)).transpose((1, 0))[:, ::-1]
rect = cv2.minAreaRect(points)
poly = cv2.boxPoints(rect).astype(int)
d_i = cv2.contourArea(poly) * 1.5 / cv2.arcLength(poly, True)
pco = pyclipper.PyclipperOffset()
pco.AddPath(poly, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
shrinked_poly = np.array(pco.Execute(-d_i))
if cv2.contourArea(shrinked_poly) < 800 / (scale * scale):
continue
bbox_list.append([shrinked_poly[1], shrinked_poly[2], shrinked_poly[3], shrinked_poly[0]])
return label, np.array(bbox_list)
: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])