如何使用 keras 对来自 OpenCV 的视频捕获的预训练模型进行预测



我的代码是

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)

在调用 model 之前使用 cv2. resize 函数更改帧。预测,因为您使用的预训练模型仅接受大小为 224x224 的图像。

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

最新更新