|
@@ -64,128 +64,130 @@ def generate_data(transformation, _dataset_path, _output, _human_thresholds, _re
|
|
|
|
|
|
print('Scene {0} of {1} ({2})'.format((id_scene + 1), n_scenes, folder_scene))
|
|
|
scene_path = os.path.join(_dataset_path, folder_scene)
|
|
|
- output_scene_path = os.path.join(_output, folder_scene)
|
|
|
+ output_scene_path = os.path.join(cfg.output_data_generated, _output, folder_scene)
|
|
|
|
|
|
# construct each zones folder name
|
|
|
zones_folder = []
|
|
|
features_folder = []
|
|
|
- zones_threshold = _human_thresholds[folder_scene]
|
|
|
|
|
|
- # get zones list info
|
|
|
- for index in zones:
|
|
|
- index_str = str(index)
|
|
|
- if len(index_str) < 2:
|
|
|
- index_str = "0" + index_str
|
|
|
+ if folder_scene in _human_thresholds:
|
|
|
|
|
|
- current_zone = "zone"+index_str
|
|
|
- zones_folder.append(current_zone)
|
|
|
- zone_path = os.path.join(output_scene_path, current_zone)
|
|
|
+ zones_threshold = _human_thresholds[folder_scene]
|
|
|
+ # get zones list info
|
|
|
+ for index in zones:
|
|
|
+ index_str = str(index)
|
|
|
+ if len(index_str) < 2:
|
|
|
+ index_str = "0" + index_str
|
|
|
|
|
|
- # custom path for feature
|
|
|
- feature_path = os.path.join(zone_path, transformation.getName())
|
|
|
+ current_zone = "zone"+index_str
|
|
|
+ zones_folder.append(current_zone)
|
|
|
+ zone_path = os.path.join(output_scene_path, current_zone)
|
|
|
|
|
|
- if not os.path.exists(feature_path):
|
|
|
- os.makedirs(feature_path)
|
|
|
+ # custom path for feature
|
|
|
+ feature_path = os.path.join(zone_path, transformation.getName())
|
|
|
|
|
|
- # custom path for interval of reconstruction and feature
|
|
|
- feature_interval_path = os.path.join(zone_path, transformation.getTransformationPath())
|
|
|
- features_folder.append(feature_interval_path)
|
|
|
+ if not os.path.exists(feature_path):
|
|
|
+ os.makedirs(feature_path)
|
|
|
|
|
|
- if not os.path.exists(feature_interval_path):
|
|
|
- os.makedirs(feature_interval_path)
|
|
|
+ # custom path for interval of reconstruction and feature
|
|
|
+ feature_interval_path = os.path.join(zone_path, transformation.getTransformationPath())
|
|
|
+ features_folder.append(feature_interval_path)
|
|
|
|
|
|
- # create for each zone the labels folder
|
|
|
- labels = [cfg.not_noisy_folder, cfg.noisy_folder]
|
|
|
+ if not os.path.exists(feature_interval_path):
|
|
|
+ os.makedirs(feature_interval_path)
|
|
|
|
|
|
- for label in labels:
|
|
|
- label_folder = os.path.join(feature_interval_path, label)
|
|
|
+ # create for each zone the labels folder
|
|
|
+ labels = [cfg.not_noisy_folder, cfg.noisy_folder]
|
|
|
|
|
|
- if not os.path.exists(label_folder):
|
|
|
- os.makedirs(label_folder)
|
|
|
+ for label in labels:
|
|
|
+ label_folder = os.path.join(feature_interval_path, label)
|
|
|
|
|
|
- # get all images of folder
|
|
|
- scene_images = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
|
|
|
- number_scene_image = len(scene_images)
|
|
|
+ if not os.path.exists(label_folder):
|
|
|
+ os.makedirs(label_folder)
|
|
|
|
|
|
- # for each images
|
|
|
- for id_img, img_path in enumerate(scene_images):
|
|
|
+ # get all images of folder
|
|
|
+ scene_images = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
|
|
|
+ number_scene_image = len(scene_images)
|
|
|
|
|
|
- current_img = Image.open(img_path)
|
|
|
- img_blocks = divide_in_blocks(current_img, cfg.sub_image_size)
|
|
|
+ # for each images
|
|
|
+ for id_img, img_path in enumerate(scene_images):
|
|
|
|
|
|
- current_quality_index = int(get_scene_image_quality(img_path))
|
|
|
+ current_img = Image.open(img_path)
|
|
|
+ img_blocks = divide_in_blocks(current_img, cfg.sub_image_size)
|
|
|
|
|
|
- for id_block, block in enumerate(img_blocks):
|
|
|
+ current_quality_index = int(get_scene_image_quality(img_path))
|
|
|
|
|
|
- ##########################
|
|
|
- # Image computation part #
|
|
|
- ##########################
|
|
|
+ for id_block, block in enumerate(img_blocks):
|
|
|
|
|
|
- label_path = features_folder[id_block]
|
|
|
+ ##########################
|
|
|
+ # Image computation part #
|
|
|
+ ##########################
|
|
|
|
|
|
- # get label folder for block
|
|
|
- if current_quality_index > zones_threshold[id_block]:
|
|
|
- label_path = os.path.join(label_path, cfg.not_noisy_folder)
|
|
|
- else:
|
|
|
- label_path = os.path.join(label_path, cfg.noisy_folder)
|
|
|
+ label_path = features_folder[id_block]
|
|
|
|
|
|
- # check if necessary to compute or not images
|
|
|
- # Data augmentation!
|
|
|
- rotations = [0, 90, 180, 270]
|
|
|
+ # get label folder for block
|
|
|
+ if current_quality_index > zones_threshold[id_block]:
|
|
|
+ label_path = os.path.join(label_path, cfg.not_noisy_folder)
|
|
|
+ else:
|
|
|
+ label_path = os.path.join(label_path, cfg.noisy_folder)
|
|
|
|
|
|
- #img_flip_labels = ['original', 'horizontal', 'vertical', 'both']
|
|
|
- img_flip_labels = ['original', 'horizontal']
|
|
|
+ # check if necessary to compute or not images
|
|
|
+ # Data augmentation!
|
|
|
+ rotations = [0, 90, 180, 270]
|
|
|
|
|
|
- output_images_path = []
|
|
|
- check_path_exists = []
|
|
|
- # rotate and flip image to increase dataset size
|
|
|
- for id, flip_label in enumerate(img_flip_labels):
|
|
|
- for rotation in rotations:
|
|
|
- output_reconstructed_filename = img_path.split('/')[-1].replace('.png', '') + '_' + zones_folder[id_block] + cfg.post_image_name_separator
|
|
|
- output_reconstructed_filename = output_reconstructed_filename + flip_label + '_' + str(rotation) + '.png'
|
|
|
- output_reconstructed_path = os.path.join(label_path, output_reconstructed_filename)
|
|
|
+ #img_flip_labels = ['original', 'horizontal', 'vertical', 'both']
|
|
|
+ img_flip_labels = ['original', 'horizontal']
|
|
|
|
|
|
- if os.path.exists(output_reconstructed_path):
|
|
|
- check_path_exists.append(True)
|
|
|
- else:
|
|
|
- check_path_exists.append(False)
|
|
|
+ output_images_path = []
|
|
|
+ check_path_exists = []
|
|
|
+ # rotate and flip image to increase dataset size
|
|
|
+ for id, flip_label in enumerate(img_flip_labels):
|
|
|
+ for rotation in rotations:
|
|
|
+ output_reconstructed_filename = img_path.split('/')[-1].replace('.png', '') + '_' + zones_folder[id_block] + cfg.post_image_name_separator
|
|
|
+ output_reconstructed_filename = output_reconstructed_filename + flip_label + '_' + str(rotation) + '.png'
|
|
|
+ output_reconstructed_path = os.path.join(label_path, output_reconstructed_filename)
|
|
|
|
|
|
- output_images_path.append(output_reconstructed_path)
|
|
|
+ if os.path.exists(output_reconstructed_path):
|
|
|
+ check_path_exists.append(True)
|
|
|
+ else:
|
|
|
+ check_path_exists.append(False)
|
|
|
|
|
|
- # compute only if not exists or necessary to replace
|
|
|
- if _replace or not np.array(check_path_exists).all():
|
|
|
- # compute image
|
|
|
- # pass block to grey level
|
|
|
- output_block = transformation.getTransformedImage(block)
|
|
|
- output_block = np.array(output_block, 'uint8')
|
|
|
-
|
|
|
- # current output image
|
|
|
- output_block_img = Image.fromarray(output_block)
|
|
|
+ output_images_path.append(output_reconstructed_path)
|
|
|
|
|
|
- #horizontal_img = output_block_img.transpose(Image.FLIP_LEFT_RIGHT)
|
|
|
- #vertical_img = output_block_img.transpose(Image.FLIP_TOP_BOTTOM)
|
|
|
- #both_img = output_block_img.transpose(Image.TRANSPOSE)
|
|
|
+ # compute only if not exists or necessary to replace
|
|
|
+ if _replace or not np.array(check_path_exists).all():
|
|
|
+ # compute image
|
|
|
+ # pass block to grey level
|
|
|
+ output_block = transformation.getTransformedImage(block)
|
|
|
+ output_block = np.array(output_block, 'uint8')
|
|
|
+
|
|
|
+ # current output image
|
|
|
+ output_block_img = Image.fromarray(output_block)
|
|
|
|
|
|
- #flip_images = [output_block_img, horizontal_img, vertical_img, both_img]
|
|
|
- #flip_images = [output_block_img, horizontal_img]
|
|
|
+ #horizontal_img = output_block_img.transpose(Image.FLIP_LEFT_RIGHT)
|
|
|
+ #vertical_img = output_block_img.transpose(Image.FLIP_TOP_BOTTOM)
|
|
|
+ #both_img = output_block_img.transpose(Image.TRANSPOSE)
|
|
|
|
|
|
- # Only current image img currenlty
|
|
|
- flip_images = [output_block_img]
|
|
|
+ #flip_images = [output_block_img, horizontal_img, vertical_img, both_img]
|
|
|
+ #flip_images = [output_block_img, horizontal_img]
|
|
|
|
|
|
- # rotate and flip image to increase dataset size
|
|
|
- counter_index = 0 # get current path index
|
|
|
- for id, flip in enumerate(flip_images):
|
|
|
- for rotation in rotations:
|
|
|
+ # Only current image img currenlty
|
|
|
+ flip_images = [output_block_img]
|
|
|
+
|
|
|
+ # rotate and flip image to increase dataset size
|
|
|
+ counter_index = 0 # get current path index
|
|
|
+ for id, flip in enumerate(flip_images):
|
|
|
+ for rotation in rotations:
|
|
|
|
|
|
- if _replace or not check_path_exists[counter_index]:
|
|
|
- rotated_output_img = flip.rotate(rotation)
|
|
|
- rotated_output_img.save(output_images_path[counter_index])
|
|
|
+ if _replace or not check_path_exists[counter_index]:
|
|
|
+ rotated_output_img = flip.rotate(rotation)
|
|
|
+ rotated_output_img.save(output_images_path[counter_index])
|
|
|
|
|
|
- counter_index +=1
|
|
|
+ counter_index +=1
|
|
|
|
|
|
- write_progress((id_img + 1) / number_scene_image)
|
|
|
+ write_progress((id_img + 1) / number_scene_image)
|
|
|
|
|
|
- print('\n')
|
|
|
+ print('\n')
|
|
|
|
|
|
print("{0}_{1} : end of data generation\n".format(transformation.getName(), transformation.getParam()))
|
|
|
|
|
@@ -231,8 +233,8 @@ def main():
|
|
|
for id, feature in enumerate(p_features):
|
|
|
|
|
|
if feature not in features_choices or feature == 'static':
|
|
|
- raise ValueError("Unknown feature, please select a correct feature (`static` excluded) : ", features_choices)
|
|
|
-
|
|
|
+ raise ValueError("Unknown feature {0}, please select a correct feature (`static` excluded) : {1}".format(feature, features_choices))
|
|
|
+
|
|
|
transformations.append(Transformation(feature, p_params[id], p_size))
|
|
|
|
|
|
human_thresholds = {}
|