-
Notifications
You must be signed in to change notification settings - Fork 11
/
1-1-Singapore_dataset_frames_generation_Asad.py
228 lines (162 loc) · 8.97 KB
/
1-1-Singapore_dataset_frames_generation_Asad.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python
# coding: utf-8
"""
The singapore maritime dataset contains videos and their annotations in the .mat file.
This shell is used to convet all videos to images , split them into training and testing sets.
and another shell (2-Singapore_dataset_convet_mat_into_csv_xml.py) is used to convet .mat files to xmls and csv for
Training deep learning models accordingly.
"""
from scipy.io import loadmat
from os import listdir
from os.path import isfile, join
import os
import cv2
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
import numpy as np
VIDEOS_PATH_ONSHORE = "/MaritimeDataset/SMD/VIS_Onshore/Videos"
OBJECT_ANNOTATIONS_ONSHORE_PATH = "/MaritimeDataset/SMD/VIS_Onshore/ObjectGT"
VIDEO_FRAMES_PATH_ONSHORE = '/MaritimeDataset/SMD/VIS_Onshore/VIS_Onshore_frames/'
VIDEOS_PATH_ONBOARD = "/MaritimeDataset/SMD/VIS_Onboard/Videos"
OBJECT_ANNOTATIONS_ONBOARD_PATH = "/MaritimeDataset/SMD/VIS_Onboard/ObjectGT"
VIDEO_FRAMES_PATH_ONBOARD = '/MaritimeDataset/SMD/VIS_Onboard/VIS_Onboard_frames/'
VIDEOS_PATH_NIR = "/MaritimeDataset/SMD/NIR/Videos"
OBJECT_ANNOTATIONS_NIR_PATH = "/MaritimeDataset/SMD/NIR/ObjectGT"
VIDEO_FRAMES_PATH_NIR = '/MaritimeDataset/SMD/NIR/NIR_frames/'
video_files_onshore = [join(VIDEOS_PATH_ONSHORE, f) for f in listdir(VIDEOS_PATH_ONSHORE)
if isfile(join(VIDEOS_PATH_ONSHORE, f))]
video_files_onboard = [join(VIDEOS_PATH_ONBOARD, f) for f in listdir(VIDEOS_PATH_ONBOARD)
if isfile(join(VIDEOS_PATH_ONBOARD, f))]
video_files_nir = [join(VIDEOS_PATH_NIR, f) for f in listdir(VIDEOS_PATH_NIR)
if isfile(join(VIDEOS_PATH_NIR, f))]
# Create dictionaries for each video in the form video_name:video_path
video_files_onshore_dict = {}
for f in listdir(VIDEOS_PATH_ONSHORE):
if isfile(join(VIDEOS_PATH_ONSHORE, f)):
video_files_onshore_dict[f.split('.')[0]] = join(VIDEOS_PATH_ONSHORE, f)
video_files_onboard_dict = {}
for f in listdir(VIDEOS_PATH_ONBOARD):
if isfile(join(VIDEOS_PATH_ONBOARD, f)):
video_files_onboard_dict[f.split('.')[0]] = join(VIDEOS_PATH_ONBOARD, f)
video_files_nir_dict = {}
for f in listdir(VIDEOS_PATH_NIR):
if isfile(join(VIDEOS_PATH_NIR, f)):
video_files_nir_dict[f.split('.')[0]] = join(VIDEOS_PATH_NIR, f)
# Load the ground truth files paths for both onboard and onshore data and generate dictionaries with it.
object_gt_files_onshore_dict = {}
for f in listdir(OBJECT_ANNOTATIONS_ONSHORE_PATH):
if isfile(join(OBJECT_ANNOTATIONS_ONSHORE_PATH, f)):
object_gt_files_onshore_dict[f.split('.')[0].replace('_ObjectGT','')] = join(OBJECT_ANNOTATIONS_ONSHORE_PATH, f)
object_gt_files_onboard_dict = {}
for f in listdir(OBJECT_ANNOTATIONS_ONBOARD_PATH):
if isfile(join(OBJECT_ANNOTATIONS_ONBOARD_PATH, f)):
object_gt_files_onboard_dict[f.split('.')[0].replace('_ObjectGT','')] = join(OBJECT_ANNOTATIONS_ONBOARD_PATH, f)
object_gt_files_nir_dict = {}
for f in listdir(OBJECT_ANNOTATIONS_NIR_PATH):
if isfile(join(OBJECT_ANNOTATIONS_NIR_PATH, f)):
object_gt_files_nir_dict[f.split('.')[0].replace('_ObjectGT','')] = join(OBJECT_ANNOTATIONS_NIR_PATH, f)
# Do some sanity checks to see if there are equal numbers of videos and ground truth files.
print('Number of onshore videos: ', len(video_files_onshore_dict))
print('Number of onshore ground truth files: ', len(object_gt_files_onshore_dict))
print('Number of onboard videos: ', len(video_files_onboard_dict))
print('Number of onboard ground truth files: ', len(object_gt_files_onboard_dict))
print('Number of near infra-red videos: ', len(video_files_nir_dict))
print('Number of near infra-red ground truth files: ', len(object_gt_files_nir_dict))
# So there are videos without ground truth files. These unlabelled data might be good for testing later. Let's find these videos.
# ground truth files are missing - find the corresponding videos
missing_files_onshore = []
for key in video_files_onshore_dict.keys():
if key not in object_gt_files_onshore_dict:
missing_files_onshore.append(key)
print("Unlabelled onshore videos: ", missing_files_onshore)
missing_files_onboard = []
for key in video_files_onboard_dict.keys():
if key not in object_gt_files_onboard_dict:
missing_files_onboard.append(key)
print("Unlabelled onbord videos: ", missing_files_onboard)
missing_files_nir = []
for key in video_files_nir_dict.keys():
if key not in object_gt_files_nir_dict:
missing_files_nir.append(key)
print("Unlabelled nir videos: ", missing_files_nir)
# set whether to remove or not the missing videos from the frames generation later
remove_missing_files = True
if remove_missing_files:
for key in missing_files_onshore:
del video_files_onshore_dict[key]
for key in missing_files_onboard:
del video_files_onboard_dict[key]
for key in missing_files_nir:
del video_files_nir_dict[key]
print()
print('Size of video dictionaries after removing the videos without ground truth:')
print('Number of onshore videos: ', len(video_files_onshore_dict))
print('Number of onshore ground truth files: ', len(object_gt_files_onshore_dict))
print('Number of onboard videos: ', len(video_files_onboard_dict))
print('Number of onboard ground truth files: ', len(object_gt_files_onboard_dict))
print('Number of near infra-red videos: ', len(video_files_nir_dict))
print('Number of near infra-red ground truth files: ', len(object_gt_files_nir_dict))
# boolean to determine whether to have all frames in one or separate folders (onshore/onboard/nir)
SEPARATE_FOLDERS = False
TRAIN_PATH = '/MaritimeDataset/SMD2/train'
TEST_PATH = '/MaritimeDataset/SMD2/test'
VIDEO_FRAMES_FOLDER_NAME_ONSHORE = '/MaritimeDataset/SMD/VIS_Onshore/VIS_Onshore_frames'
VIDEO_FRAMES_FOLDER_NAME_ONBOARD = '/MaritimeDataset/SMD/VIS_Onboard/VIS_Onboard_frames'
VIDEO_FRAMES_FOLDER_NAME_NIR = '/MaritimeDataset/SMD2/NIR/NIR_frames/'
if SEPARATE_FOLDERS:
TRAIN_ONSHORE = join(TRAIN_PATH, VIDEO_FRAMES_FOLDER_NAME_ONSHORE)
TEST_ONSHORE = join(TEST_PATH, VIDEO_FRAMES_FOLDER_NAME_ONSHORE)
TRAIN_ONBOARD = join(TRAIN_PATH, VIDEO_FRAMES_FOLDER_NAME_ONBOARD)
TEST_ONBOARD = join(TEST_PATH, VIDEO_FRAMES_FOLDER_NAME_ONBOARD)
TRAIN_NIR = join(TRAIN_PATH, VIDEO_FRAMES_FOLDER_NAME_NIR)
TEST_NIR = join(TEST_PATH, VIDEO_FRAMES_FOLDER_NAME_NIR)
folder_names = [TRAIN_PATH, TEST_PATH, TRAIN_ONSHORE, TEST_ONSHORE, TRAIN_ONBOARD, TEST_ONBOARD, TRAIN_NIR, TEST_NIR]
else:
folder_names = [TRAIN_PATH, TEST_PATH]
# first create the folders if they don't exist
for folder_name in folder_names:
if not (os.path.isdir(folder_name)):
os.mkdir(folder_name)
# frame_space means save every 5th frame, we can change it accding to our need.
#train_test_split mean the ratio to split training and testing dataset. 0.7 means 70 % for training and 30 % for testing.
def convert_videos_to_frames(video_dict, paths, frame_space=5, train_test_split=0.7):
"""
Helper function to convert any video frames into jpg images and split them into training and test dataset.
Parameters
----------
video_dict : dictionary in the form <video_name>:<video_path>
paths : tuple of the training and test paths to save the images. If both a set to the same path
then all the generated frames will be place in this (same) directory.
frame_space : the space between the generated frames. Default is 5.
train_test_split : the ration to split the frames into train and test datasets. Default is 0.7
"""
train_path = paths[0]
test_path = paths[1]
for video_key in video_dict:
vidcap = cv2.VideoCapture(video_dict.get(video_key))
# get total frames of video
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
success,image = vidcap.read()
count = 0
frame_count = 0
success = True
while success:
if count % frame_space == 0:
if (count <= train_test_split*total_frames):
cv2.imwrite(join(train_path, video_key) + "_frame%d.jpg" % count, image) # save frame as JPEG file
else:
cv2.imwrite(join(test_path, video_key) + "_frame%d.jpg" % count, image)
frame_count += 1
success,image = vidcap.read()
#print('Read a new frame: ', success)
count += 1
print("Derived %d frames" % frame_count)
if SEPARATE_FOLDERS:
convert_videos_to_frames(video_files_onboard_dict, [TRAIN_ONBOARD, TEST_ONBOARD])
convert_videos_to_frames(video_files_onshore_dict, [TRAIN_ONSHORE, TEST_ONSHORE])
convert_videos_to_frames(video_files_nir_dict, [TRAIN_NIR, TEST_NIR])
else:
convert_videos_to_frames(video_files_onboard_dict, [TRAIN_PATH, TEST_PATH])
convert_videos_to_frames(video_files_onshore_dict, [TRAIN_PATH, TEST_PATH])
convert_videos_to_frames(video_files_nir_dict, [TRAIN_PATH, TEST_PATH])