简体   繁体   中英

resize video data to fit model.predict

The model was trained the following way

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 = 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
  • Use OpenCV for loading frames of the video file
  • 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM