2023-02-28 13:39:12 +08:00
|
|
|
# Copyright (c) Alibaba, Inc. and its affiliates.
|
2023-06-26 11:23:10 +08:00
|
|
|
import random
|
2023-02-28 13:39:12 +08:00
|
|
|
import tempfile
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
import cv2
|
|
|
|
|
|
|
|
|
|
from modelscope.hub.snapshot_download import snapshot_download
|
|
|
|
|
from modelscope.models import Model
|
|
|
|
|
from modelscope.outputs import OutputKeys
|
|
|
|
|
from modelscope.pipelines import pipeline
|
|
|
|
|
from modelscope.pipelines.cv import ControllableImageGenerationPipeline
|
|
|
|
|
from modelscope.utils.constant import Tasks
|
|
|
|
|
from modelscope.utils.test_utils import test_level
|
|
|
|
|
|
|
|
|
|
|
2023-05-22 10:53:18 +08:00
|
|
|
class ControllableImageGenerationTest(unittest.TestCase):
|
2023-02-28 13:39:12 +08:00
|
|
|
|
|
|
|
|
def setUp(self) -> None:
|
|
|
|
|
self.task = Tasks.controllable_image_generation
|
|
|
|
|
self.model_id = 'dienstag/cv_controlnet_controllable-image-generation_nine-annotators'
|
|
|
|
|
self.input = {
|
|
|
|
|
'image':
|
|
|
|
|
'data/test/images/image_inpainting/image_inpainting_mask_1.png',
|
|
|
|
|
'prompt': 'flower'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
|
|
|
|
def test_run_with_model_from_modelhub(self):
|
|
|
|
|
output_image_path = tempfile.NamedTemporaryFile(suffix='.png').name
|
2023-06-26 11:23:10 +08:00
|
|
|
control_types = [
|
|
|
|
|
'canny', 'hough', 'hed', 'depth', 'normal', 'pose', 'seg',
|
|
|
|
|
'fake_scribble', 'scribble'
|
|
|
|
|
]
|
|
|
|
|
control_type = random.choice(control_types)
|
2023-02-28 13:39:12 +08:00
|
|
|
pipeline_ins = pipeline(
|
2023-06-26 11:23:10 +08:00
|
|
|
self.task, model=self.model_id, control_type=control_type)
|
2023-02-28 13:39:12 +08:00
|
|
|
output = pipeline_ins(input=self.input)[OutputKeys.OUTPUT_IMG]
|
|
|
|
|
cv2.imwrite(output_image_path, output)
|
|
|
|
|
print(
|
|
|
|
|
'pipeline: the output image path is {}'.format(output_image_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|