Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
for p in polys:
pc = pyclipper.Pyclipper()
try:
pc.AddPath(p, pyclipper.PT_CLIP, True)
except:
total_areas.append(np.inf)
background_scores.append(np.inf)
# print p
print "Failed to assign text line, probably not an issue"
continue
pc.AddPaths([r['bounding_poly'] for r in regions], pyclipper.PT_SUBJECT, True)
solution = pc.Execute(pyclipper.CT_INTERSECTION, pyclipper.PFT_NONZERO, pyclipper.PFT_NONZERO)
area = 0
for path in solution:
area += pyclipper.Area(path)
simple_path = pyclipper.SimplifyPolygon(p, pyclipper.PFT_NONZERO)
total_area = 0
for path in simple_path:
total_area += pyclipper.Area(path)
total_areas.append(total_area)
background_score = total_area - area
background_scores.append(background_score)
return np.array(scores), np.array(background_scores), np.array(total_areas)
poly = poly_tag[0]
tag = poly_tag[1]
# ignore ###
if i == 0 and tag:
cv2.fillPoly(training_mask, poly.astype(np.int32)[np.newaxis, :, :], 0)
ignore_poly_mark.append(poly_idx)
# seg map
shrinked_polys = []
if poly_idx not in ignore_poly_mark:
shrinked_polys = shrink_poly(poly.copy(), scale_ratio[i])
if not len(shrinked_polys) and poly_idx not in ignore_poly_mark:
logger.info("before shrink poly area:{} len(shrinked_poly) is 0,image {}".format(
abs(pyclipper.Area(poly)),image_name))
# if the poly is too small, then ignore it during training
cv2.fillPoly(training_mask, poly.astype(np.int32)[np.newaxis, :, :], 0)
ignore_poly_mark.append(poly_idx)
continue
for shrinked_poly in shrinked_polys:
seg_map = cv2.fillPoly(seg_map, [np.array(shrinked_poly).astype(np.int32)], 1)
seg_maps[..., i] = seg_map
return seg_maps, training_mask
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
check so that the text poly is in the same direction,
and also filter some invalid polygons
:param polys:
:param tags:
:return:
'''
(h, w) = input_size
if polys.shape[0] == 0:
return [], []
polys[:, :, 0] = np.clip(polys[:, :, 0], 0, w-1)
polys[:, :, 1] = np.clip(polys[:, :, 1], 0, h-1)
validated_polys = []
validated_tags = []
for poly, tag in zip(polys, tags):
if abs(pyclipper.Area(poly)) < 1:
continue
#clockwise
if pyclipper.Orientation(poly):
poly = poly[::-1]
validated_polys.append(poly)
validated_tags.append(tag)
return np.array(validated_polys), np.array(validated_tags)
pc = pyclipper.Pyclipper()
for i, subjectContour in enumerate(subjectContours):
try:
pc.AddPath(subjectContour, pyclipper.PT_SUBJECT)
except pyclipper.ClipperException:
# skip invalid paths with no area
if pyclipper.Area(subjectContour) != 0:
raise InvalidSubjectContourError("contour %d is invalid for clipping" % i)
for j, clipContour in enumerate(clipContours):
try:
pc.AddPath(clipContour, pyclipper.PT_CLIP)
except pyclipper.ClipperException:
# skip invalid paths with no area
if pyclipper.Area(clipContour) == 0:
raise InvalidClippingContourError("contour %d is invalid for clipping" % j)
bounds = pc.GetBounds()
if (bounds.bottom, bounds.left, bounds.top, bounds.right) == (0, 0, 0, 0):
# do nothing if there are no paths
return []
try:
solution = pc.Execute(_operationMap[operation],
_fillTypeMap[subjectFillType],
_fillTypeMap[clipFillType])
except pyclipper.ClipperException as exc:
raise ExecutionError(exc)
return [[tuple(p) for p in path] for path in solution]
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 area(vset):
""" This function calculates the area of the set of FRV or ARV """
# Initialize A as it could be calculated iteratively
A = 0
# Check multiple exteriors
if type(vset[0][0]) == list:
# Calc every exterior separately
for i in range(len(vset)):
A += pyclipper.scale_from_clipper(pyclipper.scale_from_clipper(pyclipper.Area(pyclipper.scale_to_clipper(vset[i]))))
else:
# Single exterior
A = pyclipper.scale_from_clipper(pyclipper.scale_from_clipper(pyclipper.Area(pyclipper.scale_to_clipper(vset))))
return A