1- from PIL import ImageDraw
2- from typing import Tuple
1+ """
2+ Helper utilities.
3+ """
4+
35
46def read_coco_labels (file_path ):
57 """
@@ -12,40 +14,3 @@ def read_coco_labels(file_path):
1214 pair = line .strip ().split (maxsplit = 1 )
1315 ret [int (pair [0 ])] = pair [1 ].strip ()
1416 return ret
15-
16- def draw_box (
17- draw : ImageDraw ,
18- box : Tuple [float , float , float , float ],
19- img_width : int ,
20- img_height : int ,
21- text : str = "" ,
22- color : Tuple [int , int , int ] = (255 , 255 , 0 ),
23- ) -> None :
24- """
25- Draw a bounding box on and image.
26- The bounding box is defined by the tuple (y_min, x_min, y_max, x_max)
27- where the coordinates are floats in the range [0.0, 1.0] and
28- relative to the width and height of the image.
29- For example, if an image is 100 x 200 pixels (height x width) and the bounding
30- box is `(0.1, 0.2, 0.5, 0.9)`, the upper-left and bottom-right coordinates of
31- the bounding box will be `(40, 10)` to `(180, 50)` (in (x,y) coordinates).
32- """
33-
34- line_width = 3
35- font_height = 8
36- y_min , x_min , y_max , x_max = box
37- (left , right , top , bottom ) = (
38- x_min * img_width ,
39- x_max * img_width ,
40- y_min * img_height ,
41- y_max * img_height ,
42- )
43- draw .line (
44- [(left , top ), (left , bottom ), (right , bottom ), (right , top ), (left , top )],
45- width = line_width ,
46- fill = color ,
47- )
48- if text :
49- draw .text (
50- (left + line_width , abs (top - line_width - font_height )), text , fill = color
51- )
0 commit comments