【发布时间】:2020-07-27 13:57:54
【问题描述】:
我有一个预训练模型。使用 20000 个“灰色”样本训练的模型。它正在处理“灰色”测试样本。但我想用网络摄像头测试这个模型。这是我的代码:
#Load the saved model
model = keras.models.load_model('C:\keras\handrecognition_model.h5')
video = cv2.VideoCapture(0)
while True:
_, frame = video.read()
im = Image.fromarray(frame, 'RGB')
im = im.resize((128, 128))
img_array = np.array(im)
img_array = np.expand_dims(img_array, axis=0)
prediction = int(model.predict(img_array)[0][0])
# if prediction is 0, which means I am missing on the image, then show the frame in gray color.
if prediction == 0:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow("Capturing", frame)
key = cv2.waitKey(1)
if key == ord('q'):
break
video.release()
cv2.destroyAllWindows()
有一个错误:ValueError: Error when checks input: expected conv2d_1_input to have shape (120, 320, 1) but got array with shape (128, 128, 3).
模型训练:
# Construction of model
model = Sequential()
model.add(Conv2D(32, (5, 5), activation='relu', input_shape=(120, 320, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
编辑:我像这样更新代码:
_, frame = video.read()
frame = cv2.resize(frame, (120, 360))
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
img_array = np.array(gray)
ValueError:检查输入时出错:预期 conv2d_1_input 有 4 个维度,但得到的数组形状为 (1, 360, 120)
编辑 3:我想,它正在工作。现在我将发送帧进行预测,我将找到手部 gest。如果我能做到,我会分享。谢谢。
_, frame = video.read()
frameCopy=frame.copy()
frameCopy = cv2.resize(frameCopy, (120, 320))
gray = cv2.cvtColor(frameCopy, cv2.COLOR_BGR2GRAY)
img_array = np.array(gray)
img_array = img_array.reshape(120, 320, 1)
img_array = np.expand_dims(img_array, axis=0)
【问题讨论】:
-
在将输入输入网络之前,您需要将输入重塑为网络的输入形状。
-
据我了解,我的输入形状是 (120, 320, 1)。我可以将大小调整为 128,128,但无法更改色标。
-
您的网络摄像头图像是
(128, 128, 3)。您需要将其转换为灰度并将其调整大小/填充为正确的形状。 -
我可以分享培训文章的链接吗?禁止吗?
-
这不是被禁止的,如果你认为它有助于理解你想要做什么,只需将它添加到问题中
标签: python keras deep-learning