Giter Club home page Giter Club logo

Comments (2)

gyupro avatar gyupro commented on July 2, 2024

You should parse yolo format and convert it to labelme format. Maybe something like this, but I never tested

import os
import json
import base64
import io
import numpy as np
from PIL import Image

def read_name_file(name_path):
    with open(name_path, "r") as name_file:
        names = [name.strip() for name in name_file]
    return names

def img_to_b64(img_path):
    with open(img_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

def convert_yolo_to_labelme(yolo_file, image_path, json_name=None):
    if json_name is None:
        json_name = yolo_file.rstrip(".txt") + ".json"
    
    names = read_name_file('obj.names')
    img = Image.open(image_path)
    width, height = img.size

    data = {
        "version": "4.5.7",
        "flags": {},
        "shapes": [],
        "imagePath": os.path.basename(image_path),
        "imageData": img_to_b64(image_path),
        "imageHeight": height,
        "imageWidth": width,
    }

    with open(yolo_file, 'r') as f:
        lines = f.readlines()
        for line in lines:
            parts = line.strip().split()
            cls = int(parts[0])
            cx, cy, w, h = [float(x) for x in parts[1:]]
            
            x1 = (cx - w / 2) * width
            y1 = (cy - h / 2) * height
            x2 = (cx + w / 2) * width
            y2 = (cy + h / 2) * height

            shape = {
                "label": names[cls],
                "line_color": None,
                "fill_color": None,
                "points": [[x1, y1], [x2, y2]],
                "shape_type": "rectangle",
                "flags": {}
            }

            data["shapes"].append(shape)
    
    with open(json_name, "w") as json_outfile:
        json.dump(data, json_outfile, ensure_ascii=False, indent=2)

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description="Convert TXT to JSON")
    parser.add_argument('--input', type=str, help="Path to the input TXT file", required=True)
    parser.add_argument('--image', type=str, help="Path to the image associated with TXT annotations", required=True)
    parser.add_argument('--output', type=str, help="Path to the output JSON file", default=None)

    args = parser.parse_args()
    convert_yolo_to_labelme(args.input, args.image, args.output)

from labelmetoyolosegmentation.

gyupro avatar gyupro commented on July 2, 2024

Check out here
yolo2labelme

from labelmetoyolosegmentation.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.