HumanSignal / label-studio-converter

Tools for converting Label Studio annotations into common dataset formats
https://labelstud.io/
255 stars 132 forks source link

Export to COCO error #63

Open kimyoungju49 opened 2 years ago

kimyoungju49 commented 2 years ago

Version 0.0.31rc1 worked well , but the following error comes when I use "convert_to_coco" in the after versions. <no 'labels'>

In commit 1ea3768e046198754cd412ef74af3ba6792a01e8 , "labels |= set(info['labels'])" was added in "_get_labels( )". I guess that's the point.

Should I use tag in labelstudio config to use convert_to_coco???

makseq commented 2 years ago

@kimyoungju49 Could you show the full traceback of this error? Ideally, if you can share export json file (to re-import it) and the labeling config, then I can reproduce and fix this quickly.

kimyoungju49 commented 2 years ago

config

<View>
        <header value="$header"></header>
        <Image name="image" value="$image" zoom="true"></Image>
        <RectangleLabels name="box" toName="image" canRotate="false">
                <Label value="OK" background="#4caf50"></Label>
                <Label value="NG" background="#f44336"></Label>
        </RectangleLabels>
</View>

input_data (json file) for "convert_to_coco"

[{"annotations": [{"created_at": "2021-10-27T09:57:02.131Z", "lead_time": 3.357, "result": [{"original_width": 1920, "original_height": 1080, "image_rotation": 0, "value": {"x": 13.733333333333333, "y": 45.023696682464454, "width": 18.4, "height": 27.488151658767773, "rotation": 0, "rectanglelabels": ["NG"]}, "id": "iy4eodqkvl", "from_name": "box", "to_name": "image", "type": "rectanglelabels"}]}], "data": {"image": "00001_00721.jpg", "imageSize": {"width": 1920, "height": 1080}}, "id": 0}, {"annotations": [{"created_at": "2021-10-27T09:57:10.467Z", "lead_time": 3.493, "result": [{"original_width": 425, "original_height": 566, "image_rotation": 0, "value": {"x": 16, "y": 62.36749116607774, "width": 24.470588235294116, "height": 21.554770318021202, "rotation": 0, "rectanglelabels": ["OK"]}, "id": "72PRc5_DUQ", "from_name": "box", "to_name": "image", "type": "rectanglelabels"}]}], "data": {"image": "abc.bmp", "imageSize": {"width": 425, "height": 566}}, "id": 0}]

KonstantinKorotaev commented 2 years ago

Hi @kimyoungju49 Could you please check if fix from branch fix/gh56-parse-config solves your issue?

kimyoungju49 commented 2 years ago

I tested and there are new error message like below. "Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration."

KonstantinKorotaev commented 2 years ago
  1. Could you please provide full console output and code that use to load label_config?
  2. Do you load label_config from string or from file?
  3. Could you please provide your label config?
kimyoungju49 commented 2 years ago

1-1. console output Traceback (most recent call last): File "C:\Users\yj49.kim\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 193, in _run_module_as_main "main", mod_spec) File "C:\Users\yj49.kim\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "c:\Users\yj49.kim.vscode\extensions\ms-python.python-2021.11.1422169775\pythonFiles\lib\python\debugpy__main.py", line 45, in cli.main() File "c:\Users\yj49.kim.vscode\extensions\ms-python.python-2021.11.1422169775\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, in main run() File "c:\Users\yj49.kim.vscode\extensions\ms-python.python-2021.11.1422169775\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, in run_file runpy.run_path(target_as_str, run_name=compat.force_str("main__")) File "C:\Users\yj49.kim\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 263, in run_path pkg_name=pkg_name, script_name=fname) File "C:\Users\yj49.kim\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 96, in _run_module_code mod_name, mod_spec, pkg_name, script_name) File "C:\Users\yj49.kim\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "d:\Workspace\VSCode\env_test\labelstudio-converter-test.txt", line 27, in main() File "d:\Workspace\VSCode\env_test\labelstudio-converter-test.txt", line 24, in main convert_from_labelstudio_to_coco(config_xml, input_path, data_dir, False) File "d:\Workspace\VSCode\env_test\labelstudio-converter-test.txt", line 11, in convert_from_labelstudio_to_coco c = Converter(ls_config_xml, None) File "d:\Workspace\VSCode\env_test\lib\site-packages\label_studio_converter\converter.py", line 144, in init self._schema = parse_config(config_string) File "d:\Workspace\VSCode\env_test\lib\site-packages\label_studio_converter\utils.py", line 198, in parse_config xml_tree = etree.fromstring(config_string) File "src\lxml\etree.pyx", line 3237, in lxml.etree.fromstring File "src\lxml\parser.pxi", line 1891, in lxml.etree._parseMemoryDocument ValueError: Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.

1-2. code that use to load label_config import os import json import xmltodict from label_studio_converter import Converter

def convert_from_labelstudio_to_coco(ls_config_xml, input_dir, output_dir, is_dir=True): c = Converter(ls_config_xml, None) c.convert_to_coco(input_dir, output_dir, output_image_dir=output_dir, is_dir=is_dir)

def main(): data_dir = os.path.join(os.getcwd(), 'data') config_path = os.path.join(data_dir, 'ls_config.json') input_path = os.path.join(data_dir, 'labels.json')

with open(config_path) as config_json_file: config_json = json.load(config_json_file)

config_xml = xmltodict.unparse(config_json, pretty=True)

convert_from_labelstudio_to_coco(config_xml, input_path, data_dir, False)

if name == 'main': main()

2. I load label_config as a json file and convert it to xml string. (You can check the code above) 3. config file(ls_config.json) { "View": { "header": { "@value": "$header" }, "Image": { "@name": "image", "@value": "$image", "@zoom": "true" }, "RectangleLabels": { "@name": "box", "@toName": "image", "@canRotate": "false", "Label": [ { "@value": "OK", "@background": "#4caf50" }, { "@value": "NG", "@background": "#f44336" } ] } } }

4. input_file (sample of 'labels.json') [{"annotations": [{"created_at": "2021-11-19T05:16:50.527Z", "lead_time": 5.075, "result": [{"original_width": 128, "original_height": 128, "image_rotation": 0, "value": {"x": 10.9375, "y": 21.09375, "width": 36.71875, "height": 44.53125, "rotation": 0, "rectanglelabels": ["OK"]}, "id": "SIyzo61O1e", "from_name": "box", "to_name": "image", "type": "rectanglelabels"}]}], "data": {"image": "00001_00346.bmp", "imageSize": {"width": 128, "height": 128}}, "id": 0}]