towhee
/
image-crop
copied
wxywb
3 years ago
3 changed files with 158 additions and 1 deletions
@ -1,2 +1,89 @@ |
|||||
# image-crop |
|
||||
|
# Image Crop Implementation with CV2 |
||||
|
|
||||
|
*author: David Wang* |
||||
|
|
||||
|
|
||||
|
|
||||
|
<br /> |
||||
|
|
||||
|
|
||||
|
|
||||
|
## Desription |
||||
|
|
||||
|
An image crop operator implementation with OpenCV. |
||||
|
|
||||
|
|
||||
|
|
||||
|
<br /> |
||||
|
|
||||
|
|
||||
|
|
||||
|
## Code Example |
||||
|
|
||||
|
Load a image from path './dog.jpg'. |
||||
|
|
||||
|
*Write the pipeline in simplified style:* |
||||
|
|
||||
|
```python |
||||
|
import towhee |
||||
|
|
||||
|
towhee.glob('./dog.jpg') \ |
||||
|
.image_decode.cv2() \ |
||||
|
.show() |
||||
|
``` |
||||
|
|
||||
|
*Write a same pipeline with explicit inputs/outputs name specifications:* |
||||
|
|
||||
|
```python |
||||
|
import towhee |
||||
|
|
||||
|
towhee.glob['path']('./dog.jpg') \ |
||||
|
.image_decode.cv2['path', 'img']() \ |
||||
|
.select['img']() \ |
||||
|
.show() |
||||
|
``` |
||||
|
|
||||
|
|
||||
|
|
||||
|
<img src="./show_result.jpg" height="150px"/> |
||||
|
|
||||
|
|
||||
|
|
||||
|
<br /> |
||||
|
|
||||
|
|
||||
|
|
||||
|
## Factory Constructor |
||||
|
|
||||
|
Create the operator via the following factory method |
||||
|
|
||||
|
***image_decode.cv2()*** |
||||
|
|
||||
|
|
||||
|
|
||||
|
<br /> |
||||
|
|
||||
|
|
||||
|
|
||||
|
## Interface |
||||
|
|
||||
|
An image decode operator takes an image path as input. It decodes the image back to ndarray. |
||||
|
|
||||
|
|
||||
|
|
||||
|
**Parameters:** |
||||
|
|
||||
|
**img**: *str* |
||||
|
|
||||
|
​ Image file path. |
||||
|
|
||||
|
|
||||
|
|
||||
|
**Returns**: *towhee.types.Image (a sub-class of numpy.ndarray)* |
||||
|
|
||||
|
​ The decoded image data as numpy.ndarray. |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
@ -0,0 +1,20 @@ |
|||||
|
# Copyright 2021 Zilliz. All rights reserved. |
||||
|
# |
||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
# you may not use this file except in compliance with the License. |
||||
|
# You may obtain a copy of the License at |
||||
|
# |
||||
|
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
# |
||||
|
# Unless required by applicable law or agreed to in writing, software |
||||
|
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
# See the License for the specific language governing permissions and |
||||
|
# limitations under the License. |
||||
|
|
||||
|
from .image_crop_cv2 import ImageDecodeCV2 |
||||
|
|
||||
|
|
||||
|
# The factory method |
||||
|
def image_crop(clamp = False): |
||||
|
return ImageCropCV2(clamp) |
@ -0,0 +1,50 @@ |
|||||
|
# Copyright 2021 Zilliz. All rights reserved. |
||||
|
# |
||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
# you may not use this file except in compliance with the License. |
||||
|
# You may obtain a copy of the License at |
||||
|
# |
||||
|
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
# |
||||
|
# Unless required by applicable law or agreed to in writing, software |
||||
|
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
# See the License for the specific language governing permissions and |
||||
|
# limitations under the License. |
||||
|
|
||||
|
|
||||
|
import logging |
||||
|
import cv2 |
||||
|
import requests |
||||
|
import numpy as np |
||||
|
from typing import List |
||||
|
|
||||
|
from towhee import register |
||||
|
from towhee.operator import PyOperator, OperatorFlag |
||||
|
from towhee._types import Image |
||||
|
|
||||
|
log = logging.getLogger() |
||||
|
|
||||
|
|
||||
|
@register(output_schema=['img'], |
||||
|
flag=OperatorFlag.STATELESS | OperatorFlag.REUSEABLE) |
||||
|
class ImageCropCV2(PyOperator): |
||||
|
def __init__(self, clamp = False) |
||||
|
self.clamp = clamp |
||||
|
|
||||
|
@staticmethod |
||||
|
def _clamp(x, minimum, maximum) |
||||
|
return max(minimum, min(x, maximum)) |
||||
|
|
||||
|
def __call__(self, img: np.ndarray, bboxes: List[(int, int, int, int)]): |
||||
|
h, w, _ = img.shape |
||||
|
res = [] |
||||
|
for box in bboxes: |
||||
|
x1, y1, x2, y2 = box |
||||
|
x1 = ImageCropCV2._clamp(x1, 0, w) |
||||
|
x2 = ImageCropCV2._clamp(x2, 0, w) |
||||
|
y1 = ImageCropCV2._clamp(y1, 0, h) |
||||
|
y2 = ImageCropCV2._clamp(y2, 0, h) |
||||
|
res.append(Image[y1:y2,x1:x2,:]) |
||||
|
return res |
||||
|
|
Loading…
Reference in new issue