зеркало из
				https://github.com/ssciwr/AMMICO.git
				synced 2025-10-31 05:56:05 +02:00 
			
		
		
		
	
		
			
				
	
	
		
			363 строки
		
	
	
		
			10 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			363 строки
		
	
	
		
			10 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
| {
 | |
|  "cells": [
 | |
|   {
 | |
|    "cell_type": "markdown",
 | |
|    "metadata": {},
 | |
|    "source": [
 | |
|     "# Color Detector\n",
 | |
|     "\n",
 | |
|     "\n",
 | |
|     "\n",
 | |
|     "This notebook shows primary color analysis of color image using K-Means algorithm.\n",
 | |
|     "The output are N primary colors and their corresponding percentage.\n",
 | |
|     "\n",
 | |
|     "The first cell is only run on google colab and installs the [ammico](https://github.com/ssciwr/AMMICO) package.\n",
 | |
|     "\n",
 | |
|     "After that, we can import `ammico` and read in the files given a folder path."
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": 1,
 | |
|    "metadata": {
 | |
|     "execution": {
 | |
|      "iopub.execute_input": "2023-12-13T10:00:38.050307Z",
 | |
|      "iopub.status.busy": "2023-12-13T10:00:38.050113Z",
 | |
|      "iopub.status.idle": "2023-12-13T10:00:38.057744Z",
 | |
|      "shell.execute_reply": "2023-12-13T10:00:38.057124Z"
 | |
|     }
 | |
|    },
 | |
|    "outputs": [],
 | |
|    "source": [
 | |
|     "# if running on google colab\n",
 | |
|     "# flake8-noqa-cell\n",
 | |
|     "import os\n",
 | |
|     "\n",
 | |
|     "if \"google.colab\" in str(get_ipython()):\n",
 | |
|     "    # update python version\n",
 | |
|     "    # install setuptools\n",
 | |
|     "    # %pip install setuptools==61 -qqq\n",
 | |
|     "    # install ammico\n",
 | |
|     "    %pip install git+https://github.com/ssciwr/ammico.git -qqq\n",
 | |
|     "    # mount google drive for data and API key\n",
 | |
|     "    from google.colab import drive\n",
 | |
|     "\n",
 | |
|     "    drive.mount(\"/content/drive\")"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": 2,
 | |
|    "metadata": {
 | |
|     "execution": {
 | |
|      "iopub.execute_input": "2023-12-13T10:00:38.060128Z",
 | |
|      "iopub.status.busy": "2023-12-13T10:00:38.059900Z",
 | |
|      "iopub.status.idle": "2023-12-13T10:00:51.760685Z",
 | |
|      "shell.execute_reply": "2023-12-13T10:00:51.760058Z"
 | |
|     }
 | |
|    },
 | |
|    "outputs": [],
 | |
|    "source": [
 | |
|     "import ammico\n",
 | |
|     "from ammico import utils as mutils\n",
 | |
|     "from ammico import display as mdisplay\n"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "markdown",
 | |
|    "metadata": {},
 | |
|    "source": [
 | |
|     "We select a subset of image files to try the color analysis on, see the `limit` keyword. The `find_files` function finds image files within a given directory:"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": 3,
 | |
|    "metadata": {
 | |
|     "execution": {
 | |
|      "iopub.execute_input": "2023-12-13T10:00:51.764523Z",
 | |
|      "iopub.status.busy": "2023-12-13T10:00:51.764074Z",
 | |
|      "iopub.status.idle": "2023-12-13T10:00:51.769014Z",
 | |
|      "shell.execute_reply": "2023-12-13T10:00:51.768506Z"
 | |
|     }
 | |
|    },
 | |
|    "outputs": [],
 | |
|    "source": [
 | |
|     "# Here you need to provide the path to your google drive folder\n",
 | |
|     "# or local folder containing the images\n",
 | |
|     "image_dict = mutils.find_files(\n",
 | |
|     "    path=\"data/\",\n",
 | |
|     "    limit=10,\n",
 | |
|     ")\n"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "markdown",
 | |
|    "metadata": {},
 | |
|    "source": [
 | |
|     "To check the analysis, you can inspect the analyzed elements here. Loading the results takes a moment, so please be patient. If you are sure of what you are doing, you can skip this and directly export a csv file in the step below.\n",
 | |
|     "Here, we display the color detection results provided by `colorgram` and `colour` libraries. Click on the tabs to see the results in the right sidebar. You may need to increment the `port` number if you are already running several notebook instances on the same server."
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": 4,
 | |
|    "metadata": {
 | |
|     "execution": {
 | |
|      "iopub.execute_input": "2023-12-13T10:00:51.771479Z",
 | |
|      "iopub.status.busy": "2023-12-13T10:00:51.771113Z",
 | |
|      "iopub.status.idle": "2023-12-13T10:00:51.798285Z",
 | |
|      "shell.execute_reply": "2023-12-13T10:00:51.797326Z"
 | |
|     }
 | |
|    },
 | |
|    "outputs": [
 | |
|     {
 | |
|      "data": {
 | |
|       "text/html": [
 | |
|        "\n",
 | |
|        "        <iframe\n",
 | |
|        "            width=\"100%\"\n",
 | |
|        "            height=\"650\"\n",
 | |
|        "            src=\"http://127.0.0.1:8057/\"\n",
 | |
|        "            frameborder=\"0\"\n",
 | |
|        "            allowfullscreen\n",
 | |
|        "            \n",
 | |
|        "        ></iframe>\n",
 | |
|        "        "
 | |
|       ],
 | |
|       "text/plain": [
 | |
|        "<IPython.lib.display.IFrame at 0x7f9f549037c0>"
 | |
|       ]
 | |
|      },
 | |
|      "metadata": {},
 | |
|      "output_type": "display_data"
 | |
|     }
 | |
|    ],
 | |
|    "source": [
 | |
|     "analysis_explorer = mdisplay.AnalysisExplorer(image_dict)\n",
 | |
|     "analysis_explorer.run_server(port = 8057)"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "markdown",
 | |
|    "metadata": {},
 | |
|    "source": [
 | |
|     "Instead of inspecting each of the images, you can also directly carry out the analysis and export the result into a csv. This may take a while depending on how many images you have loaded."
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": 5,
 | |
|    "metadata": {
 | |
|     "execution": {
 | |
|      "iopub.execute_input": "2023-12-13T10:00:51.801733Z",
 | |
|      "iopub.status.busy": "2023-12-13T10:00:51.801166Z",
 | |
|      "iopub.status.idle": "2023-12-13T10:00:57.146255Z",
 | |
|      "shell.execute_reply": "2023-12-13T10:00:57.145448Z"
 | |
|     }
 | |
|    },
 | |
|    "outputs": [],
 | |
|    "source": [
 | |
|     "for key in image_dict.keys():\n",
 | |
|     "    image_dict[key] = ammico.colors.ColorDetector(image_dict[key]).analyse_image()"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "markdown",
 | |
|    "metadata": {},
 | |
|    "source": [
 | |
|     "These steps are required to convert the dictionary of dictionarys into a dictionary with lists, that can be converted into a pandas dataframe and exported to a csv file."
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": 6,
 | |
|    "metadata": {
 | |
|     "execution": {
 | |
|      "iopub.execute_input": "2023-12-13T10:00:57.150305Z",
 | |
|      "iopub.status.busy": "2023-12-13T10:00:57.149910Z",
 | |
|      "iopub.status.idle": "2023-12-13T10:00:57.153495Z",
 | |
|      "shell.execute_reply": "2023-12-13T10:00:57.152991Z"
 | |
|     }
 | |
|    },
 | |
|    "outputs": [],
 | |
|    "source": [
 | |
|     "image_df = ammico.get_dataframe(image_dict)"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "markdown",
 | |
|    "metadata": {},
 | |
|    "source": [
 | |
|     "Check the dataframe:"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": 7,
 | |
|    "metadata": {
 | |
|     "execution": {
 | |
|      "iopub.execute_input": "2023-12-13T10:00:57.156601Z",
 | |
|      "iopub.status.busy": "2023-12-13T10:00:57.156195Z",
 | |
|      "iopub.status.idle": "2023-12-13T10:00:57.168780Z",
 | |
|      "shell.execute_reply": "2023-12-13T10:00:57.168151Z"
 | |
|     }
 | |
|    },
 | |
|    "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>filename</th>\n",
 | |
|        "      <th>red</th>\n",
 | |
|        "      <th>green</th>\n",
 | |
|        "      <th>blue</th>\n",
 | |
|        "      <th>yellow</th>\n",
 | |
|        "      <th>cyan</th>\n",
 | |
|        "      <th>orange</th>\n",
 | |
|        "      <th>purple</th>\n",
 | |
|        "      <th>pink</th>\n",
 | |
|        "      <th>brown</th>\n",
 | |
|        "      <th>grey</th>\n",
 | |
|        "      <th>white</th>\n",
 | |
|        "      <th>black</th>\n",
 | |
|        "    </tr>\n",
 | |
|        "  </thead>\n",
 | |
|        "  <tbody>\n",
 | |
|        "    <tr>\n",
 | |
|        "      <th>0</th>\n",
 | |
|        "      <td>data/106349S_por.png</td>\n",
 | |
|        "      <td>0.01</td>\n",
 | |
|        "      <td>0.01</td>\n",
 | |
|        "      <td>0.05</td>\n",
 | |
|        "      <td>0.0</td>\n",
 | |
|        "      <td>0</td>\n",
 | |
|        "      <td>0</td>\n",
 | |
|        "      <td>0.22</td>\n",
 | |
|        "      <td>0.0</td>\n",
 | |
|        "      <td>0.03</td>\n",
 | |
|        "      <td>0.68</td>\n",
 | |
|        "      <td>0.00</td>\n",
 | |
|        "      <td>0.00</td>\n",
 | |
|        "    </tr>\n",
 | |
|        "    <tr>\n",
 | |
|        "      <th>1</th>\n",
 | |
|        "      <td>data/102141_2_eng.png</td>\n",
 | |
|        "      <td>0.04</td>\n",
 | |
|        "      <td>0.03</td>\n",
 | |
|        "      <td>0.02</td>\n",
 | |
|        "      <td>0.0</td>\n",
 | |
|        "      <td>0</td>\n",
 | |
|        "      <td>0</td>\n",
 | |
|        "      <td>0.01</td>\n",
 | |
|        "      <td>0.0</td>\n",
 | |
|        "      <td>0.32</td>\n",
 | |
|        "      <td>0.23</td>\n",
 | |
|        "      <td>0.31</td>\n",
 | |
|        "      <td>0.05</td>\n",
 | |
|        "    </tr>\n",
 | |
|        "    <tr>\n",
 | |
|        "      <th>2</th>\n",
 | |
|        "      <td>data/102730_eng.png</td>\n",
 | |
|        "      <td>0.05</td>\n",
 | |
|        "      <td>0.00</td>\n",
 | |
|        "      <td>0.00</td>\n",
 | |
|        "      <td>0.0</td>\n",
 | |
|        "      <td>0</td>\n",
 | |
|        "      <td>0</td>\n",
 | |
|        "      <td>0.00</td>\n",
 | |
|        "      <td>0.0</td>\n",
 | |
|        "      <td>0.00</td>\n",
 | |
|        "      <td>0.66</td>\n",
 | |
|        "      <td>0.02</td>\n",
 | |
|        "      <td>0.27</td>\n",
 | |
|        "    </tr>\n",
 | |
|        "  </tbody>\n",
 | |
|        "</table>\n",
 | |
|        "</div>"
 | |
|       ],
 | |
|       "text/plain": [
 | |
|        "                filename   red  green  blue  yellow  cyan  orange  purple  \\\n",
 | |
|        "0   data/106349S_por.png  0.01   0.01  0.05     0.0     0       0    0.22   \n",
 | |
|        "1  data/102141_2_eng.png  0.04   0.03  0.02     0.0     0       0    0.01   \n",
 | |
|        "2    data/102730_eng.png  0.05   0.00  0.00     0.0     0       0    0.00   \n",
 | |
|        "\n",
 | |
|        "   pink  brown  grey  white  black  \n",
 | |
|        "0   0.0   0.03  0.68   0.00   0.00  \n",
 | |
|        "1   0.0   0.32  0.23   0.31   0.05  \n",
 | |
|        "2   0.0   0.00  0.66   0.02   0.27  "
 | |
|       ]
 | |
|      },
 | |
|      "execution_count": 7,
 | |
|      "metadata": {},
 | |
|      "output_type": "execute_result"
 | |
|     }
 | |
|    ],
 | |
|    "source": [
 | |
|     "image_df.head(10)"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "markdown",
 | |
|    "metadata": {},
 | |
|    "source": [
 | |
|     "Write the csv file - here you should provide a file path and file name for the csv file to be written."
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": 8,
 | |
|    "metadata": {
 | |
|     "execution": {
 | |
|      "iopub.execute_input": "2023-12-13T10:00:57.171066Z",
 | |
|      "iopub.status.busy": "2023-12-13T10:00:57.170759Z",
 | |
|      "iopub.status.idle": "2023-12-13T10:00:57.175058Z",
 | |
|      "shell.execute_reply": "2023-12-13T10:00:57.174570Z"
 | |
|     }
 | |
|    },
 | |
|    "outputs": [],
 | |
|    "source": [
 | |
|     "image_df.to_csv(\"data_out.csv\")"
 | |
|    ]
 | |
|   }
 | |
|  ],
 | |
|  "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.18"
 | |
|   }
 | |
|  },
 | |
|  "nbformat": 4,
 | |
|  "nbformat_minor": 2
 | |
| }
 | 
