test_plugins.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import os
  2. from PIL import Image
  3. from sorawm.iopaint.helper import encode_pil_to_base64, gen_frontend_mask
  4. from sorawm.iopaint.plugins.anime_seg import AnimeSeg
  5. from sorawm.iopaint.schema import (
  6. Device,
  7. InteractiveSegModel,
  8. RemoveBGModel,
  9. RunPluginRequest,
  10. )
  11. from sorawm.iopaint.tests.utils import check_device, current_dir, save_dir
  12. os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
  13. import cv2
  14. import pytest
  15. from sorawm.iopaint.plugins import (
  16. GFPGANPlugin,
  17. InteractiveSeg,
  18. RealESRGANUpscaler,
  19. RemoveBG,
  20. RestoreFormerPlugin,
  21. )
  22. img_p = current_dir / "bunny.jpeg"
  23. img_bytes = open(img_p, "rb").read()
  24. bgr_img = cv2.imread(str(img_p))
  25. rgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)
  26. rgb_img_base64 = encode_pil_to_base64(Image.fromarray(rgb_img), 100, {})
  27. bgr_img_base64 = encode_pil_to_base64(Image.fromarray(bgr_img), 100, {})
  28. person_p = current_dir / "image.png"
  29. person_bgr_img = cv2.imread(str(person_p))
  30. person_rgb_img = cv2.cvtColor(person_bgr_img, cv2.COLOR_BGR2RGB)
  31. person_rgb_img = cv2.resize(person_rgb_img, (512, 512))
  32. def _save(img, name):
  33. name = name.replace("/", "_")
  34. cv2.imwrite(str(save_dir / name), img)
  35. @pytest.mark.parametrize("model_name", RemoveBGModel.values())
  36. @pytest.mark.parametrize("device", Device.values())
  37. def test_remove_bg(model_name, device):
  38. check_device(device)
  39. print(f"Testing {model_name} on {device}")
  40. model = RemoveBG(model_name, device)
  41. rgba_np_img = model.gen_image(
  42. rgb_img, RunPluginRequest(name=RemoveBG.name, image=rgb_img_base64)
  43. )
  44. res = cv2.cvtColor(rgba_np_img, cv2.COLOR_RGBA2BGRA)
  45. _save(res, f"test_remove_bg_{model_name}_{device}.png")
  46. bgr_np_img = model.gen_mask(
  47. rgb_img, RunPluginRequest(name=RemoveBG.name, image=rgb_img_base64)
  48. )
  49. res_mask = gen_frontend_mask(bgr_np_img)
  50. _save(res_mask, f"test_remove_bg_frontend_mask_{model_name}_{device}.png")
  51. assert len(bgr_np_img.shape) == 2
  52. _save(bgr_np_img, f"test_remove_bg_mask_{model_name}_{device}.jpeg")
  53. def test_anime_seg():
  54. model = AnimeSeg()
  55. img = cv2.imread(str(current_dir / "anime_test.png"))
  56. img_base64 = encode_pil_to_base64(Image.fromarray(img), 100, {})
  57. res = model.gen_image(img, RunPluginRequest(name=AnimeSeg.name, image=img_base64))
  58. assert len(res.shape) == 3
  59. assert res.shape[-1] == 4
  60. _save(res, "test_anime_seg.png")
  61. res = model.gen_mask(img, RunPluginRequest(name=AnimeSeg.name, image=img_base64))
  62. assert len(res.shape) == 2
  63. _save(res, "test_anime_seg_mask.png")
  64. @pytest.mark.parametrize("device", ["cuda", "cpu", "mps"])
  65. def test_upscale(device):
  66. check_device(device)
  67. model = RealESRGANUpscaler("realesr-general-x4v3", device)
  68. res = model.gen_image(
  69. rgb_img,
  70. RunPluginRequest(name=RealESRGANUpscaler.name, image=rgb_img_base64, scale=2),
  71. )
  72. _save(res, f"test_upscale_x2_{device}.png")
  73. res = model.gen_image(
  74. rgb_img,
  75. RunPluginRequest(name=RealESRGANUpscaler.name, image=rgb_img_base64, scale=4),
  76. )
  77. _save(res, f"test_upscale_x4_{device}.png")
  78. @pytest.mark.parametrize("device", ["cuda", "cpu", "mps"])
  79. def test_gfpgan(device):
  80. check_device(device)
  81. model = GFPGANPlugin(device)
  82. res = model.gen_image(
  83. person_rgb_img, RunPluginRequest(name=GFPGANPlugin.name, image=rgb_img_base64)
  84. )
  85. _save(res, f"test_gfpgan_{device}.png")
  86. @pytest.mark.parametrize("device", ["cuda", "cpu", "mps"])
  87. def test_restoreformer(device):
  88. check_device(device)
  89. model = RestoreFormerPlugin(device)
  90. res = model.gen_image(
  91. person_rgb_img,
  92. RunPluginRequest(name=RestoreFormerPlugin.name, image=rgb_img_base64),
  93. )
  94. _save(res, f"test_restoreformer_{device}.png")
  95. @pytest.mark.parametrize("name", InteractiveSegModel.values())
  96. @pytest.mark.parametrize("device", ["cuda", "cpu", "mps"])
  97. def test_segment_anything(name, device):
  98. check_device(device)
  99. model = InteractiveSeg(name, device)
  100. new_mask = model.gen_mask(
  101. rgb_img,
  102. RunPluginRequest(
  103. name=InteractiveSeg.name,
  104. image=rgb_img_base64,
  105. clicks=([[448 // 2, 394 // 2, 1]]),
  106. ),
  107. )
  108. save_name = f"test_segment_anything_{name}_{device}.png"
  109. _save(new_mask, save_name)