-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadvideos.py
208 lines (183 loc) · 6.32 KB
/
loadvideos.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 4 15:07:05 2019
Small cv tool for annotating videos. During reading you can make superevents,
which you can annotate later in more detail.
@author: jakob
"""
import numpy as np
import tensorflow as tf
import cv2
import matplotlib.pylab as plt
import math
import os
import file_tools
def vid_to_img(file_path, save_path, size=(128,128)):
"""
Take superevents in image stream and repeat short sequences around the
superevent for precise annotation
Keys:
q = quit
SPACE = set superevent
Params
file_path = file_path to video
save_path = path were to save the images, if "" save as npy
size = out size of the images
"""
cap = cv2.VideoCapture(file_path)
org_fps = cap.get(cv2.CAP_PROP_FPS)
tmp = cap.get(cv2.CAP_PROP_FRAME_COUNT)
print("Original fps: ", org_fps)
print("Total number of frames: ", tmp)
count = 0
fl = []
intake = []
erri = 0
while(cap.isOpened()):
frame_id = cap.get(1)
ret, frame = cap.read()
if (ret is False):
print("cv error, frame: ", frame_id)
if erri > 100:
break
erri += 1
if count == tmp:
break
# if (frame_id % math.floor(fps) == 0):
if frame is not None:
cv2.namedWindow("output", cv2.WINDOW_NORMAL)
cv2.imshow('gray', frame)
frame = frame[:,:,::-1]
resized = cv2.resize(frame,(224,224),cv2.INTER_AREA)
fl.append(resized.copy())
if save_path is not "":
cv2.imwrite(save_path+str(int(frame_id))+'.jpg', resized[:,:,::-1])
key = cv2.waitKey(10)
if key == 32:
print("added intake", count)
intake.append(count)
if key == ord('q'):
break
print('\r\r\r', frame_id, ret, end='')
count += 1
cap.release()
return fl, intake
def labels(args):
pass
def repeat(images, labels, idx):
"""
Take superevents in image stream and repeat short sequences around the
superevent for precise annotation
Keys:
q = next superevent
a = add endtime
s = add starttime
r = reset labels
SPACE = pause/restart:
n = next frame
b = previous frame
1 = null
2 = fetch
3 = eat
4 = drnk
5 = return
Params
images = list of images
labels = list of labels
idx = list of superevents
"""
annotations = np.zeros(len(images))
i=0
count = 0
for i in idx:
start_count = int(i - 50)
end_count = int(i + 50)
count = start_count
anno = 0
while(1):
key = cv2.waitKey(50)
if key == ord('q'):
break
if key == ord('a'):
print("added endtime")
end_count += 20
if key == ord('s'):
print("added starttime")
start_count -= 20
if key == ord('r'):
print("reset to null")
annotations[start_count:end_count] = 0
if key == 32:
while(1):
# pause mode
k = cv2.waitKey()
if k == 32:
# resume
break
if k == ord('n'):
# one image further
count += 1
annotations[count] = anno
print("frame ", count, " is " + labels[int(annotations[count])])
if count > end_count:
count = start_count
cv2.imshow('', cv2.resize(images[count],(3*224,3*224),cv2.INTER_AREA))
if k == ord('b'):
# one image back
count -= 1
annotations[count] = anno
print("frame ", count, " is " + labels[int(annotations[count])])
if count < start_count:
count = end_count
cv2.imshow('', cv2.resize(images[count],(3*224,3*224),cv2.INTER_AREA))
# labeling
if k == ord('1'):
annotations[count] = 0
anno = 0
print("frame ", count, " is " + labels[0])
if k == ord('2'):
annotations[count] = 1
anno = 1
print("frame ", count, " is " + labels[1])
if k == ord('3'):
annotations[count] = 2
anno = 2
print("frame ", count, " is " + labels[2])
if k == ord('4'):
annotations[count] = 3
anno = 3
print("frame ", count, " is " + labels[3])
if k == ord('5'):
annotations[count] = 4
anno = 4
print("frame ", count, " is " + labels[4])
cv2.imshow('', cv2.resize(images[count],(3*224,3*224),cv2.INTER_AREA))
print("frame ", count, " is " + labels[int(annotations[count])])
count +=1
if count > end_count:
count = start_count
cv2.destroyAllWindows()
return annotations
#class Data():
# def __init__(self, data, annotations, name):
# self.data = np.array(data)
# self.labels = np.array(annotations)
# self.samples = self.data.shape[0]
# self.width = self.data.shape[1]
# self.height = self.data.shape[2]
# self.channels = self.data.shape[3]
# self.name = name
# self.label_names = ['null', 'fetch', 'eat', 'drink', 'return']
if __name__ == '__main__':
# file = ''
file = ''
loads, saves, names = file_tools.list_dir(file)
count = 0
# for i in loads:
save = saves[count]
fl, intake = vid_to_img(loads[count], "", 1)
annotations = repeat(fl, ['null', 'fetch', 'eat', 'drink', 'return'], intake)
np.save(save + 'data.npy', fl)
np.save(saves[count] + '/anno.npy', annotations)
# count += 1