-
-
Save heyodai/43548d252b6ad03f54e400dbdfc93778 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Reference: https://github.com/SkalskiP/make-sense/issues/351\n", | |
"\n", | |
"[makesense.ai](https://www.makesense.ai/) is not exporting my annotations properly to YOLO format. I believe this a bug caused by some files having the same file names (e.g. `1.jpg`).\n", | |
"\n", | |
"Fortunately, the CSV export works as expected. This notebook transforms the CSV export to YOLO format." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n", | |
"import os\n", | |
"\n", | |
"BASE_FP = './dataset'\n", | |
"DATASET_FP = f'{BASE_FP}/v3'\n", | |
"CLASSES = ['thrip', 'healthy']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"!rm -rf {DATASET_FP}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Create dataset folders\n", | |
"os.makedirs(DATASET_FP, exist_ok=True)\n", | |
"os.makedirs(f\"{DATASET_FP}/images\", exist_ok=True)\n", | |
"os.makedirs(f\"{DATASET_FP}/labels\", exist_ok=True)\n", | |
"\n", | |
"# Create `classes.txt` file\n", | |
"with open(f\"{DATASET_FP}/classes.txt\", 'w') as f:\n", | |
" for c in CLASSES:\n", | |
" f.write(f\"{c}\\n\")\n", | |
" \n", | |
"# Create `dataset.yaml` file\n", | |
"data = f\"\"\"\n", | |
"path: {DATASET_FP} \n", | |
"train: {DATASET_FP}/images\n", | |
"\n", | |
"nc: {len(CLASSES)}\n", | |
"names: {CLASSES}\n", | |
"\"\"\"\n", | |
"with open(f\"{DATASET_FP}/dataset.yaml\", 'w') as f:\n", | |
" f.write(data)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>label_name</th>\n", | |
" <th>bbox_x</th>\n", | |
" <th>bbox_y</th>\n", | |
" <th>bbox_width</th>\n", | |
" <th>bbox_height</th>\n", | |
" <th>image_name</th>\n", | |
" <th>image_width</th>\n", | |
" <th>image_height</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>thrip</td>\n", | |
" <td>0</td>\n", | |
" <td>323</td>\n", | |
" <td>1077</td>\n", | |
" <td>582</td>\n", | |
" <td>1.jpg</td>\n", | |
" <td>1080</td>\n", | |
" <td>1920</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>thrip</td>\n", | |
" <td>0</td>\n", | |
" <td>7</td>\n", | |
" <td>454</td>\n", | |
" <td>499</td>\n", | |
" <td>1.jpg</td>\n", | |
" <td>1080</td>\n", | |
" <td>1920</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>thrip</td>\n", | |
" <td>10</td>\n", | |
" <td>912</td>\n", | |
" <td>1063</td>\n", | |
" <td>994</td>\n", | |
" <td>1.jpg</td>\n", | |
" <td>1080</td>\n", | |
" <td>1920</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>thrip</td>\n", | |
" <td>7</td>\n", | |
" <td>151</td>\n", | |
" <td>523</td>\n", | |
" <td>1762</td>\n", | |
" <td>10.jpg</td>\n", | |
" <td>1080</td>\n", | |
" <td>1920</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>thrip</td>\n", | |
" <td>468</td>\n", | |
" <td>179</td>\n", | |
" <td>602</td>\n", | |
" <td>1734</td>\n", | |
" <td>10.jpg</td>\n", | |
" <td>1080</td>\n", | |
" <td>1920</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" label_name bbox_x bbox_y bbox_width bbox_height image_name image_width \\\n", | |
"0 thrip 0 323 1077 582 1.jpg 1080 \n", | |
"1 thrip 0 7 454 499 1.jpg 1080 \n", | |
"2 thrip 10 912 1063 994 1.jpg 1080 \n", | |
"3 thrip 7 151 523 1762 10.jpg 1080 \n", | |
"4 thrip 468 179 602 1734 10.jpg 1080 \n", | |
"\n", | |
" image_height \n", | |
"0 1920 \n", | |
"1 1920 \n", | |
"2 1920 \n", | |
"3 1920 \n", | |
"4 1920 " | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df = pd.read_csv('makesense.csv')\n", | |
"df.head()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(176, 8)" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"last_match = None\n", | |
"file_count = 1\n", | |
"\n", | |
"for index, row in df.iterrows():\n", | |
" class_name = row['label_name']\n", | |
" file_name = row['image_name'].split('.')[0]\n", | |
" \n", | |
" input_fp = f\"{BASE_FP}/images/{class_name}/{file_name}.jpg\"\n", | |
" output_fp = f\"{DATASET_FP}/images/image{file_count}.jpg\"\n", | |
" \n", | |
" # Copy if not already copied\n", | |
" new_match = f\"{class_name}_{file_name}\"\n", | |
" if last_match == None or last_match != new_match:\n", | |
" os.system(f\"cp {input_fp} {output_fp}\")\n", | |
" file_count += 1\n", | |
" last_match = new_match\n", | |
" \n", | |
" # Create the label row\n", | |
" cid = CLASSES.index(class_name)\n", | |
" x = row['bbox_x']\n", | |
" y = row['bbox_y']\n", | |
" w = row['bbox_width']\n", | |
" h = row['bbox_height']\n", | |
" label_format = f\"{cid} {x} {y} {w} {h}\"\n", | |
" label_file = f\"{DATASET_FP}/labels/image{file_count}.txt\"\n", | |
" \n", | |
" # Append to the label file\n", | |
" with open(label_file, 'a') as f:\n", | |
" f.write(f\"{label_format}\\n\")\n", | |
" " | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": ".venv", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.11.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment