2022-09-20 17:49:31 +08:00
|
|
|
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
|
|
|
|
2022-08-24 15:05:16 +08:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
import cv2
|
|
|
|
|
import PIL
|
|
|
|
|
|
|
|
|
|
from modelscope.outputs import OutputKeys
|
|
|
|
|
from modelscope.pipelines import pipeline
|
|
|
|
|
from modelscope.utils.constant import Tasks
|
|
|
|
|
from modelscope.utils.cv.image_utils import panoptic_seg_masks_to_image
|
|
|
|
|
from modelscope.utils.test_utils import test_level
|
|
|
|
|
|
|
|
|
|
|
2023-05-14 23:41:40 +08:00
|
|
|
class ImagePanopticSegmentationTest(unittest.TestCase):
|
2022-09-08 14:08:51 +08:00
|
|
|
|
|
|
|
|
def setUp(self) -> None:
|
|
|
|
|
self.task = Tasks.image_segmentation
|
|
|
|
|
self.model_id = 'damo/cv_swinL_panoptic-segmentation_cocopan'
|
2022-08-24 15:05:16 +08:00
|
|
|
|
2023-06-28 14:15:48 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
2022-08-24 15:05:16 +08:00
|
|
|
def test_image_panoptic_segmentation(self):
|
|
|
|
|
input_location = 'data/test/images/image_panoptic_segmentation.jpg'
|
2022-09-08 14:08:51 +08:00
|
|
|
pan_segmentor = pipeline(Tasks.image_segmentation, model=self.model_id)
|
2022-08-24 15:05:16 +08:00
|
|
|
result = pan_segmentor(input_location)
|
|
|
|
|
|
|
|
|
|
draw_img = panoptic_seg_masks_to_image(result[OutputKeys.MASKS])
|
|
|
|
|
cv2.imwrite('result.jpg', draw_img)
|
|
|
|
|
print('print test_image_panoptic_segmentation return success')
|
|
|
|
|
|
2023-06-28 14:15:48 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
2022-08-24 15:05:16 +08:00
|
|
|
def test_image_panoptic_segmentation_from_PIL(self):
|
|
|
|
|
input_location = 'data/test/images/image_panoptic_segmentation.jpg'
|
2022-09-08 14:08:51 +08:00
|
|
|
pan_segmentor = pipeline(Tasks.image_segmentation, model=self.model_id)
|
2022-08-24 15:05:16 +08:00
|
|
|
PIL_array = PIL.Image.open(input_location)
|
|
|
|
|
result = pan_segmentor(PIL_array)
|
|
|
|
|
|
|
|
|
|
draw_img = panoptic_seg_masks_to_image(result[OutputKeys.MASKS])
|
|
|
|
|
cv2.imwrite('result.jpg', draw_img)
|
|
|
|
|
print('print test_image_panoptic_segmentation from PIL return success')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|