2022-05-19 22:18:35 +08:00
|
|
|
# Copyright (c) Alibaba, Inc. and its affiliates.
|
2022-05-30 11:53:53 +08:00
|
|
|
import os.path as osp
|
2022-05-19 22:18:35 +08:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
import cv2
|
|
|
|
|
|
2022-06-27 11:09:38 +08:00
|
|
|
from modelscope.msdatasets import MsDataset
|
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-15 14:06:53 +08:00
|
|
|
from modelscope.utils.constant import ModelFile, Tasks
|
2022-06-15 14:53:49 +08:00
|
|
|
from modelscope.utils.test_utils import test_level
|
2022-05-19 22:18:35 +08:00
|
|
|
|
|
|
|
|
|
2023-05-22 10:53:18 +08:00
|
|
|
class ImageMattingTest(unittest.TestCase):
|
2022-05-19 22:18:35 +08:00
|
|
|
|
2022-06-08 14:22:23 +08:00
|
|
|
def setUp(self) -> None:
|
2022-06-17 10:25:54 +08:00
|
|
|
self.model_id = 'damo/cv_unet_image-matting'
|
2022-06-08 14:22:23 +08:00
|
|
|
|
2022-06-15 14:53:49 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
|
2022-06-01 10:20:53 +08:00
|
|
|
def test_run_with_dataset(self):
|
2022-06-17 19:33:15 +08:00
|
|
|
input_location = ['data/test/images/image_matting.png']
|
2022-06-01 10:20:53 +08:00
|
|
|
# alternatively:
|
|
|
|
|
# input_location = '/dir/to/images'
|
|
|
|
|
|
2022-06-27 11:09:38 +08:00
|
|
|
dataset = MsDataset.load(input_location, target='image')
|
2022-08-04 14:59:14 +08:00
|
|
|
img_matting = pipeline(Tasks.portrait_matting, model=self.model_id)
|
2022-06-01 10:20:53 +08:00
|
|
|
# note that for dataset output, the inference-output is a Generator that can be iterated.
|
|
|
|
|
result = img_matting(dataset)
|
2022-07-01 18:29:34 +08:00
|
|
|
cv2.imwrite('result.png', next(result)[OutputKeys.OUTPUT_IMG])
|
2022-06-01 10:20:53 +08:00
|
|
|
print(f'Output written to {osp.abspath("result.png")}')
|
2022-05-31 18:27:19 +08:00
|
|
|
|
2022-06-15 14:53:49 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
2022-05-30 11:53:53 +08:00
|
|
|
def test_run_modelhub(self):
|
2022-08-04 14:59:14 +08:00
|
|
|
img_matting = pipeline(Tasks.portrait_matting, model=self.model_id)
|
2022-05-30 11:53:53 +08:00
|
|
|
|
2022-06-17 19:33:15 +08:00
|
|
|
result = img_matting('data/test/images/image_matting.png')
|
2022-07-01 18:29:34 +08:00
|
|
|
cv2.imwrite('result.png', result[OutputKeys.OUTPUT_IMG])
|
2022-06-01 10:20:53 +08:00
|
|
|
print(f'Output written to {osp.abspath("result.png")}')
|
2022-05-30 11:53:53 +08:00
|
|
|
|
2022-06-23 16:55:48 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
|
2022-06-09 16:57:33 +08:00
|
|
|
def test_run_modelhub_default_model(self):
|
2022-08-04 14:59:14 +08:00
|
|
|
img_matting = pipeline(Tasks.portrait_matting)
|
2022-06-09 16:57:33 +08:00
|
|
|
|
2022-06-17 19:33:15 +08:00
|
|
|
result = img_matting('data/test/images/image_matting.png')
|
2022-07-01 18:29:34 +08:00
|
|
|
cv2.imwrite('result.png', result[OutputKeys.OUTPUT_IMG])
|
2022-06-09 16:57:33 +08:00
|
|
|
print(f'Output written to {osp.abspath("result.png")}')
|
|
|
|
|
|
2022-06-24 11:47:28 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
|
2022-06-21 11:10:28 +08:00
|
|
|
def test_run_with_modelscope_dataset(self):
|
2022-06-28 20:40:57 +08:00
|
|
|
dataset = MsDataset.load(
|
2022-07-20 16:38:15 +08:00
|
|
|
'fixtures_image_utils',
|
|
|
|
|
namespace='damotest',
|
|
|
|
|
split='test',
|
|
|
|
|
target='file')
|
2022-08-04 14:59:14 +08:00
|
|
|
img_matting = pipeline(Tasks.portrait_matting, model=self.model_id)
|
2022-06-21 11:10:28 +08:00
|
|
|
result = img_matting(dataset)
|
2022-07-20 16:38:15 +08:00
|
|
|
for i in range(2):
|
2022-07-01 18:29:34 +08:00
|
|
|
cv2.imwrite(f'result_{i}.png', next(result)[OutputKeys.OUTPUT_IMG])
|
2022-06-21 11:10:28 +08:00
|
|
|
print(
|
|
|
|
|
f'Output written to dir: {osp.dirname(osp.abspath("result_0.png"))}'
|
|
|
|
|
)
|
|
|
|
|
|
2022-05-19 22:18:35 +08:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|