浏览代码

modify the labelme2coco.py

sunyanfang01 5 年之前
父节点
当前提交
c6252839c7
共有 2 个文件被更改,包括 7 次插入7 次删除
  1. 2 1
      docs/data/annotation.md
  2. 5 6
      paddlex/tools/x2coco.py

+ 2 - 1
docs/data/annotation.md

@@ -36,4 +36,5 @@ paddlex --data_conversion --source labelme --to PascalVOC --pics ./pics --annota
 | --pics | 指定原图所在的目录路径  |
 | --annotations | 指定标注文件所在的目录路径 |
 
-**注意**:精灵标注的目标检测数据可以在工具内部导出为PascalVOC格式,因此paddlex未提供精灵标注数据到PascalVOC格式的转换
+**注意**:1. 精灵标注的目标检测数据可以在工具内部导出为PascalVOC格式,因此paddlex未提供精灵标注数据到PascalVOC格式的转换
+         2. 在将LabelMe数据集转换为COCO数据集时,LabelMe的图像文件名和json文件名需要一一对应,才可正确转换

+ 5 - 6
paddlex/tools/x2coco.py

@@ -96,12 +96,13 @@ class LabelMe2COCO(X2COCO):
     def __init__(self):
         super(LabelMe2COCO, self).__init__()
         
-    def generate_images_field(self, json_info, image_id):
+    def generate_images_field(self, json_info, image_file, image_id):
         image = {}
         image["height"] = json_info["imageHeight"]
         image["width"] = json_info["imageWidth"]
         image["id"] = image_id + 1
-        json_info["imagePath"] = path_normalization(json_info["imagePath"])
+        json_img_path = path_normalization(json_info["imagePath"])
+        json_info["imagePath"] = osp.join(osp.split(json_img_path)[0], image_file)
         image["file_name"] = osp.split(json_info["imagePath"])[-1]
         return image
     
@@ -152,7 +153,7 @@ class LabelMe2COCO(X2COCO):
             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)
+                img_info = self.generate_images_field(json_info, img_file, image_id)
                 self.images_list.append(img_info)
                 for shapes in json_info["shapes"]:
                     object_id = object_id + 1
@@ -360,6 +361,4 @@ class JingLing2COCO(X2COCO):
                         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))
-                        
-                        
+                                                  object_id, label_to_num))