# cvlayer
[![PyPI](https://img.shields.io/pypi/v/cvlayer?style=flat-square)](https://pypi.org/project/cvlayer/)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/cvlayer?style=flat-square)
[![GitHub](https://img.shields.io/github/license/osom8979/cvlayer?style=flat-square)](https://github.com/osom8979/cvlayer/)
OpenCV Layer Helper
## Overview
When performing image-based machine learning/inference, many situations arise that require pre-/post-processing appropriate for the domain environment.
A commonly used library today is [OpenCV](https://opencv.org/).
Image processing with [OpenCV](https://opencv.org/) requires insight into the image and the domain.
However, gaining these insights often requires doing something far from the essence. (Visualization, parameter modification, library conflicts, assertions, etc...)
Here are some problems that occurred to me:
...
## Features
...
## Installation
Install `cvlayer`:
```shell
pip install cvlayer
```
Install `cvlayer` with `opencv-python`:
```shell
pip install cvlayer[opencv]
```
Install `cvlayer` with `opencv-python-headless`:
```shell
pip install cvlayer[headless]
```
## Usage
### CvLayer
Just inherit `cvlayer.CvLayer`.
```python
from cvlayer import CvLayer
class YourApp(CvLayer):
def func(self, image):
self.cvl_cvt_color_bgr2hsv(image)
```
### CvWindow
Just inherit `cvlayer.CvWindow`.
```python
from cvlayer import CvWindow
from numpy.typing import NDArray
class YourApp(CvWindow):
def on_frame(self, image: NDArray) -> NDArray:
# TODO: Implement image processing logic ...
return image
YourApp("/path/to/video/file.mp4").run()
```
## Example
The following sample is a [Perspective Transform](https://docs.opencv.org/4.x/da/d6e/tutorial_py_geometric_transformations.html) example:
```python
from sys import argv, stderr
from sys import exit as sys_exit
from typing import List, Optional
from cvlayer import CvLayer, CvMixin, CvWindow
from cvlayer.typing import PointI
from cvlayer.palette.basic import RED
from numpy.typing import NDArray
class YourApp(CvWindow, CvMixin, CvLayer):
_points: List[PointI]
def __init__(self, source: str, destination: Optional[str] = None):
super().__init__(source, destination)
left_top = 930, 2750
left_bottom = 846, 3098
right_top = 1091, 2750
right_bottom = 1361, 3098
self._points = [left_top, left_bottom, right_top, right_bottom]
self._scale = 2, 4
def on_frame(self, image: NDArray) -> NDArray:
with self.layer("select-roi") as layer:
self.roi = layer.param("roi").build_select_roi().value
layer.frame = layer.prev_frame
with self.layer("perspective-points") as layer:
points = layer.param("pp").build_select_points(self._points).value
canvas = layer.prev_frame.copy()
for p in points:
self.cvl_draw_point(canvas, p, color=RED)
layer.frame = canvas
with self.layer("perspective-transform") as layer:
sw = layer.param("scale-width").build_uint(self._scale[0]).value
sh = layer.param("scale-height").build_uint(self._scale[1]).value
xs = list(map(lambda point: point[0], self._points))
ys = list(map(lambda point: point[1], self._points))
x1, x2 = min(xs), max(xs)
y1, y2 = min(ys), max(ys)
width, height = abs(x2 - x1) * sw, abs(y2 - y1) * sh
roi = 0, 0, width, height
m = self.cvl_get_perspective_transform_with_quadrilateral(
left_top=points[0],
left_bottom=points[1],
right_top=points[2],
right_bottom=points[3],
destination_roi=roi,
)
layer.frame = self.cvl_warp_perspective(image, m, (width, height))
with self.layer("hsv") as layer:
layer.frame = hsv = self.cvl_cvt_color_bgr2hsv(layer.prev_frame)
with self.layer("hsv-h") as layer:
layer.frame = h = hsv[:, :, 0]
with self.layer("hsv-s") as layer:
layer.frame = s = hsv[:, :, 1]
with self.layer("hsv-v") as layer:
layer.frame = v = hsv[:, :, 2]
assert h is not None
assert s is not None
assert v is not None
self.cvm_gaussian_blur("v-blur", (3, 19), 0.0, 7.0)
self.cvm_threshold_binary("v-thresh", 230)
return self.last_frame
def test_main(*args) -> None:
source = args[1]
destination = args[2] if len(args) >= 3 else None
try:
app = YourApp(source, destination)
app.run()
except Exception as e:
print(e, file=stderr)
sys_exit(1)
else:
sys_exit(0)
if __name__ == "__main__":
test_main(*argv)
```
## License
See the [LICENSE](./LICENSE) file for details. In summary,
**cvlayer** is licensed under the **MIT license**.
Raw data
{
"_id": null,
"home_page": "https://github.com/osom8979/cvlayer",
"name": "cvlayer",
"maintainer": "zer0",
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": "osom8979@gmail.com",
"keywords": null,
"author": "zer0",
"author_email": "osom8979@gmail.com",
"download_url": null,
"platform": null,
"description": "# cvlayer\n\n[![PyPI](https://img.shields.io/pypi/v/cvlayer?style=flat-square)](https://pypi.org/project/cvlayer/)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/cvlayer?style=flat-square)\n[![GitHub](https://img.shields.io/github/license/osom8979/cvlayer?style=flat-square)](https://github.com/osom8979/cvlayer/)\n\nOpenCV Layer Helper\n\n## Overview\n\nWhen performing image-based machine learning/inference, many situations arise that require pre-/post-processing appropriate for the domain environment.\n\nA commonly used library today is [OpenCV](https://opencv.org/).\n\nImage processing with [OpenCV](https://opencv.org/) requires insight into the image and the domain.\n\nHowever, gaining these insights often requires doing something far from the essence. (Visualization, parameter modification, library conflicts, assertions, etc...)\n\nHere are some problems that occurred to me:\n\n...\n\n## Features\n\n...\n\n## Installation\n\nInstall `cvlayer`:\n```shell\npip install cvlayer\n```\n\nInstall `cvlayer` with `opencv-python`:\n```shell\npip install cvlayer[opencv]\n```\n\nInstall `cvlayer` with `opencv-python-headless`:\n```shell\npip install cvlayer[headless]\n```\n\n## Usage\n\n### CvLayer\n\nJust inherit `cvlayer.CvLayer`.\n\n```python\nfrom cvlayer import CvLayer\n\n\nclass YourApp(CvLayer):\n def func(self, image):\n self.cvl_cvt_color_bgr2hsv(image)\n```\n\n### CvWindow\n\nJust inherit `cvlayer.CvWindow`.\n\n```python\nfrom cvlayer import CvWindow\nfrom numpy.typing import NDArray\n\n\nclass YourApp(CvWindow):\n def on_frame(self, image: NDArray) -> NDArray:\n # TODO: Implement image processing logic ...\n return image\n\n\nYourApp(\"/path/to/video/file.mp4\").run()\n```\n\n## Example\n\nThe following sample is a [Perspective Transform](https://docs.opencv.org/4.x/da/d6e/tutorial_py_geometric_transformations.html) example:\n\n```python\nfrom sys import argv, stderr\nfrom sys import exit as sys_exit\nfrom typing import List, Optional\n\nfrom cvlayer import CvLayer, CvMixin, CvWindow\nfrom cvlayer.typing import PointI\nfrom cvlayer.palette.basic import RED\nfrom numpy.typing import NDArray\n\n\nclass YourApp(CvWindow, CvMixin, CvLayer):\n _points: List[PointI]\n\n def __init__(self, source: str, destination: Optional[str] = None):\n super().__init__(source, destination)\n left_top = 930, 2750\n left_bottom = 846, 3098\n right_top = 1091, 2750\n right_bottom = 1361, 3098\n self._points = [left_top, left_bottom, right_top, right_bottom]\n self._scale = 2, 4\n\n def on_frame(self, image: NDArray) -> NDArray:\n with self.layer(\"select-roi\") as layer:\n self.roi = layer.param(\"roi\").build_select_roi().value\n layer.frame = layer.prev_frame\n\n with self.layer(\"perspective-points\") as layer:\n points = layer.param(\"pp\").build_select_points(self._points).value\n canvas = layer.prev_frame.copy()\n for p in points:\n self.cvl_draw_point(canvas, p, color=RED)\n layer.frame = canvas\n\n with self.layer(\"perspective-transform\") as layer:\n sw = layer.param(\"scale-width\").build_uint(self._scale[0]).value\n sh = layer.param(\"scale-height\").build_uint(self._scale[1]).value\n\n xs = list(map(lambda point: point[0], self._points))\n ys = list(map(lambda point: point[1], self._points))\n x1, x2 = min(xs), max(xs)\n y1, y2 = min(ys), max(ys)\n width, height = abs(x2 - x1) * sw, abs(y2 - y1) * sh\n roi = 0, 0, width, height\n m = self.cvl_get_perspective_transform_with_quadrilateral(\n left_top=points[0],\n left_bottom=points[1],\n right_top=points[2],\n right_bottom=points[3],\n destination_roi=roi,\n )\n layer.frame = self.cvl_warp_perspective(image, m, (width, height))\n\n with self.layer(\"hsv\") as layer:\n layer.frame = hsv = self.cvl_cvt_color_bgr2hsv(layer.prev_frame)\n with self.layer(\"hsv-h\") as layer:\n layer.frame = h = hsv[:, :, 0]\n with self.layer(\"hsv-s\") as layer:\n layer.frame = s = hsv[:, :, 1]\n with self.layer(\"hsv-v\") as layer:\n layer.frame = v = hsv[:, :, 2]\n\n assert h is not None\n assert s is not None\n assert v is not None\n\n self.cvm_gaussian_blur(\"v-blur\", (3, 19), 0.0, 7.0)\n self.cvm_threshold_binary(\"v-thresh\", 230)\n\n return self.last_frame\n\n\ndef test_main(*args) -> None:\n source = args[1]\n destination = args[2] if len(args) >= 3 else None\n\n try:\n app = YourApp(source, destination)\n app.run()\n except Exception as e:\n print(e, file=stderr)\n sys_exit(1)\n else:\n sys_exit(0)\n\n\nif __name__ == \"__main__\":\n test_main(*argv)\n```\n\n## License\n\nSee the [LICENSE](./LICENSE) file for details. In summary,\n**cvlayer** is licensed under the **MIT license**.\n\n\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "OpenCV Layer Helper",
"version": "0.32.0",
"project_urls": {
"GitHub: issues": "https://github.com/osom8979/cvlayer/issues",
"GitHub: repo": "https://github.com/osom8979/cvlayer",
"Homepage": "https://github.com/osom8979/cvlayer"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "12fe0f7f47f9581f3576f49e285ae676595f9f7e4d2c8072f02ebf45f3fd28f5",
"md5": "8212b1bb34bd28604da0dbf8c1960bcf",
"sha256": "0010a6dede5885c8c5a99b7ba5c2db713a8871dc62b2c20fed4a300d5b171052"
},
"downloads": -1,
"filename": "cvlayer-0.32.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8212b1bb34bd28604da0dbf8c1960bcf",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 197947,
"upload_time": "2024-07-04T00:41:16",
"upload_time_iso_8601": "2024-07-04T00:41:16.356304Z",
"url": "https://files.pythonhosted.org/packages/12/fe/0f7f47f9581f3576f49e285ae676595f9f7e4d2c8072f02ebf45f3fd28f5/cvlayer-0.32.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-04 00:41:16",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "osom8979",
"github_project": "cvlayer",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "cvlayer"
}