|
3 | 3 | import json |
4 | 4 | from typing import List |
5 | 5 | from logging import getLogger |
6 | | -from PIL import Image, ImageDraw |
| 6 | +from PIL import Image |
| 7 | +import cv2 |
| 8 | +import numpy as np |
7 | 9 |
|
8 | 10 | import xmltodict |
9 | 11 |
|
@@ -653,35 +655,37 @@ def export_semantic_segmentation(self, tasks: list, output_dir: str = os.path.jo |
653 | 655 |
|
654 | 656 | def __export_index_color_image(self, task: list, output_dir: str, pallete: List[int], is_instance_segmentation: bool = True, classes: list = []) -> None: |
655 | 657 | image = Image.new("RGB", (task["width"], task["height"]), 0) |
656 | | - image = image.convert('P') |
657 | | - image.putpalette(pallete) |
658 | | - draw = ImageDraw.Draw(image) |
| 658 | + image = np.array(image) |
| 659 | + image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
659 | 660 |
|
660 | 661 | index = 1 |
661 | 662 | for annotation in task["annotations"]: |
662 | 663 | color = index if is_instance_segmentation else classes.index(annotation["value"]) + 1 |
663 | 664 | if annotation["type"] == AnnotationType.segmentation.value: |
664 | 665 | for region in annotation["points"]: |
665 | 666 | for points in region: |
666 | | - pillow_draw_points = self.__get_pillow_draw_points(points) |
667 | | - draw.polygon(pillow_draw_points, fill=color) |
| 667 | + cv_draw_points = self.__get_cv_draw_points(points) |
| 668 | + cv2.fillPoly(image, [cv_draw_points], color, lineType=cv2.LINE_8, shift=0) |
668 | 669 | # hollowd points are not supported |
669 | 670 | break |
670 | 671 | elif annotation["type"] == AnnotationType.polygon.value: |
671 | | - pillow_draw_points = self.__get_pillow_draw_points(annotation["points"]) |
672 | | - draw.polygon(pillow_draw_points, fill=color) |
| 672 | + cv_draw_points = self.__get_cv_draw_points(annotation["points"]) |
| 673 | + cv2.fillPoly(image, [cv_draw_points], color, lineType=cv2.LINE_8, shift=0) |
673 | 674 | elif annotation["type"] == AnnotationType.bbox.value: |
674 | | - pillow_draw_points = self.__get_pillow_draw_points(annotation["points"]) |
675 | | - draw.polygon(pillow_draw_points, fill=color) |
| 675 | + cv_draw_points = self.__get_cv_draw_points(annotation["points"]) |
| 676 | + cv2.fillPoly(image, [cv_draw_points], color, lineType=cv2.LINE_8, shift=0) |
676 | 677 | else: |
677 | 678 | continue |
678 | 679 | index += 1 |
679 | 680 |
|
680 | 681 | image_path = os.path.join(output_dir, utils.get_basename(task["name"]) + ".png") |
681 | 682 | os.makedirs(os.path.dirname(image_path), exist_ok=True) |
| 683 | + image = Image.fromarray(image) |
| 684 | + image = image.convert('P') |
| 685 | + image.putpalette(pallete) |
682 | 686 | image.save(image_path) |
683 | 687 |
|
684 | | - def __get_pillow_draw_points(self, points: List[int]) -> List[int]: |
| 688 | + def __get_cv_draw_points(self, points: List[int]) -> List[int]: |
685 | 689 | """ |
686 | 690 | Convert points to pillow draw points. Diagonal points are not supported. |
687 | 691 | """ |
@@ -717,7 +721,11 @@ def __get_pillow_draw_points(self, points: List[int]) -> List[int]: |
717 | 721 | for i in range(int(len(points) / 2)): |
718 | 722 | new_points.append(x_points[i * 2]) |
719 | 723 | new_points.append(y_points[i * 2 + 1]) |
720 | | - return new_points |
| 724 | + |
| 725 | + cv_points = [] |
| 726 | + for i in range(int(len(new_points) / 2)): |
| 727 | + cv_points.append((new_points[i * 2], new_points[i * 2 + 1])) |
| 728 | + return np.array(cv_points) |
721 | 729 |
|
722 | 730 |
|
723 | 731 | # Annotation |
|
0 commit comments