AttributeError:'NoneType'对象没有属性'lower'

时间:2017-05-29 13:28:20

标签: python if-statement

我正在尝试运行python文件,但是我收到了这个错误:

  File "/home/hadi/Software/tensorflow/TEST_FRCN_ROOT/tools/../lib/datasets/pascal_voc.py", line 212, in _load_pascal_annotation
    cls = self._class_to_ind[obj.find('name').text.lower().strip()]
AttributeError: 'NoneType' object has no attribute 'lower'

这是产生错误的代码的一部分:

%%  Load object bounding boxes into a data frame.
        for ix, obj in enumerate(objs):
            bbox = obj.find('bndbox')
            # Make pixel indexes 0-based
            x1 = float(bbox.find('xmin').text) - 1
            y1 = float(bbox.find('ymin').text) - 1
            x2 = float(bbox.find('xmax').text) - 1
            y2 = float(bbox.find('ymax').text) - 1
            cls = self._class_to_ind[obj.find('name').text.lower().strip()]
            boxes[ix, :] = [x1, y1, x2, y2]
            gt_classes[ix] = cls
            overlaps[ix, cls] = 1.0
            seg_areas[ix] = (x2 - x1 + 1) * (y2 - y1 + 1)

我可以添加条件来处理任何对象吗?

1 个答案:

答案 0 :(得分:1)

是的,你可以像这样使用

for ix, obj in enumerate(objs):
            bbox = obj.find('bndbox')
            # Make pixel indexes 0-based
            x1 = float(bbox.find('xmin').text) - 1
            y1 = float(bbox.find('ymin').text) - 1
            x2 = float(bbox.find('xmax').text) - 1
            y2 = float(bbox.find('ymax').text) - 1
            if obj.find('name').text != None:
              cls = self._class_to_ind[obj.find('name').text.lower().strip()]
              boxes[ix, :] = [x1, y1, x2, y2]
              gt_classes[ix] = cls
              overlaps[ix, cls] = 1.0
              seg_areas[ix] = (x2 - x1 + 1) * (y2 - y1 + 1)