{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def read_images(csv_file):\n", " # Returns a df with the images in it\n", " \n", " import pandas as pd\n", " from skimage import io\n", " \n", " df = pd.read_csv(csv_file)\n", " read_images = []\n", " for file in df['image_path']:\n", " read_images.append(io.imread(file))\n", "\n", " df = df.assign(image = read_images)\n", " \n", " return df" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def create_hog(images, canny = False):\n", " # Takes a list of images, returns a list of the hog descriptors\n", " \n", " from skimage.feature import hog\n", "\n", " hog_features = []\n", " \n", " if canny: # we need a special case for canny hog because we have to specify the image as type int\n", " for image in images:\n", " hog_features.append(hog(image.astype(int),\n", " orientations = 8,\n", " pixels_per_cell = (40, 40),\n", " visualize = False)\n", " )\n", " else:\n", " for image in images:\n", " hog_features.append(hog(image,\n", " orientations = 8,\n", " pixels_per_cell = (40, 40),\n", " visualize = False)\n", " )\n", " \n", " return hog_features" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def classify_images(df, random_state = 0):\n", " # From a df, performs image classification and returns the results\n", " # Takes random_state as an input to allow different reproducible results\n", " \n", " import numpy as np\n", " from sklearn.ensemble import RandomForestClassifier\n", " from sklearn.model_selection import train_test_split\n", " \n", " x = ['hog_features', 'image', 'image_path']\n", " train, test, y_train, y_test = train_test_split(df[x],\n", " df['label'],\n", " test_size = 0.2,\n", " random_state = random_state)\n", " \n", " x_train = np.stack(train['hog_features'].values)\n", " x_test = np.stack(test['hog_features'].values)\n", " \n", " random_forest = RandomForestClassifier(n_estimators = 10, max_depth = 7, random_state = random_state)\n", " random_forest.fit(x_train, y_train.values)\n", " predictions = random_forest.predict(x_test)\n", " \n", " return predictions, y_test, test" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def print_results(predictions, test):\n", " \n", " import matplotlib.pyplot as plt\n", " import pandas as pd\n", " from skimage import io\n", "\n", " i = 0\n", " l = len(test)\n", " \n", " df_test = pd.DataFrame(test)\n", " \n", " for index, row in df_test.iterrows():\n", " plt.subplot(1, l, i+1)\n", " plt.imshow(io.imread(row['image_path']))\n", " plt.title(str(predictions[i]))\n", " i = i + 1\n", " plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.1" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }