OpenCV Layer Helper
Project description
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.
Image processing with OpenCV 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
:
pip install cvlayer
Install cvlayer
with opencv-python
:
pip install cvlayer[opencv]
Install cvlayer
with opencv-python-headless
:
pip install cvlayer[headless]
Usage
CvLayer
Just inherit cvlayer.CvLayer
.
from cvlayer import CvLayer
class YourApp(CvLayer):
def func(self, image):
self.cvl_cvt_color_bgr2hsv(image)
CvWindow
Just inherit cvlayer.CvWindow
.
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 example:
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 file for details. In summary, cvlayer is licensed under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file cvlayer-0.32.0-py3-none-any.whl
.
File metadata
- Download URL: cvlayer-0.32.0-py3-none-any.whl
- Upload date:
- Size: 197.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0010a6dede5885c8c5a99b7ba5c2db713a8871dc62b2c20fed4a300d5b171052 |
|
MD5 | 8212b1bb34bd28604da0dbf8c1960bcf |
|
BLAKE2b-256 | 12fe0f7f47f9581f3576f49e285ae676595f9f7e4d2c8072f02ebf45f3fd28f5 |