且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

给定要识别的图像集和经过训练的模型,我将如何使模型识别图像?

更新时间:2023-12-02 20:07:40

模型是使用灰度图像训练的,因此期望输入图像是灰度的. RGB图像具有3个通道.灰度图像只有1个通道.

As your model is trained with gray scale images it expects the input image to be gray scale. RGB image has 3 channels. Gray scale image has only 1 channel.

因此,当加载图像而不是代表 cv2.IMREAD_COLOR 1 时,请使用与 cv2.IMREAD_GRAYSCALE对应的 0 以灰度模式加载图像.

So, when loading the image instead of 1 which stands for cv2.IMREAD_COLOR, use 0 corresponding to cv2.IMREAD_GRAYSCALE to load the image in grayscale mode.

(注意:对于 cv2,请使用 -1 .IMREAD_UNCHANGED)请参考opencv文档

(NB: Use -1 for cv2.IMREAD_UNCHANGED Refer to the opencv documentation here for more details)

yourimage = cv2.imread("yourimage.png", 0)

要进行预测,可以在重塑后使用:

For predicting, after reshaping you can use:

predicted_value = np.argmax(model.predict(yourimage))