{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import json\n", "import os\n", "import shutil\n", "import tensorflow as tf\n", "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", "from tensorflow.keras.utils import image_dataset_from_directory\n", "from tensorflow.keras.models import Sequential\n", "from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\n", "from tensorflow.keras.callbacks import Callback" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def create_dataframe(annotations_path):\n", " with open(annotations_path, 'r') as file:\n", " data = json.load(file)\n", "\n", " images = pd.DataFrame(data['images']).rename(columns={'id': 'image_id'})[['image_id', 'file_name']]\n", "\n", " categories = pd.DataFrame(data['categories'])[['id', 'name']]\n", " categories.rename(columns={'id': 'category_id'}, inplace=True)\n", "\n", " usecols = ['image_id', 'category_id']\n", " annotations = pd.DataFrame(data['annotations'])[usecols]\n", "\n", " dataframe = annotations.merge(categories, on='category_id').merge(images, on='image_id')[['file_name', 'name']]\n", " \n", " return dataframe" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def copy_images_to_destination(base_dir, dataframe, split):\n", " images_dir = os.path.join(base_dir, 'images')\n", "\n", " for index, row in dataframe.iterrows():\n", " file_name = row['file_name']\n", " file_class = row['name']\n", "\n", " dest_dir = os.path.join(split, file_class)\n", " os.makedirs(dest_dir, exist_ok=True)\n", "\n", " source_path = os.path.join(images_dir, file_name)\n", " destination_path = os.path.join(dest_dir, file_name)\n", "\n", " shutil.copyfile(source_path, destination_path)\n", "\n", " print(\"Done copying images.\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | file_name | \n", "name | \n", "
---|---|---|
0 | \n", "131094.jpg | \n", "soft-cheese | \n", "
1 | \n", "131094.jpg | \n", "ham-raw | \n", "
2 | \n", "131094.jpg | \n", "hard-cheese | \n", "
3 | \n", "131094.jpg | \n", "bread-wholemeal | \n", "
4 | \n", "131094.jpg | \n", "cottage-cheese | \n", "
... | \n", "... | \n", "... | \n", "
76486 | \n", "117029.jpg | \n", "damson-plum | \n", "
76487 | \n", "117524.jpg | \n", "damson-plum | \n", "
76488 | \n", "117849.jpg | \n", "damson-plum | \n", "
76489 | \n", "123468.jpg | \n", "damson-plum | \n", "
76490 | \n", "095795.jpg | \n", "bean-seeds | \n", "
76491 rows × 2 columns
\n", "\n", " | file_name | \n", "name | \n", "
---|---|---|
0 | \n", "149022.jpg | \n", "espresso-with-caffeine | \n", "
1 | \n", "149022.jpg | \n", "dark-chocolate | \n", "
2 | \n", "167905.jpg | \n", "espresso-with-caffeine | \n", "
3 | \n", "121313.jpg | \n", "espresso-with-caffeine | \n", "
4 | \n", "153429.jpg | \n", "espresso-with-caffeine | \n", "
... | \n", "... | \n", "... | \n", "
1825 | \n", "144675.jpg | \n", "oat-milk | \n", "
1826 | \n", "103273.jpg | \n", "soup-potato | \n", "
1827 | \n", "159922.jpg | \n", "red-cabbage | \n", "
1828 | \n", "011275.jpg | \n", "pasta-in-conch-form | \n", "
1829 | \n", "166537.jpg | \n", "chocolate | \n", "
1830 rows × 2 columns
\n", "