HumanSignal / label-studio-sdk

Label Studio SDK
https://api.labelstud.io
Apache License 2.0
99 stars 61 forks source link

Update task existing project with label studio sdk #178

Open JaviMota opened 9 months ago

JaviMota commented 9 months ago

Hi

I have created a project using label studio UI. I have imported some images and labeled them manually. Now Im trying to import some images and update the annotations using the label studio sdk. This is my code:

LABEL_STUDIO_URL = os.getenv('LABEL_STUDIO_URL', default='xxxx')
API_KEY = "xxxxxxx"
PROJECT_ID = 6

ls = Client(url=LABEL_STUDIO_URL, api_key=API_KEY)
ls.check_connection()

//project = Project(Client(url=LABEL_STUDIO_URL, api_key=API_KEY))

project = ls.get_project(PROJECT_ID)

tasks_to_annotate = project.get_unlabeled_tasks();

images = os.listdir("./datasets/train/images/")
labels = os.listdir("./datasets/train/labels/")

with open("./datasets/notes.json") as file_labels:
    labels_schema = json.load(file_labels)

file_labels.close()

for task in tasks_to_annotate:

    name = task["file_upload"].split("-")[-1].split(".")[0]

    image = cv2.imread("./datasets/train/images/"+name+".jpg")
    height, width = image.shape[:2]

    bboxes = []
    with open("./datasets/train/labels/"+name+".txt") as file:
        for line in file:
            rect_id = uuid.uuid4().hex[0:10]
            label_id, x, y, x2, y2 = line.split()
            x = (float(x)-float(x2)/2.0)*100
            y = (float(y) - float(y2)/2.0)*100
            rect_height = float(y2)*100
            rect_width = float(x2)*100

            class_name = labels_schema["categories"][int(label_id)]["name"]

            rect_item = {
                "id": rect_id,
                "type": "rectanglelabels",
                "value": {
                    "x": x,
                    "y": y,
                    "width": rect_width,
                    "height": rect_height,
                    "rotation": 0,
                    "rectanglelabels": class_name, 
                },
                "to_name": "image",
                "from_name": "label",
                "image_rotation": 0,
                "original_width": width,
                "original_height": height, 
            }
            print(line)

            bboxes.append(rect_item)

    file.close()
    import_data = [{"id": task["id"], "data": task["data"]["image"],"result": bboxes}]
    project.update_task(task["id"], import_data)

I would like to use "update_task" but looks like using Client there is not this option. I need to use Project but when I initialize it with Client I get the following error: 'Client' object has no attribute 'rstrip'.

How can I update an existing project with new images and annotations using label studio sdk? I dont want to create a new project

In update_task I also get this error: update_task() takes 2 positional arguments but 3 were given

Thank you. Javi