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

Ragnor: <p>I am trying to convert  SynthText dataset into TotalText format and got this error please need solution.</p>
<p>import <a href="http://scipy.io" rel="noopener nofollow ugc">scipy.io</a> as sio<br>
import cv2<br>
import os<br>
from tqdm import tqdm</p>
<p>dataFile = ‘./SynthText/gt.mat’<br>
data_images_file = “./SynthText/”</p>
<p>saveFile = “./total_text2/”<br>
train_images_file = saveFile + “train_images”<br>
train_images_gts = saveFile + “train_gts”<br>
train_images_list_txt = “train_list.txt”<br>
if not os.path.exists(saveFile):<br>
os.makedirs(saveFile)<br>
if not os.path.exists(train_images_file):<br>
os.makedirs(train_images_file)<br>
if not os.path.exists(train_images_gts):<br>
os.makedirs(train_images_gts)</p>
<p>def convert2txt():<br>
fh_r = open(os.path.join(saveFile, train_images_list_txt), ‘w’, encoding=‘utf-8’)<br>
data = sio.loadmat(dataFile)<br>
for i in tqdm(range(len(data[“imnames”][0]))):<br>
# print(os.path.join(data_images_file, “{}”.format(data[“imnames”][0][i][0])))<br>
# read image<br>
image = cv2.imread(os.path.join(data_images_file, “{}”.format(data[“imnames”][0][i][0])))<br>
# save image<br>
# cv2.imwrite(os.path.join(train_images_file, “{}”.format(data[“imnames”][0][i][0].split("/")[1])), image)<br>
# write train_list.txt<br>
fh_r.write("{}".format(data[“imnames”][0][i][0].split("/")[1]) + ‘\n’)<br>
# write train_gts<br>
fh_gt = open(os.path.join(train_images_gts, “{}.txt”.format(data[“imnames”][0][i][0].split("/")[1])), ‘w’,<br>
encoding=‘utf-8’)<br>
# get word list<br>
rec = data[‘wordBB’][0][i]<br>
txt_str = “”<br>
for words in data[“txt”][0][i]:<br>
txt_str += " " + " “.join([w.strip() for w in words.split(”\n")])<br>
txt_str = txt_str.strip().split(" “)<br>
# # get word list<br>
# print(data[“txt”][0][i])<br>
print(txt_str)<br>
print(len(txt_str), len(rec[0][0]))<br>
for j in range(len(rec[0][0])):<br>
x1 = int(rec[0][0][j])<br>
y1 = int(rec[1][0][j])<br>
x2 = int(rec[0][1][j])<br>
y2 = int(rec[1][1][j])<br>
x3 = int(rec[0][2][j])<br>
y3 = int(rec[1][2][j])<br>
x4 = int(rec[0][3][j])<br>
y4 = int(rec[1][3][j])<br>
cv2.rectangle(image, (x1, y2), (x3, y3), (0, 0, 255), 4)<br>
fh_gt.write(str(x1) + “,” + str(y1) + “,” + str(x2) + “,” + str(y2)<br>
+ “,” + str(x3) + “,” + str(y3) + “,” + str(x4) + “,” + str(y4) + “,” + txt_str[j] + ‘\n’)<br>
fh_gt.close()<br>
cv2.imwrite(os.path.join(train_images_file, “{}”.format(data[“imnames”][0][i][0].split(”/")[1])), image)<br>
if i == 10:<br>
break<br>
fh_r.close()</p>
<p>convert2txt()</p>
<p>This is the code and got this error in this line " print(len(txt_str), len(rec[0][0]))"</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://blog.csdn.net/qq_39529154/article/details/108689908">
  <header class="source">
      <img src="https://g.csdnimg.cn/static/logo/favicon32.ico" class="site-icon" width="32" height="32">

      <a href="https://blog.csdn.net/qq_39529154/article/details/108689908" target="_blank" rel="noopener nofollow ugc">blog.csdn.net</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://blog.csdn.net/qq_39529154/article/details/108689908" target="_blank" rel="noopener nofollow ugc">SynthText in the Wild Dataset 转...</a></h3>

  <p>SynthText in the Wild Dataset 数据集下载地址https://www.robots.ox.ac.uk/~vgg/data/scenetext/需求https://github.com/MhLiao/DB 需要将SynthText 转换为total_text数据集格式，作为预训练模型代码# -*- coding: utf-8 -*-## ----------------------------------------------------------------</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<p>
Thank you.</p>

ptrblck: <p>I guess <code>rec[0][0]</code> is a <code>numpy.float32</code> and thus raises the error when <code>len()</code> is called on it.<br>
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.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.</p>