ivder / LabelMeYoloConverter

Convert LabelMe Annotation Tool JSON format to YOLO text file format
56 stars 17 forks source link

Read json file error #3

Open tcotte opened 2 years ago

tcotte commented 2 years ago

I had an issue reading the JSON file. I think it isn't a good idea to use lists to read each lines in this case. Therefore I changed the code and I suggest you to prefer dictionary research to retrieve the points (also it is less expensive because it is not necessary to read the picture).

In my case, I use rectangle bounding boxes and just one detection class :

with open(txt_path) as json_file:
        data = json.load(json_file)

        w = data["imageWidth"]
        h = data["imageHeight"]

        for idx, shapes in enumerate(data["shapes"]):
            x1 = shapes["points"][0][0]
            y1 = shapes["points"][0][1]
            x2 = shapes["points"][1][0]
            y2 = shapes["points"][1][1]

            cls = str(0) # just one class in my object detection project

            xmin = min(x1, x2)
            xmax = max(x1, x2)
            ymin = min(y1, y2)
            ymax = max(y1, y2)

            b = (xmin, xmax, ymin, ymax)
            bb = convert((w, h), b)

            txt_outfile.write(cls + " " + " ".join([str(a) for a in bb]) + '\n')
visittor commented 6 months ago

Agreed, using standard library is much more straightforward and easier to debug and modify than reading a file line-by-line.