Пример #1
0
def initialize_train_test_folder(file_path):
    """
    create new train, test and validation folder if
    it doesn't exist.
    :param file_path: main_path
    """
    train_folder = '{}/{}'.format(file_path, 'train')
    test_folder = '{}/{}'.format(file_path, 'test')
    valid_folder = '{}/{}'.format(file_path, 'validation')
    check_directory(train_folder)
    check_directory(test_folder)
    check_directory(valid_folder)
Пример #2
0
def batch_image_process(ori_set, target_set, process, stage=None, num_op=None):
    """

    :param ori_set:
    :param target_set:
    :param process: process can be 'crop' or 'flip'
    :return:
    """
    check_directory([TARGET_DATA_DIR, TEST_TARGET_DATA_DIR, TRAIN_TARGET_DATA_DIR, VALID_TARGET_DATA_DIR])
    check_directory([CROP_TARGET_DATA_DIR, CROP_TEST_TARGET_DATA_DIR, CROP_TRAIN_TARGET_DATA_DIR, CROP_VALID_TARGET_DATA_DIR])
    for file in os.listdir(ori_set):
        print(file)
        image_file = os.path.join(ori_set, str(file), str(file) + '.bmp')
        det_mask_file = os.path.join(ori_set, str(file), str(file) + '_detection.bmp')
        cls_mask_file = os.path.join(ori_set, str(file), str(file) + '_classification.bmp')

        image = cv2.imread(image_file)
        det_mask = cv2.imread(det_mask_file)
        cls_mask = cv2.imread(cls_mask_file)
        if process == 'crop':
            image = cv2.resize(image, (512, 512))
            det_mask = cv2.resize(det_mask, (512, 512))
            cls_mask = cv2.resize(cls_mask, (512, 512))
            crop_list = crop_image_parts(image, det_mask, cls_mask)

            list_file_create = [os.path.join(target_set, str(file)+'_1'),
                                os.path.join(target_set, str(file)+'_2'),
                                os.path.join(target_set, str(file)+'_3'),
                                os.path.join(target_set, str(file)+'_4')]
            check_directory(list_file_create)
            list_img_create = [os.path.join(target_set, str(file)+ '_1', str(file)+'_1.bmp'),
                               os.path.join(target_set, str(file)+ '_2', str(file)+'_2.bmp'),
                               os.path.join(target_set, str(file)+ '_3', str(file)+'_3.bmp'),
                               os.path.join(target_set, str(file)+'_4', str(file)+'_4.bmp'),
                               os.path.join(target_set, str(file)+'_1',str(file)+'_1_detection.bmp'),
                               os.path.join(target_set, str(file)+'_2',str(file)+'_2_detection.bmp'),
                               os.path.join(target_set, str(file)+'_3',str(file)+'_3_detection.bmp'),
                               os.path.join(target_set, str(file)+'_4',str(file)+'_4_detection.bmp'),
                               os.path.join(target_set, str(file) + '_1', str(file) + '_1_classification.bmp'),
                               os.path.join(target_set, str(file) + '_2', str(file) + '_2_classification.bmp'),
                               os.path.join(target_set, str(file) + '_3', str(file) + '_3_classification.bmp'),
                               os.path.join(target_set, str(file) + '_4', str(file) + '_4_classification.bmp'),
                               os.path.join(target_set, str(file) + '_1', str(file) + '_1_original.bmp'),
                               os.path.join(target_set, str(file) + '_2', str(file) + '_2_original.bmp'),
                               os.path.join(target_set, str(file) + '_3', str(file) + '_3_original.bmp'),
                               os.path.join(target_set, str(file) + '_4', str(file) + '_4_original.bmp')]
            for order, img in enumerate(crop_list):
                check_cv2_imwrite(list_img_create[order], img)

        elif process == 'flip':
            aug_lrimage, aug_lrdetmask, aug_lrclsmask = img_aug(image, det_mask, cls_mask, 'fliplr')
            aug_upimage, aug_updetmask, aug_upclsmask = img_aug(image, det_mask, cls_mask, 'flipup')
            list_file_create = [os.path.join(target_set, str(file) + '_lr'),
                                os.path.join(target_set, str(file) + '_up')]
            check_directory(list_file_create)
            list_img_create = [os.path.join(target_set, str(file) + '_lr', str(file) + '_lr.bmp'),
                               os.path.join(target_set, str(file) + '_up', str(file) + '_up.bmp'),
                               os.path.join(target_set, str(file) + '_lr', str(file) + '_lr_detection.bmp'),
                               os.path.join(target_set, str(file) + '_up', str(file) + '_up_detection.bmp'),
                               os.path.join(target_set, str(file) + '_lr', str(file) + '_lr_classification.bmp'),
                               os.path.join(target_set, str(file) + '_up', str(file) + '_up_classification.bmp'),
                               os.path.join(target_set, str(file) + '_lr', str(file) + '_lr_original.bmp'),
                               os.path.join(target_set, str(file) + '_up', str(file) + '_up_original.bmp')]
            flip_list = [aug_lrimage, aug_upimage,
                        aug_lrdetmask, aug_updetmask,
                        aug_lrclsmask, aug_upclsmask,
                        aug_lrimage, aug_upimage]
            for order, img in enumerate(flip_list):
                check_cv2_imwrite(list_img_create[order], img)

        elif process == 'shear' or process == 'channel' or process == 'random' or process=='zoom' or process=='rotate':
            aug_image, aug_detmask, aug_clsmask = img_aug(image, det_mask, cls_mask, process)
            list_file_create = [os.path.join(target_set, str(file) + '_' + process + str(stage))]
            check_directory(list_file_create)
            list_img_create = [os.path.join(target_set, str(file) + '_' + process + str(stage), str(file) + '_' + process + str(stage) + '.bmp'),
                               os.path.join(target_set, str(file) + '_' + process + str(stage), str(file) + '_' + process + str(stage) + '_detection.bmp'),
                               os.path.join(target_set, str(file) + '_' + process + str(stage), str(file) + '_' + process + str(stage) + '_classification.bmp'),
                               os.path.join(target_set, str(file) + '_' + process + str(stage), str(file) + '_' + process + str(stage) + '_original.bmp')]
            image_list = [aug_image, aug_detmask, aug_clsmask, aug_image]
            for order, img in enumerate(image_list):
                check_cv2_imwrite(list_img_create[order], img)
        else:
            raise ValueError('augmentation type wrong, check documents.')
