-
Notifications
You must be signed in to change notification settings - Fork 1
/
clasify.py
74 lines (64 loc) · 2.39 KB
/
clasify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
import sys
import cv2
import numpy as np
from data import DataSet
from extractor import Extractor
from tensorflow.keras.models import load_model
import time
if (len(sys.argv) == 6):
seq_length = int(sys.argv[1])
class_limit = int(sys.argv[2])
saved_model = sys.argv[3]
video_file = sys.argv[4]
output_video = sys.argv[5]
else:
print ("Usage: python clasify.py sequence_length class_limit saved_model_name video_file_name")
print ("Example: python clasify.py 75 2 lstm-features.095-0.090.hdf5 some_video.mp4")
exit (1)
capture = cv2.VideoCapture(os.path.join(video_file))
width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) # float
height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) # float
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video_writer = cv2.VideoWriter(str(output_video), fourcc, 15, (int(width), int(height)))
# Get the dataset.
data = DataSet(seq_length=seq_length, class_limit=class_limit, image_shape=(height, width, 3))
# get the model.
extract_model = Extractor(image_shape=(height, width, 3))
saved_LSTM_model = load_model(saved_model)
frames = []
frame_count = 0
while True:
ret, frame = capture.read()
# Bail out when the video file ends
if not ret:
break
start_time = time.time()
# Save each frame of the video to a list
frame_count += 1
frames.append(frame)
if frame_count < seq_length:
continue # capture frames untill you get the required number for sequence
else:
frame_count = 0
# For each frame extract feature and prepare it for classification
sequence = []
for image in frames:
features = extract_model.extract_image(image)
sequence.append(features)
# Clasify sequence
prediction = saved_LSTM_model.predict(np.expand_dims(sequence, axis=0))
print(prediction)
values = data.print_class_from_prediction(np.squeeze(prediction, axis=0))
# Add prediction to frames and write them to new video
for image in frames:
for i in range(len(values)):
cv2.putText(image, values[i], (20, 20 * i + 10), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), lineType=cv2.LINE_AA)
video_writer.write(image)
result = np.asarray(image)
frames = []
fps = 1.0 / (time.time() - start_time)
print("FPS: %.2f" % fps)
# result = cv2.cvtColor(result, cv2.COLOR_RGB2BGR)
# cv2.imshow("Output Video", result)
video_writer.release()