logo
Browse Source

Covert data type with cv2

Signed-off-by: shiyu22 <shiyu.chen@zilliz.com>
main
shiyu22 3 years ago
parent
commit
09f3bf0a89
  1. 31
      README.md
  2. 9
      animegan.py
  3. 4
      requirements.txt

31
README.md

@ -7,22 +7,35 @@ Convert an image into an animated image using [`AnimeganV2`](https://github.com/
## Code Example ## Code Example
Load an image from path './image.png'.
Load an image from path './test.png'.
*Write the pipeline in simplified style*: *Write the pipeline in simplified style*:
```python ```python
import towhee import towhee
from PIL import Image
import numpy
pipeline = towhee.glob('./image.png').image_decode.cv2().img2img_translation.animegan(model_name = 'hayao')
img = pipeline.to_list()[0]
img = numpy.transpose(img, (1,2,0))
img = Image.fromarray((img * 255).astype(numpy.uint8))
img.show()
towhee.glob('/Users/chenshiyu/workspace/data/pic/test.png') \
.image_decode.cv2() \
.img2img_translation.animegan(model_name = 'hayao') \
.show()
``` ```
<img src="./results1.png" alt="results1" style="height:120px;"/>
*Write a same pipeline with explicit inputs/outputs name specifications:*
```python
import towhee
towhee.glob['path']('/Users/chenshiyu/workspace/data/pic/test.png') \
.image_decode.cv2['path', 'origin']() \
.img2img_translation.animegan['origin', 'transformed'](model_name = 'hayao') \
.select('origin', 'transformed') \
.show()
```
<img src="./results2.png" alt="results1" style="height:120px;"/>
## Factory Constructor ## Factory Constructor
Create the operator via the following factory method Create the operator via the following factory method

9
animegan.py

@ -1,5 +1,7 @@
import os import os
import numpy
from pathlib import Path from pathlib import Path
from PIL import Image as PImage
from torchvision import transforms from torchvision import transforms
from towhee import register from towhee import register
@ -30,6 +32,11 @@ class Animegan(Operator):
@arg(1, to_image_color('RGB')) @arg(1, to_image_color('RGB'))
def __call__(self, image): def __call__(self, image):
img = self.tfms(image).unsqueeze(0) img = self.tfms(image).unsqueeze(0)
print(img.shape)
styled_image = self.model(img) styled_image = self.model(img)
styled_image = numpy.transpose(styled_image, (1,2,0))
styled_image = PImage.fromarray((styled_image * 255).astype(numpy.uint8))
styled_image = numpy.array(styled_image)
styled_image = styled_image[:, :, ::-1].copy()
return Image(styled_image, 'RGB') return Image(styled_image, 'RGB')

4
requirements.txt

@ -1,2 +1,4 @@
pathlib pathlib
torchvision
torchvision
pillow
opencv-python
Loading…
Cancel
Save