如何使用keras从OpenCV的VideoCapture预测预训练模型?

时间:2019-01-26 02:17:19

标签: python opencv keras resnet

我的代码是

model = ResNet50(weights='imagenet')

def read_cam(video_capture):
    if video_capture.isOpened():
        windowName = "yolo"
        cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
        cv2.resizeWindow(windowName, 1280, 720)
        cv2.moveWindow(windowName, 0, 0)
        cv2.setWindowTitle(windowName, "Yolo Object Detection")
        while True:
            # Check to see if the user closed the window
            if cv2.getWindowProperty(windowName, 0) < 0:
                break

            ret_val, frame = video_capture.read()
            print(frame)
            frame = np.expand_dims(frame, axis=0)
            frame = preprocess_input(frame)

            preds = model.predict(frame)
            # print(preds)
            print('Predicted:', decode_predictions(preds, top=3)[0])

但是,这会导致一些错误。首先,显然,它期望使用其他大小的数组:

ValueError: Error when checking input: expected input_1 to have shape (224, 224, 3) but got array with shape (720, 1280, 3)

1 个答案:

答案 0 :(得分:3)

使用cv2。调整大小功能可在调用model.predict之前更改框架,因为您使用的预训练模型仅接受尺寸为224x224的图像。

 frame=cv2.resize(frame,(224,224))