2022-06-09 15:18:38 +08:00
|
|
|
# Copyright (c) Alibaba, Inc. and its affiliates.
|
2022-06-10 12:56:44 +08:00
|
|
|
import os.path as osp
|
2022-06-09 15:18:38 +08:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
import cv2
|
|
|
|
|
|
2022-07-14 16:25:55 +08:00
|
|
|
from modelscope.outputs import OutputKeys
|
2022-06-09 20:16:26 +08:00
|
|
|
from modelscope.pipelines import pipeline
|
2022-06-10 12:56:44 +08:00
|
|
|
from modelscope.pipelines.base import Pipeline
|
2022-06-09 20:16:26 +08:00
|
|
|
from modelscope.utils.constant import Tasks
|
2022-09-08 14:08:51 +08:00
|
|
|
from modelscope.utils.demo_utils import DemoCompatibilityCheck
|
2022-06-15 14:53:49 +08:00
|
|
|
from modelscope.utils.test_utils import test_level
|
2022-06-09 15:18:38 +08:00
|
|
|
|
|
|
|
|
|
2022-09-08 14:08:51 +08:00
|
|
|
class ImageCartoonTest(unittest.TestCase, DemoCompatibilityCheck):
|
2022-06-09 15:18:38 +08:00
|
|
|
|
2022-06-10 12:56:44 +08:00
|
|
|
def setUp(self) -> None:
|
|
|
|
|
self.model_id = 'damo/cv_unet_person-image-cartoon_compound-models'
|
2022-09-19 11:28:01 +08:00
|
|
|
self.model_id_3d = 'damo/cv_unet_person-image-cartoon-3d_compound-models'
|
|
|
|
|
self.model_id_handdrawn = 'damo/cv_unet_person-image-cartoon-handdrawn_compound-models'
|
|
|
|
|
self.model_id_sketch = 'damo/cv_unet_person-image-cartoon-sketch_compound-models'
|
|
|
|
|
self.model_id_artstyle = 'damo/cv_unet_person-image-cartoon-artstyle_compound-models'
|
2022-09-08 14:08:51 +08:00
|
|
|
self.task = Tasks.image_portrait_stylization
|
2022-07-04 12:11:12 +08:00
|
|
|
self.test_image = 'https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/image_cartoon.png'
|
2022-06-09 15:18:38 +08:00
|
|
|
|
2022-06-10 12:56:44 +08:00
|
|
|
def pipeline_inference(self, pipeline: Pipeline, input_location: str):
|
|
|
|
|
result = pipeline(input_location)
|
|
|
|
|
if result is not None:
|
2022-07-01 18:29:34 +08:00
|
|
|
cv2.imwrite('result.png', result[OutputKeys.OUTPUT_IMG])
|
2022-06-10 12:56:44 +08:00
|
|
|
print(f'Output written to {osp.abspath("result.png")}')
|
2022-06-09 15:18:38 +08:00
|
|
|
|
2022-08-06 12:22:17 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
2022-06-10 12:56:44 +08:00
|
|
|
def test_run_modelhub(self):
|
2022-08-02 13:09:49 +08:00
|
|
|
img_cartoon = pipeline(
|
|
|
|
|
Tasks.image_portrait_stylization, model=self.model_id)
|
2022-06-10 12:56:44 +08:00
|
|
|
self.pipeline_inference(img_cartoon, self.test_image)
|
|
|
|
|
|
2022-09-19 11:28:01 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
|
|
|
|
def test_run_modelhub_3d(self):
|
|
|
|
|
img_cartoon = pipeline(
|
|
|
|
|
Tasks.image_portrait_stylization, model=self.model_id_3d)
|
|
|
|
|
self.pipeline_inference(img_cartoon, self.test_image)
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
|
|
|
|
def test_run_modelhub_handdrawn(self):
|
|
|
|
|
img_cartoon = pipeline(
|
|
|
|
|
Tasks.image_portrait_stylization, model=self.model_id_handdrawn)
|
|
|
|
|
self.pipeline_inference(img_cartoon, self.test_image)
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
|
|
|
|
def test_run_modelhub_sketch(self):
|
|
|
|
|
img_cartoon = pipeline(
|
|
|
|
|
Tasks.image_portrait_stylization, model=self.model_id_sketch)
|
|
|
|
|
self.pipeline_inference(img_cartoon, self.test_image)
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
|
|
|
|
def test_run_modelhub_artstyle(self):
|
|
|
|
|
img_cartoon = pipeline(
|
|
|
|
|
Tasks.image_portrait_stylization, model=self.model_id_artstyle)
|
|
|
|
|
self.pipeline_inference(img_cartoon, self.test_image)
|
|
|
|
|
|
2022-06-23 16:55:48 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
|
2022-06-10 12:56:44 +08:00
|
|
|
def test_run_modelhub_default_model(self):
|
2022-08-02 13:09:49 +08:00
|
|
|
img_cartoon = pipeline(Tasks.image_portrait_stylization)
|
2022-06-10 12:56:44 +08:00
|
|
|
self.pipeline_inference(img_cartoon, self.test_image)
|
2022-06-09 15:18:38 +08:00
|
|
|
|
2022-09-09 14:56:15 +08:00
|
|
|
@unittest.skip('demo compatibility test is only enabled on a needed-basis')
|
2022-09-08 14:08:51 +08:00
|
|
|
def test_demo_compatibility(self):
|
|
|
|
|
self.compatibility_check()
|
|
|
|
|
|
2022-06-09 15:18:38 +08:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|