mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-06 00:57:33 +08:00
[CVCUDA] wrap C processors in Python API (#1576)
* wrap C ops in Python API * add comments * change processor create style * processors class inherit
This commit is contained in:
@@ -15,3 +15,4 @@ from __future__ import absolute_import
|
||||
|
||||
from .manager import ProcessorManager
|
||||
from .manager import PyProcessorManager
|
||||
from .processors import *
|
||||
|
67
python/fastdeploy/vision/common/processors.py
Normal file
67
python/fastdeploy/vision/common/processors.py
Normal file
@@ -0,0 +1,67 @@
|
||||
from __future__ import absolute_import
|
||||
from ... import c_lib_wrap as C
|
||||
|
||||
|
||||
class Processor():
|
||||
def __init__(self):
|
||||
self.processor
|
||||
|
||||
def __call__(self, mat):
|
||||
self.processor(mat)
|
||||
|
||||
|
||||
class ResizeByShort(Processor):
|
||||
def __init__(self, target_size: int, interp=1, use_scale=True, max_hw=[]):
|
||||
self.processor = C.vision.processors.ResizeByShort(target_size, interp,
|
||||
use_scale, max_hw)
|
||||
"""Create a ResizeByShort operation with the given parameters.
|
||||
|
||||
:param target_size: the target short size to resize the image
|
||||
:param interp: optionally, the interpolation mode for resizing image
|
||||
:param use_scale: optionally, whether to scale image
|
||||
:param max_hw: max spatial size which is used by ResizeByShort
|
||||
"""
|
||||
|
||||
|
||||
class CenterCrop(Processor):
|
||||
def __init__(self, width, height):
|
||||
self.processor = C.vision.processors.CenterCrop(width, height)
|
||||
"""Create a CenterCrop operation with the given parameters.
|
||||
|
||||
:param width: desired width of the cropped image
|
||||
:param height: desired height of the cropped image
|
||||
"""
|
||||
|
||||
|
||||
class Pad(Processor):
|
||||
def __init__(self, top: int, bottom: int, left: int, right: int, value=[]):
|
||||
self.processor = C.vision.processors.Pad(top, bottom, left, right,
|
||||
value)
|
||||
"""Create a Pad operation with the given parameters.
|
||||
|
||||
:param top: the top padding
|
||||
:param bottom: the bottom padding
|
||||
:param left: the left padding
|
||||
:param right: the right padding
|
||||
:param value: the value that is used to pad on the input image
|
||||
"""
|
||||
|
||||
|
||||
class NormalizeAndPermute(Processor):
|
||||
def __init__(self,
|
||||
mean=[],
|
||||
std=[],
|
||||
is_scale=True,
|
||||
min=[],
|
||||
max=[],
|
||||
swap_rb=False):
|
||||
self.processor = C.vision.processors.NormalizeAndPermute(
|
||||
mean, std, is_scale, min, max, swap_rb)
|
||||
"""Creae a Normalize and a Permute operation with the given parameters.
|
||||
|
||||
:param mean A list containing the mean of each channel
|
||||
:param std A list containing the standard deviation of each channel
|
||||
:param is_scale Specifies if the image are being scaled or not
|
||||
:param min A list containing the minimum value of each channel
|
||||
:param max A list containing the maximum value of each channel
|
||||
"""
|
@@ -2,6 +2,7 @@ import fastdeploy as fd
|
||||
import cv2
|
||||
|
||||
from fastdeploy.vision.common.manager import PyProcessorManager
|
||||
from fastdeploy.vision.common.processors import *
|
||||
|
||||
|
||||
def parse_arguments():
|
||||
@@ -20,29 +21,18 @@ class CustomProcessor(PyProcessorManager):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
# create op
|
||||
hw = [500, 500]
|
||||
self.resize_op = fd.C.vision.processors.ResizeByShort(100, 1, True, hw)
|
||||
|
||||
mean = [0.485, 0.456, 0.406]
|
||||
std = [0.229, 0.224, 0.225]
|
||||
is_scale = True
|
||||
min = []
|
||||
max = []
|
||||
swap_rb = False
|
||||
self.normalize_permute_op = fd.C.vision.processors.NormalizeAndPermute(
|
||||
mean, std, is_scale, min, max, swap_rb)
|
||||
|
||||
width = 50
|
||||
height = 50
|
||||
self.centercrop_op = fd.C.vision.processors.CenterCrop(width, height)
|
||||
|
||||
top = 5
|
||||
bottom = 5
|
||||
left = 5
|
||||
right = 5
|
||||
pad_value = [225, 225, 225]
|
||||
self.pad_op = fd.C.vision.processors.Pad(top, bottom, left, right,
|
||||
pad_value)
|
||||
self.resize_op = ResizeByShort(
|
||||
target_size=100, interp=1, use_scale=True, max_hw=[500, 500])
|
||||
self.normalize_permute_op = NormalizeAndPermute(
|
||||
mean=[0.485, 0.456, 0.406],
|
||||
std=[0.229, 0.224, 0.225],
|
||||
is_scale=True,
|
||||
min=[],
|
||||
max=[],
|
||||
swap_rb=False)
|
||||
self.centercrop_op = CenterCrop(width=50, height=50)
|
||||
self.pad_op = Pad(
|
||||
top=5, bottom=5, left=5, right=5, value=[225, 225, 225])
|
||||
|
||||
def apply(self, image_batch):
|
||||
outputs = []
|
||||
|
Reference in New Issue
Block a user