test_excel.py

chez8990

Uploaded on: Sept. 20, 2021, 12:58 a.m.
.python

import os
import json
import pandas as pd

def ls_to_excel(ls_output, use_latest=True):
    """
    convert labels studio to excel

    Args:
        ls_output ([type]): [description]
        use_latest (bool, optional): [description]. Defaults to True.

    Returns:
        [type]: [description]
    """
    name = ls_output["data"]["ocr"]
    filename = os.path.basename(name)

    if use_latest is True:
        annotations = ls_output["annotations"][-1]["result"]
    else:
        annotations = ls_output["annotations"][0]["result"]

    text_groups = {}
    label_groups = {}
    for annotation in annotations:
        value = annotation["value"]
        annotation_id = annotation["id"]
        if "labels" in value:
            label_groups[annotation_id] = annotation
        if "text" in value:
            text_groups[annotation_id] = annotation

    values = {}
    values['filename'] = filename
    for annotation_id, label_annotation in label_groups.items():
        

        label = label_annotation["value"]["labels"][0]
        if annotation_id in text_groups:
            text_annotation = text_groups[annotation_id]
            text = text_annotation["value"]["text"][0]
        else:
            text = None

        values[label] = text
    return values

ls_results = json.load(open(r'C:\Users\cheches\Downloads\asc_back_of_cheques_test_set_label_studio.json', 'r'))
rows = []
for output in ls_results:
    row = ls_to_excel(output)
    rows.append(row)
rows = pd.DataFrame(rows)
rows.to_csv(r'C:\Users\cheches\Downloads\asc_back_of_cheques_test_set_label_studio.csv', index=None)

Posted: Sept. 20, 2021, 12:58 a.m.

chez8990

test file