Skip to main content

Checks if colors are in image / Detects multiple colors in images - Fast Cython algorithm

Project description

Checks if colors are in image / Detects multiple colors in images - Fast Cython algorithm

pip install cythoncolortools

Tested against Windows 10 / Python 3.11 / Anaconda

Important!

The module will be compiled when you import it for the first time. Cython and a C/C++ compiler must be installed!

How to use it in Python

import numpy as np
import cv2
from cythoncolortools import search_colors, are_any_colors_in_picture

# 4525 x 6623 x 3 picture https://www.pexels.com/pt-br/foto/foto-da-raposa-sentada-no-chao-2295744/
picpath = r"C:\Users\hansc\Downloads\pexels-alex-andrews-2295744.jpg"
pic = cv2.imread(picpath)
colors0 = np.array([[255, 255, 255]], dtype=np.uint8)
resus0 = search_colors(pic=pic, colors=colors0)
colors1 = np.array(
    [
        (66, 71, 69),
        (62, 67, 65),
        (144, 155, 153),
        (52, 57, 55),
        (127, 138, 136),
        (53, 58, 56),
        (51, 56, 54),
        (32, 27, 18),
        (24, 17, 8),
    ],
    dtype=np.uint8,
)
resus1 = search_colors(pic=pic, colors=colors1)
print(resus1)


# %timeit search_colors(pic=pic, colors=colors1, add_results=True, cpus=5)
# %timeit search_colors(pic=pic, colors=colors1, add_results=False, cpus=5)

# %timeit search_colors(pic=pic, colors=colors0, add_results=True, cpus=5)
# %timeit search_colors(pic=pic, colors=colors0, add_results=False, cpus=5)

# %timeit search_colors(pic=pic, colors=colors1, add_results=True, cpus=1)
# %timeit search_colors(pic=pic, colors=colors1, add_results=False, cpus=1)

# %timeit search_colors(pic=pic, colors=colors0, add_results=True, cpus=1)
# %timeit search_colors(pic=pic, colors=colors0, add_results=False, cpus=1)
print(search_colors(pic=pic, colors=colors1, add_results=True, cpus=5))
print(search_colors(pic=pic, colors=colors1, add_results=False, cpus=5))
print(search_colors(pic=pic, colors=colors0, add_results=True, cpus=5))
print(search_colors(pic=pic, colors=colors0, add_results=False, cpus=5))
print(search_colors(pic=pic, colors=colors1, add_results=True, cpus=1))
print(search_colors(pic=pic, colors=colors1, add_results=False, cpus=1))
print(search_colors(pic=pic, colors=colors0, add_results=True, cpus=1))
print(search_colors(pic=pic, colors=colors0, add_results=False, cpus=1))


print(are_any_colors_in_picture(pic, colors1, cpus=-1))
print(are_any_colors_in_picture(pic, colors0, cpus=-1))
print(are_any_colors_in_picture(pic, colors1, cpus=1))
print(are_any_colors_in_picture(pic, colors0, cpus=1))

print(are_any_colors_in_picture(pic, [[111, 111, 121]], cpus=-1))
print(are_any_colors_in_picture(pic, [[111, 111, 121]], cpus=1))

# 57 ms ± 2.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
# 47.9 ms ± 1.02 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
# 22 ms ± 43.6 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
# 18.8 ms ± 162 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
# 260 ms ± 8.03 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
# 256 ms ± 283 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
# 25.7 ms ± 47.2 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
# 25.8 ms ± 110 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

# [[  38    0  136  138  127]
#  [   1    1  153  155  144]
#  [  40    1  153  155  144]
#  ...
#  [1973 5903   65   67   62]
#  [1952 5904   65   67   62]
#  [2868 6041   65   67   62]]
# [[4522    0   69   71   66]
#  [   1    1  153  155  144]
#  [  40    1  153  155  144]
#  ...
#  [4522 6622    8   17   24]
#  [4523 6622    8   17   24]
#  [4524 6622    8   17   24]]
# [[  38    0]
#  [4522    0]
#  [   1    1]
#  ...
#  [2844 6622]
#  [2854 6622]
#  [2865 6622]]
# [[2085  832  255  255  255]
#  [1692  858  255  255  255]
#  [1688  896  255  255  255]
#  ...
#  [3526 5491  255  255  255]
#  [3527 5491  255  255  255]
#  [2491 5525  255  255  255]]
# [[2085  832]
#  [1692  858]
#  [1688  896]
#  ...
#  [3526 5491]
#  [3527 5491]
#  [2491 5525]]
# [[4522    0   69   71   66]
#  [4522    3   69   71   66]
#  [4523    3   69   71   66]
#  ...
#  [4522 6622    8   17   24]
#  [4523 6622    8   17   24]
#  [4524 6622    8   17   24]]
# [[4522    0]
#  [4522    3]
#  [4523    3]
#  ...
#  [4522 6622]
#  [4523 6622]
#  [4524 6622]]
# [[2085  832  255  255  255]
#  [1692  858  255  255  255]
#  [1688  896  255  255  255]
#  ...
#  [3526 5491  255  255  255]
#  [3527 5491  255  255  255]
#  [2491 5525  255  255  255]]
# [[2085  832]
#  [1692  858]
#  [1688  896]
#  ...
#  [3526 5491]
#  [3527 5491]
#  [2491 5525]]
# True
# True
# True
# True
# False
# False

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cythoncolortools-0.11.tar.gz (24.2 kB view details)

Uploaded Source

Built Distribution

cythoncolortools-0.11-py3-none-any.whl (24.3 kB view details)

Uploaded Python 3

File details

Details for the file cythoncolortools-0.11.tar.gz.

File metadata

  • Download URL: cythoncolortools-0.11.tar.gz
  • Upload date:
  • Size: 24.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for cythoncolortools-0.11.tar.gz
Algorithm Hash digest
SHA256 88998c8a13d1209951cadafe1f59719d91ee88059a37585d457cbfaded331b26
MD5 7b31ab36c99026cdfb24da6af79d70d2
BLAKE2b-256 0941d38ed0c220a6983b26d0b457f2f9b059a834d6270f5a313c2e621eaea67c

See more details on using hashes here.

File details

Details for the file cythoncolortools-0.11-py3-none-any.whl.

File metadata

File hashes

Hashes for cythoncolortools-0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 c52dca62e97f39aef52212f72138a3f081a9bb664ee0ff024bc4bd0076277e73
MD5 0eb462e7660e97bfb9b6e557fc6f3d4f
BLAKE2b-256 ab74e98bee9eee579fe74bc9c946f67c01921044d9ad6eb13c5f3dac3ef7e5c8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page