|
@ -17,7 +17,7 @@ import logging |
|
|
import cv2 |
|
|
import cv2 |
|
|
import requests |
|
|
import requests |
|
|
import numpy as np |
|
|
import numpy as np |
|
|
from typing import List |
|
|
|
|
|
|
|
|
from typing import List, Tuple |
|
|
|
|
|
|
|
|
from towhee import register |
|
|
from towhee import register |
|
|
from towhee.operator import PyOperator, OperatorFlag |
|
|
from towhee.operator import PyOperator, OperatorFlag |
|
@ -29,14 +29,14 @@ log = logging.getLogger() |
|
|
@register(output_schema=['img'], |
|
|
@register(output_schema=['img'], |
|
|
flag=OperatorFlag.STATELESS | OperatorFlag.REUSEABLE) |
|
|
flag=OperatorFlag.STATELESS | OperatorFlag.REUSEABLE) |
|
|
class ImageCropCV2(PyOperator): |
|
|
class ImageCropCV2(PyOperator): |
|
|
def __init__(self, clamp = False) |
|
|
|
|
|
|
|
|
def __init__(self, clamp = False): |
|
|
self.clamp = clamp |
|
|
self.clamp = clamp |
|
|
|
|
|
|
|
|
@staticmethod |
|
|
@staticmethod |
|
|
def _clamp(x, minimum, maximum) |
|
|
|
|
|
|
|
|
def _clamp(x, minimum, maximum): |
|
|
return max(minimum, min(x, maximum)) |
|
|
return max(minimum, min(x, maximum)) |
|
|
|
|
|
|
|
|
def __call__(self, img: np.ndarray, bboxes: List[(int, int, int, int)]): |
|
|
|
|
|
|
|
|
def __call__(self, img: np.ndarray, bboxes: List[Tuple]): |
|
|
h, w, _ = img.shape |
|
|
h, w, _ = img.shape |
|
|
res = [] |
|
|
res = [] |
|
|
for box in bboxes: |
|
|
for box in bboxes: |
|
@ -45,6 +45,6 @@ class ImageCropCV2(PyOperator): |
|
|
x2 = ImageCropCV2._clamp(x2, 0, w) |
|
|
x2 = ImageCropCV2._clamp(x2, 0, w) |
|
|
y1 = ImageCropCV2._clamp(y1, 0, h) |
|
|
y1 = ImageCropCV2._clamp(y1, 0, h) |
|
|
y2 = ImageCropCV2._clamp(y2, 0, h) |
|
|
y2 = ImageCropCV2._clamp(y2, 0, h) |
|
|
res.append(Image[y1:y2,x1:x2,:]) |
|
|
|
|
|
|
|
|
res.append(img[y1:y2,x1:x2,:]) |
|
|
return res |
|
|
return res |
|
|
|
|
|
|
|
|