python - 基数为 10 的 int() 的无效文字:'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(...)),但它仍然无法修复错误
解决方案
你的号码是十进制字符串
因此,首先,您需要先转换为浮点数,然后再转换为整数,因为它有小数点。
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
推荐阅读
- python - Celery 任务创建了一个新模型对象,但 django 信号没有拾取它
- python - Django crontab 不在后台工作
- hibernate-search - Hibernate 搜索 - 多字段排序
- android - 安卓工作室 | 谷歌播放服务库依赖错误
- django - Django:id字段的值即将到来 None
- java - Java - 计算时间间隔内每月发生的日期
- javascript - 两个日期比较 - 结束日期应该大于反应 redux 中验证的开始日期
- python - 如何在python3中将音频流写入文件
- angular - 动态改变 ng2-nouislider 的范围
- android - 安卓:权限