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 2.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import os.path as osp
  3. import unittest
  4. import cv2
  5. from modelscope.msdatasets import MsDataset
  6. from modelscope.outputs import OutputKeys
  7. from modelscope.pipelines import pipeline
  8. from modelscope.utils.constant import ModelFile, Tasks
  9. from modelscope.utils.demo_utils import DemoCompatibilityCheck
  10. from modelscope.utils.test_utils import test_level
  11. class ImageMattingTest(unittest.TestCase, DemoCompatibilityCheck):
  12. def setUp(self) -> None:
  13. self.model_id = 'damo/cv_unet_image-matting'
  14. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  15. def test_run_with_dataset(self):
  16. input_location = ['data/test/images/image_matting.png']
  17. # alternatively:
  18. # input_location = '/dir/to/images'
  19. dataset = MsDataset.load(input_location, target='image')
  20. img_matting = pipeline(Tasks.portrait_matting, model=self.model_id)
  21. # note that for dataset output, the inference-output is a Generator that can be iterated.
  22. result = img_matting(dataset)
  23. cv2.imwrite('result.png', next(result)[OutputKeys.OUTPUT_IMG])
  24. print(f'Output written to {osp.abspath("result.png")}')
  25. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  26. def test_run_modelhub(self):
  27. img_matting = pipeline(Tasks.portrait_matting, model=self.model_id)
  28. result = img_matting('data/test/images/image_matting.png')
  29. cv2.imwrite('result.png', result[OutputKeys.OUTPUT_IMG])
  30. print(f'Output written to {osp.abspath("result.png")}')
  31. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  32. def test_run_modelhub_default_model(self):
  33. img_matting = pipeline(Tasks.portrait_matting)
  34. result = img_matting('data/test/images/image_matting.png')
  35. cv2.imwrite('result.png', result[OutputKeys.OUTPUT_IMG])
  36. print(f'Output written to {osp.abspath("result.png")}')
  37. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  38. def test_run_with_modelscope_dataset(self):
  39. dataset = MsDataset.load(
  40. 'fixtures_image_utils',
  41. namespace='damotest',
  42. split='test',
  43. target='file')
  44. img_matting = pipeline(Tasks.portrait_matting, model=self.model_id)
  45. result = img_matting(dataset)
  46. for i in range(2):
  47. cv2.imwrite(f'result_{i}.png', next(result)[OutputKeys.OUTPUT_IMG])
  48. print(
  49. f'Output written to dir: {osp.dirname(osp.abspath("result_0.png"))}'
  50. )
  51. @unittest.skip('demo compatibility test is only enabled on a needed-basis')
  52. def test_demo_compatibility(self):
  53. self.compatibility_check()
  54. if __name__ == '__main__':
  55. unittest.main()