Skip to content

Commit

Permalink
Add keypoint-to-polygon conversion function
Browse files Browse the repository at this point in the history
This commit introduces a new function that converts keypoints to polygons. It achieves this by randomly sampling 10 points on a circle with a radius of 10 pixels centered around the given keypoint.
  • Loading branch information
healthonrails committed Apr 29, 2024
1 parent a557e6d commit 0663ed2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
27 changes: 27 additions & 0 deletions annolid/annotation/keypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,33 @@
from annolid.gui.shape import Shape
from annolid.utils.logger import logger
import json
import math


def keypoint_to_polygon_points(center_point,
radius=10,
num_points=10):
"""
Generate polygon points based on a given point and radius.
Args:
- center_point (list): A list containing the x and y coordinates
of the center point, e.g., [[x, y]].
- radius (int): The radius of the circle.
- num_points (int, optional): The number of points to generate (default is 10).
˝
Returns:
- points (list): A list of lists containing the x and y coordinates of
the polygon points, e.g., [[x1, y1], [x2, y2], ...].
"""
center_x, center_y = center_point[0]
points = []
for i in range(num_points):
angle = 2 * math.pi * i / num_points
x = center_x + radius * math.cos(angle)
y = center_y + radius * math.sin(angle)
points.append([x, y])
return points


def format_shape(shape):
Expand Down
4 changes: 4 additions & 0 deletions annolid/annotation/labelme2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from labelme.utils import shape_to_mask
from annolid.utils.shapes import masks_to_bboxes, polygon_center
from annolid.annotation.masks import binary_mask_to_coco_rle
from annolid.annotation.keypoints import keypoint_to_polygon_points


def read_json_file(file_path):
Expand Down Expand Up @@ -88,6 +89,9 @@ def convert_json_to_csv(json_folder, csv_file=None, progress_callback=None):
for shape in data["shapes"]:
instance_name = shape["label"]
points = shape["points"]
if shape['shape_type'] == 'point':
points = keypoint_to_polygon_points(points)
shape['shape_type'] == 'polygon'
if len(points) > 2:
mask = convert_shape_to_mask(img_shape, points)
bboxs = masks_to_bboxes(mask[None, :, :])
Expand Down

0 comments on commit 0663ed2

Please sign in to comment.