TypeError: object of type 'numpy.float32' has no len() Some pictures will report this error

I am trying to convert SynthText dataset into TotalText format and got this error please need solution.

import scipy.io as sio
import cv2
import os
from tqdm import tqdm

dataFile = ‘./SynthText/gt.mat’
data_images_file = “./SynthText/”

saveFile = “./total_text2/”
train_images_file = saveFile + “train_images”
train_images_gts = saveFile + “train_gts”
train_images_list_txt = “train_list.txt”
if not os.path.exists(saveFile):
os.makedirs(saveFile)
if not os.path.exists(train_images_file):
os.makedirs(train_images_file)
if not os.path.exists(train_images_gts):
os.makedirs(train_images_gts)

def convert2txt():
fh_r = open(os.path.join(saveFile, train_images_list_txt), ‘w’, encoding=‘utf-8’)
data = sio.loadmat(dataFile)
for i in tqdm(range(len(data[“imnames”][0]))):
# print(os.path.join(data_images_file, “{}”.format(data[“imnames”][0][i][0])))
# read image
image = cv2.imread(os.path.join(data_images_file, “{}”.format(data[“imnames”][0][i][0])))
# save image
# cv2.imwrite(os.path.join(train_images_file, “{}”.format(data[“imnames”][0][i][0].split("/")[1])), image)
# write train_list.txt
fh_r.write("{}".format(data[“imnames”][0][i][0].split("/")[1]) + ‘\n’)
# write train_gts
fh_gt = open(os.path.join(train_images_gts, “{}.txt”.format(data[“imnames”][0][i][0].split("/")[1])), ‘w’,
encoding=‘utf-8’)
# get word list
rec = data[‘wordBB’][0][i]
txt_str = “”
for words in data[“txt”][0][i]:
txt_str += " " + " “.join([w.strip() for w in words.split(”\n")])
txt_str = txt_str.strip().split(" “)
# # get word list
# print(data[“txt”][0][i])
print(txt_str)
print(len(txt_str), len(rec[0][0]))
for j in range(len(rec[0][0])):
x1 = int(rec[0][0][j])
y1 = int(rec[1][0][j])
x2 = int(rec[0][1][j])
y2 = int(rec[1][1][j])
x3 = int(rec[0][2][j])
y3 = int(rec[1][2][j])
x4 = int(rec[0][3][j])
y4 = int(rec[1][3][j])
cv2.rectangle(image, (x1, y2), (x3, y3), (0, 0, 255), 4)
fh_gt.write(str(x1) + “,” + str(y1) + “,” + str(x2) + “,” + str(y2)
+ “,” + str(x3) + “,” + str(y3) + “,” + str(x4) + “,” + str(y4) + “,” + txt_str[j] + ‘\n’)
fh_gt.close()
cv2.imwrite(os.path.join(train_images_file, “{}”.format(data[“imnames”][0][i][0].split(”/")[1])), image)
if i == 10:
break
fh_r.close()

convert2txt()

This is the code and got this error in this line " print(len(txt_str), len(rec[0][0]))"

Thank you.

I guess rec[0][0] is a numpy.float32 and thus raises the error when len() is called on it.
I’m not familiar with your code and thus don’t know what the expected output would be, but check if a single value is expected or rather a numpy array.

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.

1 Like