我知道类似的问题已经问过很多遍了,但是我从他们那里找不到太多帮助。 我正在研究验证码断路器。我已经从Kaggle开发了一个数据集,现在正在开发自己的数据集。 代码运行:
from keras import layers
from keras.models import Model
from keras.models import load_model
from keras import callbacks
import os
import cv2
import string
import numpy as np
#Init main values
symbols = string.ascii_lowercase + "0123456789" # All symbols captcha can contain
num_symbols = len(symbols)
img_shape = (60, 200, 4)
数据预处理已完成:
def preprocess_data():
n_samples = len(os.listdir(r'C:\Users\Winter\Desktop\AtlanTen\captchas\captchasML'))
X = np.zeros((n_samples, 60, 200, 4)) #1070*50*200
y = np.zeros((5, n_samples, num_symbols)) #5*1070*36
for i, pic in enumerate(os.listdir(r'C:\Users...\captchas\captchasML')):
# Read image as grayscale
img = cv2.imread(os.path.join(r'C:\Users\...'
pic_target = pic[:-4]
if len(pic_target) < 6:
# Scale and reshape image
img = img / 255.0
img = np.reshape(img, (60, 200, 4))
# Define targets and code them using OneHotEncoding
targs = np.zeros((5, num_symbols))
for j, l in enumerate(pic_target):
ind = symbols.find(l)
targs[j, ind] = 1
X[i] = img
y[:, i] = targs
# Return final data
return X, y
X, y = preprocess_data()
X_train, y_train = X[:970], y[:, :970]
X_test, y_test = X[970:], y[:, 970:]
我没有写有关网络的信息,因为那里没有发生错误。 任何随机验证码的预测代码为:
# Define function to predict captcha
def predict(filepath):
img = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
if img is not None:
img = img / 255.0
else:
print("Not detected");
res = np.array(model.predict(img[np.newaxis, :, :, np.newaxis]))
ans = np.reshape(res, (5, 36))
l_ind = []
probs = []
for a in ans:
l_ind.append(np.argmax(a))
#probs.append(np.max(a))
capt = ''
for l in l_ind:
capt += symbols[l]
return capt#, sum(probs) / 5
整个代码取自Kaggle,并做了一些修改。 但是当我尝试时:
print("Predicted Captcha =",predict(r'C:\Users...\captchas\captchasML\2D429.jfif'))
这表明:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-11-ac96e27d8d00> in <module>
----> 1 print("Predicted Captcha =",predict(r'C:\Users\Winter\Desktop\AtlanTen\captchas\captchasML\2D429.jfif'))
<ipython-input-8-ea3eaf0819e7> in predict(filepath)
6 else:
7 print("Not detected");
----> 8 res = np.array(model.predict(img[np.newaxis, :, :, np.newaxis]))
9 ans = np.reshape(res, (5, 36))
10 l_ind = []
~\anaconda3\lib\site-packages\keras\engine\training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
1439
1440 # Case 2: Symbolic tensors or Numpy array-like.
-> 1441 x, _, _ = self._standardize_user_data(x)
1442 if self.stateful:
1443 if x[0].shape[0] > batch_size and x[0].shape[0] % batch_size != 0:
~\anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
577 feed_input_shapes,
578 check_batch_axis=False, # Don't enforce the batch size.
--> 579 exception_prefix='input')
580
581 if y is not None:
~\anaconda3\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
143 ': expected ' + names[i] + ' to have shape ' +
144 str(shape) + ' but got array with shape ' +
--> 145 str(data_shape))
146 return data
147
ValueError: Error when checking input: expected input_1 to have shape (60, 200, 4) but got array with shape (60, 200, 1)
我以为我已经照顾好了图像的大小。但是我想我缺少了一些东西。有人可以帮忙吗?