简体   繁体   English

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

[英]resize video data to fit model.predict

The model was trained the following way model 的训练方式如下

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

How can I resize videos to pass them to trained_model.predict below for prediction?如何调整视频大小以将它们传递给下面的trained_model.predict进行预测?

trained_model = load_model("cyclist.h5")

trained_model.predict('7.avi')

It worked this way它是这样工作的

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))

Here is what you need to do:这是您需要做的:

  • Load the model加载 model
  • Use OpenCV for loading frames of the video file使用 OpenCV 加载视频文件的帧
  • Reshape the frame and make a predition重塑框架并做出预测

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用fit_generator()在数据生成器中应用model.predict()时出现的问题 - Issues when applying model.predict() inside data generator with fit_generator() model.predict 会导致 oom 问题,但 model.fit 不会: - model.predict leads to oom issues, but model.fit does not: model.predict()和model.fit()做什么? - What do model.predict() and model.fit() do? model.fit vs model.predict-sklearn中的差异和用法 - model.fit vs model.predict - differences & usage in sklearn keras model.predict 中的大数据量 - Large data quantities in keras model.predict Keras:我可以使用model.predict而不使用model.predict_generator来预测是否使用model.fit_generator训练模型 - Keras: can I use model.predict but not model.predict_generator to predict if I train the model with model.fit_generator 不同的model.fit输出(加载模型无训练后)和keras中的model.predict - different output of model.fit (after loading model no training) and model.predict in keras Keras模型:用于model.fit的同一数组未在model.predict中处理 - Keras Model: Same array that is used for model.fit is not being processed in model.predict tensorflow 中的 model.predict 不工作 - model.predict in tensorflow is not working Tensorflow 中 model.predict 的进展? - Progress of model.predict in Tensorflow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM