i find a post about video prediction
https://stackoverflow.com/questions/51576642/how-to-display-cnn-lstm-predicted-output-in-the-video-with-opencv
i edit as follows , but video show
from __future__ import print_function
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.layers import Embedding
from keras.layers import LSTM
from keras.layers import Conv1D, MaxPooling1D
from keras.datasets import imdb
# Embedding
max_features = 20000
maxlen = 100
embedding_size = 128
# Convolution
kernel_size = 5
filters = 64
pool_size = 4
# LSTM
lstm_output_size = 70
# Training
batch_size = 30
epochs = 2
'''
Note:
batch_size is highly sensitive.
Only 2 epochs are needed as the dataset is very small.
'''
#print('Loading data...')
#(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
#print(len(x_train), 'train sequences')
#print(len(x_test), 'test sequences')
def getit(x_train,y_train, x_test, y_test):
print('Pad sequences (samples x time)')
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
print('x_train shape:', x_train.shape)
print('x_test shape:', x_test.shape)
print('Build model...')
model = Sequential()
model.add(Embedding(max_features, embedding_size, input_length=maxlen))
model.add(Dropout(0.25))
model.add(Conv1D(filters,
kernel_size,
padding='valid',
activation='relu',
strides=1))
model.add(MaxPooling1D(pool_size=pool_size))
model.add(LSTM(lstm_output_size))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
print('Train...')
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
validation_data=(x_test, y_test))
score, acc = model.evaluate(x_test, y_test, batch_size=batch_size)
print('Test score:', score)
print('Test accuracy:', acc)
return model
import cv2
import numpy as np
vid = cv2.VideoCapture(r"C:\Users\martlee2\Downloads\stupidwoman.mp4")
while(vid.isOpened()):
ret, frame = vid.read()
if ret == True:
count_frames = 0
count_framesb = 0
frame_list = []
frame_listb = []
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = cv2.resize(frame,(480,360),interpolation=cv2.INTER_AREA)
while count_frames < 30:
frame_list.append(frame)
while count_framesb < 60 and count_framesb >= 30:
frame_listb.append(frame)
if count_frames >= 30:
count_frames = 0
if count_framesb >= 30:
count_framesb = 0
model = getit(frame_list, np.array([1 for i in range(0,30)]),frame_listb, np.array([1 for i in range(0,30)]))
frame_set = np.array(frame_list)
frame_set = frame_set.reshape(1, 15, 480, 360, 1)
pred = model.predict(frame_set)
pred_ = np.argmax(pred,axis=1) #i'm using the Model object from Keras
count_frames = count_frames + 1
count_framesb = count_framesb + 1
#frame = cv2.resize(frame,(480,360),interpolation=cv2.INTER_AREA)
#frame = cv2.putText(frame,str(texto),(0,130), cv2.FONT_HERSHEY_SIMPLEX, 2.5, (255, 0, 0), 2, cv2.LINE_AA)
cv2.imshow('Video', frame)
cv2.imshow('Video2', pred)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
vid.release()
cv2.destroyAllWindows()