From f2594aff6ae8a4343a4ceb3b10e5bd0e2264bf8e Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Sun, 7 Nov 2021 21:12:36 +0700 Subject: [PATCH 01/18] ADD the algorithms of image augmentation --- computer_vision/flip_augmentation.py | 107 ++++++++++++++++ computer_vision/mosaic_augmentation.py | 164 +++++++++++++++++++++++++ 2 files changed, 271 insertions(+) create mode 100644 computer_vision/flip_augmentation.py create mode 100644 computer_vision/mosaic_augmentation.py diff --git a/computer_vision/flip_augmentation.py b/computer_vision/flip_augmentation.py new file mode 100644 index 000000000000..610fb1e911ae --- /dev/null +++ b/computer_vision/flip_augmentation.py @@ -0,0 +1,107 @@ +import random +import cv2 +import os +import glob + +# Params +LABEL_DIR = '' +IMAGE_DIR = '' +OUTPUT_DIR = '' +FLIP_TYPE = 1 # (0 is vertical, 1 is horizontal) + + +def main(): + img_paths, annos = get_dataset(LABEL_DIR, IMAGE_DIR) + print('Processing...') + new_image, new_annos, path = update_image_and_anno( + img_paths, annos, FLIP_TYPE) + + for index in range(len(new_image)): + # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' + letter_code = random_chars(32) + file_name = path[index].split('/')[-1].rsplit('.', 1)[0] + cv2.imwrite(OUTPUT_DIR + "/{}_FLIP_{}.jpg".format(file_name,letter_code + ), new_image[index], [cv2.IMWRITE_JPEG_QUALITY, 85]) + print('Success {}/{} with {}'.format(index+1, len(new_image), file_name)) + annos_list = [] + for anno in new_annos[index]: + obj = '{} {} {} {} {}'.format( + anno[0], anno[1], anno[2], anno[3], anno[4]) + annos_list.append(obj) + with open(OUTPUT_DIR + "/{}_FLIP.txt".format(file_name), "w") as outfile: + outfile.write("\n".join(line for line in annos_list)) + + +def get_dataset(label_dir, img_dir): + ''' + Params: + - label_dir : Path to label include annotation of images + - img_dir : Path to folder contain images + Return : List of images path and labels + ''' + img_paths = [] + labels = [] + for label_file in glob.glob(os.path.join(label_dir, '*.txt')): + label_name = label_file.split('/')[-1].rsplit('.', 1)[0] + f = open(label_file, 'r') + obj_lists = f.readlines() + img_path = os.path.join(img_dir, f'{label_name}.jpg') + + boxes = [] + for obj_list in obj_lists: + obj = obj_list.rstrip('\n').split(' ') + boxes.append([int(obj[0]), float(obj[1]), + float(obj[2]), float(obj[3]), float(obj[4])]) + if not boxes: + continue + img_paths.append(img_path) + labels.append(boxes) + return img_paths, labels + + +def update_image_and_anno(img_list, anno_list, flip_type=1): + ''' + Params: + - img_list : list of all images + - anno_list : list of all annotations of specific image + - flip_type : 0 is vertical, 1 is horizontal + Return: + - new_imgs_list : image after resize + - new_annos_lists : list of new annotation after scale + - path_list : list the name of image file + ''' + new_annos_lists = [] + path_list = [] + new_imgs_list = [] + for idx in range(len(img_list)): + new_annos = [] + path = img_list[idx] + path_list.append(path) + img_annos = anno_list[idx] + img = cv2.imread(path) + if flip_type == 1: + new_img = cv2.flip(img, flip_type) + for bbox in img_annos: + x_center_new = 1 - bbox[1] + new_annos.append( + [bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]]) + elif flip_type == 0: + new_img = cv2.flip(img, flip_type) + for bbox in img_annos: + y_center_new = 1 - bbox[2] + new_annos.append( + [bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]]) + new_annos_lists.append(new_annos) + new_imgs_list.append(new_img) + return new_imgs_list, new_annos_lists, path_list + + +def random_chars(number_char): + # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' + letter_code = 'abcdefghijklmnopqrstuvwxyz0123456789' + return ''.join(random.choice(letter_code) for _ in range(number_char)) + + +if __name__ == '__main__': + main() + print('DONE ✅') diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py new file mode 100644 index 000000000000..eb42cb204622 --- /dev/null +++ b/computer_vision/mosaic_augmentation.py @@ -0,0 +1,164 @@ +'''Source: https://github.com/jason9075/opencv-mosaic-data-aug''' + +import random +import cv2 +import os +import glob +import numpy as np + +# Parrameters +OUTPUT_SIZE = (720, 1280) # Height, Width +SCALE_RANGE = (0.4, 0.6) # if height or width lower than this scale, drop it. +FILTER_TINY_SCALE = 1 / 100 +LABEL_DIR = '' +IMG_DIR = '' +OUTPUT_DIR = '' +NUMBER_IMAGES = 250 + + +def main(): + img_paths, annos = get_dataset(LABEL_DIR, IMG_DIR) + for index in range(NUMBER_IMAGES): + idxs = random.sample(range(len(annos)), 4) + new_image, new_annos, path = update_image_and_anno(img_paths, annos, + idxs, + OUTPUT_SIZE, SCALE_RANGE, + filter_scale=FILTER_TINY_SCALE) + + # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' + letter_code = random_chars(32) + file_name = path.split('/')[-1].rsplit('.', 1)[0] + cv2.imwrite(OUTPUT_DIR + "/{}_MOSAIC_{}.jpg".format(file_name, + letter_code), new_image, [cv2.IMWRITE_JPEG_QUALITY, 85]) + print('Successed {}/{} with {}'.format(index+1, NUMBER_IMAGES, file_name)) + annos_list = [] + for anno in new_annos: + width = anno[3] - anno[1] + height = anno[4] - anno[2] + x_center = anno[1] + width/2 + y_center = anno[2] + height/2 + obj = '{} {} {} {} {}'.format( + anno[0], x_center, y_center, width, height) + annos_list.append(obj) + with open(OUTPUT_DIR + "/{}_MOSAIC_{}.txt".format(file_name, letter_code), "w") as outfile: + outfile.write("\n".join(line for line in annos_list)) + + +def get_dataset(label_dir, img_dir): + ''' + Params: + - label_dir : Path to label include annotation of images + - img_dir : Path to folder contain images + Return : List of images path and labels + ''' + img_paths = [] + labels = [] + for label_file in glob.glob(os.path.join(label_dir, '*.txt')): + label_name = label_file.split('/')[-1].rsplit('.', 1)[0] + f = open(label_file, 'r') + obj_lists = f.readlines() + img_path = os.path.join(img_dir, f'{label_name}.jpg') + + boxes = [] + for obj_list in obj_lists: + obj = obj_list.rstrip('\n').split(' ') + xmin = float(obj[1]) - float(obj[3])/2 + ymin = float(obj[2]) - float(obj[4])/2 + xmax = float(obj[1]) + float(obj[3])/2 + ymax = float(obj[2]) + float(obj[4])/2 + + boxes.append([int(obj[0]), xmin, ymin, xmax, ymax]) + if not boxes: + continue + img_paths.append(img_path) + labels.append(boxes) + return img_paths, labels + + +def update_image_and_anno(all_img_list, all_annos, idxs, output_size, scale_range, filter_scale=0.): + ''' + Params: + - 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 + - filter_scale : the condition of downscale image and bounding box + Return: + - output_img : image after resize + - new_anno : list of new annotation after scale + - path[0] : get the name of image file + ''' + output_img = np.zeros([output_size[0], output_size[1], 3], dtype=np.uint8) + scale_x = scale_range[0] + \ + random.random() * (scale_range[1] - scale_range[0]) + scale_y = scale_range[0] + \ + random.random() * (scale_range[1] - scale_range[0]) + divid_point_x = int(scale_x * output_size[1]) + divid_point_y = int(scale_y * output_size[0]) + + new_anno = [] + path_list = [] + for i, idx in enumerate(idxs): + path = all_img_list[idx] + path_list.append(path) + img_annos = all_annos[idx] + img = cv2.imread(path) + if i == 0: # top-left + img = cv2.resize(img, (divid_point_x, divid_point_y)) + output_img[:divid_point_y, :divid_point_x, :] = img + for bbox in img_annos: + xmin = bbox[1] * scale_x + ymin = bbox[2] * scale_y + xmax = bbox[3] * scale_x + ymax = bbox[4] * scale_y + new_anno.append([bbox[0], xmin, ymin, xmax, ymax]) + elif i == 1: # top-right + img = cv2.resize( + img, (output_size[1] - divid_point_x, divid_point_y)) + output_img[:divid_point_y, divid_point_x:output_size[1], :] = img + for bbox in img_annos: + xmin = scale_x + bbox[1] * (1 - scale_x) + ymin = bbox[2] * scale_y + xmax = scale_x + bbox[3] * (1 - scale_x) + ymax = bbox[4] * scale_y + new_anno.append([bbox[0], xmin, ymin, xmax, ymax]) + elif i == 2: # bottom-left + img = cv2.resize( + img, (divid_point_x, output_size[0] - divid_point_y)) + output_img[divid_point_y:output_size[0], :divid_point_x, :] = img + for bbox in img_annos: + xmin = bbox[1] * scale_x + ymin = scale_y + bbox[2] * (1 - scale_y) + xmax = bbox[3] * scale_x + ymax = scale_y + bbox[4] * (1 - scale_y) + new_anno.append([bbox[0], xmin, ymin, xmax, ymax]) + else: # bottom-right + img = cv2.resize( + img, (output_size[1] - divid_point_x, output_size[0] - divid_point_y)) + output_img[divid_point_y:output_size[0], + divid_point_x:output_size[1], :] = img + for bbox in img_annos: + xmin = scale_x + bbox[1] * (1 - scale_x) + ymin = scale_y + bbox[2] * (1 - scale_y) + xmax = scale_x + bbox[3] * (1 - scale_x) + ymax = scale_y + bbox[4] * (1 - scale_y) + new_anno.append([bbox[0], xmin, ymin, xmax, ymax]) + + # Remove bounding box small than scale of filter + if 0 < filter_scale: + new_anno = [anno for anno in new_anno if + filter_scale < (anno[3] - anno[1]) and filter_scale < (anno[4] - anno[2])] + + return output_img, new_anno, path_list[0] + + +def random_chars(number_char): + # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' + letter_code = 'abcdefghijklmnopqrstuvwxyz0123456789' + return ''.join(random.choice(letter_code) for _ in range(number_char)) + + +if __name__ == '__main__': + main() + print('DONE ✅') From 39e4b97a520793e83bbbb9789af78ede8ae14cec Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Sun, 7 Nov 2021 21:33:32 +0700 Subject: [PATCH 02/18] ADD the algorithms of image augmentation --- computer_vision/flip_augmentation.py | 22 ++++++++++------------ computer_vision/mosaic_augmentation.py | 20 +++++++++----------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/computer_vision/flip_augmentation.py b/computer_vision/flip_augmentation.py index 610fb1e911ae..a5c89853bcfa 100644 --- a/computer_vision/flip_augmentation.py +++ b/computer_vision/flip_augmentation.py @@ -10,7 +10,7 @@ FLIP_TYPE = 1 # (0 is vertical, 1 is horizontal) -def main(): +def main() -> None: img_paths, annos = get_dataset(LABEL_DIR, IMAGE_DIR) print('Processing...') new_image, new_annos, path = update_image_and_anno( @@ -20,25 +20,23 @@ def main(): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' letter_code = random_chars(32) file_name = path[index].split('/')[-1].rsplit('.', 1)[0] - cv2.imwrite(OUTPUT_DIR + "/{}_FLIP_{}.jpg".format(file_name,letter_code - ), new_image[index], [cv2.IMWRITE_JPEG_QUALITY, 85]) - print('Success {}/{} with {}'.format(index+1, len(new_image), file_name)) + cv2.imwrite(OUTPUT_DIR + f"/{file_name}_FLIP_{letter_code}.jpg", new_image[index], [cv2.IMWRITE_JPEG_QUALITY, 85]) + print(f'Success {index+1}/{len(new_image)} with {file_name}') annos_list = [] for anno in new_annos[index]: - obj = '{} {} {} {} {}'.format( - anno[0], anno[1], anno[2], anno[3], anno[4]) + obj = f'{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}' annos_list.append(obj) - with open(OUTPUT_DIR + "/{}_FLIP.txt".format(file_name), "w") as outfile: + with open(OUTPUT_DIR + f"/{file_name}_FLIP{letter_code}.txt", "w") as outfile: outfile.write("\n".join(line for line in annos_list)) def get_dataset(label_dir, img_dir): - ''' + """ Params: - label_dir : Path to label include annotation of images - img_dir : Path to folder contain images Return : List of images path and labels - ''' + """ img_paths = [] labels = [] for label_file in glob.glob(os.path.join(label_dir, '*.txt')): @@ -60,7 +58,7 @@ def get_dataset(label_dir, img_dir): def update_image_and_anno(img_list, anno_list, flip_type=1): - ''' + """ Params: - img_list : list of all images - anno_list : list of all annotations of specific image @@ -68,8 +66,8 @@ def update_image_and_anno(img_list, anno_list, flip_type=1): Return: - new_imgs_list : image after resize - new_annos_lists : list of new annotation after scale - - path_list : list the name of image file - ''' + - path_list : list the name of image file + """ new_annos_lists = [] path_list = [] new_imgs_list = [] diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py index eb42cb204622..e91d60d315ef 100644 --- a/computer_vision/mosaic_augmentation.py +++ b/computer_vision/mosaic_augmentation.py @@ -16,7 +16,7 @@ NUMBER_IMAGES = 250 -def main(): +def main() -> None: img_paths, annos = get_dataset(LABEL_DIR, IMG_DIR) for index in range(NUMBER_IMAGES): idxs = random.sample(range(len(annos)), 4) @@ -28,29 +28,27 @@ def main(): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' letter_code = random_chars(32) file_name = path.split('/')[-1].rsplit('.', 1)[0] - cv2.imwrite(OUTPUT_DIR + "/{}_MOSAIC_{}.jpg".format(file_name, - letter_code), new_image, [cv2.IMWRITE_JPEG_QUALITY, 85]) - print('Successed {}/{} with {}'.format(index+1, NUMBER_IMAGES, file_name)) + cv2.imwrite(OUTPUT_DIR + f"/{file_name}_MOSAIC_{letter_code}.jpg", new_image, [cv2.IMWRITE_JPEG_QUALITY, 85]) + print(f'Successed {index+1}/{NUMBER_IMAGES} with {file_name}') annos_list = [] for anno in new_annos: width = anno[3] - anno[1] height = anno[4] - anno[2] x_center = anno[1] + width/2 y_center = anno[2] + height/2 - obj = '{} {} {} {} {}'.format( - anno[0], x_center, y_center, width, height) + obj = f'{anno[0]} {x_center} {y_center} {width} {height}' annos_list.append(obj) - with open(OUTPUT_DIR + "/{}_MOSAIC_{}.txt".format(file_name, letter_code), "w") as outfile: + with open(OUTPUT_DIR + f"/{file_name}_MOSAIC_{letter_code}.txt", "w") as outfile: outfile.write("\n".join(line for line in annos_list)) def get_dataset(label_dir, img_dir): - ''' + """ Params: - label_dir : Path to label include annotation of images - img_dir : Path to folder contain images Return : List of images path and labels - ''' + """ img_paths = [] labels = [] for label_file in glob.glob(os.path.join(label_dir, '*.txt')): @@ -76,7 +74,7 @@ def get_dataset(label_dir, img_dir): def update_image_and_anno(all_img_list, all_annos, idxs, output_size, scale_range, filter_scale=0.): - ''' + """ Params: - all_img_list : list of all images - all_annos : list of all annotations of specific image @@ -88,7 +86,7 @@ def update_image_and_anno(all_img_list, all_annos, idxs, output_size, scale_rang - output_img : image after resize - new_anno : list of new annotation after scale - path[0] : get the name of image file - ''' + """ output_img = np.zeros([output_size[0], output_size[1], 3], dtype=np.uint8) scale_x = scale_range[0] + \ random.random() * (scale_range[1] - scale_range[0]) From cb0eef0cb73ef7e8b3797b12f1329f231b9c34c1 Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Sun, 7 Nov 2021 21:38:31 +0700 Subject: [PATCH 03/18] ADD the algorithms of image augmentation --- computer_vision/flip_augmentation.py | 16 +++++++++------- computer_vision/mosaic_augmentation.py | 18 ++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/computer_vision/flip_augmentation.py b/computer_vision/flip_augmentation.py index a5c89853bcfa..751c0ee5fc2b 100644 --- a/computer_vision/flip_augmentation.py +++ b/computer_vision/flip_augmentation.py @@ -3,6 +3,10 @@ import os import glob +""" +Flip image and bounding box for computer vision task +""" + # Params LABEL_DIR = '' IMAGE_DIR = '' @@ -32,9 +36,8 @@ def main() -> None: def get_dataset(label_dir, img_dir): """ - Params: - - label_dir : Path to label include annotation of images - - img_dir : Path to folder contain images + - label_dir : Path to label include annotation of images + - img_dir : Path to folder contain images Return : List of images path and labels """ img_paths = [] @@ -59,10 +62,9 @@ def get_dataset(label_dir, img_dir): def update_image_and_anno(img_list, anno_list, flip_type=1): """ - Params: - - img_list : list of all images - - anno_list : list of all annotations of specific image - - flip_type : 0 is vertical, 1 is horizontal + - img_list : list of all images + - anno_list : list of all annotations of specific image + - flip_type : 0 is vertical, 1 is horizontal Return: - new_imgs_list : image after resize - new_annos_lists : list of new annotation after scale diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py index e91d60d315ef..5088b914b5e0 100644 --- a/computer_vision/mosaic_augmentation.py +++ b/computer_vision/mosaic_augmentation.py @@ -44,9 +44,8 @@ def main() -> None: def get_dataset(label_dir, img_dir): """ - Params: - - label_dir : Path to label include annotation of images - - img_dir : Path to folder contain images + - label_dir : Path to label include annotation of images + - img_dir : Path to folder contain images Return : List of images path and labels """ img_paths = [] @@ -75,13 +74,12 @@ def get_dataset(label_dir, img_dir): def update_image_and_anno(all_img_list, all_annos, idxs, output_size, scale_range, filter_scale=0.): """ - Params: - - 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 - - filter_scale : the condition of downscale image and bounding box + - 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 + - filter_scale : the condition of downscale image and bounding box Return: - output_img : image after resize - new_anno : list of new annotation after scale From cb509aa85ffe8ffc84b2516c18a753bcfcc274d0 Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Sun, 7 Nov 2021 21:49:55 +0700 Subject: [PATCH 04/18] ADD the algorithms of image augmentation --- computer_vision/flip_augmentation.py | 10 ++++++---- computer_vision/mosaic_augmentation.py | 9 +++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/computer_vision/flip_augmentation.py b/computer_vision/flip_augmentation.py index 751c0ee5fc2b..525892fd5d36 100644 --- a/computer_vision/flip_augmentation.py +++ b/computer_vision/flip_augmentation.py @@ -2,9 +2,11 @@ import cv2 import os import glob +from typing import List """ Flip image and bounding box for computer vision task +https://paperswithcode.com/method/randomhorizontalflip """ # Params @@ -34,10 +36,10 @@ def main() -> None: outfile.write("\n".join(line for line in annos_list)) -def get_dataset(label_dir, img_dir): +def get_dataset(label_dir: str, img_dir: str) -> List: """ - - label_dir : Path to label include annotation of images - - img_dir : Path to folder contain images + - label_dir : Path to label include annotation of images + - img_dir : Path to folder contain images Return : List of images path and labels """ img_paths = [] @@ -60,7 +62,7 @@ def get_dataset(label_dir, img_dir): return img_paths, labels -def update_image_and_anno(img_list, anno_list, flip_type=1): +def update_image_and_anno(img_list: List, anno_list: List, flip_type: int=1) -> List: """ - img_list : list of all images - anno_list : list of all annotations of specific image diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py index 5088b914b5e0..4c5441a0f70e 100644 --- a/computer_vision/mosaic_augmentation.py +++ b/computer_vision/mosaic_augmentation.py @@ -5,6 +5,7 @@ import os import glob import numpy as np +from typing import List # Parrameters OUTPUT_SIZE = (720, 1280) # Height, Width @@ -42,10 +43,10 @@ def main() -> None: outfile.write("\n".join(line for line in annos_list)) -def get_dataset(label_dir, img_dir): +def get_dataset(label_dir: str, img_dir: str) -> List: """ - - label_dir : Path to label include annotation of images - - img_dir : Path to folder contain images + - label_dir : Path to label include annotation of images + - img_dir : Path to folder contain images Return : List of images path and labels """ img_paths = [] @@ -72,7 +73,7 @@ def get_dataset(label_dir, img_dir): return img_paths, labels -def update_image_and_anno(all_img_list, all_annos, idxs, output_size, scale_range, filter_scale=0.): +def update_image_and_anno(all_img_list: List, all_annos: List, idxs: int, output_size: int, scale_range: int, filter_scale: int=0.) -> List: """ - all_img_list : list of all images - all_annos : list of all annotations of specific image From b89a6a54ba972fda10c433ed38f2843e74f914bc Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Sun, 7 Nov 2021 21:57:43 +0700 Subject: [PATCH 05/18] ADD the algorithms of image augmentation --- computer_vision/flip_augmentation.py | 7 ++++++- computer_vision/mosaic_augmentation.py | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/computer_vision/flip_augmentation.py b/computer_vision/flip_augmentation.py index 525892fd5d36..77744b7bb676 100644 --- a/computer_vision/flip_augmentation.py +++ b/computer_vision/flip_augmentation.py @@ -17,6 +17,11 @@ def main() -> None: + """ + Get images list and annotations list from input dir. + Update new images and annotations. + Save images and annotations in output dir. + """ img_paths, annos = get_dataset(LABEL_DIR, IMAGE_DIR) print('Processing...') new_image, new_annos, path = update_image_and_anno( @@ -98,7 +103,7 @@ def update_image_and_anno(img_list: List, anno_list: List, flip_type: int=1) -> return new_imgs_list, new_annos_lists, path_list -def random_chars(number_char): +def random_chars(number_char: int) -> str: # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' letter_code = 'abcdefghijklmnopqrstuvwxyz0123456789' return ''.join(random.choice(letter_code) for _ in range(number_char)) diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py index 4c5441a0f70e..7ec537d268df 100644 --- a/computer_vision/mosaic_augmentation.py +++ b/computer_vision/mosaic_augmentation.py @@ -18,6 +18,11 @@ def main() -> None: + """ + Get images list and annotations list from input dir. + Update new images and annotations. + Save images and annotations in output dir. + """ img_paths, annos = get_dataset(LABEL_DIR, IMG_DIR) for index in range(NUMBER_IMAGES): idxs = random.sample(range(len(annos)), 4) @@ -150,7 +155,7 @@ def update_image_and_anno(all_img_list: List, all_annos: List, idxs: int, output return output_img, new_anno, path_list[0] -def random_chars(number_char): +def random_chars(number_char: int) -> str: # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' letter_code = 'abcdefghijklmnopqrstuvwxyz0123456789' return ''.join(random.choice(letter_code) for _ in range(number_char)) From a659ade0e21fa9503f5877fb9e25138eac1a395a Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Sun, 7 Nov 2021 22:00:46 +0700 Subject: [PATCH 06/18] ADD the algorithms of image augmentation --- computer_vision/flip_augmentation.py | 5 ++++- computer_vision/mosaic_augmentation.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/computer_vision/flip_augmentation.py b/computer_vision/flip_augmentation.py index 77744b7bb676..26bf323636bb 100644 --- a/computer_vision/flip_augmentation.py +++ b/computer_vision/flip_augmentation.py @@ -104,7 +104,10 @@ def update_image_and_anno(img_list: List, anno_list: List, flip_type: int=1) -> def random_chars(number_char: int) -> str: - # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' + """ + Automatic generate random 32 characters. + Get random string code: '7b7ad245cdff75241935e4dd860f3bad' + """ letter_code = 'abcdefghijklmnopqrstuvwxyz0123456789' return ''.join(random.choice(letter_code) for _ in range(number_char)) diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py index 7ec537d268df..b64b84a50e81 100644 --- a/computer_vision/mosaic_augmentation.py +++ b/computer_vision/mosaic_augmentation.py @@ -156,7 +156,10 @@ def update_image_and_anno(all_img_list: List, all_annos: List, idxs: int, output def random_chars(number_char: int) -> str: - # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' + """ + Automatic generate random 32 characters. + Get random string code: '7b7ad245cdff75241935e4dd860f3bad' + """ letter_code = 'abcdefghijklmnopqrstuvwxyz0123456789' return ''.join(random.choice(letter_code) for _ in range(number_char)) From c978ce1f9077b7010794ad1715ddf8ce05159492 Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Sun, 7 Nov 2021 22:41:45 +0700 Subject: [PATCH 07/18] UPDATE format code --- computer_vision/flip_augmentation.py | 14 ++++++++------ computer_vision/mosaic_augmentation.py | 7 +++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/computer_vision/flip_augmentation.py b/computer_vision/flip_augmentation.py index 26bf323636bb..d52fbd92b25f 100644 --- a/computer_vision/flip_augmentation.py +++ b/computer_vision/flip_augmentation.py @@ -31,13 +31,13 @@ def main() -> None: # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' letter_code = random_chars(32) file_name = path[index].split('/')[-1].rsplit('.', 1)[0] - cv2.imwrite(OUTPUT_DIR + f"/{file_name}_FLIP_{letter_code}.jpg", new_image[index], [cv2.IMWRITE_JPEG_QUALITY, 85]) + file_root = f"{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}" + cv2.imwrite(f"/{file_root}.jpg", new_image[index], [cv2.IMWRITE_JPEG_QUALITY, 85]) print(f'Success {index+1}/{len(new_image)} with {file_name}') annos_list = [] - for anno in new_annos[index]: - obj = f'{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}' - annos_list.append(obj) - with open(OUTPUT_DIR + f"/{file_name}_FLIP{letter_code}.txt", "w") as outfile: + obj = ["{} {} {} {} {}".format(*anno) for anno in new_annos[index]] + annos_list.append(obj[0]) + with open(f"/{file_root}.txt", "w") as outfile: outfile.write("\n".join(line for line in annos_list)) @@ -103,10 +103,12 @@ def update_image_and_anno(img_list: List, anno_list: List, flip_type: int=1) -> return new_imgs_list, new_annos_lists, path_list -def random_chars(number_char: int) -> str: +def random_chars(number_char: int=32) -> str: """ Automatic generate random 32 characters. Get random string code: '7b7ad245cdff75241935e4dd860f3bad' + >>> random_chars(32) + '7b7ad245cdff75241935e4dd860f3bad' """ letter_code = 'abcdefghijklmnopqrstuvwxyz0123456789' return ''.join(random.choice(letter_code) for _ in range(number_char)) diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py index b64b84a50e81..7395e1ce837a 100644 --- a/computer_vision/mosaic_augmentation.py +++ b/computer_vision/mosaic_augmentation.py @@ -34,7 +34,8 @@ def main() -> None: # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' letter_code = random_chars(32) file_name = path.split('/')[-1].rsplit('.', 1)[0] - cv2.imwrite(OUTPUT_DIR + f"/{file_name}_MOSAIC_{letter_code}.jpg", new_image, [cv2.IMWRITE_JPEG_QUALITY, 85]) + file_root = f"{OUTPUT_DIR}/{file_name}_MOSAIC_{letter_code}" + cv2.imwrite(f"{file_root}.jpg", new_image, [cv2.IMWRITE_JPEG_QUALITY, 85]) print(f'Successed {index+1}/{NUMBER_IMAGES} with {file_name}') annos_list = [] for anno in new_annos: @@ -44,7 +45,7 @@ def main() -> None: y_center = anno[2] + height/2 obj = f'{anno[0]} {x_center} {y_center} {width} {height}' annos_list.append(obj) - with open(OUTPUT_DIR + f"/{file_name}_MOSAIC_{letter_code}.txt", "w") as outfile: + with open(f"{file_root}.txt", "w") as outfile: outfile.write("\n".join(line for line in annos_list)) @@ -159,6 +160,8 @@ def random_chars(number_char: int) -> str: """ Automatic generate random 32 characters. Get random string code: '7b7ad245cdff75241935e4dd860f3bad' + >>> random_chars(32) + '7b7ad245cdff75241935e4dd860f3bad' """ letter_code = 'abcdefghijklmnopqrstuvwxyz0123456789' return ''.join(random.choice(letter_code) for _ in range(number_char)) From 338b595bbd8020b7392ea367571805b6731d705f Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Sun, 7 Nov 2021 23:03:43 +0700 Subject: [PATCH 08/18] UPDATE format and recode structure --- computer_vision/flip_augmentation.py | 27 ++++++++++++++++---------- computer_vision/mosaic_augmentation.py | 24 +++++++++++++++-------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/computer_vision/flip_augmentation.py b/computer_vision/flip_augmentation.py index d52fbd92b25f..4cd64aedb79a 100644 --- a/computer_vision/flip_augmentation.py +++ b/computer_vision/flip_augmentation.py @@ -1,8 +1,10 @@ import random -import cv2 import os import glob -from typing import List +from string import digits, ascii_lowercase + +import numpy as np +import cv2 """ Flip image and bounding box for computer vision task @@ -21,6 +23,7 @@ def main() -> None: Get images list and annotations list from input dir. Update new images and annotations. Save images and annotations in output dir. + >>> pass # A doctest is not possible for this function. """ img_paths, annos = get_dataset(LABEL_DIR, IMAGE_DIR) print('Processing...') @@ -32,7 +35,8 @@ def main() -> None: letter_code = random_chars(32) file_name = path[index].split('/')[-1].rsplit('.', 1)[0] file_root = f"{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}" - cv2.imwrite(f"/{file_root}.jpg", new_image[index], [cv2.IMWRITE_JPEG_QUALITY, 85]) + cv2.imwrite(f"/{file_root}.jpg", + new_image[index], [cv2.IMWRITE_JPEG_QUALITY, 85]) print(f'Success {index+1}/{len(new_image)} with {file_name}') annos_list = [] obj = ["{} {} {} {} {}".format(*anno) for anno in new_annos[index]] @@ -41,18 +45,19 @@ def main() -> None: outfile.write("\n".join(line for line in annos_list)) -def get_dataset(label_dir: str, img_dir: str) -> List: +def get_dataset(label_dir: str, img_dir: str) -> list: """ - label_dir : Path to label include annotation of images - img_dir : Path to folder contain images - Return : List of images path and labels + Return : List of images path and labels + >>> pass # A doctest is not possible for this function. """ img_paths = [] labels = [] for label_file in glob.glob(os.path.join(label_dir, '*.txt')): label_name = label_file.split('/')[-1].rsplit('.', 1)[0] - f = open(label_file, 'r') - obj_lists = f.readlines() + with open(label_file, 'r') as in_file: + obj_lists = in_file.readlines() img_path = os.path.join(img_dir, f'{label_name}.jpg') boxes = [] @@ -67,7 +72,7 @@ def get_dataset(label_dir: str, img_dir: str) -> List: return img_paths, labels -def update_image_and_anno(img_list: List, anno_list: List, flip_type: int=1) -> List: +def update_image_and_anno(img_list: list, anno_list: list, flip_type: int = 1) -> list: """ - img_list : list of all images - anno_list : list of all annotations of specific image @@ -76,6 +81,7 @@ def update_image_and_anno(img_list: List, anno_list: List, flip_type: int=1) -> - new_imgs_list : image after resize - new_annos_lists : list of new annotation after scale - path_list : list the name of image file + >>> pass # A doctest is not possible for this function. """ new_annos_lists = [] path_list = [] @@ -103,14 +109,15 @@ def update_image_and_anno(img_list: List, anno_list: List, flip_type: int=1) -> return new_imgs_list, new_annos_lists, path_list -def random_chars(number_char: int=32) -> str: +def random_chars(number_char: int = 32) -> str: """ Automatic generate random 32 characters. Get random string code: '7b7ad245cdff75241935e4dd860f3bad' >>> random_chars(32) '7b7ad245cdff75241935e4dd860f3bad' """ - letter_code = 'abcdefghijklmnopqrstuvwxyz0123456789' + assert number_char > 1, "The number of character should greater than 1" + letter_code = ascii_lowercase + digits return ''.join(random.choice(letter_code) for _ in range(number_char)) diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py index 7395e1ce837a..d7330b66ecb8 100644 --- a/computer_vision/mosaic_augmentation.py +++ b/computer_vision/mosaic_augmentation.py @@ -1,11 +1,12 @@ '''Source: https://github.com/jason9075/opencv-mosaic-data-aug''' import random -import cv2 import os import glob +from string import digits, ascii_lowercase + import numpy as np -from typing import List +import cv2 # Parrameters OUTPUT_SIZE = (720, 1280) # Height, Width @@ -22,6 +23,7 @@ def main() -> None: Get images list and annotations list from input dir. Update new images and annotations. Save images and annotations in output dir. + >>> pass # A doctest is not possible for this function. """ img_paths, annos = get_dataset(LABEL_DIR, IMG_DIR) for index in range(NUMBER_IMAGES): @@ -35,7 +37,8 @@ def main() -> None: letter_code = random_chars(32) file_name = path.split('/')[-1].rsplit('.', 1)[0] file_root = f"{OUTPUT_DIR}/{file_name}_MOSAIC_{letter_code}" - cv2.imwrite(f"{file_root}.jpg", new_image, [cv2.IMWRITE_JPEG_QUALITY, 85]) + cv2.imwrite(f"{file_root}.jpg", new_image, + [cv2.IMWRITE_JPEG_QUALITY, 85]) print(f'Successed {index+1}/{NUMBER_IMAGES} with {file_name}') annos_list = [] for anno in new_annos: @@ -49,18 +52,19 @@ def main() -> None: outfile.write("\n".join(line for line in annos_list)) -def get_dataset(label_dir: str, img_dir: str) -> List: +def get_dataset(label_dir: str, img_dir: str) -> list: """ - label_dir : Path to label include annotation of images - img_dir : Path to folder contain images Return : List of images path and labels + >>> pass # A doctest is not possible for this function. """ img_paths = [] labels = [] for label_file in glob.glob(os.path.join(label_dir, '*.txt')): label_name = label_file.split('/')[-1].rsplit('.', 1)[0] - f = open(label_file, 'r') - obj_lists = f.readlines() + with open(label_file, 'r') as in_file: + obj_lists = in_file.readlines() img_path = os.path.join(img_dir, f'{label_name}.jpg') boxes = [] @@ -79,7 +83,7 @@ def get_dataset(label_dir: str, img_dir: str) -> List: return img_paths, labels -def update_image_and_anno(all_img_list: List, all_annos: List, idxs: int, output_size: int, scale_range: int, filter_scale: int=0.) -> List: +def update_image_and_anno(all_img_list: list, all_annos: list, idxs: int, output_size: int, scale_range: int, filter_scale: int = 0.) -> list: """ - all_img_list : list of all images - all_annos : list of all annotations of specific image @@ -91,6 +95,7 @@ def update_image_and_anno(all_img_list: List, all_annos: List, idxs: int, output - output_img : image after resize - new_anno : list of new annotation after scale - path[0] : get the name of image file + >>> pass # A doctest is not possible for this function. """ output_img = np.zeros([output_size[0], output_size[1], 3], dtype=np.uint8) scale_x = scale_range[0] + \ @@ -162,8 +167,11 @@ def random_chars(number_char: int) -> str: Get random string code: '7b7ad245cdff75241935e4dd860f3bad' >>> random_chars(32) '7b7ad245cdff75241935e4dd860f3bad' + >>> len(random_chars(32)) + 32 """ - letter_code = 'abcdefghijklmnopqrstuvwxyz0123456789' + assert number_char > 1, "The number of character should greater than 1" + letter_code = ascii_lowercase + digits return ''.join(random.choice(letter_code) for _ in range(number_char)) From 92589111bac10728073ae789636fd6ad5a9baa03 Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Sun, 7 Nov 2021 23:07:57 +0700 Subject: [PATCH 09/18] UPDATE format import library --- computer_vision/flip_augmentation.py | 7 ++++--- computer_vision/mosaic_augmentation.py | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/computer_vision/flip_augmentation.py b/computer_vision/flip_augmentation.py index 4cd64aedb79a..68b04c86c53f 100644 --- a/computer_vision/flip_augmentation.py +++ b/computer_vision/flip_augmentation.py @@ -1,10 +1,11 @@ -import random -import os import glob +import os +import random from string import digits, ascii_lowercase -import numpy as np import cv2 +import numpy as np + """ Flip image and bounding box for computer vision task diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py index d7330b66ecb8..97be95db1d37 100644 --- a/computer_vision/mosaic_augmentation.py +++ b/computer_vision/mosaic_augmentation.py @@ -1,12 +1,13 @@ '''Source: https://github.com/jason9075/opencv-mosaic-data-aug''' -import random -import os import glob +import os +import random from string import digits, ascii_lowercase -import numpy as np import cv2 +import numpy as np + # Parrameters OUTPUT_SIZE = (720, 1280) # Height, Width From 4d0a5a19a9f982f453b17d28da3288cf0f2c322d Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Sun, 7 Nov 2021 23:10:53 +0700 Subject: [PATCH 10/18] UPDATE code structure --- computer_vision/flip_augmentation.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/computer_vision/flip_augmentation.py b/computer_vision/flip_augmentation.py index 68b04c86c53f..d05a0d7d746b 100644 --- a/computer_vision/flip_augmentation.py +++ b/computer_vision/flip_augmentation.py @@ -40,8 +40,9 @@ def main() -> None: new_image[index], [cv2.IMWRITE_JPEG_QUALITY, 85]) print(f'Success {index+1}/{len(new_image)} with {file_name}') annos_list = [] - obj = ["{} {} {} {} {}".format(*anno) for anno in new_annos[index]] - annos_list.append(obj[0]) + for anno in new_annos[index]: + obj = f"{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}" + annos_list.append(obj) with open(f"/{file_root}.txt", "w") as outfile: outfile.write("\n".join(line for line in annos_list)) From 6d85e8e6379a9e67fb28593697d91574bd6b848a Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Sun, 7 Nov 2021 23:46:19 +0700 Subject: [PATCH 11/18] Fix all checks have failded --- computer_vision/flip_augmentation.py | 53 +++++++++------- computer_vision/mosaic_augmentation.py | 85 +++++++++++++++----------- 2 files changed, 81 insertions(+), 57 deletions(-) diff --git a/computer_vision/flip_augmentation.py b/computer_vision/flip_augmentation.py index d05a0d7d746b..af3c9db24652 100644 --- a/computer_vision/flip_augmentation.py +++ b/computer_vision/flip_augmentation.py @@ -13,9 +13,9 @@ """ # Params -LABEL_DIR = '' -IMAGE_DIR = '' -OUTPUT_DIR = '' +LABEL_DIR = "" +IMAGE_DIR = "" +OUTPUT_DIR = "" FLIP_TYPE = 1 # (0 is vertical, 1 is horizontal) @@ -27,18 +27,20 @@ def main() -> None: >>> pass # A doctest is not possible for this function. """ img_paths, annos = get_dataset(LABEL_DIR, IMAGE_DIR) - print('Processing...') + print("Processing...") new_image, new_annos, path = update_image_and_anno( img_paths, annos, FLIP_TYPE) for index in range(len(new_image)): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' letter_code = random_chars(32) - file_name = path[index].split('/')[-1].rsplit('.', 1)[0] + file_name = path[index].split("/")[-1].rsplit(".", 1)[0] file_root = f"{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}" - cv2.imwrite(f"/{file_root}.jpg", - new_image[index], [cv2.IMWRITE_JPEG_QUALITY, 85]) - print(f'Success {index+1}/{len(new_image)} with {file_name}') + cv2.imwrite( + f"/{file_root}.jpg", new_image[index], [ + cv2.IMWRITE_JPEG_QUALITY, 85] + ) + print(f"Success {index+1}/{len(new_image)} with {file_name}") annos_list = [] for anno in new_annos[index]: obj = f"{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}" @@ -52,21 +54,28 @@ def get_dataset(label_dir: str, img_dir: str) -> list: - label_dir : Path to label include annotation of images - img_dir : Path to folder contain images Return : List of images path and labels - >>> pass # A doctest is not possible for this function. + >>> pass # A doctest is not possible for this function. """ img_paths = [] labels = [] - for label_file in glob.glob(os.path.join(label_dir, '*.txt')): - label_name = label_file.split('/')[-1].rsplit('.', 1)[0] - with open(label_file, 'r') as in_file: + for label_file in glob.glob(os.path.join(label_dir, "*.txt")): + label_name = label_file.split("/")[-1].rsplit(".", 1)[0] + with open(label_file, "r") as in_file: obj_lists = in_file.readlines() - img_path = os.path.join(img_dir, f'{label_name}.jpg') + img_path = os.path.join(img_dir, f"{label_name}.jpg") boxes = [] for obj_list in obj_lists: - obj = obj_list.rstrip('\n').split(' ') - boxes.append([int(obj[0]), float(obj[1]), - float(obj[2]), float(obj[3]), float(obj[4])]) + obj = obj_list.rstrip("\n").split(" ") + boxes.append( + [ + int(obj[0]), + float(obj[1]), + float(obj[2]), + float(obj[3]), + float(obj[4]), + ] + ) if not boxes: continue img_paths.append(img_path) @@ -82,7 +91,7 @@ def update_image_and_anno(img_list: list, anno_list: list, flip_type: int = 1) - Return: - new_imgs_list : image after resize - new_annos_lists : list of new annotation after scale - - path_list : list the name of image file + - path_list : list the name of image file >>> pass # A doctest is not possible for this function. """ new_annos_lists = [] @@ -115,14 +124,14 @@ def random_chars(number_char: int = 32) -> str: """ Automatic generate random 32 characters. Get random string code: '7b7ad245cdff75241935e4dd860f3bad' - >>> random_chars(32) - '7b7ad245cdff75241935e4dd860f3bad' + >>> len(random_chars(32)) + 32 """ assert number_char > 1, "The number of character should greater than 1" letter_code = ascii_lowercase + digits - return ''.join(random.choice(letter_code) for _ in range(number_char)) + return "".join(random.choice(letter_code) for _ in range(number_char)) -if __name__ == '__main__': +if __name__ == "__main__": main() - print('DONE ✅') + print("DONE ✅") diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py index 97be95db1d37..63ddb210a113 100644 --- a/computer_vision/mosaic_augmentation.py +++ b/computer_vision/mosaic_augmentation.py @@ -1,4 +1,4 @@ -'''Source: https://github.com/jason9075/opencv-mosaic-data-aug''' +"""Source: https://github.com/jason9075/opencv-mosaic-data-aug""" import glob import os @@ -13,9 +13,9 @@ OUTPUT_SIZE = (720, 1280) # Height, Width SCALE_RANGE = (0.4, 0.6) # if height or width lower than this scale, drop it. FILTER_TINY_SCALE = 1 / 100 -LABEL_DIR = '' -IMG_DIR = '' -OUTPUT_DIR = '' +LABEL_DIR = "" +IMG_DIR = "" +OUTPUT_DIR = "" NUMBER_IMAGES = 250 @@ -29,25 +29,29 @@ def main() -> None: img_paths, annos = get_dataset(LABEL_DIR, IMG_DIR) for index in range(NUMBER_IMAGES): idxs = random.sample(range(len(annos)), 4) - new_image, new_annos, path = update_image_and_anno(img_paths, annos, - idxs, - OUTPUT_SIZE, SCALE_RANGE, - filter_scale=FILTER_TINY_SCALE) + new_image, new_annos, path = update_image_and_anno( + img_paths, + annos, + idxs, + OUTPUT_SIZE, + SCALE_RANGE, + filter_scale=FILTER_TINY_SCALE, + ) # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' letter_code = random_chars(32) - file_name = path.split('/')[-1].rsplit('.', 1)[0] + file_name = path.split("/")[-1].rsplit(".", 1)[0] file_root = f"{OUTPUT_DIR}/{file_name}_MOSAIC_{letter_code}" cv2.imwrite(f"{file_root}.jpg", new_image, [cv2.IMWRITE_JPEG_QUALITY, 85]) - print(f'Successed {index+1}/{NUMBER_IMAGES} with {file_name}') + print(f"Successed {index+1}/{NUMBER_IMAGES} with {file_name}") annos_list = [] for anno in new_annos: width = anno[3] - anno[1] height = anno[4] - anno[2] - x_center = anno[1] + width/2 - y_center = anno[2] + height/2 - obj = f'{anno[0]} {x_center} {y_center} {width} {height}' + x_center = anno[1] + width / 2 + y_center = anno[2] + height / 2 + obj = f"{anno[0]} {x_center} {y_center} {width} {height}" annos_list.append(obj) with open(f"{file_root}.txt", "w") as outfile: outfile.write("\n".join(line for line in annos_list)) @@ -62,19 +66,19 @@ def get_dataset(label_dir: str, img_dir: str) -> list: """ img_paths = [] labels = [] - for label_file in glob.glob(os.path.join(label_dir, '*.txt')): - label_name = label_file.split('/')[-1].rsplit('.', 1)[0] - with open(label_file, 'r') as in_file: + for label_file in glob.glob(os.path.join(label_dir, "*.txt")): + label_name = label_file.split("/")[-1].rsplit(".", 1)[0] + with open(label_file, "r") as in_file: obj_lists = in_file.readlines() - img_path = os.path.join(img_dir, f'{label_name}.jpg') + img_path = os.path.join(img_dir, f"{label_name}.jpg") boxes = [] for obj_list in obj_lists: - obj = obj_list.rstrip('\n').split(' ') - xmin = float(obj[1]) - float(obj[3])/2 - ymin = float(obj[2]) - float(obj[4])/2 - xmax = float(obj[1]) + float(obj[3])/2 - ymax = float(obj[2]) + float(obj[4])/2 + obj = obj_list.rstrip("\n").split(" ") + xmin = float(obj[1]) - float(obj[3]) / 2 + ymin = float(obj[2]) - float(obj[4]) / 2 + xmax = float(obj[1]) + float(obj[3]) / 2 + ymax = float(obj[2]) + float(obj[4]) / 2 boxes.append([int(obj[0]), xmin, ymin, xmax, ymax]) if not boxes: @@ -84,7 +88,14 @@ def get_dataset(label_dir: str, img_dir: str) -> list: return img_paths, labels -def update_image_and_anno(all_img_list: list, all_annos: list, idxs: int, output_size: int, scale_range: int, filter_scale: int = 0.) -> list: +def update_image_and_anno( + all_img_list: list, + all_annos: list, + idxs: int, + output_size: int, + scale_range: int, + filter_scale: int = 0.0, +) -> list: """ - all_img_list : list of all images - all_annos : list of all annotations of specific image @@ -125,7 +136,7 @@ def update_image_and_anno(all_img_list: list, all_annos: list, idxs: int, output elif i == 1: # top-right img = cv2.resize( img, (output_size[1] - divid_point_x, divid_point_y)) - output_img[:divid_point_y, divid_point_x:output_size[1], :] = img + output_img[:divid_point_y, divid_point_x: output_size[1], :] = img for bbox in img_annos: xmin = scale_x + bbox[1] * (1 - scale_x) ymin = bbox[2] * scale_y @@ -135,7 +146,7 @@ def update_image_and_anno(all_img_list: list, all_annos: list, idxs: int, output elif i == 2: # bottom-left img = cv2.resize( img, (divid_point_x, output_size[0] - divid_point_y)) - output_img[divid_point_y:output_size[0], :divid_point_x, :] = img + output_img[divid_point_y: output_size[0], :divid_point_x, :] = img for bbox in img_annos: xmin = bbox[1] * scale_x ymin = scale_y + bbox[2] * (1 - scale_y) @@ -144,9 +155,12 @@ def update_image_and_anno(all_img_list: list, all_annos: list, idxs: int, output new_anno.append([bbox[0], xmin, ymin, xmax, ymax]) else: # bottom-right img = cv2.resize( - img, (output_size[1] - divid_point_x, output_size[0] - divid_point_y)) - output_img[divid_point_y:output_size[0], - divid_point_x:output_size[1], :] = img + img, (output_size[1] - divid_point_x, + output_size[0] - divid_point_y) + ) + output_img[ + divid_point_y: output_size[0], divid_point_x: output_size[1], : + ] = img for bbox in img_annos: xmin = scale_x + bbox[1] * (1 - scale_x) ymin = scale_y + bbox[2] * (1 - scale_y) @@ -156,8 +170,11 @@ def update_image_and_anno(all_img_list: list, all_annos: list, idxs: int, output # Remove bounding box small than scale of filter if 0 < filter_scale: - new_anno = [anno for anno in new_anno if - filter_scale < (anno[3] - anno[1]) and filter_scale < (anno[4] - anno[2])] + new_anno = [ + anno + for anno in new_anno + if filter_scale < (anno[3] - anno[1]) and filter_scale < (anno[4] - anno[2]) + ] return output_img, new_anno, path_list[0] @@ -166,16 +183,14 @@ def random_chars(number_char: int) -> str: """ Automatic generate random 32 characters. Get random string code: '7b7ad245cdff75241935e4dd860f3bad' - >>> random_chars(32) - '7b7ad245cdff75241935e4dd860f3bad' >>> len(random_chars(32)) 32 """ assert number_char > 1, "The number of character should greater than 1" letter_code = ascii_lowercase + digits - return ''.join(random.choice(letter_code) for _ in range(number_char)) + return "".join(random.choice(letter_code) for _ in range(number_char)) -if __name__ == '__main__': +if __name__ == "__main__": main() - print('DONE ✅') + print("DONE ✅") From f6136b2df410dca769dca34572a65c072a6a60a1 Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Mon, 8 Nov 2021 00:00:36 +0700 Subject: [PATCH 12/18] FIX variable format --- computer_vision/flip_augmentation.py | 18 ++++++------ computer_vision/mosaic_augmentation.py | 40 +++++++++++--------------- 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/computer_vision/flip_augmentation.py b/computer_vision/flip_augmentation.py index af3c9db24652..028b4eb5b3f0 100644 --- a/computer_vision/flip_augmentation.py +++ b/computer_vision/flip_augmentation.py @@ -28,8 +28,7 @@ def main() -> None: """ img_paths, annos = get_dataset(LABEL_DIR, IMAGE_DIR) print("Processing...") - new_image, new_annos, path = update_image_and_anno( - img_paths, annos, FLIP_TYPE) + new_image, new_annos, path = update_image_and_anno(img_paths, annos, FLIP_TYPE) for index in range(len(new_image)): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' @@ -37,8 +36,7 @@ def main() -> None: file_name = path[index].split("/")[-1].rsplit(".", 1)[0] file_root = f"{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}" cv2.imwrite( - f"/{file_root}.jpg", new_image[index], [ - cv2.IMWRITE_JPEG_QUALITY, 85] + f"/{file_root}.jpg", new_image[index], [cv2.IMWRITE_JPEG_QUALITY, 85] ) print(f"Success {index+1}/{len(new_image)} with {file_name}") annos_list = [] @@ -49,7 +47,7 @@ def main() -> None: outfile.write("\n".join(line for line in annos_list)) -def get_dataset(label_dir: str, img_dir: str) -> list: +def get_dataset(label_dir: str, img_dir: str) -> tuple(list, list): """ - label_dir : Path to label include annotation of images - img_dir : Path to folder contain images @@ -83,7 +81,9 @@ def get_dataset(label_dir: str, img_dir: str) -> list: return img_paths, labels -def update_image_and_anno(img_list: list, anno_list: list, flip_type: int = 1) -> list: +def update_image_and_anno( + img_list: list, anno_list: list, flip_type: int = 1 +) -> tuple(list, list, list): """ - img_list : list of all images - anno_list : list of all annotations of specific image @@ -107,14 +107,12 @@ def update_image_and_anno(img_list: list, anno_list: list, flip_type: int = 1) - new_img = cv2.flip(img, flip_type) for bbox in img_annos: x_center_new = 1 - bbox[1] - new_annos.append( - [bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]]) + new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]]) elif flip_type == 0: new_img = cv2.flip(img, flip_type) for bbox in img_annos: y_center_new = 1 - bbox[2] - new_annos.append( - [bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]]) + new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]]) new_annos_lists.append(new_annos) new_imgs_list.append(new_img) return new_imgs_list, new_annos_lists, path_list diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py index 63ddb210a113..f04c4ea9db5f 100644 --- a/computer_vision/mosaic_augmentation.py +++ b/computer_vision/mosaic_augmentation.py @@ -42,8 +42,7 @@ def main() -> None: letter_code = random_chars(32) file_name = path.split("/")[-1].rsplit(".", 1)[0] file_root = f"{OUTPUT_DIR}/{file_name}_MOSAIC_{letter_code}" - cv2.imwrite(f"{file_root}.jpg", new_image, - [cv2.IMWRITE_JPEG_QUALITY, 85]) + cv2.imwrite(f"{file_root}.jpg", new_image, [cv2.IMWRITE_JPEG_QUALITY, 85]) print(f"Successed {index+1}/{NUMBER_IMAGES} with {file_name}") annos_list = [] for anno in new_annos: @@ -57,7 +56,7 @@ def main() -> None: outfile.write("\n".join(line for line in annos_list)) -def get_dataset(label_dir: str, img_dir: str) -> list: +def get_dataset(label_dir: str, img_dir: str) -> tuple(list, list): """ - label_dir : Path to label include annotation of images - img_dir : Path to folder contain images @@ -92,10 +91,10 @@ def update_image_and_anno( all_img_list: list, all_annos: list, idxs: int, - output_size: int, - scale_range: int, - filter_scale: int = 0.0, -) -> list: + output_size: tuple(int, int), + scale_range: tuple(float, float), + filter_scale: float = 0.0, +) -> tuple(list, list, str): """ - all_img_list : list of all images - all_annos : list of all annotations of specific image @@ -110,19 +109,17 @@ def update_image_and_anno( >>> pass # A doctest is not possible for this function. """ output_img = np.zeros([output_size[0], output_size[1], 3], dtype=np.uint8) - scale_x = scale_range[0] + \ - random.random() * (scale_range[1] - scale_range[0]) - scale_y = scale_range[0] + \ - random.random() * (scale_range[1] - scale_range[0]) + scale_x = scale_range[0] + random.random() * (scale_range[1] - scale_range[0]) + scale_y = scale_range[0] + random.random() * (scale_range[1] - scale_range[0]) divid_point_x = int(scale_x * output_size[1]) divid_point_y = int(scale_y * output_size[0]) new_anno = [] path_list = [] - for i, idx in enumerate(idxs): - path = all_img_list[idx] + for i, index in enumerate(idxs): + path = all_img_list[index] path_list.append(path) - img_annos = all_annos[idx] + img_annos = all_annos[index] img = cv2.imread(path) if i == 0: # top-left img = cv2.resize(img, (divid_point_x, divid_point_y)) @@ -134,9 +131,8 @@ def update_image_and_anno( ymax = bbox[4] * scale_y new_anno.append([bbox[0], xmin, ymin, xmax, ymax]) elif i == 1: # top-right - img = cv2.resize( - img, (output_size[1] - divid_point_x, divid_point_y)) - output_img[:divid_point_y, divid_point_x: output_size[1], :] = img + img = cv2.resize(img, (output_size[1] - divid_point_x, divid_point_y)) + output_img[:divid_point_y, divid_point_x : output_size[1], :] = img for bbox in img_annos: xmin = scale_x + bbox[1] * (1 - scale_x) ymin = bbox[2] * scale_y @@ -144,9 +140,8 @@ def update_image_and_anno( ymax = bbox[4] * scale_y new_anno.append([bbox[0], xmin, ymin, xmax, ymax]) elif i == 2: # bottom-left - img = cv2.resize( - img, (divid_point_x, output_size[0] - divid_point_y)) - output_img[divid_point_y: output_size[0], :divid_point_x, :] = img + img = cv2.resize(img, (divid_point_x, output_size[0] - divid_point_y)) + output_img[divid_point_y : output_size[0], :divid_point_x, :] = img for bbox in img_annos: xmin = bbox[1] * scale_x ymin = scale_y + bbox[2] * (1 - scale_y) @@ -155,11 +150,10 @@ def update_image_and_anno( new_anno.append([bbox[0], xmin, ymin, xmax, ymax]) else: # bottom-right img = cv2.resize( - img, (output_size[1] - divid_point_x, - output_size[0] - divid_point_y) + img, (output_size[1] - divid_point_x, output_size[0] - divid_point_y) ) output_img[ - divid_point_y: output_size[0], divid_point_x: output_size[1], : + divid_point_y : output_size[0], divid_point_x : output_size[1], : ] = img for bbox in img_annos: xmin = scale_x + bbox[1] * (1 - scale_x) From 0aac0898a867507c6225a9eee97c146531714183 Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Mon, 8 Nov 2021 00:23:06 +0700 Subject: [PATCH 13/18] FIX variable format --- computer_vision/flip_augmentation.py | 10 +++++----- computer_vision/mosaic_augmentation.py | 9 +++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/computer_vision/flip_augmentation.py b/computer_vision/flip_augmentation.py index 028b4eb5b3f0..bc2c7adf98d9 100644 --- a/computer_vision/flip_augmentation.py +++ b/computer_vision/flip_augmentation.py @@ -1,10 +1,10 @@ import glob import os import random -from string import digits, ascii_lowercase +from string import ascii_lowercase, digits +from typing import List, Tuple import cv2 -import numpy as np """ @@ -47,7 +47,7 @@ def main() -> None: outfile.write("\n".join(line for line in annos_list)) -def get_dataset(label_dir: str, img_dir: str) -> tuple(list, list): +def get_dataset(label_dir: str, img_dir: str) -> Tuple[List, List]: """ - label_dir : Path to label include annotation of images - img_dir : Path to folder contain images @@ -58,7 +58,7 @@ def get_dataset(label_dir: str, img_dir: str) -> tuple(list, list): labels = [] for label_file in glob.glob(os.path.join(label_dir, "*.txt")): label_name = label_file.split("/")[-1].rsplit(".", 1)[0] - with open(label_file, "r") as in_file: + with open(label_file) as in_file: obj_lists = in_file.readlines() img_path = os.path.join(img_dir, f"{label_name}.jpg") @@ -83,7 +83,7 @@ def get_dataset(label_dir: str, img_dir: str) -> tuple(list, list): def update_image_and_anno( img_list: list, anno_list: list, flip_type: int = 1 -) -> tuple(list, list, list): +) -> Tuple[List, List, List]: """ - img_list : list of all images - anno_list : list of all annotations of specific image diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py index f04c4ea9db5f..33da4e98ff32 100644 --- a/computer_vision/mosaic_augmentation.py +++ b/computer_vision/mosaic_augmentation.py @@ -3,7 +3,8 @@ import glob import os import random -from string import digits, ascii_lowercase +from string import ascii_lowercase, digits +from typing import List, Tuple import cv2 import numpy as np @@ -56,7 +57,7 @@ def main() -> None: outfile.write("\n".join(line for line in annos_list)) -def get_dataset(label_dir: str, img_dir: str) -> tuple(list, list): +def get_dataset(label_dir: str, img_dir: str) -> Tuple[List, List]: """ - label_dir : Path to label include annotation of images - img_dir : Path to folder contain images @@ -67,7 +68,7 @@ def get_dataset(label_dir: str, img_dir: str) -> tuple(list, list): labels = [] for label_file in glob.glob(os.path.join(label_dir, "*.txt")): label_name = label_file.split("/")[-1].rsplit(".", 1)[0] - with open(label_file, "r") as in_file: + with open(label_file) as in_file: obj_lists = in_file.readlines() img_path = os.path.join(img_dir, f"{label_name}.jpg") @@ -94,7 +95,7 @@ def update_image_and_anno( output_size: tuple(int, int), scale_range: tuple(float, float), filter_scale: float = 0.0, -) -> tuple(list, list, str): +) -> Tuple(List, List, str): """ - all_img_list : list of all images - all_annos : list of all annotations of specific image From 4b4f0ea249cb7369d2215ef0e2a655b34ced7de6 Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Mon, 8 Nov 2021 00:37:07 +0700 Subject: [PATCH 14/18] FIX variable format --- computer_vision/flip_augmentation.py | 5 ++--- computer_vision/mosaic_augmentation.py | 7 +++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/computer_vision/flip_augmentation.py b/computer_vision/flip_augmentation.py index bc2c7adf98d9..740c633dbdd4 100644 --- a/computer_vision/flip_augmentation.py +++ b/computer_vision/flip_augmentation.py @@ -2,7 +2,6 @@ import os import random from string import ascii_lowercase, digits -from typing import List, Tuple import cv2 @@ -47,7 +46,7 @@ def main() -> None: outfile.write("\n".join(line for line in annos_list)) -def get_dataset(label_dir: str, img_dir: str) -> Tuple[List, List]: +def get_dataset(label_dir: str, img_dir: str) -> tuple[list, list]: """ - label_dir : Path to label include annotation of images - img_dir : Path to folder contain images @@ -83,7 +82,7 @@ def get_dataset(label_dir: str, img_dir: str) -> Tuple[List, List]: def update_image_and_anno( img_list: list, anno_list: list, flip_type: int = 1 -) -> Tuple[List, List, List]: +) -> tuple[list, list, list]: """ - img_list : list of all images - anno_list : list of all annotations of specific image diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py index 33da4e98ff32..978307b10532 100644 --- a/computer_vision/mosaic_augmentation.py +++ b/computer_vision/mosaic_augmentation.py @@ -4,7 +4,6 @@ import os import random from string import ascii_lowercase, digits -from typing import List, Tuple import cv2 import numpy as np @@ -57,7 +56,7 @@ def main() -> None: outfile.write("\n".join(line for line in annos_list)) -def get_dataset(label_dir: str, img_dir: str) -> Tuple[List, List]: +def get_dataset(label_dir: str, img_dir: str) -> tuple[list, list]: """ - label_dir : Path to label include annotation of images - img_dir : Path to folder contain images @@ -91,11 +90,11 @@ def get_dataset(label_dir: str, img_dir: str) -> Tuple[List, List]: def update_image_and_anno( all_img_list: list, all_annos: list, - idxs: int, + idxs: list[int], output_size: tuple(int, int), scale_range: tuple(float, float), filter_scale: float = 0.0, -) -> Tuple(List, List, str): +) -> tuple(list, list, str): """ - all_img_list : list of all images - all_annos : list of all annotations of specific image From 535fe4594bbaff910d38e5687f12bc904b039ba7 Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Mon, 8 Nov 2021 00:55:55 +0700 Subject: [PATCH 15/18] FIX code structure --- computer_vision/flip_augmentation.py | 1 - computer_vision/mosaic_augmentation.py | 7 +++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/computer_vision/flip_augmentation.py b/computer_vision/flip_augmentation.py index 740c633dbdd4..236ad7df919b 100644 --- a/computer_vision/flip_augmentation.py +++ b/computer_vision/flip_augmentation.py @@ -2,7 +2,6 @@ import os import random from string import ascii_lowercase, digits - import cv2 diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py index 978307b10532..363c61402d26 100644 --- a/computer_vision/mosaic_augmentation.py +++ b/computer_vision/mosaic_augmentation.py @@ -4,7 +4,6 @@ import os import random from string import ascii_lowercase, digits - import cv2 import numpy as np @@ -91,10 +90,10 @@ def update_image_and_anno( all_img_list: list, all_annos: list, idxs: list[int], - output_size: tuple(int, int), - scale_range: tuple(float, float), + output_size: tuple[int, int], + scale_range: tuple[float, float], filter_scale: float = 0.0, -) -> tuple(list, list, str): +) -> tuple[list, list, str]: """ - all_img_list : list of all images - all_annos : list of all annotations of specific image From 7b9aace3915183687446c88af02d3b2432f3817a Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Mon, 8 Nov 2021 01:07:48 +0700 Subject: [PATCH 16/18] FIX code structure --- computer_vision/flip_augmentation.py | 14 ++++++-------- computer_vision/mosaic_augmentation.py | 4 ++-- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/computer_vision/flip_augmentation.py b/computer_vision/flip_augmentation.py index 236ad7df919b..2df436cb2acc 100644 --- a/computer_vision/flip_augmentation.py +++ b/computer_vision/flip_augmentation.py @@ -26,17 +26,15 @@ def main() -> None: """ img_paths, annos = get_dataset(LABEL_DIR, IMAGE_DIR) print("Processing...") - new_image, new_annos, path = update_image_and_anno(img_paths, annos, FLIP_TYPE) + new_images, new_annos, paths = update_image_and_anno(img_paths, annos, FLIP_TYPE) - for index in range(len(new_image)): + for index, image in enumerate(new_images): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' letter_code = random_chars(32) - file_name = path[index].split("/")[-1].rsplit(".", 1)[0] + file_name = paths[index].split(os.sep)[-1].rsplit(".", 1)[0] file_root = f"{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}" - cv2.imwrite( - f"/{file_root}.jpg", new_image[index], [cv2.IMWRITE_JPEG_QUALITY, 85] - ) - print(f"Success {index+1}/{len(new_image)} with {file_name}") + cv2.imwrite(f"/{file_root}.jpg", image, [cv2.IMWRITE_JPEG_QUALITY, 85]) + print(f"Success {index+1}/{len(new_images)} with {file_name}") annos_list = [] for anno in new_annos[index]: obj = f"{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}" @@ -55,7 +53,7 @@ def get_dataset(label_dir: str, img_dir: str) -> tuple[list, list]: img_paths = [] labels = [] for label_file in glob.glob(os.path.join(label_dir, "*.txt")): - label_name = label_file.split("/")[-1].rsplit(".", 1)[0] + label_name = label_file.split(os.sep)[-1].rsplit(".", 1)[0] with open(label_file) as in_file: obj_lists = in_file.readlines() img_path = os.path.join(img_dir, f"{label_name}.jpg") diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py index 363c61402d26..b1f5199ade08 100644 --- a/computer_vision/mosaic_augmentation.py +++ b/computer_vision/mosaic_augmentation.py @@ -39,7 +39,7 @@ def main() -> None: # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' letter_code = random_chars(32) - file_name = path.split("/")[-1].rsplit(".", 1)[0] + file_name = path.split(os.sep)[-1].rsplit(".", 1)[0] file_root = f"{OUTPUT_DIR}/{file_name}_MOSAIC_{letter_code}" cv2.imwrite(f"{file_root}.jpg", new_image, [cv2.IMWRITE_JPEG_QUALITY, 85]) print(f"Successed {index+1}/{NUMBER_IMAGES} with {file_name}") @@ -65,7 +65,7 @@ def get_dataset(label_dir: str, img_dir: str) -> tuple[list, list]: img_paths = [] labels = [] for label_file in glob.glob(os.path.join(label_dir, "*.txt")): - label_name = label_file.split("/")[-1].rsplit(".", 1)[0] + label_name = label_file.split(os.sep)[-1].rsplit(".", 1)[0] with open(label_file) as in_file: obj_lists = in_file.readlines() img_path = os.path.join(img_dir, f"{label_name}.jpg") From 686d073bc7d19cf7fb52b99e133bd947e1c467a6 Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Mon, 8 Nov 2021 09:01:47 +0700 Subject: [PATCH 17/18] FIX code structure --- computer_vision/flip_augmentation.py | 2 +- computer_vision/mosaic_augmentation.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/computer_vision/flip_augmentation.py b/computer_vision/flip_augmentation.py index 2df436cb2acc..1272357fd03e 100644 --- a/computer_vision/flip_augmentation.py +++ b/computer_vision/flip_augmentation.py @@ -2,8 +2,8 @@ import os import random from string import ascii_lowercase, digits -import cv2 +import cv2 """ Flip image and bounding box for computer vision task diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py index b1f5199ade08..41f1445e4b61 100644 --- a/computer_vision/mosaic_augmentation.py +++ b/computer_vision/mosaic_augmentation.py @@ -4,10 +4,10 @@ import os import random from string import ascii_lowercase, digits + import cv2 import numpy as np - # Parrameters OUTPUT_SIZE = (720, 1280) # Height, Width SCALE_RANGE = (0.4, 0.6) # if height or width lower than this scale, drop it. From e1a43761d2612d6306ebc4deca8748ff69b3d040 Mon Sep 17 00:00:00 2001 From: vnk8071 Date: Mon, 8 Nov 2021 12:00:07 +0700 Subject: [PATCH 18/18] FIX code structure --- computer_vision/mosaic_augmentation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py index 41f1445e4b61..4fd81957ce2a 100644 --- a/computer_vision/mosaic_augmentation.py +++ b/computer_vision/mosaic_augmentation.py @@ -42,7 +42,7 @@ def main() -> None: file_name = path.split(os.sep)[-1].rsplit(".", 1)[0] file_root = f"{OUTPUT_DIR}/{file_name}_MOSAIC_{letter_code}" cv2.imwrite(f"{file_root}.jpg", new_image, [cv2.IMWRITE_JPEG_QUALITY, 85]) - print(f"Successed {index+1}/{NUMBER_IMAGES} with {file_name}") + print(f"Succeeded {index+1}/{NUMBER_IMAGES} with {file_name}") annos_list = [] for anno in new_annos: width = anno[3] - anno[1]