Last active
November 28, 2018 12:33
-
-
Save metal3d/d373162abbdda06f55b6264f9fce2970 to your computer and use it in GitHub Desktop.
Keras make prediction on image sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from IPython.display import HTML, display | |
import cv2 as cv | |
import os | |
import matplotlib.pyplot as plt | |
def make_prediction(model, movie, nframe, classes, shape=(224, 224), nchan=3): | |
video = cv.VideoCapture(movie) | |
frames = [] | |
while True: | |
grabbed, frame = video.read() | |
if not grabbed: | |
break | |
frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB) | |
frame = cv.resize(frame, shape) | |
frames.append(frame) | |
jump = len(frames)//nframe | |
frames = frames[::jump][:nframe] | |
checking = movie.split(os.path.sep)[-2] | |
display(HTML('<b>Checking %s</b>' % checking)) | |
plt.figure(figsize=(18, 10)) | |
for i, frame in enumerate(frames[::2]): | |
plt.subplot(1, nframe//2, i+1) | |
plt.imshow(frame) | |
plt.show() | |
seq = np.array(frames) | |
seq = np.reshape(seq, (1, nframe,) + shape + (nchan,)) | |
res = model.predict(seq) | |
cl = np.argmax(res).astype('int') | |
cl = classes[cl] | |
style='color: green' | |
response = "CORRECT" | |
if cl != checking: | |
style='color: red' | |
response="WRONG" | |
display(HTML('Result: <b style="%s">%s : %s</b>' % (style, response, cl))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment