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