You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_image_matting.py 3.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import os.path as osp
  3. import shutil
  4. import tempfile
  5. import unittest
  6. import cv2
  7. from modelscope.fileio import File
  8. from modelscope.msdatasets import MsDataset
  9. from modelscope.pipelines import pipeline
  10. from modelscope.utils.constant import ModelFile, Tasks
  11. from modelscope.utils.test_utils import test_level
  12. class ImageMattingTest(unittest.TestCase):
  13. def setUp(self) -> None:
  14. self.model_id = 'damo/cv_unet_image-matting'
  15. @unittest.skip('deprecated, download model from model hub instead')
  16. def test_run_with_direct_file_download(self):
  17. model_path = 'http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs' \
  18. '.com/data/test/maas/image_matting/matting_person.pb'
  19. with tempfile.TemporaryDirectory() as tmp_dir:
  20. model_file = osp.join(tmp_dir, ModelFile.TF_GRAPH_FILE)
  21. with open(model_file, 'wb') as ofile:
  22. ofile.write(File.read(model_path))
  23. img_matting = pipeline(Tasks.image_matting, model=tmp_dir)
  24. result = img_matting('data/test/images/image_matting.png')
  25. cv2.imwrite('result.png', result['output_png'])
  26. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  27. def test_run_with_dataset(self):
  28. input_location = ['data/test/images/image_matting.png']
  29. # alternatively:
  30. # input_location = '/dir/to/images'
  31. dataset = MsDataset.load(input_location, target='image')
  32. img_matting = pipeline(Tasks.image_matting, model=self.model_id)
  33. # note that for dataset output, the inference-output is a Generator that can be iterated.
  34. result = img_matting(dataset)
  35. cv2.imwrite('result.png', next(result)['output_png'])
  36. print(f'Output written to {osp.abspath("result.png")}')
  37. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  38. def test_run_modelhub(self):
  39. img_matting = pipeline(Tasks.image_matting, model=self.model_id)
  40. result = img_matting('data/test/images/image_matting.png')
  41. cv2.imwrite('result.png', result['output_png'])
  42. print(f'Output written to {osp.abspath("result.png")}')
  43. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  44. def test_run_modelhub_default_model(self):
  45. img_matting = pipeline(Tasks.image_matting)
  46. result = img_matting('data/test/images/image_matting.png')
  47. cv2.imwrite('result.png', result['output_png'])
  48. print(f'Output written to {osp.abspath("result.png")}')
  49. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  50. def test_run_with_modelscope_dataset(self):
  51. dataset = MsDataset.load('beans', split='train', target='image')
  52. img_matting = pipeline(Tasks.image_matting, model=self.model_id)
  53. result = img_matting(dataset)
  54. for i in range(10):
  55. cv2.imwrite(f'result_{i}.png', next(result)['output_png'])
  56. print(
  57. f'Output written to dir: {osp.dirname(osp.abspath("result_0.png"))}'
  58. )
  59. if __name__ == '__main__':
  60. unittest.main()