MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / update_image_and_anno

Function update_image_and_anno

computer_vision/mosaic_augmentation.py:87–169  ·  view source on GitHub ↗

- all_img_list : list of all images - all_annos : list of all annotations of specific image - idxs : index of image in list - output_size : size of output image (Height, Width) - scale_range : range of scale image

(
    all_img_list: list,
    all_annos: list,
    idxs: list[int],
    output_size: tuple[int, int],
    scale_range: tuple[float, float],
    filter_scale: float = 0.0,
)

Source from the content-addressed store, hash-verified

85
86
87def update_image_and_anno(
88 all_img_list: list,
89 all_annos: list,
90 idxs: list[int],
91 output_size: tuple[int, int],
92 scale_range: tuple[float, float],
93 filter_scale: float = 0.0,
94) -> tuple[list, list, str]:
95 """
96 - all_img_list <type: list>: list of all images
97 - all_annos <type: list>: list of all annotations of specific image
98 - idxs <type: list>: index of image in list
99 - output_size <type: tuple>: size of output image (Height, Width)
100 - scale_range <type: tuple>: range of scale image
101 - filter_scale <type: float>: the condition of downscale image and bounding box
102 Return:
103 - output_img <type: narray>: image after resize
104 - new_anno <type: list>: list of new annotation after scale
105 - path[0] <type: string>: get the name of image file
106 """
107 output_img = np.zeros([output_size[0], output_size[1], 3], dtype=np.uint8)
108 scale_x = scale_range[0] + random.random() * (scale_range[1] - scale_range[0])
109 scale_y = scale_range[0] + random.random() * (scale_range[1] - scale_range[0])
110 divid_point_x = int(scale_x * output_size[1])
111 divid_point_y = int(scale_y * output_size[0])
112
113 new_anno = []
114 path_list = []
115 for i, index in enumerate(idxs):
116 path = all_img_list[index]
117 path_list.append(path)
118 img_annos = all_annos[index]
119 img = cv2.imread(path)
120 if i == 0: # top-left
121 img = cv2.resize(img, (divid_point_x, divid_point_y))
122 output_img[:divid_point_y, :divid_point_x, :] = img
123 for bbox in img_annos:
124 xmin = bbox[1] * scale_x
125 ymin = bbox[2] * scale_y
126 xmax = bbox[3] * scale_x
127 ymax = bbox[4] * scale_y
128 new_anno.append([bbox[0], xmin, ymin, xmax, ymax])
129 elif i == 1: # top-right
130 img = cv2.resize(img, (output_size[1] - divid_point_x, divid_point_y))
131 output_img[:divid_point_y, divid_point_x : output_size[1], :] = img
132 for bbox in img_annos:
133 xmin = scale_x + bbox[1] * (1 - scale_x)
134 ymin = bbox[2] * scale_y
135 xmax = scale_x + bbox[3] * (1 - scale_x)
136 ymax = bbox[4] * scale_y
137 new_anno.append([bbox[0], xmin, ymin, xmax, ymax])
138 elif i == 2: # bottom-left
139 img = cv2.resize(img, (divid_point_x, output_size[0] - divid_point_y))
140 output_img[divid_point_y : output_size[0], :divid_point_x, :] = img
141 for bbox in img_annos:
142 xmin = bbox[1] * scale_x
143 ymin = scale_y + bbox[2] * (1 - scale_y)
144 xmax = bbox[3] * scale_x

Callers 1

mainFunction · 0.70

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected