Search code examples
pythonpython-3.7

TypeError: Int object is not iterable


I am trying to generate dataset for OCR using different fonts, but upon a certain for loop, the iteration giving me and error Typeerror: int object is not iterable. I have searched enough to conclude that most of the answers on StackOverFlow suggests to use the range in my for loop including (len) but I am not sure if I follow that.

The function is as follows:

def gen_rand_string_data(data_count,
                     min_char_count=3,
                     max_char_count=8,
                     max_char=16,
                     x_pos='side',
                     img_size=(32, 256, 1),
                     font=cv2.FONT_HERSHEY_SIMPLEX,
                     font_scale=np.arange(0.7, 1, 0.1),
                     thickness=range(1, 3, 1)):
'''
random string data generation
'''
start_time = dt.datetime.now()
images = []
labels = []
color = (255, 255, 255)
count = 0
char_list = list(string.ascii_letters) \
            + list(string.digits) \
            + list(' ')
while (1):

    for fs in font_scale:
        for thick in thickness:
            for f in font:
                img = np.zeros(img_size, np.uint8)
                char_count = np.random.randint(min_char_count, \
                                               (max_char_count + 1))
                rand_str = ''.join(np.random.choice(char_list, \
                                                    char_count))
                # generate image data
                text_size = cv2.getTextSize(rand_str, f, fs, thick)[0]
                if x_pos == 'side':
                    org_x = 0
                else:
                    org_x = (img_size[1] - text_size[0]) // 2
                org_y = (img_size[0] + text_size[1]) // 2
                cv2.putText(img, rand_str, (org_x, org_y), f, fs, \
                            color, thick, cv2.LINE_AA)

                label = list(rand_str) + [' '] \
                        * (max_char - len(rand_str))
                for i, t in enumerate(label):
                    label[i] = char_list.index(t)

                label = np.uint8(label)
                images.append(img)
                labels.append(label)
                count += 1
                if count == data_count:
                    break
            else:
                continue
            break
        else:
            continue
        break
    else:
        continue
    break
end_time = dt.datetime.now()
print("time taken to generate data", end_time - start_time)
return images, labels

The error raised is at line : for f in font:

What am I doing wrong here? Do I have to use the range()?


Solution

  • font=cv2.FONT_HERSHEY_SIMPLEX
    for f in font:
        ...
    

    In CV2, the font is a simple integer representing the font itself, I'm not entirely sure(a) why you're trying to iterate over it.

    If you wanted to iterate over sizes of the font, you would have to use (for example) the fontScale parameter of putText().

    If you want to iterate over a collection of fonts, you have to provide that collection, such as with one of:

    font = [cv2.FONT_HERSHEY_SIMPLEX]                         # one font as a collection
    font = [cv2.FONT_HERSHEY_SIMPLEX, cv2.FONT_HERSHEY_PLAIN] # two fonts
    

    If you only have the one font, then don't iterate over it at all. Get rid of the for f in font line (unindenting the stuff currently "inside" it) and just use font wherever you're currently using f.


    (a) Python is having similar troubles trying to figure out your intent :-)