Spaces:
Runtime error
Runtime error
Download image_segmentor.py from limingcv/ControlNet-Plus-Plus: direct link, hf CLI and curl.
- Browser
- Download file 1.48 kB
-
https://huggingface.co/spaces/limingcv/ControlNet-Plus-Plus/resolve/b15958c61898732b3e1f0121406e31b1856a1784/image_segmentor.py
- Command line
-
hf download hf://spaces/limingcv/ControlNet-Plus-Plus@b15958c61898732b3e1f0121406e31b1856a1784/image_segmentor.py
-
curl -L -o image_segmentor.py https://huggingface.co/spaces/limingcv/ControlNet-Plus-Plus/resolve/b15958c61898732b3e1f0121406e31b1856a1784/image_segmentor.py
1.48 kB
| import cv2 | |
| import numpy as np | |
| import PIL.Image | |
| import torch | |
| from controlnet_aux.util import HWC3, ade_palette | |
| from transformers import AutoImageProcessor, UperNetForSemanticSegmentation | |
| from cv_utils import resize_image | |
| class ImageSegmentor: | |
| def __init__(self): | |
| self.image_processor = AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-small") | |
| self.image_segmentor = UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-small") | |
| def __call__(self, image: np.ndarray, **kwargs) -> PIL.Image.Image: | |
| detect_resolution = kwargs.pop("detect_resolution", 512) | |
| image_resolution = kwargs.pop("image_resolution", 512) | |
| image = HWC3(image) | |
| image = resize_image(image, resolution=detect_resolution) | |
| image = PIL.Image.fromarray(image) | |
| pixel_values = self.image_processor(image, return_tensors="pt").pixel_values | |
| outputs = self.image_segmentor(pixel_values) | |
| seg = self.image_processor.post_process_semantic_segmentation(outputs, target_sizes=[image.size[::-1]])[0] | |
| color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8) | |
| for label, color in enumerate(ade_palette()): | |
| color_seg[seg == label, :] = color | |
| color_seg = color_seg.astype(np.uint8) | |
| color_seg = resize_image(color_seg, resolution=image_resolution, interpolation=cv2.INTER_NEAREST) | |
| return PIL.Image.fromarray(color_seg) | |