调整视频数据大小以适合model.predict

2024-04-25 05:14:20 发布

您现在位置:Python中文网/ 问答频道 /正文

模型按照以下方式进行训练

model = keras.Sequential()
model.add(Conv2D(64, (3, 3), input_shape=(16, 120, 120, 3), padding='same', activation='relu'))

如何调整视频大小以将其传递到下面的trained_model.predict进行预测

trained_model = load_model("cyclist.h5")

trained_model.predict('7.avi')

Tags: 模型addinputmodel方式activationpredictkeras
2条回答

它是这样工作的

import cv2
import numpy as np

file = '7.avi'
cap = cv2.VideoCapture(file)
frameCount = 16
frameWidth = 120
frameHeight = 120

buf = np.empty((frameCount, frameHeight, frameWidth, 3), np.dtype('uint8'))

fc = 0
ret = True

while (fc < frameCount and ret):
    buf[fc] = cv2.resize(cap.read()[1], (frameWidth, frameHeight), fx=0, fy=0, interpolation=cv2.INTER_CUBIC)
    fc += 1

cap.release()
cv2.destroyAllWindows()

trained_model.predict(np.expand_dims(buf, axis=0))

以下是您需要做的:

  • 加载模型
  • 使用OpenCV加载视频文件的帧
  • 重塑框架并做出预测

from tensorflow.keras.models import load_model
import numpy as np
import cv2

trained_model = load_model("cyclist.h5")

image_width = 120
image_height = 120
batch_size = 16

cap = cv2.VideoCapture('7.avi')

while cap.isOpened():

    # Load a frame
    ret, image = cap.read()

    if ret:

        # print(image.shape)
        image = cv2.resize(image, (image_width, image_height))

        image_to_predict = np.expand_dims(image, axis=0)
        # print(image_to_predict.shape)

        # Make a prediction
        prediction = trained_model.predict(image_to_predict, batch_size=batch_size)

        print(prediction)
    
    # press q to exit
    if cv2.waitKey(1) == ord('q'):
        cap.release()
        cv2.destroyAllWindows()
        break

相关问题 更多 >