labelimg,Json格式出现“TypeError: string indices must be integers“错误的解决办法

问题描述

今天用labelimg打开训练数据集发现以下错误

Traceback (most recent call last):
  File "C:\Users\qwen\anaconda32\lib\site-packages\labelImg\labelImg.py", line 1348, in open_prev_image
    self.load_file(filename)
  File "C:\Users\qwen\anaconda32\lib\site-packages\labelImg\labelImg.py", line 1111, in load_file
    self.show_bounding_box_from_annotation_file(file_path)
  File "C:\Users\qwen\anaconda32\lib\site-packages\labelImg\labelImg.py", line 1146, in show_bounding_box_from_annotation_file
    self.load_create_ml_json_by_filename(json_path, file_path)
  File "C:\Users\qwen\anaconda32\lib\site-packages\labelImg\labelImg.py", line 1571, in load_create_ml_json_by_filename
    create_ml_parse_reader = CreateMLReader(json_path, file_path)
  File "C:\Users\qwen\anaconda32\lib\site-packages\libs\create_ml_io.py", line 102, in __init__
    self.parse_json()
  File "C:\Users\qwen\anaconda32\lib\site-packages\libs\create_ml_io.py", line 118, in parse_json
    if image["image"] == self.filename:
TypeError: string indices must be integers

傻眼了,一下不知怎么解决,还好,最后找到办法了。


原因分析:

  1. 首先重新打一下标签,看看json格式内容为:
[
   {
 "image": "1-0-OK.png",
 "annotations": [
   {
     "label": "\u5212\u75d5",
     "coordinates": {
       "x": 200.0,
       "y": 255.0,
       "width": 26.0,
       "height": 119.0
     }
   },
   {
     "label": "\u5212\u75d5",
     "coordinates": {
       "x": 200.0,
       "y": 255.0,
       "width": 26.0,
       "height": 119.0
     }
   },
   {
     "label": "\u5212\u75d5",
     "coordinates": {
       "x": 200.0,
       "y": 255.0,
       "width": 26.0,
       "height": 119.0
     }
   },
   {
     "label": "\u5212\u75d5",
     "coordinates": {
       "x": 200.0,
       "y": 255.0,
       "width": 26.0,
       "height": 119.0
     }
   }
 ]
}
]
  1. 导致出现错误的内容为
{
  "image": "1-0-OK.png",
  "annotations": [
    {
      "label": "\u5212\u75d5",
      "coordinates": {
        "x": 200.0,
        "y": 255.0,
        "width": 26.0,
        "height": 119.0
      }
    },
    {
      "label": "\u5212\u75d5",
      "coordinates": {
        "x": 200.0,
        "y": 255.0,
        "width": 26.0,
        "height": 119.0
      }
    },
    {
      "label": "\u5212\u75d5",
      "coordinates": {
        "x": 200.0,
        "y": 255.0,
        "width": 26.0,
        "height": 119.0
      }
    },
    {
      "label": "\u5212\u75d5",
      "coordinates": {
        "x": 200.0,
        "y": 255.0,
        "width": 26.0,
        "height": 119.0
      }
    }
  ]
}

是不是没有看到有啥区别,是的!刚开始也没有看出来,然后查看源码:

C:\Users\qwen\anaconda32\lib\site-packages\libs\create_ml_io.py

增加调试代码,如下图:
在这里插入图片描述
重新再运行labelimg查看命令行日志信息,如图:
在这里插入图片描述
结合代码可知118行,此时image变量是一个字符串"image",无法image[“image”]来取值,image应该是一个字典,而前面的for image in output_dict,那么output_dict应该是一个可迭代类型,比如list等。

        for image in output_dict:
            print(image)
            if image["image"] == self.filename:
                for shape in image["annotations"]:
                    self.add_shape(shape["label"], shape["coordinates"])

解决方案:

将打标签的所有数据前面都加一个[],即可解决此问题。即,由原来的{}变为[{}]即可。

[{
  "image": "1-0-OK.png",
  "annotations": [
    {
      "label": "\u5212\u75d5",
      "coordinates": {
        "x": 200.0,
        "y": 255.0,
        "width": 26.0,
        "height": 119.0
      }
    },
    {
      "label": "\u5212\u75d5",
      "coordinates": {
        "x": 200.0,
        "y": 255.0,
        "width": 26.0,
        "height": 119.0
      }
    },
    {
      "label": "\u5212\u75d5",
      "coordinates": {
        "x": 200.0,
        "y": 255.0,
        "width": 26.0,
        "height": 119.0
      }
    },
    {
      "label": "\u5212\u75d5",
      "coordinates": {
        "x": 200.0,
        "y": 255.0,
        "width": 26.0,
        "height": 119.0
      }
    }
  ]
}]

这个image返回完美对象为字典。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/womengdoushizhongguo/article/details/129800402