text_html_output_1.png
import cv2

import glob

import numpy as np

import pandas as pd

RESIZED_WIDTH, RESIZED_HEIGHT = (224, 224)
EACH_WIDTH, EACH_HEIGHT = (RESIZED_WIDTH // 2, RESIZED_HEIGHT // 2)
OUTPUT_FORMAT = 'jpg'
OUTPUT_DIR = 'data_argument_224_224'

train_dir = 'train_images'
train_paths = glob.glob(f'{data_dir}/{train_dir}/*.jpg')

TRAIN_DF = pd.read_csv('../input/pp-csv/clearned_train.csv')
TRAIN_DF

EXTRA_TRAIN = pd.DataFrame(columns=['image', 'labels'])

def concat_img(imgs):
    if len(imgs) < 4:
        return
    output_shape = (EACH_WIDTH, EACH_HEIGHT)
    imgs = [cv2.resize(img, output_shape) for img in imgs]
    upper_img = cv2.vconcat([imgs[0], imgs[1]])
    lower_img = cv2.vconcat([imgs[2], imgs[3]])
    img = cv2.hconcat([upper_img, lower_img])
    return img

length = len(TRAIN_DF)
indexs = np.random.randint(0, length, size=4)
for count in range(3000):
    imgs = []
    new_labels = []
    indexs = np.random.randint(0, length, size=4)
    for index in indexs:
        filepath = train_paths[index]
        img = cv2.imread(filepath)
        imgs.append(img)
        labels = TRAIN_DF.iloc[index, 1]
        for disease in labels.split(' '):
            if disease == 'healthy' or disease in new_labels:
                continue
            new_labels.append(disease)
    if not new_labels:
        new_label = 'healthy'
    else:
        new_label = ' '.join(new_labels)
    img_name = f'{count}.jpg'
    new_img = concat_img(imgs)
    new_file_path = f'{OUTPUT_DIR}/{train_dir}/{img_name}'
    cv2.imwrite(new_file_path, new_img)
    EXTRA_TRAIN = EXTRA_TRAIN.append({'image': img_name, 'labels': new_label}, ignore_index=True)

EXTRA_TRAIN