# Compiled Cython Code - Detects colors in images 2-3 x faster than Numpy
### pip install locate-pixelcolor-cythonsingle
#### 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_cythonsingle 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=pic, colors=colors0)
51 ms ± 201 µ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=pic, colors=colors1)
443 ms ± 1.19 ms per loop (mean ± std. dev. of 7 runs, 1 loop 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
# cython: language_level=3
import numpy as np
cimport numpy as np
cpdef searchforcolor(np.uint8_t[::1] pic, np.uint8_t[::1] colors, int width, int totallengthpic, int totallengthcolor, int[::1] outputx, int[ ::1] outputy, int[::1] lastresult):
cdef int counter = 0
cdef unsigned char r, g, b
cdef int i, j
for i in range(0, totallengthcolor, 3):
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]):
dividend = j // 3
quotient = dividend // width
remainder = dividend % width
outputx[counter] = quotient
outputy[counter] = remainder
lastresult[0] = counter
counter += 1
# .\python.exe .\colorsearchcythonsinglesetup.py build_ext --inplace
```
### setup.py to compile the code
```python
# cython: language_level=3
from setuptools import Extension, setup
from Cython.Build import cythonize
import numpy as np
ext_modules = [
Extension("colorsearchcythonsingle", ["colorsearchcythonsingle.pyx"], include_dirs=[np.get_include()],define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")])
]
setup(
name='colorsearchcythonsingle',
ext_modules=cythonize(ext_modules),
)
# .\python.exe .\colorsearchcythonsinglesetup.py build_ext --inplace
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/locate_pixelcolor_cythonsingle",
"name": "locate-pixelcolor-cythonsingle",
"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/fc/d0/a7872dfc16bc2fcdbfd08702c0e9efbb7f631a67dd5e5961487631a0649f/locate_pixelcolor_cythonsingle-0.11.tar.gz",
"platform": null,
"description": "# Compiled Cython Code - Detects colors in images 2-3 x faster than Numpy \r\n\r\n### pip install locate-pixelcolor-cythonsingle\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_cythonsingle 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=pic, colors=colors0)\r\n51 ms \u00c2\u00b1 201 \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=pic, colors=colors1)\r\n443 ms \u00c2\u00b1 1.19 ms per loop (mean \u00c2\u00b1 std. dev. of 7 runs, 1 loop 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# cython: language_level=3\r\n\r\nimport numpy as np\r\ncimport numpy as np\r\n\r\ncpdef searchforcolor(np.uint8_t[::1] pic, np.uint8_t[::1] colors, int width, int totallengthpic, int totallengthcolor, int[::1] outputx, int[ ::1] outputy, int[::1] lastresult):\r\n cdef int counter = 0\r\n cdef unsigned char r, g, b\r\n cdef int i, j\r\n\r\n for i in range(0, totallengthcolor, 3):\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 dividend = j // 3\r\n quotient = dividend // width\r\n remainder = dividend % width\r\n outputx[counter] = quotient\r\n outputy[counter] = remainder\r\n lastresult[0] = counter\r\n counter += 1\r\n\r\n# .\\python.exe .\\colorsearchcythonsinglesetup.py build_ext --inplace\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# 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(\"colorsearchcythonsingle\", [\"colorsearchcythonsingle.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='colorsearchcythonsingle',\r\n ext_modules=cythonize(ext_modules),\r\n)\r\n\r\n\r\n# .\\python.exe .\\colorsearchcythonsinglesetup.py build_ext --inplace\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Compiled Cython Code - Detects colors in images 2-3 x faster than Numpy",
"version": "0.11",
"split_keywords": [
"cython",
"bot",
"rgb",
"color",
"search"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c6945e90eb87e5ec9831d5f259fc770fbe0b44421b56d3bd3638af318e9411f9",
"md5": "9a8b328006b82b4d190c70a6cfc30271",
"sha256": "0a7103920fbe91d38fb84a20d6914deb25249943e76f9ab2c5bcd159c8e0bbad"
},
"downloads": -1,
"filename": "locate_pixelcolor_cythonsingle-0.11-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9a8b328006b82b4d190c70a6cfc30271",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 81436,
"upload_time": "2023-04-27T18:31:58",
"upload_time_iso_8601": "2023-04-27T18:31:58.306812Z",
"url": "https://files.pythonhosted.org/packages/c6/94/5e90eb87e5ec9831d5f259fc770fbe0b44421b56d3bd3638af318e9411f9/locate_pixelcolor_cythonsingle-0.11-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "fcd0a7872dfc16bc2fcdbfd08702c0e9efbb7f631a67dd5e5961487631a0649f",
"md5": "f7a84f322f5833d8939e0e8e78668db7",
"sha256": "0f4b374692422e96c37f1a6ef360570bbf19c74c641c1489d855b27bad587282"
},
"downloads": -1,
"filename": "locate_pixelcolor_cythonsingle-0.11.tar.gz",
"has_sig": false,
"md5_digest": "f7a84f322f5833d8939e0e8e78668db7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 80397,
"upload_time": "2023-04-27T18:32:00",
"upload_time_iso_8601": "2023-04-27T18:32:00.928262Z",
"url": "https://files.pythonhosted.org/packages/fc/d0/a7872dfc16bc2fcdbfd08702c0e9efbb7f631a67dd5e5961487631a0649f/locate_pixelcolor_cythonsingle-0.11.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-27 18:32:00",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "hansalemaos",
"github_project": "locate_pixelcolor_cythonsingle",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "Cython",
"specs": []
},
{
"name": "numpy",
"specs": []
},
{
"name": "setuptools",
"specs": []
}
],
"lcname": "locate-pixelcolor-cythonsingle"
}