Python 类型错误:';元组';当我试图识别照片时,对象不能解释为整数

Python 类型错误:';元组';当我试图识别照片时,对象不能解释为整数,python,opencv,machine-learning,keras,Python,Opencv,Machine Learning,Keras,我想测试一个经过训练的模型,我使用keras和opencv,代码如下: 创建参数解析器并传递它们 ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="path to input image we are going to classify") ap.add_argument("-

我想测试一个经过训练的模型,我使用keras和opencv,代码如下:

创建参数解析器并传递它们

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
                help="path to input image we are going to classify")
ap.add_argument("-m", "--model", required=True,
                help="path to trained Keras model")
ap.add_argument("-l", "--label-bin", required=True,
                help="path to label binarizer")
ap.add_argument("-w", "--width", type=int, default=28,
                help="target spatial dimension width")
ap.add_argument("-e", "--height", type=int, default=28,
                help="target spatial dimension height")
ap.add_argument("-f", "--flatten", type=int, default=-1,
                help="whether or not we should flatten the image")
args = vars(ap.parse_args())
加载输入图像并将其大小更改为所需大小

image = cv2.imread(args['image'])
output = image.copy()
image = cv2.resize(image, (args['width'], args['height']))
# scaling the pixel values to the range
image = image.astype("float") / 255.0
# check whether you need to smooth the image and add the batch size
if args['flatten'] > 0:
    image = image.flatten()
    image = image.reshape((1, image.shape))

# otherwise, we work with CNN -- don't smooth the image
# and just add the package size
else:
    image = image.reshape((1, image.shape, image.shape,image.shape))

print(" loading network and label binarizer...")
model = load_model(args['model'])
lb = pickle.loads(open(args['label-bin'], "rb").read())

# recognizing the image
preds = model.predict(image)

# finding the index of the class label with the highest probability
# matches
i = preds.argmax(axis=1)
label = lb.classes_

text = "{}: {:.2f}%".format(label, preds * 100)
cv2.putText(output, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
            (0, 0, 255), 2)

# showing the output image
cv2.imshow("Image", output)
cv2.waitKey(0)
但我得到了以下错误:

image = image.change the shape((1, Image.form, image.form,image.form))
TypeError: the 'tuple' object cannot be interpreted as an integer
如果args['flatten']>0,我会得到同样的结果


有人能帮你解决这个问题吗?

你有两个圆括号,第一个参数就是元组。删除双引号,您就可以排序了。@go2nirvana,我这样做了,但是出现了错误remained@go2nirvana,我做这个图像=图像。重塑(1,image.shape)请提供一个,以及整个错误输出。我建议你读书。