{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "b4f4da53", "metadata": {}, "outputs": [], "source": [ "import os\n", "import pandas as pd\n", "from tqdm import tqdm\n", "from PIL import Image as Im\n", "from huggingface_hub import notebook_login\n", "from datasets import load_dataset, Dataset, Image" ] }, { "cell_type": "markdown", "id": "aa9019fe", "metadata": {}, "source": [ "### Load a set with coordinates" ] }, { "cell_type": "code", "execution_count": 2, "id": "11f31770", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
filenamex_fromy_fromwidthheightsign_classsign_id
0autosave01_02_2012_09_13_33.jpg64937618182_10
1autosave01_02_2012_09_13_34.jpg67135620212_10
2autosave01_02_2012_09_13_35.jpg71133227262_10
3autosave01_02_2012_09_13_36.jpg76429037362_10
4autosave01_02_2012_09_13_36.jpg68438417171_231
\n", "
" ], "text/plain": [ " filename x_from y_from width height sign_class \\\n", "0 autosave01_02_2012_09_13_33.jpg 649 376 18 18 2_1 \n", "1 autosave01_02_2012_09_13_34.jpg 671 356 20 21 2_1 \n", "2 autosave01_02_2012_09_13_35.jpg 711 332 27 26 2_1 \n", "3 autosave01_02_2012_09_13_36.jpg 764 290 37 36 2_1 \n", "4 autosave01_02_2012_09_13_36.jpg 684 384 17 17 1_23 \n", "\n", " sign_id \n", "0 0 \n", "1 0 \n", "2 0 \n", "3 0 \n", "4 1 " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = pd.read_csv('full-gt.csv') # from URL: https://graphics.cs.msu.ru/projects/traffic-sign-recognition.html\n", "data.head()" ] }, { "cell_type": "code", "execution_count": 3, "id": "1da9267c", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "['autosave01_02_2012_09_13_32.jpg',\n", " 'autosave01_02_2012_09_13_33.jpg',\n", " 'autosave01_02_2012_09_13_34.jpg',\n", " 'autosave01_02_2012_09_13_35.jpg',\n", " 'autosave01_02_2012_09_13_36.jpg']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.listdir('rtsd-frames')[:5] # from URL: https://www.kaggle.com/datasets/watchman/rtsd-dataset" ] }, { "cell_type": "markdown", "id": "dd0a8dfc", "metadata": {}, "source": [ "### Saving crop files" ] }, { "cell_type": "code", "execution_count": 4, "id": "e2cf6b7e", "metadata": {}, "outputs": [], "source": [ "source_dir = 'rtsd-frames'\n", "target_dir = 'dataset'\n", "\n", "if not os.path.exists(target_dir):\n", " os.makedirs(target_dir)\n", "\n", "def get_sign(\n", " filename, x_from, y_from, width, height, sign_class, sign_id, \n", " img_path=source_dir, res_path=target_dir\n", " ): \n", " img = Im.open(f'{img_path}/{filename}')\n", " img = img.crop((x_from, y_from, x_from + width, y_from + height))\n", " filename = f'{sign_class}___{sign_id}__{filename}'\n", " img.save(f'{target_dir}/{filename}')\n", " return {'filename': filename, 'sign_class': sign_class, 'sign_id': sign_id}" ] }, { "cell_type": "code", "execution_count": 5, "id": "7d81a1cf", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████████████████████████████████████████████████████████████████████| 104358/104358 [18:08<00:00, 95.90it/s]\n" ] } ], "source": [ "result, bad_data = [], []\n", "for i in tqdm(range(len(data))):\n", " try:\n", " result.append(get_sign(**data.iloc[i].to_dict()))\n", " except Exception as e:\n", " bad_data.append((e, data.iloc[i].to_dict()))\n", " print('.', end='')" ] }, { "cell_type": "code", "execution_count": 6, "id": "c288b477", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "104358" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(os.listdir(target_dir))" ] }, { "cell_type": "code", "execution_count": 7, "id": "51aae0a9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "104358" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(result)" ] }, { "cell_type": "code", "execution_count": 8, "id": "454cc4f4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(bad_data)" ] }, { "cell_type": "markdown", "id": "4fadacb7", "metadata": {}, "source": [ "### Metadata generation" ] }, { "cell_type": "code", "execution_count": 9, "id": "f9ee692c", "metadata": {}, "outputs": [], "source": [ "pd.DataFrame(result).to_csv(f'{target_dir}.csv', index=False)" ] }, { "cell_type": "code", "execution_count": 10, "id": "07d9969e", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
file_nameadditional_feature
02_1___0__autosave01_02_2012_09_13_33.jpg2_1
12_1___0__autosave01_02_2012_09_13_34.jpg2_1
22_1___0__autosave01_02_2012_09_13_35.jpg2_1
32_1___0__autosave01_02_2012_09_13_36.jpg2_1
41_23___1__autosave01_02_2012_09_13_36.jpg1_23
\n", "
" ], "text/plain": [ " file_name additional_feature\n", "0 2_1___0__autosave01_02_2012_09_13_33.jpg 2_1\n", "1 2_1___0__autosave01_02_2012_09_13_34.jpg 2_1\n", "2 2_1___0__autosave01_02_2012_09_13_35.jpg 2_1\n", "3 2_1___0__autosave01_02_2012_09_13_36.jpg 2_1\n", "4 1_23___1__autosave01_02_2012_09_13_36.jpg 1_23" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame(result)\n", "df.drop(columns=['sign_id'], inplace=True)\n", "df.columns = ['file_name', 'additional_feature']\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 11, "id": "57a8c62a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
file_nameadditional_feature
02_1___0__autosave01_02_2012_09_13_33.jpg2_1
12_1___0__autosave01_02_2012_09_13_34.jpg2_1
22_1___0__autosave01_02_2012_09_13_35.jpg2_1
32_1___0__autosave01_02_2012_09_13_36.jpg2_1
41_23___1__autosave01_02_2012_09_13_36.jpg1_23
\n", "
" ], "text/plain": [ " file_name additional_feature\n", "0 2_1___0__autosave01_02_2012_09_13_33.jpg 2_1\n", "1 2_1___0__autosave01_02_2012_09_13_34.jpg 2_1\n", "2 2_1___0__autosave01_02_2012_09_13_35.jpg 2_1\n", "3 2_1___0__autosave01_02_2012_09_13_36.jpg 2_1\n", "4 1_23___1__autosave01_02_2012_09_13_36.jpg 1_23" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.to_json(f'{target_dir}/metadata.jsonl', orient='records', lines=True); df.head()" ] }, { "cell_type": "code", "execution_count": 12, "id": "7245bea0", "metadata": {}, "outputs": [], "source": [ "metadata = pd.read_csv(f'{target_dir}.csv') # или metadata = df.copy()\n", "metadata.columns = ['image', 'sign_class', 'sign_id']\n", "metadata['image'] = metadata['image'].apply(lambda x: f'{target_dir}/{x}')\n", "metadata = metadata.to_dict(orient='list')" ] }, { "cell_type": "markdown", "id": "591c55b5", "metadata": {}, "source": [ "### Creating a formatted dataset" ] }, { "cell_type": "code", "execution_count": 13, "id": "6d5fb041", "metadata": {}, "outputs": [], "source": [ "dataset = Dataset.from_dict(metadata).cast_column(\"image\", Image())" ] }, { "cell_type": "code", "execution_count": 14, "id": "acd56ad2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dataset({\n", " features: ['image', 'sign_class', 'sign_id'],\n", " num_rows: 104358\n", "})" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset" ] }, { "cell_type": "code", "execution_count": 15, "id": "98bb6893", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'image': ,\n", " 'sign_class': '2_1',\n", " 'sign_id': 0}" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset[2]" ] }, { "cell_type": "code", "execution_count": 16, "id": "3ab455b9", "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAABsAAAAaCAIAAADJ6rCnAAAFtklEQVR4nC3VSZYbxxEA0BgyawZQGJpky6TJpuyl176RL20/P4mmyJ67ANSUQ0R4ozP8xcf9P/+lqgBgJiGuYVlAMzAS0W63+/Dhw+FwMLN5nmPIjLiMPqUUwpJyQLOqLpqmKks/DINzjr1zzB5AzATRFa5QLykhgjG5pqw2TdtWNRGVzi/LEmMOaKYxpyA5EUNKNk9pnjHGKCLO1DnyaATAiJgBzQkZAlhTlfvdod/u2rpBgNJ5TzzjPOMElk2TaTSEnCgGyTkzs6hTVQdiDpnZA4ClDIqeXeFdv9ue+v2u7URSWFdE9Gi157pAMkfGIVDOWVLKkkWEwAsAqLkcU1F4RjLNkjKKVlWx225Oh77vagc6Xc/D8OqI66b0zIeNl45D68dxvIzXeY6CgoQ5RzZTJZfigiBkycxIU+Gp75rTvjvsNp40TsN4fpyGV3bI2tZ13W92RE5Vx5a9V0Jb5iBm05IFEoN3Jmtc1hyAiDxh21THfXPad11DIVyn61te3hxMZGA5Spy58QVXrirrcuMdlo5fL9O8pJATk0cmhxhijCBalK5um65zu23RdVRyni+vYXpGHZvKGIUtWBplLQWa0m/LumHXIWI2MJtzrgwdELp5enb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7Qd13jiUXw7bL+9v3H/dNLiHmNWUSJC0lhnjHn6At2vrCcU86gIKUrVdHMouSU0rhM4zJGieAZvSuhNJP1erl/fBnnCK5UiGrgGXPWHFdGI2CnFpCBCAFSjBJhNUIEFkm+LMTy0/PzmtZpmdq2RbSn+59Pb5cQgievqmYWY1RV732Ma8rgxAIAmAEAIbAioTKiAGZREwU1w8mKM6cUzCwmS1lzUqUkSUxEiUDkTw3F/wPe2gVRfByOAwAAAABJRU5ErkJggg==\n", "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset[2]['image']" ] }, { "cell_type": "markdown", "id": "49420851", "metadata": {}, "source": [ "### Uploading to remote storage" ] }, { "cell_type": "code", "execution_count": 17, "id": "1e3a67b4", "metadata": {}, "outputs": [], "source": [ "notebook_login()" ] }, { "cell_type": "code", "execution_count": 18, "id": "e59e80c5", "metadata": {}, "outputs": [], "source": [ "dataset.push_to_hub(\"eleldar/rtsd_cleaned\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }