1

I am working on a captcha recognition project with Keras library. For training set, I am using the following function to generate at most 5 digit captchas.

def genData(n=1000, max_digs=5, width=60):
    capgen = ImageCaptcha()
    data = []
    target = []
    for i in range(n):
        x = np.random.randint(0, 10 ** max_digs)
        img = misc.imread(capgen.generate(str(x)))
        img = np.mean(img, axis=2)[:, :width]
        data.append(img.flatten())
        target.append(x)
    return np.array(data), np.array(target)

Then, I am trying to reshape training data array like following;

train_data = train_data.reshape(train_data.shape[0], 60, 60, 3)

I guess my captchas have 3 color channel. However, when I tried to reshape the training data I am facing with the following error;

ValueError: cannot reshape array of size 3600000 into shape (1000,60,60,3)

Note: If I try with 1 instead of 3. the error is not occurring but my accuracy is not even close to %1

1 Answer 1

1

You are creating a single channel image by taking the mean. The error says that you are trying to reshape an array with 3600000 elements in an array three times as big (1000*60*60*3 = 10800000). Adapt your function the example below to get it to work.

Also, because you are decreasing the width of your image to 60 pixels the target is not correct anymore. This explains the low accuracy. Try using a bigger width and your accuracy will most likely increase (e.g 150-155).

def genData(n=1000, max_digs=5, width=60):
    capgen = ImageCaptcha()
    data = []
    target = []
    for i in range(n):
        x = np.random.randint(0, 10 ** max_digs)
        img = misc.imread(capgen.generate(str(x)))
        img = img[:,:width,:]
        data.append(img.flatten())
        target.append(x)
    return np.array(data), np.array(target)
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot for your suggestions. I didn't realize that taking mean is reducing the depth to 1. I will continue with the same structure but I have one more question. I am creating captchas with different lengths, so the size of the each image is different. Do I need to train my model separately for each length? @Wilmar
No that would take to much time and effort. It would be better to resize the captha's so that they are all the same the same size. But, what you are doing is decreasing the width. It is better to increase the width of the captchas which are too small, by adding zeros or whatever values. Than you won't loose information

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.