opencv2.py 744 B

12345678910111213141516171819202122232425262728293031
  1. import cv2
  2. from sorawm.iopaint.schema import InpaintRequest
  3. from .base import InpaintModel
  4. flag_map = {"INPAINT_NS": cv2.INPAINT_NS, "INPAINT_TELEA": cv2.INPAINT_TELEA}
  5. class OpenCV2(InpaintModel):
  6. name = "cv2"
  7. pad_mod = 1
  8. is_erase_model = True
  9. @staticmethod
  10. def is_downloaded() -> bool:
  11. return True
  12. def forward(self, image, mask, config: InpaintRequest):
  13. """Input image and output image have same size
  14. image: [H, W, C] RGB
  15. mask: [H, W, 1]
  16. return: BGR IMAGE
  17. """
  18. cur_res = cv2.inpaint(
  19. image[:, :, ::-1],
  20. mask,
  21. inpaintRadius=config.cv2_radius,
  22. flags=flag_map[config.cv2_flag],
  23. )
  24. return cur_res