Get images list and annotations list from input dir. Update new images and annotations. Save images and annotations in output dir.
()
| 19 | |
| 20 | |
| 21 | def main() -> None: |
| 22 | """ |
| 23 | Get images list and annotations list from input dir. |
| 24 | Update new images and annotations. |
| 25 | Save images and annotations in output dir. |
| 26 | """ |
| 27 | img_paths, annos = get_dataset(LABEL_DIR, IMG_DIR) |
| 28 | for index in range(NUMBER_IMAGES): |
| 29 | idxs = random.sample(range(len(annos)), 4) |
| 30 | new_image, new_annos, path = update_image_and_anno( |
| 31 | img_paths, |
| 32 | annos, |
| 33 | idxs, |
| 34 | OUTPUT_SIZE, |
| 35 | SCALE_RANGE, |
| 36 | filter_scale=FILTER_TINY_SCALE, |
| 37 | ) |
| 38 | |
| 39 | # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' |
| 40 | letter_code = random_chars(32) |
| 41 | file_name = path.split(os.sep)[-1].rsplit(".", 1)[0] |
| 42 | file_root = f"{OUTPUT_DIR}/{file_name}_MOSAIC_{letter_code}" |
| 43 | cv2.imwrite(f"{file_root}.jpg", new_image, [cv2.IMWRITE_JPEG_QUALITY, 85]) |
| 44 | print(f"Succeeded {index + 1}/{NUMBER_IMAGES} with {file_name}") |
| 45 | annos_list = [] |
| 46 | for anno in new_annos: |
| 47 | width = anno[3] - anno[1] |
| 48 | height = anno[4] - anno[2] |
| 49 | x_center = anno[1] + width / 2 |
| 50 | y_center = anno[2] + height / 2 |
| 51 | obj = f"{anno[0]} {x_center} {y_center} {width} {height}" |
| 52 | annos_list.append(obj) |
| 53 | with open(f"{file_root}.txt", "w") as outfile: |
| 54 | outfile.write("\n".join(line for line in annos_list)) |
| 55 | |
| 56 | |
| 57 | def get_dataset(label_dir: str, img_dir: str) -> tuple[list, list]: |
no test coverage detected