{ "cells": [ { "cell_type": "code", "id": "9ca8370e29b99d9b", "metadata": { "collapsed": true, "ExecuteTime": { "end_time": "2024-04-17T07:40:35.575275Z", "start_time": "2024-04-17T07:40:35.397474Z" } }, "source": [ "import json\n", "import os\n", "import shutil\n", "\n", "import pandas as pd" ], "execution_count": 2, "outputs": [] }, { "metadata": { "ExecuteTime": { "end_time": "2024-04-17T07:59:30.461558Z", "start_time": "2024-04-17T07:59:30.458532Z" } }, "cell_type": "code", "source": [ "def load_dataset(input_annotations_file: str, input_img_dir: str, output_dir: str):\n", " with open(input_annotations_file, \"r\") as f:\n", " dataset = json.load(f)\n", " df_annotation = pd.DataFrame(dataset['annotations']).drop(['segmentation', 'attribute_ids'], axis='columns')\n", " df_images = pd.DataFrame(dataset['images'])\n", " df_categories = pd.DataFrame(dataset['categories'])\n", "\n", " df_dataset = pd.merge(df_images, df_annotation, how='inner', left_on='id', right_on='image_id')\n", " df_dataset = pd.merge(df_dataset, df_categories, how='inner', left_on='category_id', right_on='id')\n", " df_dataset['path_to_image'] = input_img_dir + df_dataset['file_name']\n", " return df_dataset\n" ], "id": "initial_id", "execution_count": 48, "outputs": [] }, { "metadata": { "ExecuteTime": { "end_time": "2024-04-17T07:59:30.763506Z", "start_time": "2024-04-17T07:59:30.759581Z" } }, "cell_type": "code", "source": [ "def save_labels_one_image(df_one_image: pd.DataFrame, output_folder: str, subfolder: str):\n", " path_to_image = df_one_image.iloc[0]['path_to_image']\n", " image_id = df_one_image.iloc[0]['image_id']\n", " image_width = df_one_image.iloc[0]['width']\n", " image_height = df_one_image.iloc[0]['height']\n", "\n", " def format_one_box(one_line) -> str:\n", " label_index = str(one_line['category_id'])\n", "\n", " [x_center, y_center, width, height] = one_line['bbox']\n", " x_center_normalised, y_center_normalised = (x_center+width/2) / image_width, (y_center+height/2) / image_height\n", " width_normalised, height_normalised = width / image_width, height / image_height\n", " box = ' '.join(map(str, [x_center_normalised, y_center_normalised, width_normalised, height_normalised]))\n", "\n", " line = f'{label_index} {box}'\n", " return line\n", "\n", " boxes_formatted = df_one_image.apply(format_one_box, axis='columns').tolist()\n", "\n", " with open(os.path.join(output_folder, 'labels', subfolder, f'{image_id}.txt'), 'w') as f:\n", " f.write('\\n'.join(boxes_formatted))\n", " shutil.copy(path_to_image, os.path.join(output_folder, 'images', subfolder, f'{image_id}.jpg'))\n", "\n", " return image_id\n" ], "id": "aa0ec50c0b8b00f3", "execution_count": 49, "outputs": [] }, { "metadata": { "ExecuteTime": { "end_time": "2024-04-17T07:59:33.282829Z", "start_time": "2024-04-17T07:59:32.281139Z" } }, "cell_type": "code", "source": [ "path_to_json_validation = \"./dataset/fashionpedia2020/instances_attributes_val2020.json\"\n", "path_to_image_folder_validation = './dataset/fashionpedia2020/val_test2020/test/'\n", "output_folder_validation = 'val2020'\n", "fashionpedia_dataset_validation = load_dataset(path_to_json_validation, path_to_image_folder_validation,\n", " 'dataset/fashionpedia-yolo')\n", "fashionpedia_dataset_validation.groupby(['image_id']).apply(save_labels_one_image, './dataset/fashionpedia-yolo',\n", " output_folder_validation)" ], "id": "31756215355a324e", "execution_count": 50, "outputs": [] }, { "metadata": { "ExecuteTime": { "end_time": "2024-04-17T08:00:11.718962Z", "start_time": "2024-04-17T07:59:33.326724Z" } }, "cell_type": "code", "source": [ "path_to_json_train = \"./dataset/fashionpedia2020/instances_attributes_train2020.json\"\n", "path_to_image_folder_train = './dataset/fashionpedia2020/train2020/train/'\n", "output_folder_train = 'train2020'\n", "fashionpedia_dataset_train = load_dataset(path_to_json_train, path_to_image_folder_train, 'dataset/fashionpedia-yolo')\n", "fashionpedia_dataset_train.groupby(['image_id']).apply(save_labels_one_image, './dataset/fashionpedia-yolo',\n", " output_folder_train)" ], "id": "ba95b509750ea10b", "execution_count": 51, "outputs": [] }, { "metadata": {}, "cell_type": "code", "execution_count": null, "source": "", "id": "77dff2cb00f9b08c", "outputs": [] }, { "metadata": { "ExecuteTime": { "end_time": "2024-04-17T08:00:11.733525Z", "start_time": "2024-04-17T08:00:11.731305Z" } }, "cell_type": "code", "source": [ "from matplotlib import pyplot as plt\n", "\n", "COLORS = ['lightcoral', 'yellowgreen', 'darkturquoise', 'hotpink', 'mediumslateblue']\n", "\n", "\n", "def plot_results(pil_img, boxes, class_labels):\n", " plt.figure(figsize=(16, 10))\n", " plt.imshow(pil_img)\n", " ax = plt.gca()\n", " colors = COLORS * 100\n", " for [xcenter, ycenter, xlength, ylength], c, label in zip(boxes, colors, class_labels):\n", " xmin = (xcenter-(xlength/2)) * pil_img.width\n", " xmax = (xcenter+(xlength/2)) * pil_img.width\n", " ymin = (ycenter-(ylength/2)) * pil_img.height\n", " ymax = (ycenter+(ylength/2)) * pil_img.height\n", " \n", " print(xmin, ymin)\n", " ax.add_patch(plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin,\n", " fill=False, color=c, linewidth=2))\n", " ax.text(xmin, ymin, str(label), fontsize=15,\n", " bbox=dict(facecolor=c, alpha=0.5))\n", " plt.axis('off')\n", " plt.show()" ], "id": "a16ebfa1cf517c1a", "execution_count": 52, "outputs": [] }, { "metadata": { "ExecuteTime": { "end_time": "2024-04-17T08:00:11.921853Z", "start_time": "2024-04-17T08:00:11.733970Z" } }, "cell_type": "code", "source": [ "from PIL import Image\n", "\n", "pil_image = Image.open('dataset/fashionpedia-yolo/images/train2020/23.jpg')\n", "with open('dataset/fashionpedia-yolo/labels/train2020/23.txt' ) as f:\n", " labels = f.readlines()\n", "\n", "class_labels = []\n", "box_center_length = []\n", "for a_label in labels:\n", " labels_split = a_label.split(' ')\n", " class_labels.append(int(labels_split[0]))\n", " box_center_length.append(list(map(float, labels_split[1:])))\n", "plot_results(pil_image,box_center_length, class_labels)" ], "id": "8c5dacbcecca7d42", "execution_count": 53, "outputs": [] }, { "metadata": {}, "cell_type": "code", "execution_count": null, "source": "", "id": "d7ac9abea09107e1", "outputs": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }