Skip to main content

RGB search with numba - 2-3 x faster than numpy

pip install locate-pixelcolor-numba

Important!

This is a compiled .pyd file (Numba AOT), if you can't import it, run the following code to generate a new pyd file, and replace it with the old .pyd file.

from numba_aot_compiler import compnumba #pip install numba-aot-compiler
import numpy as np
from numba import uint8, uint16


def search_colors(r, g, b, rgbs, divider):
    res = np.zeros(b.shape, dtype=np.uint16)
    res2 = np.zeros(b.shape, dtype=np.uint16)
    zaehler = 0
    for rgb in rgbs:
        rr, gg, bb = rgb
        for i in range(r.shape[0]):
            if r[i] == rr:
                if g[i] == gg:
                    if b[i] == bb:
                        dvquot, dvrem = divmod(i, divider)
                        res[zaehler] = dvquot
                        res2[zaehler] = dvrem
                        zaehler = zaehler + 1
    results = np.dstack((res[:zaehler], res2[:zaehler]))
    return results


compi2 = compnumba(
    fu=search_colors,
    funcname="search_colors",
    file="searchcolorsnumba",
    folder="locate_pixelcolor_numba",
    signature=(uint8[:], uint8[:], uint8[:], uint8[:, :], uint16),
    parallel=True,
    fastmath=True,
    nogil=True,
)

How to use it

from locate_pixelcolor_numba import search_colors
import cv2
import time
import numpy as np
pic = cv2.imread(r"pexels-alex-andrews-2295744.jpg") # https://www.pexels.com/pt-br/foto/foto-da-raposa-sentada-no-chao-2295744/
colors = 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)
search_colors(pic,colors)
Out[2]: 
array([[[   0, 4522],
        [   3, 4522],
        [   3, 4523],
        ...,
        [6622, 4522],
        [6622, 4523],
        [6622, 4524]]], dtype=uint16)
%timeit search_colors(pic,colors)
413 ms ± 1.22 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

# More benchmarks: https://github.com/hansalemaos/locate_pixelcolor_cpp

Metadata

Release files for locate-pixelcolor-numba 0.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for locate-pixelcolor-numba 0.11
File Size Uploaded Provenance Actions
locate_pixelcolor_numba-0.11.tar.gz 23.6 kB Without provenance Details for locate_pixelcolor_numba-0.11.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for locate-pixelcolor-numba 0.11
File Interpreter ABI Platform Actions
locate_pixelcolor_numba-0.11-py3-none-any.whl Python 3 none any Details for locate_pixelcolor_numba-0.11-py3-none-any.whl

Total release size: 47.3 kB

Release files / locate_pixelcolor_numba-0.11.tar.gz

Download URL locate_pixelcolor_numba-0.11.tar.gz
Size 23.6 kB
Tags Source
SHA-256 checksum
How to use checksums
a67a601cc616ad6c82aba1e4f6f543f9c68a5e88ffdaf7b7d0c1377cac9af12b
BLAKE2b-256 checksum
How to use checksums
0059e1dfb51c225699388d671a4eac77cbe6f633a2dd5ac5e5e6d6f1a8789d60
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Release files / locate_pixelcolor_numba-0.11-py3-none-any.whl

Download URL locate_pixelcolor_numba-0.11-py3-none-any.whl
Size 23.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c8943a7425073654ad77d459da76d09bed5cfbea2c9b9c29c3f2abf005091a5d
BLAKE2b-256 checksum
How to use checksums
d68b3ea90f7cded847ae5c284e649c6990f5520242c7b281856ab418843007d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Security for locate-pixelcolor-numba 0.11

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.11 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page