Skip to content

Commit f8d56a2

Browse files
authored
[CVCUDA] wrap C processors in Python API (#1576)
* wrap C ops in Python API * add comments * change processor create style * processors class inherit
1 parent 3d36e26 commit f8d56a2

File tree

3 files changed

+81
-23
lines changed

3 files changed

+81
-23
lines changed

python/fastdeploy/vision/common/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515

1616
from .manager import ProcessorManager
1717
from .manager import PyProcessorManager
18+
from .processors import *
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from __future__ import absolute_import
2+
from ... import c_lib_wrap as C
3+
4+
5+
class Processor():
6+
def __init__(self):
7+
self.processor
8+
9+
def __call__(self, mat):
10+
self.processor(mat)
11+
12+
13+
class ResizeByShort(Processor):
14+
def __init__(self, target_size: int, interp=1, use_scale=True, max_hw=[]):
15+
self.processor = C.vision.processors.ResizeByShort(target_size, interp,
16+
use_scale, max_hw)
17+
"""Create a ResizeByShort operation with the given parameters.
18+
19+
:param target_size: the target short size to resize the image
20+
:param interp: optionally, the interpolation mode for resizing image
21+
:param use_scale: optionally, whether to scale image
22+
:param max_hw: max spatial size which is used by ResizeByShort
23+
"""
24+
25+
26+
class CenterCrop(Processor):
27+
def __init__(self, width, height):
28+
self.processor = C.vision.processors.CenterCrop(width, height)
29+
"""Create a CenterCrop operation with the given parameters.
30+
31+
:param width: desired width of the cropped image
32+
:param height: desired height of the cropped image
33+
"""
34+
35+
36+
class Pad(Processor):
37+
def __init__(self, top: int, bottom: int, left: int, right: int, value=[]):
38+
self.processor = C.vision.processors.Pad(top, bottom, left, right,
39+
value)
40+
"""Create a Pad operation with the given parameters.
41+
42+
:param top: the top padding
43+
:param bottom: the bottom padding
44+
:param left: the left padding
45+
:param right: the right padding
46+
:param value: the value that is used to pad on the input image
47+
"""
48+
49+
50+
class NormalizeAndPermute(Processor):
51+
def __init__(self,
52+
mean=[],
53+
std=[],
54+
is_scale=True,
55+
min=[],
56+
max=[],
57+
swap_rb=False):
58+
self.processor = C.vision.processors.NormalizeAndPermute(
59+
mean, std, is_scale, min, max, swap_rb)
60+
"""Creae a Normalize and a Permute operation with the given parameters.
61+
62+
:param mean A list containing the mean of each channel
63+
:param std A list containing the standard deviation of each channel
64+
:param is_scale Specifies if the image are being scaled or not
65+
:param min A list containing the minimum value of each channel
66+
:param max A list containing the maximum value of each channel
67+
"""

tutorials/vision_processor/python/preprocess.py

+13-23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import cv2
33

44
from fastdeploy.vision.common.manager import PyProcessorManager
5+
from fastdeploy.vision.common.processors import *
56

67

78
def parse_arguments():
@@ -20,29 +21,18 @@ class CustomProcessor(PyProcessorManager):
2021
def __init__(self) -> None:
2122
super().__init__()
2223
# create op
23-
hw = [500, 500]
24-
self.resize_op = fd.C.vision.processors.ResizeByShort(100, 1, True, hw)
25-
26-
mean = [0.485, 0.456, 0.406]
27-
std = [0.229, 0.224, 0.225]
28-
is_scale = True
29-
min = []
30-
max = []
31-
swap_rb = False
32-
self.normalize_permute_op = fd.C.vision.processors.NormalizeAndPermute(
33-
mean, std, is_scale, min, max, swap_rb)
34-
35-
width = 50
36-
height = 50
37-
self.centercrop_op = fd.C.vision.processors.CenterCrop(width, height)
38-
39-
top = 5
40-
bottom = 5
41-
left = 5
42-
right = 5
43-
pad_value = [225, 225, 225]
44-
self.pad_op = fd.C.vision.processors.Pad(top, bottom, left, right,
45-
pad_value)
24+
self.resize_op = ResizeByShort(
25+
target_size=100, interp=1, use_scale=True, max_hw=[500, 500])
26+
self.normalize_permute_op = NormalizeAndPermute(
27+
mean=[0.485, 0.456, 0.406],
28+
std=[0.229, 0.224, 0.225],
29+
is_scale=True,
30+
min=[],
31+
max=[],
32+
swap_rb=False)
33+
self.centercrop_op = CenterCrop(width=50, height=50)
34+
self.pad_op = Pad(
35+
top=5, bottom=5, left=5, right=5, value=[225, 225, 225])
4636

4737
def apply(self, image_batch):
4838
outputs = []

0 commit comments

Comments
 (0)