Răsfoiți Sursa

fix conflicts

FlyingQianMM 5 ani în urmă
părinte
comite
9fe2815351
3 a modificat fișierele cu 86 adăugiri și 24 ștergeri
  1. 13 0
      paddlex/command.py
  2. 3 1
      paddlex/tools/x2coco.py
  3. 70 23
      paddlex/tools/x2voc.py

+ 13 - 0
paddlex/command.py

@@ -15,6 +15,7 @@
 from six import text_type as _text_type
 import argparse
 import sys
+import os
 import os.path as osp
 import paddlex.utils.logging as logging
 
@@ -176,6 +177,16 @@ def main():
         assert args.pics is not None, "--pics should be defined to confirm the pictures path"
         assert args.annotations is not None, "--annotations should be defined to confirm the annotations path"
         assert args.save_dir is not None, "--save_dir should be defined to store taregt dataset"
+        if args.source not in ['labelme', 'jingling', 'easydata']:
+            logging.error(
+                "The source format {} is not one of labelme/jingling/easydata".
+                format(args.source),
+                exit=False)
+        if args.to not in ['PascalVOC', 'MSCOCO', 'SEG', 'ImageNet']:
+            logging.error(
+                "The to format {} is not one of PascalVOC/MSCOCO/SEG/ImageNet".
+                format(args.to),
+                exit=False)
         if args.source == 'labelme' and args.to == 'ImageNet':
             logging.error(
                 "The labelme dataset can not convert to the ImageNet dataset.",
@@ -184,6 +195,8 @@ def main():
             logging.error(
                 "The jingling dataset can not convert to the PascalVOC dataset.",
                 exit=False)
+        if not osp.exists(args.save_dir):
+            os.makedirs(args.save_dir)
         pdx.tools.convert.dataset_conversion(args.source, args.to, args.pics,
                                              args.annotations, args.save_dir)
 

+ 3 - 1
paddlex/tools/x2coco.py

@@ -87,7 +87,9 @@ class X2COCO(object):
         coco_data["categories"] = self.categories_list
         coco_data["annotations"] = self.annotations_list
         json_path = osp.join(dataset_save_dir, "annotations.json")
-        json.dump(coco_data, open(json_path, "w"), indent=4, cls=MyEncoder)
+        f = open(json_path, "w")
+        json.dump(coco_data, f, indent=4, cls=MyEncoder)
+        f.close()
 
 
 class LabelMe2COCO(X2COCO):

+ 70 - 23
paddlex/tools/x2voc.py

@@ -22,10 +22,24 @@ import shutil
 import numpy as np
 from .base import MyEncoder, is_pic, get_encoding
 
+ch2en = {
+    u'不导电': 'bu_dao_dian',
+    u'擦花': 'ca_hua',
+    u'角位漏底': 'jiao_wei_lou_di',
+    u'桔皮': 'ju_pi',
+    u'漏底': 'lou_di',
+    u'喷流': 'pen_liu',
+    u'起坑': 'qi_keng',
+    u'漆泡': 'qi_pao',
+    u'杂色': 'za_se',
+    u'脏点': 'zang_dian'
+}
+
+
 class X2VOC(object):
     def __init__(self):
         pass
-    
+
     def convert(self, image_dir, json_dir, dataset_save_dir):
         """转换。
         Args:
@@ -44,32 +58,37 @@ class X2VOC(object):
         for img_name in os.listdir(image_dir):
             if is_pic(img_name):
                 shutil.copyfile(
-                            osp.join(image_dir, img_name),
-                            osp.join(new_image_dir, img_name))
+                    osp.join(image_dir, img_name),
+                    osp.join(new_image_dir, img_name))
         # Convert the json files.
         xml_dir = osp.join(dataset_save_dir, "Annotations")
         if osp.exists(xml_dir):
             shutil.rmtree(xml_dir)
         os.makedirs(xml_dir)
         self.json2xml(new_image_dir, json_dir, xml_dir)
-        
-        
+
+
 class LabelMe2VOC(X2VOC):
     """将使用LabelMe标注的数据集转换为VOC数据集。
     """
+
     def __init__(self):
         pass
-    
+
     def json2xml(self, image_dir, json_dir, xml_dir):
         import xml.dom.minidom as minidom
+        i = 0
+        print('length: ', len(os.listdir(image_dir)))
         for img_name in os.listdir(image_dir):
             img_name_part = osp.splitext(img_name)[0]
             json_file = osp.join(json_dir, img_name_part + ".json")
+            i += 1
+            print(i, " ", img_name)
             if not osp.exists(json_file):
                 os.remove(os.remove(osp.join(image_dir, img_name)))
                 continue
-            xml_doc = minidom.Document() 
-            root = xml_doc.createElement("annotation") 
+            xml_doc = minidom.Document()
+            root = xml_doc.createElement("annotation")
             xml_doc.appendChild(root)
             node_folder = xml_doc.createElement("folder")
             node_folder.appendChild(xml_doc.createTextNode("JPEGImages"))
@@ -77,11 +96,17 @@ class LabelMe2VOC(X2VOC):
             node_filename = xml_doc.createElement("filename")
             node_filename.appendChild(xml_doc.createTextNode(img_name))
             root.appendChild(node_filename)
+            print(i, " ", json_file)
             with open(json_file, mode="r", \
                               encoding=get_encoding(json_file)) as j:
                 json_info = json.load(j)
-                h = json_info["imageHeight"]
-                w = json_info["imageWidth"]
+                if 'imageHeight' in json_info and 'imageWidth' in json_info:
+                    h = json_info["imageHeight"]
+                    w = json_info["imageWidth"]
+                else:
+                    img_file = osp.join(image_dir, img_name)
+                    im_data = cv2.imread(img_file)
+                    h, w, c = im_data.shape
                 node_size = xml_doc.createElement("size")
                 node_width = xml_doc.createElement("width")
                 node_width.appendChild(xml_doc.createTextNode(str(w)))
@@ -94,12 +119,24 @@ class LabelMe2VOC(X2VOC):
                 node_size.appendChild(node_depth)
                 root.appendChild(node_size)
                 for shape in json_info["shapes"]:
-                    if shape["shape_type"] != "rectangle":
-                        continue
+                    if 'shape_type' in shape:
+                        if shape["shape_type"] != "rectangle":
+                            continue
+                        (xmin, ymin), (xmax, ymax) = shape["points"]
+                        xmin, xmax = sorted([xmin, xmax])
+                        ymin, ymax = sorted([ymin, ymax])
+                    else:
+                        points = shape["points"]
+                        points_num = len(points)
+                        x = [points[i][0] for i in range(points_num)]
+                        y = [points[i][1] for i in range(points_num)]
+                        xmin = min(x)
+                        xmax = max(x)
+                        ymin = min(y)
+                        ymax = max(y)
                     label = shape["label"]
-                    (xmin, ymin), (xmax, ymax) = shape["points"]
-                    xmin, xmax = sorted([xmin, xmax])
-                    ymin, ymax = sorted([ymin, ymax])
+                    label = ch2en[label]
+                    #print(label)
                     node_obj = xml_doc.createElement("object")
                     node_name = xml_doc.createElement("name")
                     node_name.appendChild(xml_doc.createTextNode(label))
@@ -123,15 +160,21 @@ class LabelMe2VOC(X2VOC):
                     node_obj.appendChild(node_box)
                     root.appendChild(node_obj)
             with open(osp.join(xml_dir, img_name_part + ".xml"), 'w') as fxml:
-                xml_doc.writexml(fxml, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
-                    
-                    
+                xml_doc.writexml(
+                    fxml,
+                    indent='\t',
+                    addindent='\t',
+                    newl='\n',
+                    encoding="utf-8")
+
+
 class EasyData2VOC(X2VOC):
     """将使用EasyData标注的分割数据集转换为VOC数据集。
     """
+
     def __init__(self):
         pass
-    
+
     def json2xml(self, image_dir, json_dir, xml_dir):
         import xml.dom.minidom as minidom
         for img_name in os.listdir(image_dir):
@@ -140,8 +183,8 @@ class EasyData2VOC(X2VOC):
             if not osp.exists(json_file):
                 os.remove(os.remove(osp.join(image_dir, img_name)))
                 continue
-            xml_doc = minidom.Document() 
-            root = xml_doc.createElement("annotation") 
+            xml_doc = minidom.Document()
+            root = xml_doc.createElement("annotation")
             xml_doc.appendChild(root)
             node_folder = xml_doc.createElement("folder")
             node_folder.appendChild(xml_doc.createTextNode("JPEGImages"))
@@ -195,5 +238,9 @@ class EasyData2VOC(X2VOC):
                     node_obj.appendChild(node_box)
                     root.appendChild(node_obj)
             with open(osp.join(xml_dir, img_name_part + ".xml"), 'w') as fxml:
-                xml_doc.writexml(fxml, indent='\t', addindent='\t', newl='\n', encoding="utf-8")                    
-                    
+                xml_doc.writexml(
+                    fxml,
+                    indent='\t',
+                    addindent='\t',
+                    newl='\n',
+                    encoding="utf-8")