imagegeneration
imagegeneration ¶
Module for functions to generate test image dataset
Classes¶
NumberDataGenerator
dataclass
¶
Generator of images with numbers for an object detection test dataset
Functions¶
generate_images ¶
Generate images based on a configuration (self). Returns image_location to generated images.
Source code in niceml/utilities/imagegeneration.py
Functions¶
convert_image_to_df_row ¶
Takes an image and converts it to a row of data for a pandas DataFrame.
Parameters:
-
identifier
(str
) –str: Value to identify the image
-
label
(str
) –str: Target label of the image
-
image
(Union[ndarray, Image]
) –Union[np.ndarray,ImageType]: Image to convert into a dataframe row
-
target_size
(Optional[Union[Tuple[int, int], ImageSize]]
, default:None
) –Optional[Union[Tuple[int, int]: Specify the target size of the image
Returns: A dictionary representing a dataframe row
Source code in niceml/utilities/imagegeneration.py
create_objdet_instance_label_from_text_label ¶
create_objdet_instance_label_from_text_label(
class_name,
img_size=None,
bounding_box_width=None,
bounding_box_height=None,
text_layer_position=None,
rotation=None,
)
Creates an instance of an ObjDetInstanceLabel
including bounding box
information if all parameters are given. Otherwise, only the class_name is included.
Parameters:
-
class_name
(str
) –Name of the class to label
-
img_size
(ImageSize
, default:None
) –Size of image the label fits to
-
bounding_box_width
(int
, default:None
) –Width of bounding box
-
bounding_box_height
(int
, default:None
) –Height of bounding box
-
text_layer_position
(Tuple[int, int]
, default:None
) –Position of text to be labeled on the image
-
rotation
(int
, default:None
) –Rotation of the text
Returns:
-
ObjDetInstanceLabel
–ObjDetInstanceLabel with class information and,
-
ObjDetInstanceLabel
–if given,position of the label (bounding box)
Source code in niceml/utilities/imagegeneration.py
crop_text_layer_to_text ¶
Takes in a text layer (image object) and returns the same image, but cropped to only include the number
Parameters:
-
text_layer
(Image
) –image layer including text and is otherwise transparent (0)
Returns:
-
Image
–cropped version of text_layer with only the number part of the image object
Source code in niceml/utilities/imagegeneration.py
draw_number_on_image ¶
draw_number_on_image(
img,
random_generator,
max_number,
random_font_size,
img_size,
rotate,
mask_img,
)
Draws a number on an image, rotates it randomly (if rotate == True) and returns text information (text,text layer, text layer position) Args: img: image to draw the numbers on random_generator: Generator of random numbers max_number: Maximum number of digits that can be generated random_font_size: Size of numbers in image img_size: Size of the generated image rotate: Whether the number should be rotated randomly or not mask_img: Image with label information
Returns:
-
Image
–Tuple of 6 parameters: The image, the drawn number, the text_layer, the position of
-
str
–the number on the image, rotation of the drawn number, mask_img with updated label
-
Image
–information.
Source code in niceml/utilities/imagegeneration.py
generate_number_image ¶
generate_number_image(
random_generator,
location=None,
max_number=10,
img_size=ImageSize(256, 256),
rotate=False,
font_size_min=80,
font_size_max=140,
detection_label=False,
max_amount=3,
save=False,
)
Creates a series of generated images with random numbers on them and saves them, if save == True, including the label information (= position of numbers). Images are either a random color or a thumbnail image. Returns a list of created classes and the output_location.
Parameters:
-
random_generator
–Generator of random numbers
-
location
(Union[dict, LocationConfig]
, default:None
) –Output location of the image and its label
-
max_number
(int
, default:10
) –Maximum number of digits that can be generated
-
img_size
(ImageSize
, default:ImageSize(256, 256)
) –Size of the generated image
-
font_size_min
(int
, default:80
) –Minimum font size of the numbers in the image
-
font_size_max
(int
, default:140
) –Maximum font size of the numbers in the image
-
detection_label
(bool
, default:False
) –Determine if the label should be usable for object detection
-
max_amount
(int
, default:3
) –Maximum number of numbers in a single image
-
rotate
(bool
, default:False
) –Whether the drawn numbers should be rotated randomly or not
-
save
(bool
, default:False
) –Save the generated images to given output location
Returns: The generated image, its mask_img and the instance_labels of the numbers
Source code in niceml/utilities/imagegeneration.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
|
generate_test_images ¶
generate_test_images(
location,
sample_count,
seed=1234,
max_number=10,
img_size=ImageSize(256, 256),
font_size_min=80,
font_size_max=140,
detection_labels=False,
max_amount=3,
rotate=False,
save=True,
)
Wrapper function for 'generate_number_image', which creates a series of generated images with random numbers on them and saves them, if save == True, including the label information (= position of numbers). Returns a list of created classes and the output_location
Parameters:
-
location
(Union[dict, LocationConfig]
) –Output location of the image and its label
-
sample_count
(int
) –Number of images to be generated
-
seed
(Optional[int]
, default:1234
) –Seed for the random number generator
-
max_number
–Maximum number of digits that can be generated
-
img_size
(ImageSize
, default:ImageSize(256, 256)
) –Size of the generated image
-
font_size_min
(int
, default:80
) –Minimum font size of the numbers in the image
-
font_size_max
(int
, default:140
) –Maximum font size of the numbers in the image
-
detection_labels
(bool
, default:False
) –Determine if the label should be usable for object detection
-
max_amount
(int
, default:3
) –Maximum number of numbers in a single image
-
rotate
(bool
, default:False
) –Whether the drawn numbers should be rotated randomly or not
-
save
(bool
, default:True
) –Save the generated images to given output location
Returns:
-
Tuple[List[str], Union[dict, LocationConfig]]
–List of label classes and a location configuration
Source code in niceml/utilities/imagegeneration.py
get_rand_font ¶
Returns a randomly selected FreeTypeFont
from ten predefined font names
Source code in niceml/utilities/imagegeneration.py
get_rand_rotation ¶
get_random_color ¶
get_random_position ¶
Returns a random x and y coordinate inside an image size with a padding of a given font size
Source code in niceml/utilities/imagegeneration.py
get_scalar_columns_of_tabular_data ¶
The get_scalar_columns_of_tabular_data function returns a list of the scalar columns in the tabular data.
Parameters:
-
tabular_data_location
(Union[dict, LocationConfig]
) –Specify the location of the data
-
tabular_data_file_name
(str
, default:'train.parq'
) –Specify the name of the parquet file containing tabular data
Returns:
-
List[str]
–A list of the scalar columns in the tabular data
Source code in niceml/utilities/imagegeneration.py
load_label_from_json ¶
Loads an object detection label from a file_path
Source code in niceml/utilities/imagegeneration.py
save_image ¶
Saves an image to a file_path and creates folders if not exist
save_image_label_as_json ¶
Saves an ObjDetImageLabel to a file_path and creates folders if not exist