locate-pixelcolor-cythonmulti


Namelocate-pixelcolor-cythonmulti JSON
Version 0.13 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/locate_pixelcolor_cythonmulti
SummaryCompiled Cython Code (parallel) - Detects colors in images 5-10 x faster than Numpy
upload_time2023-04-27 18:36:58
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords cython bot rgb color search
VCS
bugtrack_url
requirements Cython numpy setuptools
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Detects colors in images 5-10 x faster than Numpy 

### pip install locate-pixelcolor-cythonmulti

#### Tested+compiled against Windows 10 / Python 3.10 / Anaconda

#### If you can't import it, compile it on your system (code at the end of this page)



### How to use it in Python 

```python
import numpy as np
import cv2
from locate_pixelcolor_cythonmulti import search_colors
# 4525 x 6623 x 3 picture https://www.pexels.com/pt-br/foto/foto-da-raposa-sentada-no-chao-2295744/
picx = r"C:\Users\hansc\Downloads\pexels-alex-andrews-2295744.jpg"
pic = cv2.imread(picx)
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)
####################################################################
%timeit resus0=search_colors(pic,colors0)
32.3 ms ± 279 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

b,g,r = pic[...,0],pic[...,1],pic[...,2]
%timeit np.where(((b==255)&(g==255)&(r==255)))
150 ms ± 209 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
####################################################################
%timeit resus1=search_colors(pic, colors1)
151 ms ± 3.21 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

%timeit np.where(((b==66)&(g==71)&(r==69))|((b==62)&(g==67)&(r==65))|((b==144)&(g==155)&(r==153))|((b==52)&(g==57)&(r==55))|((b==127)&(g==138)&(r==136))|((b==53)&(g==58)&(r==56))|((b==51)&(g==56)&(r==54))|((b==32)&(g==27)&(r==18))|((b==24)&(g==17)&(r==8)))
1 s ± 16.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
####################################################################
```


### The Cython Code 

```python
# distutils: language = c++
# cython: language_level=3
# distutils: extra_compile_args = /openmp
# distutils: extra_link_args = /openmp


from cython.parallel cimport prange
cimport cython
import numpy as np
cimport numpy as np
import cython
from collections import defaultdict

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cpdef searchforcolor(unsigned char[:] pic, unsigned char[:] colors, int width, int totallengthpic, int totallengthcolor):
    cdef my_dict = defaultdict(list)
    cdef int i, j
    cdef unsigned char r,g,b
    for i in prange(0, totallengthcolor, 3,nogil=True):
        r = colors[i]
        g = colors[i + 1]
        b = colors[i + 2]
        for j in range(0, totallengthpic, 3):
            if (r == pic[j]) and (g == pic[j+1]) and (b == pic[j+2]):
                with gil:
                    my_dict[(r,g,b)].append(j )

    for key in my_dict.keys():
        my_dict[key] = np.dstack(np.divmod(np.array(my_dict[key]) // 3, width))[0]
    return my_dict

```


### setup.py to compile the code 


