提交 d8dfdc92 编写于 作者: z37757's avatar z37757

use Polygon from shapely

上级 3352ddb3
......@@ -18,7 +18,7 @@ https://github.com/open-mmlab/mmocr/blob/main/mmocr/datasets/pipelines/transform
import numpy as np
from PIL import Image, ImageDraw
import cv2
import Polygon as plg
from shapely.geometry import Polygon
import math
from ppocr.utils.poly_nms import poly_intersection
......@@ -129,16 +129,16 @@ class RandomCropFlip:
pts = np.stack([[xmin, xmax, xmax, xmin],
[ymin, ymin, ymax, ymax]]).T.astype(np.int32)
pp = plg.Polygon(pts)
pp = Polygon(pts)
fail_flag = False
for polygon, ignore_tag in zip(polygons, ignore_tags):
ppi = plg.Polygon(polygon.reshape(-1, 2))
ppi = Polygon(polygon.reshape(-1, 2))
ppiou, _ = poly_intersection(ppi, pp)
if np.abs(ppiou - float(ppi.area())) > self.epsilon and \
if np.abs(ppiou - float(ppi.area)) > self.epsilon and \
np.abs(ppiou) > self.epsilon:
fail_flag = True
break
elif np.abs(ppiou - float(ppi.area())) < self.epsilon:
elif np.abs(ppiou - float(ppi.area)) < self.epsilon:
polys_new.append(polygon)
ignore_tags_new.append(ignore_tag)
else:
......
......@@ -13,7 +13,7 @@
# limitations under the License.
import numpy as np
import Polygon as plg
from shapely.geometry import Polygon
def points2polygon(points):
......@@ -33,7 +33,7 @@ def points2polygon(points):
assert (points.size % 2 == 0) and (points.size >= 8)
point_mat = points.reshape([-1, 2])
return plg.Polygon(point_mat)
return Polygon(point_mat)
def poly_intersection(poly_det, poly_gt):
......@@ -46,13 +46,11 @@ def poly_intersection(poly_det, poly_gt):
Returns:
intersection_area (float): The intersection area between two polygons.
"""
assert isinstance(poly_det, plg.Polygon)
assert isinstance(poly_gt, plg.Polygon)
assert isinstance(poly_det, Polygon)
assert isinstance(poly_gt, Polygon)
poly_inter = poly_det & poly_gt
if len(poly_inter) == 0:
return 0, poly_inter
return poly_inter.area(), poly_inter
poly_inter = poly_det.buffer(0.001) & poly_gt.buffer(0.001)
return poly_inter.area, poly_inter
def poly_union(poly_det, poly_gt):
......@@ -65,11 +63,11 @@ def poly_union(poly_det, poly_gt):
Returns:
union_area (float): The union area between two polygons.
"""
assert isinstance(poly_det, plg.Polygon)
assert isinstance(poly_gt, plg.Polygon)
assert isinstance(poly_det, Polygon)
assert isinstance(poly_gt, Polygon)
area_det = poly_det.area()
area_gt = poly_gt.area()
area_det = poly_det.area
area_gt = poly_gt.area
area_inters, _ = poly_intersection(poly_det, poly_gt)
return area_det + area_gt - area_inters
......@@ -114,8 +112,8 @@ def poly_iou(poly_det, poly_gt):
Returns:
iou (float): The IOU between two polygons.
"""
assert isinstance(poly_det, plg.Polygon)
assert isinstance(poly_gt, plg.Polygon)
assert isinstance(poly_det, Polygon)
assert isinstance(poly_gt, Polygon)
area_inters, _ = poly_intersection(poly_det, poly_gt)
area_union = poly_union(poly_det, poly_gt)
if area_union == 0:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册