Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: similar percentage for colors #64

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2
jobs:
build:
machine:
image: ubuntu-2004:202010-01
image: ubuntu-2004:2024.04.4
steps:
- checkout
- run:
Expand Down
34 changes: 20 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
repos:
- repo: https://github.com/ambv/black
rev: 22.8.0
hooks:
- id: black
language_version: python3
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v1.2.3
hooks:
- id: flake8
- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.10.1
hooks:
- id: isort
language_version: python3
- repo: https://github.com/ambv/black
rev: 22.8.0
hooks:
- id: black
language_version: python3
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v1.2.3
hooks:
- id: flake8
- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.10.1
hooks:
- id: isort
language_version: python3

# sets up .pre-commit-ci.yaml to ensure pre-commit dependencies stay up to date
ci:
autoupdate_schedule: weekly
skip: []
submodules: false
9 changes: 9 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ ColorDetect Changelog
=====================


.. _1.6.1:
1.6.0 (14-08-2024)
==================
Fix
---------

- ColorDetect fails when it gets multiple colors of the same dominance.



.. _1.6.0:
1.6.0 (22-09-2022)
Expand Down
4 changes: 2 additions & 2 deletions colordetect/col_share.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ def sort_order(object_description: dict, key_count: int = 5, ascending: bool = T
The order to perform the dictionary sort. By default, set to True.
:return: A sorted dictionary with specific number
"""
if type(key_count) != int:
if not isinstance(key_count, int):
raise TypeError(
f"color_count has to be an integer. Provided {type(key_count)} "
)

if type(ascending) != bool:
if not isinstance(ascending, bool):
raise TypeError(
f"The value of the 'ascending' parameter is a boolean. Provided {type(ascending)} "
)
Expand Down
31 changes: 16 additions & 15 deletions colordetect/color_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,21 @@ def get_segmented_image(
f"upper_bound has to be a tuple of integers. Provided {type(upper_bound)} "
)

if type(erode_iterations) != int:
if not isinstance(erode_iterations, int):
raise TypeError(
f"erode_iterations has to be an integer. Provided {type(erode_iterations)} "
)

if type(dilate_iterations) != int:
if not isinstance(dilate_iterations, int):
raise TypeError(
f"dilate_iterations has to be an integer. Provided {type(dilate_iterations)} "
)

if type(gc_iterations) != int:
if not isinstance(gc_iterations, int):
raise TypeError(
f"gc_iterations has to be a an integer. Provided {type(gc_iterations)} "
)
if type(use_grab_cut) != bool:
if not isinstance(use_grab_cut, bool):
raise TypeError(
f"use_grab_cut has to be a boolean. Provided {type(use_grab_cut)} "
)
Expand Down Expand Up @@ -191,7 +191,7 @@ def get_color_count(
:return: color description
"""

if type(color_count) != int:
if not isinstance(color_count, int):
raise TypeError(
f"color_count has to be an integer. Provided {type(color_count)} "
)
Expand Down Expand Up @@ -236,13 +236,15 @@ def _format_color(self, rgb_value, color_format: str):
r0, g0, b0 = int(rgb_value[0]), int(rgb_value[1]), int(rgb_value[2])
try:
nearest = webcolors.rgb_to_name((r0, g0, b0))
except ValueError: # Calculate distances between rgb value and CSS3 rgb colours to determine the closest
except (
ValueError
): # Calculate distances between rgb value and CSS3 rgb colours to determine the closest
distances = {}
for k, v in webcolors.CSS3_HEX_TO_NAMES.items():
r1, g1, b1 = webcolors.hex_to_rgb(k)
distances[
((r0 - r1) ** 2 + (g0 - g1) ** 2 + (b0 - b1) ** 2)
] = v # Ignore sqrt as it has no significant effect
distances[((r0 - r1) ** 2 + (g0 - g1) ** 2 + (b0 - b1) ** 2)] = (
v # Ignore sqrt as it has no significant effect
)
nearest = distances[min(distances.keys())]
return nearest

Expand All @@ -255,11 +257,10 @@ def _find_unique_colors(self, cluster, centroids) -> dict:
hist /= hist.sum()

# iterate through each cluster's color and percentage
colors = sorted(
[((percent * 100), color) for (percent, color) in zip(hist, centroids)]
)
colors = [(percent * 100, color) for (percent, color) in zip(hist, centroids)]
colors.sort(key=lambda x: (x[0], id(x[1])))

for (percent, color) in colors:
for percent, color in colors:
color.astype("uint8").tolist()
return dict(colors)

Expand Down Expand Up @@ -369,7 +370,7 @@ def write_text(
Space betweeen the lines
:return:
"""
if type(text) != str:
if not isinstance(text, str):
raise TypeError(
f"text should be a string.Provided {text} of type {type(text)}"
)
Expand Down Expand Up @@ -419,7 +420,7 @@ def save_image(self, location: str = ".", file_name: str = "out.jpg"):
if not image_folder.exists():
raise NotADirectoryError("The storage folder does not exist.")

if type(file_name) != str:
if not isinstance(file_name, str):
raise TypeError(f"file_name should be a string.Provided {type(file_name)}")

image_to_save = image_folder / file_name
Expand Down
6 changes: 3 additions & 3 deletions colordetect/video_color_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get_video_frames(
* hex - #FFFF00 for yellow
:return: color_description dictionary
"""
if type(frame_color_count) != int:
if not isinstance(frame_color_count, int):
raise TypeError(
f"frame_color_count has to be an integer. Provided {type(frame_color_count)} "
)
Expand All @@ -69,7 +69,7 @@ def get_video_frames(
if color_format not in color_format_options:
raise ValueError(f"Invalid color format: {color_format}")

if type(progress) != bool:
if not isinstance(progress, bool):
raise TypeError(f"Progress should be a boolean. Provided {type(progress)}")

count = 0
Expand Down Expand Up @@ -149,7 +149,7 @@ def get_time_frame_color(
if color_format not in color_format_options:
raise ValueError(f"Invalid color format: {color_format}")

if type(color_count) != int:
if not isinstance(color_count, int):
raise TypeError(
f"color_count to extract has to be an integer. Provided {type(color_count)} "
)
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"sphinx.ext.doctest",
"sphinx.ext.viewcode", # package rst sources with docs
"sphinx.ext.intersphinx",
"sphinx.ext.autosectionlabel"
"sphinx.ext.autosectionlabel",
]

# Add any paths that contain templates here, relative to this directory.
Expand Down
160 changes: 80 additions & 80 deletions requirements/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,83 +1,83 @@
alabaster>==0.7.12
appdirs>==1.4.4
argh>==0.26.2
attrs>==19.3.0
autodoc>==0.5.0
autopep8>==1.5
Babel>==2.8.0
beautifulsoup4>==4.8.2
certifi>==2019.11.28
cfgv>==3.2.0
chardet>==3.0.4
commonmark>==0.9.1
cycler>==0.10.0
decorator>==4.4.2
distlib>==0.3.1
docutils>==0.16
filelock>==3.0.12
identify>==1.5.12
idna>==2.9
imagesize>==1.2.0
importlib-metadata>==1.5.0
importlib-resources>==5.0.0
imutils>==0.5.3
Jinja2>==2.11.1
joblib>==0.14.1
kiwisolver>==1.2.0
livereload>==2.6.1
MarkupSafe>==1.1.1
matplotlib>==3.2.1
mock>==4.0.2
more-itertools>==8.2.0
numpy>==1.18.1
opencv-python>==4.2.0.32
packaging>==20.1
pathtools>==0.1.2
pluggy>==0.13.1
port-for>==0.3.1
pre-commit>==2.9.0
py>==1.8.1
pycodestyle>==2.5.0
Pygments>==2.6.1
pyparsing>==2.4.6
pytest>==5.3.5
pytest-datafiles>==2.0
python-dateutil>==2.8.1
pytz>==2019.3
PyYAML>==5.3.1
recommonmark>==0.6.0
requests>==2.23.0
rinoh-typeface-dejavuserif>==0.1.1
rinoh-typeface-texgyrecursor>==0.1.1
rinoh-typeface-texgyreheros>==0.1.1
rinoh-typeface-texgyrepagella>==0.1.1
rinohtype>==0.4.0
rstcheck>==3.3.1
scikit-learn>==0.22.2.post1
scipy>==1.4.1
six>==1.14.0
snowballstemmer>==2.0.0
soupsieve>==2.0
Sphinx>==2.4.4
sphinx-autobuild>==0.7.1
sphinx-autodoc-typehints>==1.10.3
sphinxcontrib-applehelp>==1.0.2
sphinxcontrib-devhelp>==1.0.2
sphinxcontrib-htmlhelp>==1.0.3
sphinxcontrib-jsmath>==1.0.1
sphinxcontrib-qthelp>==1.0.3
sphinxcontrib-serializinghtml>==1.1.4
toml>==0.10.2
tornado>==6.0.4
urllib3>==1.25.8
virtualenv>==20.3.0
waitress>==1.4.3
watchdog>==0.10.2
wcwidth>==0.1.8
webcolors>==1.11.1
WebOb>==1.8.6
WebTest>==2.0.34
zipp>==3.0.0
alabaster
appdirs
argh
attrs
autodoc
autopep8
Babel
beautifulsoup4
certifi
cfgv
chardet
commonmark
cycler
decorator
distlib
docutils
filelock
identify
idna
imagesize
importlib-metadata
importlib-resources
imutils
Jinja2
joblib
kiwisolver
livereload
MarkupSafe
matplotlib
mock
more-itertools
numpy
opencv-python
packaging
pathtools
pluggy
port-for
pre-commit
py
pycodestyle
Pygments
pyparsing
pytest
pytest-datafiles
python-dateutil
pytz
PyYAML
recommonmark
requests
rinoh-typeface-dejavuserif
rinoh-typeface-texgyrecursor
rinoh-typeface-texgyreheros
rinoh-typeface-texgyrepagella
rinohtype
rstcheck
scikit-learn
scipy
six
snowballstemmer
soupsieve
Sphinx
sphinx-autobuild
sphinx-autodoc-typehints
sphinxcontrib-applehelp
sphinxcontrib-devhelp
sphinxcontrib-htmlhelp
sphinxcontrib-jsmath
sphinxcontrib-qthelp
sphinxcontrib-serializinghtml
toml
tornado
urllib3
virtualenv
waitress
watchdog
wcwidth
webcolors
WebOb
WebTest
zipp
black
flake8
isort
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="ColorDetect",
version="1.6.0",
version="1.6.1",
author="Marvin Kweyu",
author_email="[email protected]",
description="Detect and recognize colors in images or video",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def test_get_time_frame_color_returns_video_colors_at_given_time(video):

(image, colors_at_time) = user_video.get_time_frame_color(time=10000)
assert isinstance(image, ColorDetect)
assert type(colors_at_time) == dict
assert isinstance(colors_at_time, dict)

assert len(colors_at_time) == 5

Expand All @@ -196,7 +196,7 @@ def test_get_time_frame_gets_right_params(video):
user_video.get_time_frame_color(color_format="invalid_random_format")

with pytest.raises(TypeError):
user_video.get_time_frame_color(colors="45")
user_video.get_time_frame_color(color_count="45")

# give a negative time to get color from
with pytest.raises(ValueError):
Expand Down
Loading