-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into provide_err_msg_empty_file_location
- Loading branch information
Showing
25 changed files
with
1,334 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import numpy as np | ||
import webcolors | ||
import pandas as pd | ||
from collections import defaultdict | ||
import colorgram | ||
import colour | ||
from ammico.utils import get_color_table, AnalysisMethod | ||
|
||
COLOR_SCHEMES = [ | ||
"CIE 1976", | ||
"CIE 1994", | ||
"CIE 2000", | ||
"CMC", | ||
"ITP", | ||
"CAM02-LCD", | ||
"CAM02-SCD", | ||
"CAM02-UCS", | ||
"CAM16-LCD", | ||
"CAM16-SCD", | ||
"CAM16-UCS", | ||
"DIN99", | ||
] | ||
|
||
|
||
class ColorDetector(AnalysisMethod): | ||
def __init__( | ||
self, | ||
subdict: dict, | ||
delta_e_method: str = "CIE 1976", | ||
) -> None: | ||
"""Color Analysis class, analyse hue and identify named colors. | ||
Args: | ||
subdict (dict): The dictionary containing the image path. | ||
delta_e_method (str): The calculation method used for assigning the | ||
closest color name, defaults to "CIE 1976". | ||
The available options are: 'CIE 1976', 'CIE 1994', 'CIE 2000', | ||
'CMC', 'ITP', 'CAM02-LCD', 'CAM02-SCD', 'CAM02-UCS', 'CAM16-LCD', | ||
'CAM16-SCD', 'CAM16-UCS', 'DIN99' | ||
""" | ||
super().__init__(subdict) | ||
self.subdict.update(self.set_keys()) | ||
self.merge_color = True | ||
self.n_colors = 100 | ||
if delta_e_method not in COLOR_SCHEMES: | ||
raise ValueError( | ||
"Invalid selection for assigning the color name. Please select one of {}".format( | ||
COLOR_SCHEMES | ||
) | ||
) | ||
self.delta_e_method = delta_e_method | ||
|
||
def set_keys(self) -> dict: | ||
colors = { | ||
"red": 0, | ||
"green": 0, | ||
"blue": 0, | ||
"yellow": 0, | ||
"cyan": 0, | ||
"orange": 0, | ||
"purple": 0, | ||
"pink": 0, | ||
"brown": 0, | ||
"grey": 0, | ||
"white": 0, | ||
"black": 0, | ||
} | ||
return colors | ||
|
||
def analyse_image(self): | ||
""" | ||
Uses the colorgram library to extract the n most common colors from the images. | ||
One problem is, that the most common colors are taken before beeing categorized, | ||
so for small values it might occur that the ten most common colors are shades of grey, | ||
while other colors are present but will be ignored. Because of this n_colors=100 was chosen as default. | ||
The colors are then matched to the closest color in the CSS3 color list using the delta-e metric. | ||
They are then merged into one data frame. | ||
The colors can be reduced to a smaller list of colors using the get_color_table function. | ||
These colors are: "red", "green", "blue", "yellow","cyan", "orange", "purple", "pink", "brown", "grey", "white", "black". | ||
Returns: | ||
dict: Dictionary with color names as keys and percentage of color in image as values. | ||
""" | ||
filename = self.subdict["filename"] | ||
|
||
colors = colorgram.extract(filename, self.n_colors) | ||
for color in colors: | ||
rgb_name = self.rgb2name( | ||
color.rgb, | ||
merge_color=self.merge_color, | ||
delta_e_method=self.delta_e_method, | ||
) | ||
self.subdict[rgb_name] += round(color.proportion, 2) | ||
|
||
return self.subdict | ||
|
||
def rgb2name( | ||
self, c, merge_color: bool = True, delta_e_method: str = "CIE 1976" | ||
) -> str: | ||
"""Take an rgb color as input and return the closest color name from the CSS3 color list. | ||
Args: | ||
c (Union[List,tuple]): RGB value. | ||
merge_color (bool, Optional): Whether color name should be reduced, defaults to True. | ||
Returns: | ||
str: Closest matching color name. | ||
""" | ||
if len(c) != 3: | ||
raise ValueError("Input color must be a list or tuple of length 3 (RGB).") | ||
|
||
h_color = "#{:02x}{:02x}{:02x}".format(int(c[0]), int(c[1]), int(c[2])) | ||
try: | ||
output_color = webcolors.hex_to_name(h_color, spec="css3") | ||
output_color = output_color.lower().replace("grey", "gray") | ||
except ValueError: | ||
delta_e_lst = [] | ||
filtered_colors = webcolors.CSS3_NAMES_TO_HEX | ||
|
||
for _, img_hex in filtered_colors.items(): | ||
cur_clr = webcolors.hex_to_rgb(img_hex) | ||
# calculate color Delta-E | ||
delta_e = colour.delta_E(c, cur_clr, method=delta_e_method) | ||
delta_e_lst.append(delta_e) | ||
# find lowest delta-e | ||
min_diff = np.argsort(delta_e_lst)[0] | ||
output_color = ( | ||
str(list(filtered_colors.items())[min_diff][0]) | ||
.lower() | ||
.replace("grey", "gray") | ||
) | ||
|
||
# match color to reduced list: | ||
if merge_color: | ||
for reduced_key, reduced_color_sub_list in get_color_table().items(): | ||
if str(output_color).lower() in [ | ||
str(color_name).lower() | ||
for color_name in reduced_color_sub_list["ColorName"] | ||
]: | ||
output_color = reduced_key.lower() | ||
break | ||
return output_color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Pink;Pink;purple;purple;red;red;orange;orange;yellow;yellow;green;green;cyan;cyan;blue;blue;brown;brown;white;white;grey;grey;black;black | ||
ColorName;HEX;ColorName;HEX;ColorName;HEX;ColorName;HEX;ColorName;HEX;ColorName;HEX;ColorName;HEX;ColorName;HEX;ColorName;HEX;ColorName;HEX;ColorName;HEX;ColorName;HEX | ||
Pink;#FFC0CB;Lavender;#E6E6FA;LightSalmon;#FFA07A;Orange;#FFA500;Gold;#FFD700;GreenYellow;#ADFF2F;Aqua;#00FFFF;CadetBlue;#5F9EA0;Cornsilk;#FFF8DC;White;#FFFFFF;Gainsboro;#DCDCDC;Black;#000000 | ||
LightPink;#FFB6C1;Thistle;#D8BFD8;Salmon;#FA8072;DarkOrange;#FF8C00;Yellow;#FFFF00;Chartreuse;#7FFF00;Cyan;#00FFFF;SteelBlue;#4682B4;BlanchedAlmond;#FFEBCD;Snow;#FFFAFA;LightGray;#D3D3D3;; | ||
HotPink;#FF69B4;Plum;#DDA0DD;DarkSalmon;#E9967A;Coral;#FF7F50;LightYellow;#FFFFE0;LawnGreen;#7CFC00;LightCyan;#E0FFFF;LightSteelBlue;#B0C4DE;Bisque;#FFE4C4;HoneyDew;#F0FFF0;Silver;#C0C0C0;; | ||
DeepPink;#FF1493;Orchid;#DA70D6;LightCoral;#F08080;Tomato;#FF6347;LemonChiffon;#FFFACD;Lime;#00FF00;PaleTurquoise;#AFEEEE;LightBlue;#ADD8E6;NavajoWhite;#FFDEAD;MintCream;#F5FFFA;DarkGray;#A9A9A9;; | ||
PaleVioletRed;#DB7093;Violet;#EE82EE;IndianRed;#CD5C5C;OrangeRed;#FF4500;LightGoldenRodYellow;#FAFAD2;LimeGreen;#32CD32;Aquamarine;#7FFFD4;PowderBlue;#B0E0E6;Wheat;#F5DEB3;Azure;#F0FFFF;DimGray;#696969;; | ||
MediumVioletRed;#C71585;Fuchsia;#FF00FF;Crimson;#DC143C;;;PapayaWhip;#FFEFD5;PaleGreen;#98FB98;Turquoise;#40E0D0;LightSkyBlue;#87CEFA;BurlyWood;#DEB887;AliceBlue;#F0F8FF;Gray;#808080;; | ||
;;Magenta;#FF00FF;Red;#FF0000;;;Moccasin;#FFE4B5;LightGreen;#90EE90;MediumTurquoise;#48D1CC;SkyBlue;#87CEEB;Tan;#D2B48C;GhostWhite;#F8F8FF;LightSlateGray;#778899;; | ||
;;MediumOrchid;#BA55D3;FireBrick;#B22222;;;PeachPuff;#FFDAB9;MediumSpringGreen;#00FA9A;DarkTurquoise;#00CED1;CornflowerBlue;#6495ED;RosyBrown;#BC8F8F;WhiteSmoke;#F5F5F5;SlateGray;#708090;; | ||
;;DarkOrchid;#9932CC;DarkRed;#8B0000;;;PaleGoldenRod;#EEE8AA;SpringGreen;#00FF7F;;;DeepSkyBlue;#00BFFF;SandyBrown;#F4A460;SeaShell;#FFF5EE;DarkSlateGray;#2F4F4F;; | ||
;;DarkViolet;#9400D3;;;;;Khaki;#F0E68C;MediumSeaGreen;#3CB371;;;DodgerBlue;#1E90FF;GoldenRod;#DAA520;Beige;#F5F5DC;;;; | ||
;;BlueViolet;#8A2BE2;;;;;DarkKhaki;#BDB76B;SeaGreen;#2E8B57;;;RoyalBlue;#4169E1;DarkGoldenRod;#B8860B;OldLace;#FDF5E6;;;; | ||
;;DarkMagenta;#8B008B;;;;;;;ForestGreen;#228B22;;;Blue;#0000FF;Peru;#CD853F;FloralWhite;#FFFAF0;;;; | ||
;;Purple;#800080;;;;;;;Green;#008000;;;MediumBlue;#0000CD;Chocolate;#D2691E;Ivory;#FFFFF0;;;; | ||
;;MediumPurple;#9370DB;;;;;;;DarkGreen;#006400;;;DarkBlue;#00008B;Olive;#808000;AntiqueWhite;#FAEBD7;;;; | ||
;;MediumSlateBlue;#7B68EE;;;;;;;YellowGreen;#9ACD32;;;Navy;#000080;SaddleBrown;#8B4513;Linen;#FAF0E6;;;; | ||
;;SlateBlue;#6A5ACD;;;;;;;OliveDrab;#6B8E23;;;MidnightBlue;#191970;Sienna;#A0522D;LavenderBlush;#FFF0F5;;;; | ||
;;DarkSlateBlue;#483D8B;;;;;;;DarkOliveGreen;#556B2F;;;;;Brown;#A52A2A;MistyRose;#FFE4E1;;;; | ||
;;RebeccaPurple;#663399;;;;;;;MediumAquaMarine;#66CDAA;;;;;Maroon;#800000;;;;;; | ||
;;Indigo;#4B0082;;;;;;;DarkSeaGreen;#8FBC8F;;;;;;;;;;;; | ||
;;;;;;;;;;LightSeaGreen;#20B2AA;;;;;;;;;;;; | ||
;;;;;;;;;;DarkCyan;#008B8B;;;;;;;;;;;; | ||
;;;;;;;;;;Teal;#008080;;;;;;;;;;;; |
Oops, something went wrong.