以 10 为底的 int() 的无效文字:'1044.0'

invalid literal for int() with base 10: '1044.0'

我正在尝试 运行 在终端中创建以下代码 train.record

!python convert_to_tfrecord.py -x xml -l label_map.pbtxt -o train.record -i images

其中 xml 是 xml 个文件的文件夹,images 是原始图像的文件夹。它给了我错误:

Traceback (most recent call last):
  File "convert_to_tfrecord.py", line 165, in <module>
    tf.app.run()
  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/platform/app.py", line 40, in run
    _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
  File "/usr/local/lib/python3.7/dist-packages/absl/app.py", line 303, in run
    _run_main(main, args)
  File "/usr/local/lib/python3.7/dist-packages/absl/app.py", line 251, in _run_main
    sys.exit(main(argv))
  File "convert_to_tfrecord.py", line 152, in main
    examples = xml_to_csv(args.xml_dir)
  File "convert_to_tfrecord.py", line 82, in xml_to_csv
    int(root.find('size')[0].text),
ValueError: invalid literal for int() with base 10: '1044.0'

convert_to_tfrecord.py的相关代码是:

def xml_to_csv(path):
    """Iterates through all .xml files (generated by labelImg) in a given directory and combines
    them in a single Pandas dataframe.
    Parameters:
    ----------
    path : str
        The path containing the .xml files
    Returns
    -------
    Pandas DataFrame
        The produced dataframe
    """

    xml_list = []
    for xml_file in glob.glob(path + '/*.xml'):
        tree = ET.parse(xml_file)
        root = tree.getroot()
        for member in root.findall('object'):
            value = (root.find('filename').text,
                     int(root.find('size')[0].text),
                     int(root.find('size')[1].text),
                     member[0].text,
                     int(member[4][0].text),
                     int(member[4][1].text),
                     int(member[4][2].text),
                     int(member[4][3].text)
                     )
            xml_list.append(value)
    column_name = ['filename', 'width', 'height',
                   'class', 'xmin', 'ymin', 'xmax', 'ymax']
    xml_df = pd.DataFrame(xml_list, columns=column_name)
    return xml_df

好像是宽度的问题,但是不知道怎么解决。感谢任何帮助!

编辑:我刚刚尝试了 int(float(...)),但它仍然没有修复错误

您的号码是十进制字符串

因此,首先,您需要转换为 float,然后转换为 int,因为它有一个小数。

def xml_to_csv(path):
    """Iterates through all .xml files (generated by labelImg) in a given directory and combines
    them in a single Pandas dataframe.
    Parameters:
    ----------
    path : str
        The path containing the .xml files
    Returns
    -------
    Pandas DataFrame
        The produced dataframe
    """

    xml_list = []
    for xml_file in glob.glob(path + '/*.xml'):
        tree = ET.parse(xml_file)
        root = tree.getroot()
        for member in root.findall('object'):
            value = (root.find('filename').text,
                     int(float(root.find('size')[0].text)),
                     int(float(root.find('size')[1].text)),
                     member[0].text,
                     int(float(member[4][0].text)),
                     int(float(member[4][1].text)),
                     int(float(member[4][2].text)),
                     int(float(member[4][3].text))
                     )
            xml_list.append(value)
    column_name = ['filename', 'width', 'height',
                   'class', 'xmin', 'ymin', 'xmax', 'ymax']
    xml_df = pd.DataFrame(xml_list, columns=column_name)
    return xml_df

例如:

>>> int('1044.0')
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    int('1044.0')
ValueError: invalid literal for int() with base 10: '1044.0'
>>> int(float('1044.0'))
1044