|
|
@@ -100,7 +100,7 @@ class LabelMe2COCO(X2COCO):
|
|
|
image["height"] = json_info["imageHeight"]
|
|
|
image["width"] = json_info["imageWidth"]
|
|
|
image["id"] = image_id + 1
|
|
|
- image["file_name"] = json_info["imagePath"].split("/")[-1]
|
|
|
+ image["file_name"] = osp.split(json_info["imagePath"])[-1]
|
|
|
return image
|
|
|
|
|
|
def generate_polygon_anns_field(self, height, width,
|
|
|
@@ -144,7 +144,7 @@ class LabelMe2COCO(X2COCO):
|
|
|
img_name_part = osp.splitext(img_file)[0]
|
|
|
json_file = osp.join(json_dir, img_name_part + ".json")
|
|
|
if not osp.exists(json_file):
|
|
|
- os.remove(os.remove(osp.join(image_dir, img_file)))
|
|
|
+ os.remove(osp.join(image_dir, img_file))
|
|
|
continue
|
|
|
image_id = image_id + 1
|
|
|
with open(json_file, mode='r', \
|
|
|
@@ -216,7 +216,7 @@ class EasyData2COCO(X2COCO):
|
|
|
img_name_part = osp.splitext(img_file)[0]
|
|
|
json_file = osp.join(json_dir, img_name_part + ".json")
|
|
|
if not osp.exists(json_file):
|
|
|
- os.remove(os.remove(osp.join(image_dir, img_file)))
|
|
|
+ os.remove(osp.join(image_dir, img_file))
|
|
|
continue
|
|
|
image_id = image_id + 1
|
|
|
with open(json_file, mode='r', \
|
|
|
@@ -255,3 +255,107 @@ class EasyData2COCO(X2COCO):
|
|
|
self.annotations_list.append(
|
|
|
self.generate_polygon_anns_field(points, segmentation, label, image_id, object_id,
|
|
|
label_to_num))
|
|
|
+
|
|
|
+
|
|
|
+class JingLing2COCO(X2COCO):
|
|
|
+ """将使用EasyData标注的检测或分割数据集转换为COCO数据集。
|
|
|
+ """
|
|
|
+ def __init__(self):
|
|
|
+ super(JingLing2COCO, self).__init__()
|
|
|
+
|
|
|
+ def generate_images_field(self, json_info, image_id):
|
|
|
+ image = {}
|
|
|
+ image["height"] = json_info["size"]["height"]
|
|
|
+ image["width"] = json_info["size"]["width"]
|
|
|
+ image["id"] = image_id + 1
|
|
|
+ image["file_name"] = osp.split(json_info["path"])[-1]
|
|
|
+ return image
|
|
|
+
|
|
|
+ def generate_polygon_anns_field(self, height, width,
|
|
|
+ points, label, image_id,
|
|
|
+ object_id, label_to_num):
|
|
|
+ annotation = {}
|
|
|
+ annotation["segmentation"] = [list(np.asarray(points).flatten())]
|
|
|
+ annotation["iscrowd"] = 0
|
|
|
+ annotation["image_id"] = image_id + 1
|
|
|
+ annotation["bbox"] = list(map(float, self.get_bbox(height, width, points)))
|
|
|
+ annotation["area"] = annotation["bbox"][2] * annotation["bbox"][3]
|
|
|
+ annotation["category_id"] = label_to_num[label]
|
|
|
+ annotation["id"] = object_id + 1
|
|
|
+ return annotation
|
|
|
+
|
|
|
+ def get_bbox(self, height, width, points):
|
|
|
+ polygons = points
|
|
|
+ mask = np.zeros([height, width], dtype=np.uint8)
|
|
|
+ mask = PIL.Image.fromarray(mask)
|
|
|
+ xy = list(map(tuple, polygons))
|
|
|
+ PIL.ImageDraw.Draw(mask).polygon(xy=xy, outline=1, fill=1)
|
|
|
+ mask = np.array(mask, dtype=bool)
|
|
|
+ index = np.argwhere(mask == 1)
|
|
|
+ rows = index[:, 0]
|
|
|
+ clos = index[:, 1]
|
|
|
+ left_top_r = np.min(rows)
|
|
|
+ left_top_c = np.min(clos)
|
|
|
+ right_bottom_r = np.max(rows)
|
|
|
+ right_bottom_c = np.max(clos)
|
|
|
+ return [
|
|
|
+ left_top_c, left_top_r, right_bottom_c - left_top_c,
|
|
|
+ right_bottom_r - left_top_r
|
|
|
+ ]
|
|
|
+
|
|
|
+ def parse_json(self, img_dir, json_dir):
|
|
|
+ image_id = -1
|
|
|
+ object_id = -1
|
|
|
+ labels_list = []
|
|
|
+ label_to_num = {}
|
|
|
+ for img_file in os.listdir(img_dir):
|
|
|
+ img_name_part = osp.splitext(img_file)[0]
|
|
|
+ json_file = osp.join(json_dir, img_name_part + ".json")
|
|
|
+ if not osp.exists(json_file):
|
|
|
+ os.remove(osp.join(image_dir, img_file))
|
|
|
+ continue
|
|
|
+ image_id = image_id + 1
|
|
|
+ with open(json_file, mode='r', \
|
|
|
+ encoding=get_encoding(json_file)) as j:
|
|
|
+ json_info = json.load(j)
|
|
|
+ img_info = self.generate_images_field(json_info, image_id)
|
|
|
+ self.images_list.append(img_info)
|
|
|
+ anns_type = "bndbox"
|
|
|
+ for i, obj in enumerate(json_info["outputs"]["object"]):
|
|
|
+ if i == 0:
|
|
|
+ if "polygon" in obj:
|
|
|
+ anns_type = "polygon"
|
|
|
+ else:
|
|
|
+ if anns_type not in obj:
|
|
|
+ continue
|
|
|
+ object_id = object_id + 1
|
|
|
+ label = obj["name"]
|
|
|
+ if label not in labels_list:
|
|
|
+ self.categories_list.append(\
|
|
|
+ self.generate_categories_field(label, labels_list))
|
|
|
+ labels_list.append(label)
|
|
|
+ label_to_num[label] = len(labels_list)
|
|
|
+ if anns_type == "polygon":
|
|
|
+ points = []
|
|
|
+ for j in range(int(len(obj["polygon"]) / 2.0)):
|
|
|
+ points.append([obj["polygon"]["x" + str(j + 1)],
|
|
|
+ obj["polygon"]["y" + str(j + 1)]])
|
|
|
+ self.annotations_list.append(
|
|
|
+ self.generate_polygon_anns_field(json_info["size"]["height"],
|
|
|
+ json_info["size"]["width"],
|
|
|
+ points,
|
|
|
+ label,
|
|
|
+ image_id,
|
|
|
+ object_id,
|
|
|
+ label_to_num))
|
|
|
+ if anns_type == "bndbox":
|
|
|
+ points = []
|
|
|
+ points.append([obj["bndbox"]["xmin"], obj["bndbox"]["ymin"]])
|
|
|
+ points.append([obj["bndbox"]["xmax"], obj["bndbox"]["ymax"]])
|
|
|
+ points.append([obj["bndbox"]["xmin"], obj["bndbox"]["ymax"]])
|
|
|
+ points.append([obj["bndbox"]["xmax"], obj["bndbox"]["ymin"]])
|
|
|
+ self.annotations_list.append(
|
|
|
+ self.generate_rectangle_anns_field(points, label, image_id,
|
|
|
+ object_id, label_to_num))
|
|
|
+
|
|
|
+
|