Skip to content

Commit 706a792

Browse files
authored
Utility functions
Some functions that can be useful for dealing with coordinates.
1 parent bb16ab9 commit 706a792

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

utils/utils.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
""" Some functions that can be useful for dealing with coordinates. """
2+
3+
def non_negative(coord):
4+
5+
"""
6+
Sets negative coordinates to zero. This fixes bugs in some labeling tools.
7+
8+
Input:
9+
coord: Int or float
10+
Any number that represents a coordinate, whether normalized or not.
11+
"""
12+
13+
if coord < 0:
14+
return 0
15+
else:
16+
return coord
17+
18+
def pixel2yolo(dim, pixel_coords):
19+
20+
"""
21+
Transforms coordinates in YOLO format to coordinates in pixels.
22+
23+
Input:
24+
dim: Tuple or list
25+
Image size (width, height).
26+
pixel_coords: List
27+
Bounding box coordinates in pixels (xmin, ymin, xmax, ymax).
28+
Output:
29+
yolo_coords: List
30+
Bounding box coordinates in YOLO format (xcenter, ycenter, width, height).
31+
"""
32+
33+
dw = 1/dim[0]
34+
dh = 1/dim[1]
35+
xcenter = non_negative(dw*(pixel_coords[0] + pixel_coords[2])/2)
36+
ycenter = non_negative(dh*(pixel_coords[1] + pixel_coords[3])/2)
37+
width = non_negative(dw*(pixel_coords[2] - pixel_coords[0]))
38+
height = non_negative(dh*(pixel_coords[3] - pixel_coords[1]))
39+
40+
yolo_coords = [xcenter, ycenter, width, height]
41+
42+
return yolo_coords
43+
44+
def yolo2pixel(dim, yolo_coords):
45+
46+
"""
47+
Transforms coordinates in YOLO format to coordinates in pixels.
48+
49+
Input:
50+
dim: Tuple or list
51+
Image size (width, height).
52+
yolo_coords: List
53+
Bounding box coordinates in YOLO format (xcenter, ycenter, width, height).
54+
Output:
55+
pixel_coords: List
56+
Bounding box coordinates in pixels (xmin, ymin, xmax, ymax).
57+
"""
58+
59+
xmin = non_negative(round(dim[0] * (yolo_coords[0] - yolo_coords[2]/2)))
60+
xmax = non_negative(round(dim[0] * (yolo_coords[0] + yolo_coords[2]/2)))
61+
ymin = non_negative(round(dim[1] * (yolo_coords[1] - yolo_coords[3]/2)))
62+
ymax = non_negative(round(dim[1] * (yolo_coords[1] + yolo_coords[3]/2)))
63+
64+
pixel_coords = [xmin, ymin, xmax, ymax]
65+
66+
return pixel_coords

0 commit comments

Comments
 (0)