Пример #3
0
 def __init__(self, data_path = None, old_filename = None, new_filename = None):
     self.data_path = data_path
     self.old_filename = '{}/{}'.format(data_path, old_filename)
     self.new_filename = '{}/{}'.format(data_path, new_filename)
     check_directory(self.new_filename)
     initialize_train_test_folder(self.new_filename)
Пример #4
0
def persistence_image_data_to_tfrecords(x_data,
                                        y_data,
                                        data_type,
                                        split_index=128):
    """
        persistence_image_data_to_tfrecords(x_data, y_data, data_type, split_index)
        - Make images into tfrecords and save
        - Crop / Resize / Convert rgb into grayscale
    """

    TARGET_DIR = _get_target_dir()
    OUTPUT_DIR = os.path.join(FLAGS.output_dir, TARGET_DIR, data_type)
    IMAGE_DIR = FLAGS.image_dir

    myutil.check_directory("./resized_img")  # Check directory of "resized_img"
    myutil.check_directory("./tfrecords")
    myutil.check_directory("./tfrecords/cropping")
    myutil.check_directory("./tfrecords/cropping/train")
    myutil.check_directory("./tfrecords/cropping/test")
    myutil.check_directory(OUTPUT_DIR)  # Check directory of OUTPUT_DIR

    writer = None
    sess = None
    current_index = 0

    from sklearn import preprocessing
    le = preprocessing.LabelEncoder()
    le.fit(y_data)
    y_data_size = len(le.classes_)

    # https://stackoverflow.com/questions/45427637/is-there-a-more-simple-way-to-handle-batch-inputs-from-tfrecords

    for images_filename, y_label in zip(x_data, y_data):
        if not (images_filename[-3:] == "jpg"):
            print("Error - ", images_filename)
            continue
        if current_index % split_index == 0:
            if writer:
                writer.close()
            if sess:
                sess.close()
            tf.reset_default_graph()
            graph = tf.get_default_graph()
            sess = tf.Session(graph=graph)
            sess.run(tf.global_variables_initializer())

            record_filename = "{output_dir}/{data_type}-{current_index}.tfrecords".format(
                output_dir=OUTPUT_DIR,
                data_type=data_type,
                current_index=current_index)
            print("=============>", record_filename)
            print("current index : {0}".format(current_index, ))
            writer = tf.python_io.TFRecordWriter(record_filename)

        file_full_path = os.path.join(IMAGE_DIR, y_label, images_filename)
        try:
            image_file = tf.read_file(file_full_path)
            decoded_img = tf.image.decode_jpeg(
                image_file, channels=3)  # 'jpg' file decoding

        except tf.errors.InvalidArgumentError as e:
            print(e)
            print("Error : ", images_filename)
            continue

        image_list = [decoded_img]

        # If I wanna crop images
        if FLAGS.cropping:
            image_list = []

            # Get size information
            size_info = iputil.get_orginal_size_info(y_label, images_filename)
            target_img = tf.image.resize_images(
                decoded_img, [size_info["height"], size_info["width"]])
            # Get bounding box to crop image (it is list)
            # bounding_box = [[xmin,ymin,bounding_width,bounding_height]]
            bounding_box = iputil.get_bounding_size_info(
                y_label, images_filename)

            # Crop image with bounding box
            for box in bounding_box:

                # tf.img.crop_to_bounding_box
                # tf.img.* 함수들은 width보다 height를 먼저 쓴다
                # -> 이미지 좌표상 x,y,width,height의 순서를 잘 확인하지 않으면 전처리가 잘못 적용되니 유의할 것!
                cropped_img = tf.image.crop_to_bounding_box(
                    target_img, box[1], box[0], box[3], box[2])

                # cropped_img = tf.image.crop_and_resize(decoded_img, bounding_box, i, crop_size=[FLAGS.img_height, FLAGS.img_width])
                """
                tf.image.crop_and_resize를 사용하고 싶은데 뭔가 자꾸 오류가 남...
                tf.image.crop_and_resize 함수 내에 세팅하는 값의 문제 - 함수 사용법을 좀더 살펴봐야 함!
                tf.image.crop_and_resize(
                    image,
                    boxes,
                    box_ind,
                    crop_size,
                    method='bilinear',
                    extrapolation_value=0,
                    name=None
                )
                """

                image_list.append(cropped_img)

        for image in image_list:
            try:
                imsave(
                    "./resized_img/" + images_filename + "_" +
                    str(current_index) + ".jpeg", sess.run(image))
            except OSError as e:
                print(e)

            gray_img = tf.image.rgb_to_grayscale(image)  # rgb to grayscale
            resized_img = tf.image.resize_images(
                gray_img, [FLAGS.img_width, FLAGS.img_height
                           ])  # Resize image with new width and height

            image_bytes = sess.run(tf.cast(resized_img, tf.uint8)).tobytes()

            y_data_label = le.transform([y_label])
            lbl_one_hot = tf.one_hot(y_data_label[0], y_data_size, 1, 0)
            image_label = sess.run(tf.cast(lbl_one_hot, tf.uint8)).tobytes()

            feature = {
                'label': _bytes_feature(image_label),
                'image': _bytes_feature(image_bytes)
            }

            example = tf.train.Example(features=tf.train.Features(
                feature=feature))

            writer.write(example.SerializeToString())
            current_index += 1

    writer.close()
