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-06-08 14:22:23 +08:00
|
|
|
import shutil
|
2022-05-19 22:18:35 +08:00
|
|
|
import tempfile
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
import cv2
|
|
|
|
|
|
2022-06-09 20:16:26 +08:00
|
|
|
from modelscope.fileio import File
|
|
|
|
|
from modelscope.pipelines import pipeline
|
|
|
|
|
from modelscope.pydatasets import PyDataset
|
2022-06-15 14:06:53 +08:00
|
|
|
from modelscope.utils.constant import ModelFile, Tasks
|
2022-06-09 20:16:26 +08:00
|
|
|
from modelscope.utils.hub import get_model_cache_dir
|
2022-05-19 22:18:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class ImageMattingTest(unittest.TestCase):
|
|
|
|
|
|
2022-06-08 14:22:23 +08:00
|
|
|
def setUp(self) -> None:
|
2022-06-15 14:06:53 +08:00
|
|
|
self.model_id = 'damo/cv_unet_image-matting_damo'
|
2022-06-08 14:22:23 +08:00
|
|
|
# switch to False if downloading everytime is not desired
|
|
|
|
|
purge_cache = True
|
|
|
|
|
if purge_cache:
|
|
|
|
|
shutil.rmtree(
|
2022-06-09 16:57:33 +08:00
|
|
|
get_model_cache_dir(self.model_id), ignore_errors=True)
|
2022-06-08 14:22:23 +08:00
|
|
|
|
2022-06-15 14:06:53 +08:00
|
|
|
@unittest.skip('deprecated, download model from model hub instead')
|
|
|
|
|
def test_run_with_direct_file_download(self):
|
2022-05-19 22:18:35 +08:00
|
|
|
model_path = 'http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs' \
|
|
|
|
|
'.com/data/test/maas/image_matting/matting_person.pb'
|
2022-05-30 11:53:53 +08:00
|
|
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
2022-06-15 14:06:53 +08:00
|
|
|
model_file = osp.join(tmp_dir, ModelFile.TF_GRAPH_FILE)
|
2022-05-30 11:53:53 +08:00
|
|
|
with open(model_file, 'wb') as ofile:
|
|
|
|
|
ofile.write(File.read(model_path))
|
|
|
|
|
img_matting = pipeline(Tasks.image_matting, model=tmp_dir)
|
2022-05-19 22:18:35 +08:00
|
|
|
|
|
|
|
|
result = img_matting(
|
|
|
|
|
'http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/data/test/maas/image_matting/test.png'
|
|
|
|
|
)
|
|
|
|
|
cv2.imwrite('result.png', result['output_png'])
|
|
|
|
|
|
2022-06-01 10:20:53 +08:00
|
|
|
def test_run_with_dataset(self):
|
|
|
|
|
input_location = [
|
|
|
|
|
'http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/data/test/maas/image_matting/test.png'
|
|
|
|
|
]
|
|
|
|
|
# alternatively:
|
|
|
|
|
# input_location = '/dir/to/images'
|
|
|
|
|
|
|
|
|
|
dataset = PyDataset.load(input_location, target='image')
|
2022-06-08 14:22:23 +08:00
|
|
|
img_matting = pipeline(Tasks.image_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)
|
|
|
|
|
cv2.imwrite('result.png', next(result)['output_png'])
|
|
|
|
|
print(f'Output written to {osp.abspath("result.png")}')
|
2022-05-31 18:27:19 +08:00
|
|
|
|
2022-05-30 11:53:53 +08:00
|
|
|
def test_run_modelhub(self):
|
2022-06-08 14:22:23 +08:00
|
|
|
img_matting = pipeline(Tasks.image_matting, model=self.model_id)
|
2022-05-30 11:53:53 +08:00
|
|
|
|
|
|
|
|
result = img_matting(
|
|
|
|
|
'http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/data/test/maas/image_matting/test.png'
|
|
|
|
|
)
|
|
|
|
|
cv2.imwrite('result.png', result['output_png'])
|
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-09 16:57:33 +08:00
|
|
|
def test_run_modelhub_default_model(self):
|
|
|
|
|
img_matting = pipeline(Tasks.image_matting)
|
|
|
|
|
|
|
|
|
|
result = img_matting(
|
|
|
|
|
'http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/data/test/maas/image_matting/test.png'
|
|
|
|
|
)
|
|
|
|
|
cv2.imwrite('result.png', result['output_png'])
|
|
|
|
|
print(f'Output written to {osp.abspath("result.png")}')
|
|
|
|
|
|
2022-05-19 22:18:35 +08:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|