labelme_json_to_dataset不能转换关键点的问题(coordinate list must contain at least 2 coordinates)解决办法
如果json文件中有关键点,在使用labelme_json_to_dataset.exe时就会有如下错误:
UserWarning: This script is aimed to demonstrate how to convert the JSON file to a single image dataset, and not to handle multiple JSON files to generate a real-use dataset. warnings.warn("This script is aimed to demonstrate how to convert the\n" Traceback (most recent call last): File "E:\Anaconda3\lib\runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "E:\Anaconda3\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "E:\Anaconda3\Scripts\labelme_json_to_dataset.exe\__main__.py", line 9, in <module> File "E:\Anaconda3\lib\site-packages\labelme\cli\json_to_dataset.py", line 62, in main lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value) File "E:\Anaconda3\lib\site-packages\labelme\utils\shape.py", line 35, in shapes_to_label mask = polygons_to_mask(img_shape[:2], polygons) File "E:\Anaconda3\lib\site-packages\labelme\utils\shape.py", line 12, in polygons_to_mask PIL.ImageDraw.Draw(mask).polygon(xy=xy, outline=1, fill=1) File "E:\Anaconda3\lib\site-packages\PIL\ImageDraw.py", line 185, in polygon self.draw.draw_polygon(xy, fill, 1) TypeError: coordinate list must contain at least 2 coordinates
解决办法是:
打开python目录下的..\Lib\site-packages\labelme\utils\shape.py
将第8行的polygons_to_mask函数改成如下:
1 def polygons_to_mask(img_shape, polygons): 2 mask = np.zeros(img_shape[:2], dtype=np.uint8) 3 mask = PIL.Image.fromarray(mask) 4 if len(polygons)==1: 5 polygons.append(polygons[0]) 6 xy = list(map(tuple, polygons)) 7 PIL.ImageDraw.Draw(mask).polygon(xy=xy, outline=1, fill=1) 8 mask = np.array(mask, dtype=bool) 9 return mask