【问题标题】:How can I use Keras with webcam?如何将 Keras 与网络摄像头一起使用?
【发布时间】: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)

编辑 2:这里训练文章: https://towardsdatascience.com/tutorial-using-deep-learning-and-cnns-to-make-a-hand-gesture-recognition-model-371770b63a51

编辑 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


【解决方案1】:

编辑后回答您的问题: 您需要 4 个维度而不是三个维度:(批量大小、通道、宽度、高度)。所以尝试以下方法:

img_array = np.array(gray)
img_array = img_array.reshape(1, 1, 360, 120)

【讨论】:

    【解决方案2】:
        im = Image.fromarray(frame, 'RGB')
        im = im.resize((128, 128))
        im = im.convert('RGB')
    

    这会将您的灰度图像转换为 RGB。它只会将您当前拥有的一维的值复制到 3 维。如果它抛出错误, 试试这个

        im = im.convert('L')
        im = im.convert('RGB')
    

    'L' 首先将其转换为黑白,以防您的输入有时不仅仅是一维的

    【讨论】:

    • 我已经尝试过“L”,但仍然出现同样的错误。所有训练和测试样本灰度。但我想从相机测试这个模型。我添加了一个链接。或许有帮助。
    猜你喜欢
    • 2016-07-14
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2013-02-05
    相关资源
    最近更新 更多