使用字符串加载注释时出现问题,无法从字符串转换为浮点值



我在加载注释文件时得到了一个Value error: Cannot convert string to float,所以我将代码修改为:

def floats_from_string(line):
nums = []
try:
for num in line.split(" "):
nums.append(float(num))
except ValueError:
pass
return nums
def load_annoataion(p):
text_polys = []
text_tags = []
if not os.path.exists(p):
return np.array(text_polys, dtype=np.float32)
with open(p, 'r') as f:
reader = csv.reader(f)
for line in reader:
label = line[-1]
line = [i.strip('ufeff').strip('xefxbbxbf') for i in line]
new_line = floats_from_string(line)
x1, y1, x2, y2, x3, y3, x4, y4 = list(map(float, new_line[:8]))
text_polys.append([[x1, y1], [x2, y2], [x3, y3], [x4, y4]])
if label == '*' or label == '###':
text_tags.append(True)
else:
text_tags.append(False)
return np.array(text_polys, dtype=np.float32), np.array(text_tags, dtype=np.bool)

更新代码后,我现在得到这个错误:

File "/Users/shwaitkumar/Downloads/EAST-master/icdar.py", line 46, in floats_from_string
for num in line.split(" "):
AttributeError: 'list' object has no attribute 'split'

我不擅长编程,但我认为我实现的将字符串转换为浮点值的解决方案是错误的。这是我需要加载到模型中的注释文件。请帮忙,让我的模型可以加载它:

15.025299 79.619064 91.971375 27.37761 111.49409 87.36937 120.44259 144.5673 और
195.26416 345.93964 346.07916 195.40369 296.7271 296.54498 411.9508 412.13293 किस
544.8015 579.83813 541.4978 506.46115 42.720642 60.455795 136.19897 118.46382 दिन
275.59427 311.88095 302.1434 265.85672 134.48518 159.5067 173.62825 148.60674 रूप
30.469978 163.88913 164.9093 31.490135 182.98358 181.51782 274.3758 275.84155 इस
33.57235 184.95844 185.26584 33.879738 354.1837 353.62552 436.9943 437.55246 तरह
-2.6164436 45.761616 53.37768 4.9996223 155.51007 137.70114 158.39023 176.19916 साथ
343.8163 512.32336 512.94495 344.4379 134.2903 132.54332 192.49599 194.24297 एकाएक
337.52948 504.81384 505.13098 337.84662 241.95956 240.91986 291.94846 292.98816 रमानाथ
507.9361 555.0499 523.0799 475.96606 4.7673645 56.682793 85.69593 33.780502 गया

您将传递一个List作为floats_from_string的参数,然后将其视为字符串。.split是一个字符串方法,而不是List方法。您可以保持这种行为,但只需更改for循环:

def floats_from_string(numbers):
nums = []
try:
for num in numbers:
nums.append(float(num))
except ValueError:
pass
return nums

为什么要将所有值映射到一个浮点值,而函数已经这样做了?我会把你的代码改成这样:

...
nums = floats_from_string(line)
text_polys.append([
[nums[0], nums[1]], [nums[2], nums[3]], [nums[4], nums[5]], [nums[6], nums[7]]
])
...

最新更新