AMMICO/notebooks/colors_expression.ipynb
xiaohemaikoo fdcb228294
M objdect (#23)
* colors expression by KMean algorithm

* object detection by imageai

* object detection by cvlib

* add encapsulation of object detection

* remove encapsulation of objdetect v0

* objects expression to dict

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* added imageai to requirements

* add objects to dictionary

* update for AnalysisMethod baseline

* add objects dection support explore_analysis display

* extend python version of misinf to allow imageai

* account for older python

* use global functionality for dict to csv convert

* update for docker build

* docker will build now but ipywidgets still not working

* test code

* include test data folder in repo

* add some sample images

* load cvs labels to dict

* add test data

* retrigger checks

* add map to human coding

* get orders from dict, missing dep

* add module to test accuracy

* retrigger checks

* retrigger checks

* now removing imageai

* removed imageai

* move labelmanager to analyse

* multiple faces in mydict

* fix pre-commit issues

* map mydict

* hide imageai

* objects default using cvlib, isolate and disable imageai

* correct python version

* refactor faces tests

* refactor objects tests

* sonarcloud issues

* refactor utils tests

* address code smells

* update readme

* update notebook without imageai

Co-authored-by: Ma Xianghe <825074348@qq.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: iulusoy <inga.ulusoy@uni-heidelberg.de>
2022-10-04 11:34:44 +02:00

153 строки
4.1 KiB
Plaintext
Generated

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook shows primary color analysis of color image using K-Means algorithm.\n",
"The output are N primary colors and their corresponding percentage."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.cluster import KMeans\n",
"import matplotlib.pyplot as plt\n",
"import cv2\n",
"import numpy as np\n",
"import requests"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def centroid_histogram(clt):\n",
" # grab the number of different clusters and create a histogram\n",
" # based on the number of pixels assigned to each cluster\n",
" numLabels = np.arange(0, len(np.unique(clt.labels_)) + 1)\n",
" (hist, _) = np.histogram(clt.labels_, bins=numLabels)\n",
"\n",
" # normalize the histogram, such that it sums to one\n",
" hist = hist.astype(\"float\")\n",
" hist /= hist.sum()\n",
"\n",
" # return the histogram\n",
" return hist\n",
"\n",
"\n",
"def plot_colors(hist, centroids):\n",
" # initialize the bar chart representing the relative frequency\n",
" # of each of the colors\n",
" bar = np.zeros((50, 300, 3), dtype=\"uint8\")\n",
" startX = 0\n",
" # loop over the percentage of each cluster and the color of\n",
" # each cluster\n",
" for (percent, color) in zip(hist, centroids):\n",
" # plot the relative percentage of each cluster\n",
" endX = startX + (percent * 300)\n",
" cv2.rectangle(\n",
" bar, (int(startX), 0), (int(endX), 50), color.astype(\"uint8\").tolist(), -1\n",
" )\n",
" startX = endX\n",
"\n",
" # return the bar chart\n",
" return bar"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# load the image and convert it from BGR to RGB so that\n",
"# we can dispaly it with matplotlib\n",
"# image_path = './data/blue.jpg'\n",
"# image = cv2.imread(image_path)\n",
"\n",
"file = requests.get(\n",
" \"https://heibox.uni-heidelberg.de/thumbnail/537e6da0a8b44069bc96/1024/images/100361_asm.png\"\n",
")\n",
"image = cv2.imdecode(np.fromstring(file.content, np.uint8), 1)\n",
"\n",
"# BGR-->RGB cv to matplotlib show\n",
"image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
"\n",
"# show our image\n",
"plt.figure()\n",
"plt.axis(\"off\")\n",
"plt.imshow(image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# reshape the image to be a list of pixels\n",
"image = image.reshape((image.shape[0] * image.shape[1], 3))\n",
"\n",
"# cluster the pixel intensities\n",
"clt = KMeans(n_clusters=8)\n",
"clt.fit(image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# build a histogram of clusters and then create a figure\n",
"# representing the number of pixels labeled to each color\n",
"hist = centroid_histogram(clt)\n",
"bar = plot_colors(hist, clt.cluster_centers_)\n",
"\n",
"# show our color bart\n",
"plt.figure()\n",
"plt.axis(\"off\")\n",
"plt.imshow(bar)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for (percent, color) in zip(hist, clt.cluster_centers_):\n",
" print(\"color:\", color, \" percentage:\", percent)"
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}