Пример #5
0
def batch_crop_image_parts(ori_set, target_set):
    for file in os.listdir(ori_set):
        print(file)
        image_file = os.path.join(ori_set, str(file), str(file) + '.bmp')
        det_mask_file = os.path.join(ori_set, str(file),
                                     str(file) + '_detection.bmp')
        cls_mask_file = os.path.join(ori_set, str(file),
                                     str(file) + '_classification.bmp')
        image = cv2.imread(image_file)
        image = cv2.resize(image, (512, 512))
        det_mask = cv2.imread(det_mask_file)
        det_mask = cv2.resize(det_mask, (512, 512))
        cls_mask = cv2.imread(cls_mask_file)
        cls_mask = cv2.resize(cls_mask, (512, 512))
        crop_list = crop_image_parts(image, det_mask, cls_mask)

        list_file_create = [
            os.path.join(target_set,
                         str(file) + '_1'),
            os.path.join(target_set,
                         str(file) + '_2'),
            os.path.join(target_set,
                         str(file) + '_3'),
            os.path.join(target_set,
                         str(file) + '_4')
        ]
        check_directory(list_file_create)
        list_img_create = [
            os.path.join(target_set,
                         str(file) + '_1',
                         str(file) + '_1.bmp'),
            os.path.join(target_set,
                         str(file) + '_2',
                         str(file) + '_2.bmp'),
            os.path.join(target_set,
                         str(file) + '_3',
                         str(file) + '_3.bmp'),
            os.path.join(target_set,
                         str(file) + '_4',
                         str(file) + '_4.bmp'),
            os.path.join(target_set,
                         str(file) + '_1',
                         str(file) + '_1_detection.bmp'),
            os.path.join(target_set,
                         str(file) + '_2',
                         str(file) + '_2_detection.bmp'),
            os.path.join(target_set,
                         str(file) + '_3',
                         str(file) + '_3_detection.bmp'),
            os.path.join(target_set,
                         str(file) + '_4',
                         str(file) + '_4_detection.bmp'),
            os.path.join(target_set,
                         str(file) + '_1',
                         str(file) + '_1_classification.bmp'),
            os.path.join(target_set,
                         str(file) + '_2',
                         str(file) + '_2_classification.bmp'),
            os.path.join(target_set,
                         str(file) + '_3',
                         str(file) + '_3_classification.bmp'),
            os.path.join(target_set,
                         str(file) + '_4',
                         str(file) + '_4_classification.bmp'),
            os.path.join(target_set,
                         str(file) + '_1',
                         str(file) + '_1_original.bmp'),
            os.path.join(target_set,
                         str(file) + '_2',
                         str(file) + '_2_original.bmp'),
            os.path.join(target_set,
                         str(file) + '_3',
                         str(file) + '_3_original.bmp'),
            os.path.join(target_set,
                         str(file) + '_4',
                         str(file) + '_4_original.bmp')
        ]

        for order, img in enumerate(crop_list):
            check_cv2_imwrite(list_img_create[order], img)