Skip to content

Commit

Permalink
Add conversion from point to bbox (#26)
Browse files Browse the repository at this point in the history
* Add conversion from point to bbox

* Code formatting
  • Loading branch information
raultapia committed Nov 5, 2023
1 parent 4ecfc7a commit 9ef8748
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions labelme2coco/labelme2coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,27 @@ def get_coco_from_labelme_folder(
category_id = category_ind
coco.add_category(CocoCategory(id=category_id, name=category_name))
category_ind += 1
# circles and lines to segmentation

# convert circles, lines, and points to bbox/segmentation
if shape["shape_type"] == "circle":
(cx,cy), (x1,y1) = shape["points"]
r = np.linalg.norm(np.array([x1-cx,y1-cy]))
angles = np.linspace(0,2*np.pi,50*(int(r)+1))
(cx, cy), (x1, y1) = shape["points"]
r = np.linalg.norm(np.array([x1 - cx, y1 - cy]))
angles = np.linspace(0, 2 * np.pi, 50 * (int(r) + 1))
x = cx + r * np.cos(angles)
y = cy + r * np.sin(angles)
points = np.rint(np.append(x,y).reshape(-1,2,order='F'))
points = np.rint(np.append(x, y).reshape(-1, 2, order='F'))
_, index = np.unique(points, return_index=True, axis=0)
shape["points"] = points[np.sort(index)]
shape["shape_type"] = "polygon"
elif shape["shape_type"] == "line":
(x1,y1), (x2,y2) = shape["points"]
shape["points"] = [x1,y1,x2,y2,x2+1e-3,y2+1e-3,x1+1e-3,y1+1e-3]
(x1, y1), (x2, y2) = shape["points"]
shape["points"] = [x1, y1, x2, y2, x2 + 1e-3, y2 + 1e-3, x1 + 1e-3, y1 + 1e-3]
shape["shape_type"] = "polygon"

elif shape["shape_type"] == "point":
(x1, y1) = shape["points"][0]
shape["points"] = [[x1, y1], [x1 + 1, y1 + 1]]
shape["shape_type"] = "rectangle"

# parse bbox/segmentation
if shape["shape_type"] == "rectangle":
x1 = shape["points"][0][0]
Expand Down

0 comments on commit 9ef8748

Please sign in to comment.