[英]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:这是您需要做的:
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.