{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"mkdir masks","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"# Resizing and copy mask from panda2 ","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nimport os\nimport csv\nImage.MAX_IMAGE_PIXELS = None\ncount = 0\n\n# Set the directory where the images and the CSV file are located\ndirectory = \"/kaggle/input/isup-training/images\"\ngt_directory = '/kaggle/input/panda2/train_label_masks'\n# Load the CSV file containing the image IDs\nwith open(\"/kaggle/input/prostate-cancer-grade-assessment/train.csv\", \"r\") as file:\n    reader = csv.reader(file)\n    next(reader) # skip the header row\n    print('started')\n    for row in reader:\n        # Extract the image ID from the current row of the CSV file\n        image_id = row[0]\n        \n        # Construct the paths for the actual image and the ground truth image\n        actual_img_path = os.path.join(directory, f\"{image_id}.png\")\n        gt_img_path = os.path.join(gt_directory, f\"{image_id}.png\")\n        \n        # Check if the actual image file and the ground truth image file exist\n        if os.path.isfile(actual_img_path) and os.path.isfile(gt_img_path):\n            # Load actual image and ground truth image\n            actual_img = Image.open(actual_img_path)\n            gt_img = Image.open(gt_img_path)\n\n            # Get size of actual image and ground truth image\n            actual_size = actual_img.size\n            gt_size = gt_img.size\n\n            # Calculate scaling factor\n            scale_factor = actual_size[0] / gt_size[0]\n\n            # Resize ground truth image\n            #resized_gt_img = gt_img.resize((int(gt_size[0] * scale_factor), int(gt_size[1] * scale_factor)),resample=Image.NEAREST)\n            resized_gt_img = gt_img.resize(actual_size)\n            \n            # Save resized ground truth image\n            resized_gt_img.save(os.path.join('/kaggle/working/masks/',f\"{image_id}.png\"))\n            count +=1\n            print(image_id)\n            print(count)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# > **checking if the sizes are accurate**","metadata":{}},{"cell_type":"code","source":"import cv2\n\nimage_path = \"/kaggle/input/isup-training/images/0018ae58b01bdadc8e347995b69f99aa.png\"\nmask_path = \"/kaggle/working/masks/0018ae58b01bdadc8e347995b69f99aa.png\"\n\nimage = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)\nmask = cv2.imread(mask_path, cv2.IMREAD_UNCHANGED)\n\nprint(\"Image shape:\", image.shape)\nprint(\"Mask shape:\", mask.shape)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Converting images and masks to tiles","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"!mkdir tiles\n!mkdir tiles/images\n!mkdir tiles/masks","metadata":{"execution":{"iopub.status.busy":"2023-06-16T06:24:29.034677Z","iopub.execute_input":"2023-06-16T06:24:29.03524Z","iopub.status.idle":"2023-06-16T06:24:32.337145Z","shell.execute_reply.started":"2023-06-16T06:24:29.035188Z","shell.execute_reply":"2023-06-16T06:24:32.335672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport os\n\n\ndef create_tiles(image, mask, tile_size, images_output_folder, masks_output_folder, image_filename, threshold=0.05):\n    height, width = image.shape[:2]\n\n    for y in range(0, height, tile_size):\n        for x in range(0, width, tile_size):\n            tile_image = image[y:y + tile_size, x:x + tile_size]\n            tile_mask = mask[y:y + tile_size, x:x + tile_size]\n\n            mean_color = np.mean(tile_image, axis=(0, 1))\n            mean_color_value = np.mean(mean_color)\n\n            if mean_color_value < 255 * (1 - threshold):\n                tile_image_path = os.path.join(images_output_folder, f'{image_filename}_tile_{y}_{x}.png')\n                tile_mask_path = os.path.join(masks_output_folder, f'{image_filename}_tile_{y}_{x}.png')\n\n                cv2.imwrite(tile_image_path, tile_image)\n                \n                # Check if the mask is a single-channel image and save it accordingly\n             \n                cv2.imwrite(tile_mask_path, tile_mask[:, :, 0])\n              \ndef process_images(images_folder, masks_folder, tile_size, images_output_folder, masks_output_folder):\n    image_files = os.listdir(images_folder)\n    count = 0\n    for image_file in image_files:\n        if image_file.endswith('.png'):\n            image_path = os.path.join(images_folder, image_file)\n            mask_path = os.path.join(masks_folder, image_file)\n\n            image = cv2.imread(image_path, cv2.IMREAD_COLOR)\n            mask = cv2.imread(mask_path, cv2.IMREAD_COLOR)\n            if image is None or mask is None:\n                \n                print(f\"Warning: Unable to read image or mask for file {image_file}. Skipping...\")\n                continue\n                \n            image_filename = os.path.splitext(image_file)[0]\n            print(image_filename)\n            count+=1\n            print(count)\n            \n\n            create_tiles(image, mask, tile_size, images_output_folder, masks_output_folder, image_filename)\n\n\n# Input parameters\nimages_folder = '/kaggle/input/tiff-to-png/images'\nmasks_folder = '/kaggle/input/tiff-to-png/masks'\ntile_size = 640\nimages_output_folder = '/kaggle/working/tiles/images'\nmasks_output_folder = '/kaggle/working/tiles/masks'\n\n# Process the images\nprocess_images(images_folder, masks_folder, tile_size, images_output_folder, masks_output_folder)","metadata":{"execution":{"iopub.status.busy":"2023-06-16T06:26:05.119232Z","iopub.execute_input":"2023-06-16T06:26:05.11968Z","iopub.status.idle":"2023-06-16T07:17:12.687245Z","shell.execute_reply.started":"2023-06-16T06:26:05.119638Z","shell.execute_reply":"2023-06-16T07:17:12.68445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**listing the filenames**","metadata":{}},{"cell_type":"code","source":"ls /kaggle/working/tiles/images ","metadata":{"execution":{"iopub.status.busy":"2023-04-20T08:24:51.294461Z","iopub.execute_input":"2023-04-20T08:24:51.294889Z","iopub.status.idle":"2023-04-20T08:24:52.399951Z","shell.execute_reply.started":"2023-04-20T08:24:51.294852Z","shell.execute_reply":"2023-04-20T08:24:52.398435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# creating annotation files","metadata":{}},{"cell_type":"code","source":"mkdir labels","metadata":{"execution":{"iopub.status.busy":"2023-06-16T07:53:08.679827Z","iopub.execute_input":"2023-06-16T07:53:08.680668Z","iopub.status.idle":"2023-06-16T07:53:09.796084Z","shell.execute_reply.started":"2023-06-16T07:53:08.680606Z","shell.execute_reply":"2023-06-16T07:53:09.794455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport os\nimport cv2\nimport numpy as np\nfrom skimage import measure\n\ndef create_annotations(ground_truth_image, output_annotation_path):\n    class_ids = {\n        2: 'green',\n        4: 'orange',\n        5: 'red'\n    }\n\n    # Create a mapping between the original class IDs and the new class IDs for YOLOv5 training\n    class_id_mapping = {original_id: new_id for new_id, original_id in enumerate(class_ids)}\n\n    gt_img = cv2.imread(ground_truth_image, cv2.IMREAD_GRAYSCALE)\n    height, width = gt_img.shape\n\n    annotation_data = []\n\n    for class_id, color_name in class_ids.items():\n        mask = np.where(gt_img == class_id, 255, 0).astype(np.uint8)\n        contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n\n        for contour in contours:\n            x, y, w, h = cv2.boundingRect(contour)\n            x_center, y_center = (x + w / 2) / width, (y + h / 2) / height\n            w, h = w / width, h / height\n\n            annotation_data.append(f\"{class_id} {x_center} {y_center} {w} {h}\")\n\n    with open(output_annotation_path, 'w') as f:\n        f.write('\\n'.join(annotation_data))\n\n\nground_truth_dir = '/kaggle/working/tiles/masks'\nannotation_output_dir = '/kaggle/working/labels'\ncount = 0\nos.makedirs(annotation_output_dir, exist_ok=True)\n\nfor filename in os.listdir(ground_truth_dir):\n    if filename.endswith('.jpg') or filename.endswith('.png'):\n        ground_truth_image = os.path.join(ground_truth_dir, filename)\n        output_annotation_path = os.path.join(annotation_output_dir, os.path.splitext(filename)[0] + '.txt')\n        create_annotations(ground_truth_image, output_annotation_path)\n        count +=1\n        \n","metadata":{"execution":{"iopub.status.busy":"2023-06-16T07:53:13.811536Z","iopub.execute_input":"2023-06-16T07:53:13.81204Z","iopub.status.idle":"2023-06-16T07:55:29.72939Z","shell.execute_reply.started":"2023-06-16T07:53:13.81199Z","shell.execute_reply":"2023-06-16T07:55:29.727802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/working/tiles/images | wc -l\n!ls /kaggle/working/tiles/masks | wc -l\n!ls /kaggle/working/tiles/labels | wc -l","metadata":{"execution":{"iopub.status.busy":"2023-06-16T08:29:19.160101Z","iopub.execute_input":"2023-06-16T08:29:19.160591Z","iopub.status.idle":"2023-06-16T08:29:22.97966Z","shell.execute_reply.started":"2023-06-16T08:29:19.160546Z","shell.execute_reply":"2023-06-16T08:29:22.977789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mv tiles train","metadata":{"execution":{"iopub.status.busy":"2023-04-14T00:01:29.409305Z","iopub.execute_input":"2023-04-14T00:01:29.409917Z","iopub.status.idle":"2023-04-14T00:01:30.567367Z","shell.execute_reply.started":"2023-04-14T00:01:29.409869Z","shell.execute_reply":"2023-04-14T00:01:30.56548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir tiles/labels","metadata":{"execution":{"iopub.status.busy":"2023-06-16T08:28:50.721558Z","iopub.execute_input":"2023-06-16T08:28:50.722172Z","iopub.status.idle":"2023-06-16T08:28:51.828874Z","shell.execute_reply.started":"2023-06-16T08:28:50.722117Z","shell.execute_reply":"2023-06-16T08:28:51.826877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls val","metadata":{"execution":{"iopub.status.busy":"2023-04-14T00:07:37.322304Z","iopub.execute_input":"2023-04-14T00:07:37.323694Z","iopub.status.idle":"2023-04-14T00:07:38.422996Z","shell.execute_reply.started":"2023-04-14T00:07:37.323599Z","shell.execute_reply":"2023-04-14T00:07:38.421146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\ndef update_class_ids(input_dir, output_dir):\n    class_id_mapping = {\n        2: 0,  # Green\n        4: 1,  # Orange\n        5: 2,  # Red\n    }\n\n    os.makedirs(output_dir, exist_ok=True)\n\n    for filename in os.listdir(input_dir):\n        if filename.endswith('.txt'):\n            input_file = os.path.join(input_dir, filename)\n            output_file = os.path.join(output_dir, filename)\n\n            with open(input_file, 'r') as f:\n                lines = f.readlines()\n\n            new_lines = []\n            for line in lines:\n                class_id, x, y, w, h = line.strip().split()\n                new_class_id = class_id_mapping[int(class_id)]\n                new_lines.append(f\"{new_class_id} {x} {y} {w} {h}\")\n\n            with open(output_file, 'w') as f:\n                f.write('\\n'.join(new_lines))\n\ninput_annotation_dir = '/kaggle/working/labels'\noutput_annotation_dir = '/kaggle/working/tiles/labels'\n\nupdate_class_ids(input_annotation_dir, output_annotation_dir)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-16T08:29:10.717872Z","iopub.execute_input":"2023-06-16T08:29:10.718396Z","iopub.status.idle":"2023-06-16T08:29:13.214745Z","shell.execute_reply.started":"2023-06-16T08:29:10.718348Z","shell.execute_reply":"2023-06-16T08:29:13.213231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\nimport random\n\n# Set the paths to the images and annotations folders\nimages_path = '/kaggle/working/train/images'\nannotations_path = '/kaggle/working/train/labels'\n\n# Set the path to the val folder\nval_path = '/kaggle/working/val'\n\n# Create the images and labels folders in the val folder\nos.makedirs(os.path.join(val_path, 'images'))\nos.makedirs(os.path.join(val_path, 'labels'))\n\n# Get a list of all the image file names\nimage_files = os.listdir(images_path)\n\n# Randomly select 2000 images\nselected_images = random.sample(image_files, 10000)\n\n# Move the selected images and their corresponding annotations to the val folder\nfor image_file in selected_images:\n    # Move the image\n    shutil.move(os.path.join(images_path, image_file), os.path.join(val_path, 'images'))\n    \n    # Move the annotation\n    annotation_file = image_file.split('.')[0] + '.txt'  # assuming the annotations have the same name as the images, but with a .txt extension\n    shutil.move(os.path.join(annotations_path, annotation_file), os.path.join(val_path, 'labels'))\n","metadata":{"execution":{"iopub.status.busy":"2023-04-14T00:07:53.195128Z","iopub.execute_input":"2023-04-14T00:07:53.195626Z","iopub.status.idle":"2023-04-14T00:07:54.891521Z","shell.execute_reply.started":"2023-04-14T00:07:53.195578Z","shell.execute_reply":"2023-04-14T00:07:54.889959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rm -r /kaggle/working/labels","metadata":{"execution":{"iopub.status.busy":"2023-06-16T08:30:02.882425Z","iopub.execute_input":"2023-06-16T08:30:02.882915Z","iopub.status.idle":"2023-06-16T08:30:04.558101Z","shell.execute_reply.started":"2023-06-16T08:30:02.88287Z","shell.execute_reply":"2023-06-16T08:30:04.556086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r tiles.zip tiles","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls val/images |wc -l\n!ls val/labels |wc -l","metadata":{"execution":{"iopub.status.busy":"2023-04-14T00:09:20.710617Z","iopub.execute_input":"2023-04-14T00:09:20.711118Z","iopub.status.idle":"2023-04-14T00:09:23.06569Z","shell.execute_reply.started":"2023-04-14T00:09:20.71108Z","shell.execute_reply":"2023-04-14T00:09:23.063185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rm -r label","metadata":{"execution":{"iopub.status.busy":"2023-04-14T00:10:59.314581Z","iopub.execute_input":"2023-04-14T00:10:59.315115Z","iopub.status.idle":"2023-04-14T00:11:00.412019Z","shell.execute_reply.started":"2023-04-14T00:10:59.315067Z","shell.execute_reply":"2023-04-14T00:11:00.410356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\n\n\ndef create_yolo_annotations(ground_truth_folder, annotation_folder, image_folder):\n    color_map = {\n        (0, 0, 0): -1,        # black\n        (128, 128, 128): 0,   # gray\n        (0, 255, 0): 1,       # green\n        (255, 255, 0): 2,     # yellow\n        (255, 165, 0): 3,     # orange\n        (255, 0, 0): 4        # red\n    }\n    count = 0\n\n    for image_name in os.listdir(ground_truth_folder):\n        base_name, _ = os.path.splitext(image_name)\n        ground_truth_path = os.path.join(ground_truth_folder, image_name)\n        image_path = os.path.join(image_folder, base_name + '.png')  # Assuming JPEG images\n\n        ground_truth_image = cv2.imread(ground_truth_path)\n        original_image = cv2.imread(image_path)\n\n        height, width, _ = original_image.shape\n\n        annotation_file_path = os.path.join(annotation_folder, base_name + '.txt')\n        count += 1\n        print(count)\n        with open(annotation_file_path, 'w') as f:\n            for color, class_id in color_map.items():\n                if class_id == -1:\n                    continue\n\n                mask = np.all(ground_truth_image == color, axis=-1)\n                contours, _ = cv2.findContours(mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n\n                for cnt in contours:\n                    x, y, w, h = cv2.boundingRect(cnt)\n                    x_center = (x + w / 2) / width\n                    y_center = (y + h / 2) / height\n                    w_norm = w / width\n                    h_norm = h / height\n\n                    f.write(f\"{class_id} {x_center} {y_center} {w_norm} {h_norm}\\n\")\n\nground_truth_folder = \"/kaggle/working/tiles/masks\"\nannotation_folder = \"/kaggle/working/annotations\"\nimage_folder = \"/kaggle/working/tiles/images\"\n\ncreate_yolo_annotations(ground_truth_folder, annotation_folder, image_folder)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/working/images | wc -l\n!ls /kaggle/working/tiles/masks | wc -l\n!ls /kaggle/working/annotations | wc -l","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Trying YOLO5","metadata":{}},{"cell_type":"code","source":"!pip install -r \"https://raw.githubusercontent.com/ultralytics/yolov5/master/requirements.txt\"\n!git clone https://github.com/ultralytics/yolov5.git\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mv /kaggle/working/train/tiles /kaggle/working","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mv /kaggle/working/annotations /kaggle/working/labels","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\nimport random\n\n# Set the paths to the images and annotations folders\nimages_path = '/kaggle/working/tiles/images'\nannotations_path = '/kaggle/working/annotations'\n\n# Set the path to the val folder\nval_path = '/kaggle/working/'\n\n# Create the images and labels folders in the val folder\nos.makedirs(os.path.join(val_path, 'images'))\nos.makedirs(os.path.join(val_path, 'labels'))\n\n# Get a list of all the image file names\nimage_files = os.listdir(images_path)\n\n# Randomly select 2000 images\nselected_images = random.sample(image_files, 2000)\n\n# Move the selected images and their corresponding annotations to the val folder\nfor image_file in selected_images:\n    # Move the image\n    shutil.move(os.path.join(images_path, image_file), os.path.join(val_path, 'images'))\n    \n    # Move the annotation\n    annotation_file = image_file.split('.')[0] + '.txt'  # assuming the annotations have the same name as the images, but with a .txt extension\n    shutil.move(os.path.join(annotations_path, annotation_file), os.path.join(val_path, 'labels'))\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**yaml file**","metadata":{}},{"cell_type":"code","source":"train: /kaggle/working/train\nval: path/to/val/images\n\nnc: 5\nnames: ['cell', 'Gleason_Grade_3', 'Benign', 'Gleason_Grade_4', 'Gleason_Grade_5']\n\nbatch_size: 16\nepochs: 50\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import yaml\nimport os\n\ntrain_dir = '/kaggle/working/train'\nval_dir = '/kaggle/working/val'\nnum_classes = 5\nclass_names = ['cell', 'Gleason_Grade_3', 'Benign', 'Gleason_Grade_4', 'Gleason_Grade_5']\nbatch_size = 16\nepochs = 10\n\ndata = {\n    'train': train_dir,\n    'val': val_dir,\n    'nc': num_classes,\n    'names': class_names,\n    'batch_size': batch_size,\n    'epochs': epochs\n}\n\nwith open(os.path.join(train_dir, 'prostate_cancer_data.yaml'), 'w') as f:\n    yaml.dump(data, f)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/working/val/labels | wc -l","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python yolov5/train.py --img 640 --batch 16 --epochs 10 --data /kaggle/working/train/prostate_cancer_data.yaml --cfg yolov5s.yaml --weights yolov5s.pt --name yolov5s_prostate_cancer\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python yolov5/val.py --weights /kaggle/working/yolov5s.pt --data /kaggle/working/train/prostate_cancer_data.yaml --img 640 --iou-thres 0.5 --conf-thres 0.5\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls /kaggle/working/train","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install tqdm","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"creating ","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**testing the images**","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport numpy as np\n\n%matplotlib inline\nimage_id = 'ffe9bcababc858e04840669e788065a1'\n\noriginal_image = \"/kaggle/working/tiles/images/158754df49e00760f8e4659a05e7cc0c_tile_2816_1024.png\"\nlabel_image_semantic = \"/kaggle/working/tiles/masks/158754df49e00760f8e4659a05e7cc0c_tile_2816_1024.png\"\n\nfig, axs = plt.subplots(1, 2, figsize=(16, 8), constrained_layout=True)\n\ncmap = matplotlib.colors.ListedColormap(['black', 'gray', 'green', 'yellow', 'orange', 'red'])\n\naxs[0].imshow( Image.open(original_image))\naxs[0].grid(False)\n\nlabel_image_semantic = Image.open(label_image_semantic)\nlabel_image_semantic = np.asarray(label_image_semantic)\naxs[1].imshow(label_image_semantic,cmap=cmap,interpolation='nearest', vmin=0, vmax=5)\naxs[1].grid(False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport numpy as np\n\n%matplotlib inline\nimage_id = 'ffe9bcababc858e04840669e788065a1'\n\noriginal_image = \"/kaggle/working/tiles/images/158754df49e00760f8e4659a05e7cc0c_tile_2816_1024.png\"\nlabel_image_semantic = \"/kaggle/working/tiles/masks/158754df49e00760f8e4659a05e7cc0c_tile_2816_1024.png\"\n\nfig, axs = plt.subplots(1, 2, figsize=(16, 8), constrained_layout=True)\n\ncmap = matplotlib.colors.ListedColormap(['black', 'gray', 'green', 'yellow', 'orange', 'red'])\n\naxs[0].imshow( Image.open(original_image))\naxs[0].grid(False)\n\nlabel_image_semantic = Image.open(label_image_semantic)\nlabel_image_semantic = np.asarray(label_image_semantic)\naxs[1].imshow(label_image_semantic,cmap=cmap,interpolation='nearest', vmin=0, vmax=5)\naxs[1].grid(False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"ziping the files","metadata":{}},{"cell_type":"code","source":"ls /kaggle/working/annotations","metadata":{"execution":{"iopub.status.busy":"2023-04-05T09:55:50.357678Z","iopub.execute_input":"2023-04-05T09:55:50.358145Z","iopub.status.idle":"2023-04-05T09:55:51.467465Z","shell.execute_reply.started":"2023-04-05T09:55:50.358106Z","shell.execute_reply":"2023-04-05T09:55:51.465806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport numpy as np\n\n%matplotlib inline\nimage_id = 'ffe9bcababc858e04840669e788065a1'\n\noriginal_image = \"/kaggle/working/tiles/images/158754df49e00760f8e4659a05e7cc0c_tile_2816_1024.png\"\nlabel_image_semantic = \"/kaggle/working/tiles/masks/158754df49e00760f8e4659a05e7cc0c_tile_2816_1024.png\"\n\nfig, axs = plt.subplots(1, 2, figsize=(16, 8), constrained_layout=True)\n\ncmap = matplotlib.colors.ListedColormap(['black', 'gray', 'green', 'yellow', 'orange', 'red'])\n\naxs[0].imshow( Image.open(original_image))\naxs[0].grid(False)\n\nlabel_image_semantic = Image.open(label_image_semantic)\nlabel_image_semantic = np.asarray(label_image_semantic)\naxs[1].imshow(label_image_semantic,cmap=cmap,interpolation='nearest', vmin=0, vmax=5)\naxs[1].grid(False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" !apt-get install libgtk2.0-dev pkg-config -y\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# working code for creating bounding box","metadata":{}},{"cell_type":"code","source":"\nimport os\nimport cv2\nimport numpy as np\nfrom skimage import measure\n\ndef create_annotations(ground_truth_image, output_annotation_path):\n    class_ids = {\n        0: 'black',\n        1: 'gray',\n        2: 'green',\n        3: 'yellow',\n        4: 'orange',\n        5: 'red'\n    }\n\n    gt_img = cv2.imread(ground_truth_image, cv2.IMREAD_GRAYSCALE)\n    height, width = gt_img.shape\n\n    annotation_data = []\n\n    for class_id, color_name in class_ids.items():\n        mask = np.where(gt_img == class_id, 255, 0).astype(np.uint8)\n        contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n\n        for contour in contours:\n            x, y, w, h = cv2.boundingRect(contour)\n            x_center, y_center = (x + w / 2) / width, (y + h / 2) / height\n            w, h = w / width, h / height\n\n            annotation_data.append(f\"{class_id} {x_center} {y_center} {w} {h}\")\n\n    with open(output_annotation_path, 'w') as f:\n        f.write('\\n'.join(annotation_data))\n\nground_truth_dir = '/kaggle/working/tiles/masks'\nannotation_output_dir = '/kaggle/working/annotations'\ncount = 0\nos.makedirs(annotation_output_dir, exist_ok=True)\n\nfor filename in os.listdir(ground_truth_dir):\n    if filename.endswith('.jpg') or filename.endswith('.png'):\n        ground_truth_image = os.path.join(ground_truth_dir, filename)\n        output_annotation_path = os.path.join(annotation_output_dir, os.path.splitext(filename)[0] + '.txt')\n        create_annotations(ground_truth_image, output_annotation_path)\n        count +=1\n        print(count)\n        if count == 10:\n            break\n","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:16:11.737446Z","iopub.execute_input":"2023-04-05T10:16:11.737959Z","iopub.status.idle":"2023-04-05T10:16:11.857163Z","shell.execute_reply.started":"2023-04-05T10:16:11.737913Z","shell.execute_reply":"2023-04-05T10:16:11.855971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls /kaggle/working/annotations","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport os\nimport matplotlib.pyplot as plt\n\ndef visualize_annotations(image_path, annotation_path):\n    image = cv2.imread(image_path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)  # Convert to RGB for matplotlib\n    with open(annotation_path, 'r') as f:\n        lines = f.readlines()\n\n    for line in lines:\n        data = line.strip().split(' ')\n        class_id, x_center, y_center, width, height = map(float, data)\n        x_center, y_center, width, height = x_center * image.shape[1], y_center * image.shape[0], width * image.shape[1], height * image.shape[0]\n        x1, y1, x2, y2 = int(x_center - width / 2), int(y_center - height / 2), int(x_center + width / 2), int(y_center + height / 2)\n        cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)\n        cv2.putText(image, str(int(class_id)), (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)\n\n    plt.imshow(image)\n    plt.show()\n\nimage_path = '/kaggle/working/train/images/008069b542b0439ed69b194674051964_tile_2048_13568.png'\nannotation_path = '/kaggle/working/annotations/008069b542b0439ed69b194674051964_tile_2048_13568.txt'\n\nvisualize_annotations(image_path, annotation_path)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:16:15.686564Z","iopub.execute_input":"2023-04-05T10:16:15.687064Z","iopub.status.idle":"2023-04-05T10:16:16.053403Z","shell.execute_reply.started":"2023-04-05T10:16:15.68702Z","shell.execute_reply":"2023-04-05T10:16:16.051936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport os\nimport matplotlib.pyplot as plt\n\ndef visualize_annotations(image_path, annotation_path):\n    image = cv2.imread(image_path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)  # Convert to RGB for matplotlib\n    with open(annotation_path, 'r') as f:\n        lines = f.readlines()\n\n    for line in lines:\n        data = line.strip().split(' ')\n        class_id, x_center, y_center, width, height = map(float, data)\n        x_center, y_center, width, height = x_center * image.shape[1], y_center * image.shape[0], width * image.shape[1], height * image.shape[0]\n        x1, y1, x2, y2 = int(x_center - width / 2), int(y_center - height / 2), int(x_center + width / 2), int(y_center + height / 2)\n        cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)\n        cv2.putText(image, str(int(class_id)), (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)\n\n    plt.imshow(image)\n    plt.show()\n\n# ... (rest of the code remains the same)\n\n\nimage_dir = '/kaggle/working/train/images'\nannotation_dir = '/kaggle/working/train/labels'\n\nfor filename in os.listdir(image_dir):\n    if filename.endswith('.jpg') or filename.endswith('.png'):\n        image_path = os.path.join(image_dir, filename)\n        annotation_path = os.path.join(annotation_dir, os.path.splitext(filename)[0] + '.txt')\n        if os.path.exists(annotation_path):\n            visualize_annotations(image_path, annotation_path)\n        else:\n            print(f\"Annotation file not found for {filename}\")\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!make -j8\n!sudo make install\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rm tiles.zip","metadata":{"execution":{"iopub.status.busy":"2023-06-16T08:57:14.88586Z","iopub.execute_input":"2023-06-16T08:57:14.886939Z","iopub.status.idle":"2023-06-16T08:57:16.864507Z","shell.execute_reply.started":"2023-06-16T08:57:14.886884Z","shell.execute_reply":"2023-06-16T08:57:16.86276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nimport os\n\n# Set the path of the folder to be compressed and zipped\nfolder_path = \"/kaggle/working/tiles\"\n\n# Set the name and path of the output zip file\nzip_path = \"/kaggle/working/tiles.zip\"\n\n# Create a zip file object with write permission\nwith zipfile.ZipFile(zip_path, 'w', compression=zipfile.ZIP_DEFLATED) as zip_file:\n    # Loop through all the files in the folder and add them to the zip file\n    for root, dirs, files in os.walk(folder_path):\n        for file in files:\n            file_path = os.path.join(root, file)\n            zip_file.write(file_path, os.path.relpath(file_path, folder_path))\n","metadata":{"execution":{"iopub.status.busy":"2023-06-16T08:34:54.906834Z","iopub.execute_input":"2023-06-16T08:34:54.908224Z","iopub.status.idle":"2023-06-16T08:44:57.744421Z","shell.execute_reply.started":"2023-06-16T08:34:54.908161Z","shell.execute_reply":"2023-06-16T08:44:57.741313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget -r /kaggle/working/tiles","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nimport os\n\n# The path to the folder you want to zip\nfolder_path = '/kaggle/working/tiles'\n\n# The path and name of the zip file you want to create\nzip_file_path = '/kaggle/working/tiles.zip'\n\n# The maximum size you want the zip file to be in GB\nmax_size_gb = 6\n\n# Convert the max size to bytes\nmax_size_bytes = max_size_gb * 1024 * 1024 * 1024\n\n# Create a ZipFile object with the name of the zip file\nzip_file = zipfile.ZipFile(zip_file_path, mode='w', compression=zipfile.ZIP_DEFLATED)\n\n# Walk through the folder and add each file to the zip file\nfor folder_name, subfolders, file_names in os.walk(folder_path):\n    for file_name in file_names:\n        file_path = os.path.join(folder_name, file_name)\n        file_size = os.path.getsize(file_path)\n\n        # If the zip file is already larger than the maximum size, stop adding files\n        if zip_file.fp.tell() + file_size > max_size_bytes:\n            break\n\n        zip_file.write(file_path)\n\n    # If the zip file is already larger than the maximum size, stop adding files\n    if zip_file.fp.tell() > max_size_bytes:\n        break\n\n# Close the ZipFile object\nzip_file.close()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"def tiling(location):\n\n    def create_tiles(image, tile_size, images_output_folder, image_filename, threshold=0.05):\n\n        height, width = image.shape[:2]\n\n        for y in range(0, height, tile_size):\n            for x in range(0, width, tile_size):\n                tile_image = image[y:y + tile_size, x:x + tile_size]\n\n                mean_color = np.mean(tile_image, axis=(0, 1))\n                mean_color_value = np.mean(mean_color)\n\n                if mean_color_value < 255 * (1 - threshold):\n                    tile_image_path = os.path.join(\n                        images_output_folder, f'{image_filename}_tile_{y}_{x}.png')\n\n                    cv2.imwrite(tile_image_path, tile_image)\n\n    def process_single_image(image_path,  tile_size, images_output_folder):\n        image = cv2.imread(image_path, cv2.IMREAD_COLOR)\n\n        if image is None:\n            print(\n                f\"Warning: Unable to read image or mask for file {os.path.basename(image_path)}. Skipping...\")\n            return\n\n        image_filename = os.path.splitext(os.path.basename(image_path))[0]\n        print(image_filename)\n        filename = image_filename\n\n        create_tiles(image,  tile_size, images_output_folder, image_filename)\n\n    # Input parameters\n    image_path = location\n    print(location)\n\n    tile_size = 256\n    images_output_folder = 'tiles'\n\n    # Process the single image\n    process_single_image(image_path, tile_size, images_output_folder)\n\n    return images_output_folder","metadata":{}},{"cell_type":"code","source":"def tiling(location):\n\n    def create_tiles(image, tile_size, images_output_folder, image_filename, threshold=0.05):\n\n        height, width = image.shape[:2]\n\n        for y in range(0, height, tile_size):\n            for x in range(0, width, tile_size):\n                tile_image = image[y:y + tile_size, x:x + tile_size]\n\n                mean_color = np.mean(tile_image, axis=(0, 1))\n                mean_color_value = np.mean(mean_color)\n\n                if mean_color_value < 255 * (1 - threshold):\n                    tile_image_path = os.path.join(\n                        images_output_folder, f'{image_filename}_tile_{y}_{x}.png')\n\n                    cv2.imwrite(tile_image_path, tile_image)\n\n    def process_single_image(image_path,  tile_size, images_output_folder):\n        image = cv2.imread(image_path, cv2.IMREAD_COLOR)\n\n        if image is None:\n            print(\n                f\"Warning: Unable to read image or mask for file {os.path.basename(image_path)}. Skipping...\")\n            return\n\n        image_filename = os.path.splitext(os.path.basename(image_path))[0]\n        print(image_filename)\n        filename = image_filename\n\n        create_tiles(image,  tile_size, images_output_folder, image_filename)\n\n    # Input parameters\n    image_path = location\n    print(location)\n\n    tile_size = 256\n    images_output_folder = 'tiles'\n\n    # Process the single image\n    process_single_image(image_path, tile_size, images_output_folder)\n\n    return images_output_folder","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tiling(location):\n\n    def create_tiles(image, tile_size, images_output_folder, image_filename, threshold=0.05):\n\n        height, width = image.shape[:2]\n\n        for y in range(0, height, tile_size):\n            for x in range(0, width, tile_size):\n                tile_image = image[y:y + tile_size, x:x + tile_size]\n\n                mean_color = np.mean(tile_image, axis=(0, 1))\n                mean_color_value = np.mean(mean_color)\n\n                if mean_color_value < 255 * (1 - threshold):\n                    tile_image_path = os.path.join(\n                        images_output_folder, f'{image_filename}_tile_{y}_{x}.png')\n\n                    cv2.imwrite(tile_image_path, tile_image)\n\n    def process_single_image(image_path,  tile_size, images_output_folder):\n        image = cv2.imread(image_path, cv2.IMREAD_COLOR)\n\n        if image is None:\n            print(\n                f\"Warning: Unable to read image or mask for file {os.path.basename(image_path)}. Skipping...\")\n            return\n\n        image_filename = os.path.splitext(os.path.basename(image_path))[0]\n        print(image_filename)\n        filename = image_filename\n\n        create_tiles(image,  tile_size, images_output_folder, image_filename)\n\n    # Input parameters\n    image_path = location\n    print(location)\n\n    tile_size = 256\n    images_output_folder = 'tiles'\n\n    # Process the single image\n    process_single_image(image_path, tile_size, images_output_folder)\n\n    return images_output_folder","metadata":{"execution":{"iopub.status.busy":"2023-04-14T00:31:23.990702Z","iopub.execute_input":"2023-04-14T00:31:23.992084Z","iopub.status.idle":"2023-04-14T00:31:31.884054Z","shell.execute_reply.started":"2023-04-14T00:31:23.992027Z","shell.execute_reply":"2023-04-14T00:31:31.882521Z"},"trusted":true},"execution_count":null,"outputs":[]}]}