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_semantic_segmentation.py 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import unittest
  3. import cv2
  4. import PIL
  5. from modelscope.outputs import OutputKeys
  6. from modelscope.pipelines import pipeline
  7. from modelscope.utils.constant import Tasks
  8. from modelscope.utils.cv.image_utils import semantic_seg_masks_to_image
  9. from modelscope.utils.demo_utils import DemoCompatibilityCheck
  10. from modelscope.utils.test_utils import test_level
  11. class ImageSemanticSegmentationTest(unittest.TestCase, DemoCompatibilityCheck):
  12. def setUp(self) -> None:
  13. self.task = 'image-segmentation'
  14. self.model_id = 'damo/cv_swinL_semantic-segmentation_cocopanmerge'
  15. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  16. def test_image_semantic_segmentation_panmerge(self):
  17. input_location = 'data/test/images/image_semantic_segmentation.jpg'
  18. segmenter = pipeline(Tasks.image_segmentation, model=self.model_id)
  19. result = segmenter(input_location)
  20. draw_img = semantic_seg_masks_to_image(result[OutputKeys.MASKS])
  21. cv2.imwrite('result.jpg', draw_img)
  22. print('test_image_semantic_segmentation_panmerge DONE')
  23. PIL_array = PIL.Image.open(input_location)
  24. result = segmenter(PIL_array)
  25. draw_img = semantic_seg_masks_to_image(result[OutputKeys.MASKS])
  26. cv2.imwrite('result.jpg', draw_img)
  27. print('test_image_semantic_segmentation_panmerge_from_PIL DONE')
  28. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  29. def test_image_semantic_segmentation_vitadapter(self):
  30. model_id = 'damo/cv_vitadapter_semantic-segmentation_cocostuff164k'
  31. input_location = 'data/test/images/image_semantic_segmentation.jpg'
  32. segmenter = pipeline(Tasks.image_segmentation, model=model_id)
  33. result = segmenter(input_location)
  34. draw_img = semantic_seg_masks_to_image(result[OutputKeys.MASKS])
  35. cv2.imwrite('result.jpg', draw_img)
  36. print('test_image_semantic_segmentation_vitadapter DONE')
  37. PIL_array = PIL.Image.open(input_location)
  38. result = segmenter(PIL_array)
  39. draw_img = semantic_seg_masks_to_image(result[OutputKeys.MASKS])
  40. cv2.imwrite('result.jpg', draw_img)
  41. print('test_image_semantic_segmentation_vitadapter_from_PIL DONE')
  42. @unittest.skip('demo compatibility test is only enabled on a needed-basis')
  43. def test_demo_compatibility(self):
  44. self.compatibility_check()
  45. if __name__ == '__main__':
  46. unittest.main()