```python
# distutils: language = c++
# cython: language_level=3

from setuptools import Extension, setup
from Cython.Build import cythonize
import numpy as np
ext_modules = [
    Extension("colorsearchcythonmulti", ["colorsearchcythonmulti.pyx"], include_dirs=[np.get_include()],define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")])
]

setup(
    name='colorsearchcythonmulti',
    ext_modules=cythonize(ext_modules),
)


# .\python.exe .\colorsearchcythonmultisetup.py build_ext --inplace
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/locate_pixelcolor_cythonmulti",
    "name": "locate-pixelcolor-cythonmulti",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "cython,bot,rgb,color,search",
    "author": "Johannes Fischer",
    "author_email": "aulasparticularesdealemaosp@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/fe/bc/c15693705f4087584a40a04187df02d3cf54a57b3f3785b190d300784151/locate_pixelcolor_cythonmulti-0.13.tar.gz",
    "platform": null,
    "description": "# Detects colors in images 5-10 x faster than Numpy \r\n\r\n### pip install locate-pixelcolor-cythonmulti\r\n\r\n#### Tested+compiled against Windows 10 / Python 3.10 / Anaconda\r\n\r\n#### If you can't import it, compile it on your system (code at the end of this page)\r\n\r\n\r\n\r\n### How to use it in Python \r\n\r\n```python\r\nimport numpy as np\r\nimport cv2\r\nfrom locate_pixelcolor_cythonmulti import search_colors\r\n# 4525 x 6623 x 3 picture https://www.pexels.com/pt-br/foto/foto-da-raposa-sentada-no-chao-2295744/\r\npicx = r\"C:\\Users\\hansc\\Downloads\\pexels-alex-andrews-2295744.jpg\"\r\npic = cv2.imread(picx)\r\ncolors0 = np.array([[255, 255, 255]],dtype=np.uint8)\r\nresus0 = search_colors(pic=pic, colors=colors0)\r\ncolors1=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)\r\nresus1 =  search_colors(pic=pic, colors=colors1)\r\n####################################################################\r\n%timeit resus0=search_colors(pic,colors0)\r\n32.3 ms \u00c2\u00b1 279 \u00c2\u00b5s per loop (mean \u00c2\u00b1 std. dev. of 7 runs, 10 loops each)\r\n\r\nb,g,r = pic[...,0],pic[...,1],pic[...,2]\r\n%timeit np.where(((b==255)&(g==255)&(r==255)))\r\n150 ms \u00c2\u00b1 209 \u00c2\u00b5s per loop (mean \u00c2\u00b1 std. dev. of 7 runs, 10 loops each)\r\n####################################################################\r\n%timeit resus1=search_colors(pic, colors1)\r\n151 ms \u00c2\u00b1 3.21 ms per loop (mean \u00c2\u00b1 std. dev. of 7 runs, 10 loops each)\r\n\r\n%timeit np.where(((b==66)&(g==71)&(r==69))|((b==62)&(g==67)&(r==65))|((b==144)&(g==155)&(r==153))|((b==52)&(g==57)&(r==55))|((b==127)&(g==138)&(r==136))|((b==53)&(g==58)&(r==56))|((b==51)&(g==56)&(r==54))|((b==32)&(g==27)&(r==18))|((b==24)&(g==17)&(r==8)))\r\n1 s \u00c2\u00b1 16.1 ms per loop (mean \u00c2\u00b1 std. dev. of 7 runs, 1 loop each)\r\n####################################################################\r\n```\r\n\r\n\r\n### The Cython Code \r\n\r\n```python\r\n# distutils: language = c++\r\n# cython: language_level=3\r\n# distutils: extra_compile_args = /openmp\r\n# distutils: extra_link_args = /openmp\r\n\r\n\r\nfrom cython.parallel cimport prange\r\ncimport cython\r\nimport numpy as np\r\ncimport numpy as np\r\nimport cython\r\nfrom collections import defaultdict\r\n\r\n@cython.boundscheck(False)\r\n@cython.wraparound(False)\r\n@cython.cdivision(True)\r\ncpdef searchforcolor(unsigned char[:] pic, unsigned char[:] colors, int width, int totallengthpic, int totallengthcolor):\r\n    cdef my_dict = defaultdict(list)\r\n    cdef int i, j\r\n    cdef unsigned char r,g,b\r\n    for i in prange(0, totallengthcolor, 3,nogil=True):\r\n        r = colors[i]\r\n        g = colors[i + 1]\r\n        b = colors[i + 2]\r\n        for j in range(0, totallengthpic, 3):\r\n            if (r == pic[j]) and (g == pic[j+1]) and (b == pic[j+2]):\r\n                with gil:\r\n                    my_dict[(r,g,b)].append(j )\r\n\r\n    for key in my_dict.keys():\r\n        my_dict[key] = np.dstack(np.divmod(np.array(my_dict[key]) // 3, width))[0]\r\n    return my_dict\r\n\r\n```\r\n\r\n\r\n### setup.py to compile the code \r\n\r\n\r\n```python\r\n# distutils: language = c++\r\n# cython: language_level=3\r\n\r\nfrom setuptools import Extension, setup\r\nfrom Cython.Build import cythonize\r\nimport numpy as np\r\next_modules = [\r\n    Extension(\"colorsearchcythonmulti\", [\"colorsearchcythonmulti.pyx\"], include_dirs=[np.get_include()],define_macros=[(\"NPY_NO_DEPRECATED_API\", \"NPY_1_7_API_VERSION\")])\r\n]\r\n\r\nsetup(\r\n    name='colorsearchcythonmulti',\r\n    ext_modules=cythonize(ext_modules),\r\n)\r\n\r\n\r\n# .\\python.exe .\\colorsearchcythonmultisetup.py build_ext --inplace\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Compiled Cython Code (parallel) - Detects colors in images 5-10 x faster than Numpy",
    "version": "0.13",
    "split_keywords": [
        "cython",
        "bot",
        "rgb",
        "color",
        "search"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7bc3b4cc7b96abbe80f87e1d8c1904cfaedc2f47b7ab645380e2f3fc0e1ffca6",
                "md5": "9e4159527282d79b16f4b8dd176add13",
                "sha256": "8ad87aff38a27740b247e6980f15a549bf85301250d43bcd242aead808a15e3a"
            },
            "downloads": -1,
            "filename": "locate_pixelcolor_cythonmulti-0.13-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9e4159527282d79b16f4b8dd176add13",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 84575,
            "upload_time": "2023-04-27T18:36:56",
            "upload_time_iso_8601": "2023-04-27T18:36:56.336794Z",
            "url": "https://files.pythonhosted.org/packages/7b/c3/b4cc7b96abbe80f87e1d8c1904cfaedc2f47b7ab645380e2f3fc0e1ffca6/locate_pixelcolor_cythonmulti-0.13-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "febcc15693705f4087584a40a04187df02d3cf54a57b3f3785b190d300784151",
                "md5": "f5d060ba1d2cde26dbe568b4f8795447",
                "sha256": "62e11222623debfcb19e94b7ba6c5435f8139a4f1e67ae4201fe0127f3b64203"
            },
            "downloads": -1,
            "filename": "locate_pixelcolor_cythonmulti-0.13.tar.gz",
            "has_sig": false,
            "md5_digest": "f5d060ba1d2cde26dbe568b4f8795447",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 83303,
            "upload_time": "2023-04-27T18:36:58",
            "upload_time_iso_8601": "2023-04-27T18:36:58.872355Z",
            "url": "https://files.pythonhosted.org/packages/fe/bc/c15693705f4087584a40a04187df02d3cf54a57b3f3785b190d300784151/locate_pixelcolor_cythonmulti-0.13.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-27 18:36:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "hansalemaos",
    "github_project": "locate_pixelcolor_cythonmulti",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "Cython",
            "specs": []
        },
        {
            "name": "numpy",
            "specs": []
        },
        {
            "name": "setuptools",
            "specs": []
        }
    ],
    "lcname": "locate-pixelcolor-cythonmulti"
}
        
Elapsed time: 0.06291s