You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.9 KiB
111 lines
3.9 KiB
3 years ago
|
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||
|
#
|
||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
# you may not use this file except in compliance with the License.
|
||
|
# You may obtain a copy of the License at
|
||
|
#
|
||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
|
#
|
||
|
# Unless required by applicable law or agreed to in writing, software
|
||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
# See the License for the specific language governing permissions and
|
||
|
# limitations under the License.
|
||
|
|
||
3 years ago
|
import os
|
||
|
import os.path as osp
|
||
|
import shutil
|
||
|
import cv2
|
||
|
import numpy as np
|
||
|
import json
|
||
|
import argparse
|
||
|
import glob
|
||
|
from tqdm import tqdm
|
||
|
from PIL import Image
|
||
|
from collections import defaultdict
|
||
3 years ago
|
from utils import Timer
|
||
3 years ago
|
|
||
|
|
||
|
def _mkdir_p(path):
|
||
|
if not osp.exists(path):
|
||
|
os.makedirs(path)
|
||
|
|
||
|
|
||
|
def _save_palette(label, save_path):
|
||
|
bin_colormap = np.ones((256, 3)) * 255
|
||
|
bin_colormap[0, :] = [0, 0, 0]
|
||
|
bin_colormap = bin_colormap.astype(np.uint8)
|
||
3 years ago
|
visualimg = Image.fromarray(label, "P")
|
||
3 years ago
|
palette = bin_colormap
|
||
3 years ago
|
visualimg.putpalette(palette)
|
||
3 years ago
|
visualimg.save(save_path, format='PNG')
|
||
|
|
||
|
|
||
3 years ago
|
def _save_mask(annotation, image_size, save_path):
|
||
|
mask = np.zeros(image_size, dtype=np.int32)
|
||
3 years ago
|
for contour_points in annotation:
|
||
|
contour_points = np.array(contour_points).reshape((-1, 2))
|
||
3 years ago
|
contour_points = np.round(contour_points).astype(np.int32)[
|
||
|
np.newaxis, :]
|
||
3 years ago
|
cv2.fillPoly(mask, contour_points, 1)
|
||
|
_save_palette(mask.astype("uint8"), save_path)
|
||
|
|
||
|
|
||
|
def _read_geojson(json_path):
|
||
|
with open(json_path, "r") as f:
|
||
|
jsoner = json.load(f)
|
||
|
imgs = jsoner["images"]
|
||
|
images = defaultdict(list)
|
||
3 years ago
|
sizes = defaultdict(list)
|
||
3 years ago
|
for img in imgs:
|
||
|
images[img["id"]] = img["file_name"]
|
||
3 years ago
|
sizes[img["file_name"]] = (img["height"], img["width"])
|
||
3 years ago
|
anns = jsoner["annotations"]
|
||
|
annotations = defaultdict(list)
|
||
|
for ann in anns:
|
||
|
annotations[images[ann["image_id"]]].append(ann["segmentation"])
|
||
3 years ago
|
return annotations, sizes
|
||
3 years ago
|
|
||
|
|
||
3 years ago
|
@Timer
|
||
3 years ago
|
def convert_data(raw_folder, end_folder):
|
||
3 years ago
|
print("-- Initializing --")
|
||
|
img_folder = osp.join(raw_folder, "images")
|
||
|
save_img_folder = osp.join(end_folder, "img")
|
||
|
save_lab_folder = osp.join(end_folder, "gt")
|
||
|
_mkdir_p(save_img_folder)
|
||
|
_mkdir_p(save_lab_folder)
|
||
|
names = os.listdir(img_folder)
|
||
|
print("-- Loading annotations --")
|
||
|
anns = {}
|
||
3 years ago
|
sizes = {}
|
||
3 years ago
|
jsons = glob.glob(osp.join(raw_folder, "*.json"))
|
||
|
for json in jsons:
|
||
3 years ago
|
j_ann, j_size = _read_geojson(json)
|
||
|
anns.update(j_ann)
|
||
|
sizes.update(j_size)
|
||
3 years ago
|
print("-- Converting datas --")
|
||
|
for k in tqdm(names):
|
||
3 years ago
|
# for k in tqdm(anns.keys()):
|
||
3 years ago
|
img_path = osp.join(img_folder, k)
|
||
|
img_save_path = osp.join(save_img_folder, k)
|
||
|
ext = "." + k.split(".")[-1]
|
||
|
lab_save_path = osp.join(save_lab_folder, k.replace(ext, ".png"))
|
||
|
shutil.copy(img_path, img_save_path)
|
||
|
if k in anns.keys():
|
||
3 years ago
|
_save_mask(anns[k], sizes[k], lab_save_path)
|
||
3 years ago
|
else: # have not anns
|
||
3 years ago
|
_save_palette(np.zeros(sizes[k], dtype="uint8"), \
|
||
3 years ago
|
lab_save_path)
|
||
|
|
||
|
|
||
|
parser = argparse.ArgumentParser(description="input parameters")
|
||
|
parser.add_argument("--raw_folder", type=str, required=True, \
|
||
|
help="The folder path about original data, where `images` saves the original image, `annotation.json` saves the corresponding annotation information.")
|
||
|
parser.add_argument("--save_folder", type=str, required=True, \
|
||
3 years ago
|
help="The folder path to save the results, where `img` saves the image and `gt` saves the label.")
|
||
3 years ago
|
|
||
|
if __name__ == "__main__":
|
||
|
args = parser.parse_args()
|
||
3 years ago
|
convert_data(args.raw_folder, args.save_folder)
|