|
| 1 | +import cv2 |
| 2 | +import os |
| 3 | +import random |
| 4 | +import numpy as np |
| 5 | +from random import randint |
| 6 | +import albumentations as A |
| 7 | +import numpy as np |
| 8 | +import cv2 |
| 9 | +import sys |
| 10 | +import os |
| 11 | +from tqdm import tqdm |
| 12 | +from utils import GetOverlappingBlocks, getListOfFiles, ImageResize |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +transform = A.Compose([ |
| 17 | + A.OneOf([ |
| 18 | + A.ISONoise(p=0.4), |
| 19 | + A.JpegCompression(quality_lower=50, quality_upper=70, always_apply=False, p=0.8), |
| 20 | + ], p=0.6), |
| 21 | + A.OneOf([ |
| 22 | + A.MotionBlur(blur_limit=10,p=.8), |
| 23 | + A.MedianBlur(blur_limit=3, p=0.75), |
| 24 | + A.GaussianBlur(blur_limit=7, p=0.75), |
| 25 | + ], p=0.8), |
| 26 | + A.OneOf([ |
| 27 | + A.RandomBrightnessContrast(brightness_limit=0.3, contrast_limit=0.3,p=0.75), |
| 28 | + A.RandomShadow(num_shadows_lower=1, num_shadows_upper=18, shadow_dimension=6, p=0.85), |
| 29 | + ], p=0.8), |
| 30 | + ]) |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +def GenerateTrainingBlocks(data_folder,gt_folder,dataset_path='./dataset',M=256,N=256): |
| 35 | + print(data_folder) |
| 36 | + print('Generating training blocks!!!') |
| 37 | + train_path = dataset_path + '/' + data_folder + '_Trainblocks' |
| 38 | + |
| 39 | + if not os.path.exists(train_path): |
| 40 | + os.makedirs(train_path) |
| 41 | + |
| 42 | + |
| 43 | + train_filenames = train_path + '/train_block_names.txt' |
| 44 | + f = open(train_filenames, 'w') |
| 45 | + |
| 46 | + data_path = dataset_path + '/' + data_folder |
| 47 | + gt_path = dataset_path + '/' + gt_folder |
| 48 | + |
| 49 | + print(data_path) |
| 50 | + |
| 51 | + filenames = getListOfFiles(data_path) |
| 52 | + cnt = 0 |
| 53 | + print(filenames) |
| 54 | + for name in tqdm(filenames): |
| 55 | + print(name) |
| 56 | + gt_filename = gt_path + '/' + name |
| 57 | + in_filename = data_path + '/' + name |
| 58 | + print(gt_filename) |
| 59 | + print(in_filename) |
| 60 | + gt_image_initial = cv2.imread(gt_filename) |
| 61 | + in_image_initial = cv2.imread(in_filename) |
| 62 | + print(gt_image_initial.shape,in_image_initial.shape) |
| 63 | + for scale in [0.7,1.0,1.4]: |
| 64 | + gt_image = ImageResize(gt_image_initial, scale) |
| 65 | + in_image = ImageResize(in_image_initial, scale) |
| 66 | + h,w,c = in_image.shape |
| 67 | + gt_img = GetOverlappingBlocks(gt_image,Part=8) |
| 68 | + in_img = GetOverlappingBlocks(in_image,Part=8) |
| 69 | + for i in range(len(gt_img)): |
| 70 | + train_img_path = train_path + '/block_' + str(cnt) + '.png' |
| 71 | + gt_img_path = train_path + '/gtblock_' + str(cnt) + '.png' |
| 72 | + cv2.imwrite(train_img_path,in_img[i]) |
| 73 | + #cv2.imwrite(train_img_path,PreProcessInput(in_img[i])) |
| 74 | + cv2.imwrite(gt_img_path,gt_img[i]) |
| 75 | + t_name = 'block_' + str(cnt) + '.png' |
| 76 | + f.write(t_name) |
| 77 | + f.write('\n') |
| 78 | + cnt += 1 |
| 79 | + Random_Block_Number_PerImage = int(len(gt_img)/5) |
| 80 | + for i in range(Random_Block_Number_PerImage): |
| 81 | + |
| 82 | + if(in_image.shape[0]-M>1 and in_image.shape[1]-N>1): |
| 83 | + y = random.randint(1, in_image.shape[0]-M) |
| 84 | + x = random.randint(1, in_image.shape[1]-N) |
| 85 | + in_part_img = in_image[y:y+M,x:x+N,:].copy() |
| 86 | + gt_part_img = gt_image[y:y+M,x:x+N,:].copy() |
| 87 | + train_img_path = train_path + '/block_' + str(cnt) + '.png' |
| 88 | + gt_img_path = train_path + '/gtblock_' + str(cnt) + '.png' |
| 89 | + in_part_img = cv2.cvtColor(in_part_img, cv2.COLOR_BGR2RGB) |
| 90 | + augmented_image = transform(image=in_part_img)['image'] |
| 91 | + augmented_image = cv2.cvtColor(augmented_image, cv2.COLOR_RGB2BGR) |
| 92 | + |
| 93 | + cv2.imwrite(train_img_path,augmented_image) |
| 94 | + cv2.imwrite(gt_img_path,gt_part_img) |
| 95 | + t_name = 'block_' + str(cnt) + '.png' |
| 96 | + f.write(t_name) |
| 97 | + f.write('\n') |
| 98 | + cnt += 1 |
| 99 | + else: |
| 100 | + break |
| 101 | + in_part_img = np.zeros((M,N,3),dtype=np.uint8) |
| 102 | + gt_part_img = np.zeros((M,N,3),dtype=np.uint8) |
| 103 | + in_part_img[:,:,:] = 255 |
| 104 | + gt_part_img[:,:,:] = 255 |
| 105 | + |
| 106 | + if(in_image.shape[0]-M<=1 and in_image.shape[1]-N>1): |
| 107 | + y = 0 |
| 108 | + x = random.randint(1, in_image.shape[1]-N) |
| 109 | + in_part_img[:h,:,:] = in_image[:,x:x+N,:].copy() |
| 110 | + gt_part_img[:h,:,:] = gt_image[:,x:x+N,:].copy() |
| 111 | + if(in_image.shape[0]-M>1 and in_image.shape[1]-N<=1): |
| 112 | + x = 0 |
| 113 | + y = random.randint(1, in_image.shape[0]-M) |
| 114 | + in_part_img[:,:w,:] = in_image[y:y+M,:,:].copy() |
| 115 | + gt_part_img[:,:w,:] = gt_image[y:y+M,:,:].copy() |
| 116 | + |
| 117 | + |
| 118 | + train_img_path = train_path + '/block_' + str(cnt) + '.png' |
| 119 | + gt_img_path = train_path + '/gtblock_' + str(cnt) + '.png' |
| 120 | + in_part_img = cv2.cvtColor(in_part_img, cv2.COLOR_BGR2RGB) |
| 121 | + augmented_image = transform(image=in_part_img)['image'] |
| 122 | + augmented_image = cv2.cvtColor(augmented_image, cv2.COLOR_RGB2BGR) |
| 123 | + |
| 124 | + cv2.imwrite(train_img_path,augmented_image) |
| 125 | + cv2.imwrite(gt_img_path,gt_part_img) |
| 126 | + t_name = 'block_' + str(cnt) + '.png' |
| 127 | + f.write(t_name) |
| 128 | + f.write('\n') |
| 129 | + cnt += 1 |
| 130 | + #print(cnt) |
| 131 | + |
| 132 | + |
| 133 | + f.close() |
| 134 | + |
| 135 | + print('Total number of training blocks generated: ', cnt) |
| 136 | + |
| 137 | + return train_path, train_filenames |
| 138 | + |
| 139 | + |
0 commit comments