-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvideo_process.py
More file actions
345 lines (271 loc) · 13.1 KB
/
Copy pathvideo_process.py
File metadata and controls
345 lines (271 loc) · 13.1 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import cv2
import numpy as np
from matplotlib import pyplot as plt
from sklearn.cluster import DBSCAN
from sklearn.cluster import KMeans
import hdbscan
import skimage.measure
import os
import sys
from util import *
import random
import math
import json
from collections import Counter
from img import CImage
class CVideo:
def __init__(self, video_name, frame_folder):
self.name = video_name
self.images = []
with open("%s/frames.txt" % frame_folder) as fin:
lines = fin.readlines()
self.frame_num = int(lines[0].strip())
self.filter_frames = [int(f) for f in lines[1].strip().split(" ")]
print 'frame number', self.frame_num, len(self.filter_frames)
for frame in range(1, self.frame_num+1):
if frame in self.filter_frames:
img = cv2.imread("%s/%d.png" % (frame_folder, frame))
cimg = CImage(frame, img)
cimg.preprocess()
self.images.append(cimg)
else:
# read later...
self.images.append(None)
if len(self.images) > 0:
self.height = self.images[0].height
self.width = self.images[0].width
print 'height/width', self.height, self.width
def cluster_lines(self, linemap="../linemap"):
lines = []
# frames = [image.name for image in self.images]
for idx, f in enumerate(self.filter_frames):
image = self.images[f-1]
# flatten_images.append(image.img_gray.flatten())
for line in image.v_long_lines:
lines.append(line + [idx])
for line in image.h_long_lines:
lines.append(line + [idx])
# lines.extend(image.v_long_lines])
# lines.extend(image.h_long_lines])
# print self.images[0].img_gray
# print flatten_images[0].reshape(self.height, self.width)
# clusters = KMeans(n_clusters=10, random_state=0).fit(np.array(flatten_images))
# print clusters.labels_
print 'clustering lines...'
dbscan = DBSCAN(eps=2, min_samples=len(
self.filter_frames)*0.2, metric=hv_line_overlap_sim)
clusters = dbscan.fit(np.array(lines))
# clusters = hdbscan.HDBSCAN().fit(np.array(flatten_images))
blank_image = np.zeros((self.height, self.width, 3), np.uint8)
cluster_num = max(clusters.labels_) + 1
cluster_lines = []
image_vectors = np.zeros((len(self.filter_frames), cluster_num))
cluster_lines = []
for i in range(0, cluster_num):
line_indexes = [idx for idx, l in enumerate(
clusters.labels_) if l == i]
color = (random.randint(0, 255), random.randint(
0, 255), random.randint(0, 255))
# temp_lines = [lines[l] for l in line_indexes]
# if temp_lines[0][0] == temp_lines[0][2]:
# temp_lines = sorted(temp_lines, key=lambda x:(x[3]-x[1]), reverse=True)
max_len = 0
longest = None
for l in line_indexes:
line = lines[l]
x1, y1, x2, y2, fid = line
line_len = math.sqrt((y2-y1) * (y2-y1) + (x2-x1) * (x2-x1))
if line_len > max_len:
max_len = line_len
longest = [x1, y1, x2, y2]
# fid = self.filter_frames.index(f)
# image_vectors[f, i] += 1
image_vectors[fid, i] = 1
cv2.line(blank_image, (x1, y1), (x2, y2), color, 1)
cluster_lines.append(longest)
print image_vectors
# clusters = hdbscan.HDBSCAN().fit(image_vectors)
print 'clustering images...'
clusters = DBSCAN(eps=1, min_samples=len(
self.filter_frames)*0.05).fit(image_vectors)
# clusters = DBSCAN(eps=0.1, min_samples=len(self.images)*0.01).fit(image_vectors)
res = {}
cluster_num = max(clusters.labels_) + 1
for i in range(0, cluster_num):
image_indexes = [idx for idx, l in enumerate(
clusters.labels_) if l == i]
print i
print [self.filter_frames[f] for f in image_indexes]
res[i] = {}
res[i]['frames'] = [self.filter_frames[f] for f in image_indexes]
blank_image2 = np.zeros((self.height, self.width, 3), np.uint8)
line_seg = image_vectors[image_indexes[0]]
res_lines = []
for idx, seg in enumerate(line_seg):
if seg == 1:
res_lines.append(cluster_lines[idx])
x1, y1, x2, y2 = cluster_lines[idx]
cv2.line(blank_image2, (x1, y1), (x2, y2), color, 1)
res[i]['linemap'] = res_lines
cv2.imwrite("%s/%s-%d.png" % (linemap, self.name, i), blank_image2)
with open("%s/%s.json" % (linemap, self.name), "w") as fout:
json.dump(res, fout, indent=4, default=dump_numpy)
cv2.imwrite("%s/%s.png" % (linemap, self.name), blank_image)
def crop_rects(self, linemap="../linemap", crop_out="Crops"):
with open('%s/%s.json' % (linemap, self.name)) as fin:
clusters = json.load(fin)
for cid in clusters:
cluster = clusters[cid]
if len(cluster['linemap']) == 0:
continue
hlines = [[int(x1), int(y1), int(x2), int(y2)]
for x1, y1, x2, y2 in cluster['linemap'] if y1 == y2]
vlines = [[int(x1), int(y1), int(x2), int(y2)]
for x1, y1, x2, y2 in cluster['linemap'] if x1 == x2]
vlines = sorted(vlines, key=lambda x: x[0])
hlines = sorted(hlines, key=lambda x: x[1])
if len(vlines) == 0 or abs(vlines[0][0] - 0) > self.width * 0.01:
vlines.insert(0, [0, 0, 0, self.height])
else:
vlines[0] = 0, 0, 0, self.height
if abs(self.width - vlines[-1][0]) > self.width * 0.01:
vlines.append([self.width, 0, self.width, self.height])
else:
vlines[-1] = self.width, 0, self.width, self.height
# vlines[-1][1], vlines[-1][3] = 0, self.height
if len(hlines) == 0 or abs(hlines[0][1] - 0) > self.height * 0.01:
hlines.insert(0, [0, 0, self.width, 0])
else:
hlines[0] = 0, 0, self.width, 0
# hlines[0][0], hlines[0][2] = 0, self.width
if abs(self.height - hlines[-1][1]) > self.height * 0.01:
hlines.append([0, self.height, self.width, self.height])
else:
hlines[-1] = 0, self.height, self.width, self.height
# hlines[-1][0], hlines[-1][2] = 0, self.width
print len(vlines), len(hlines)
xarray = []
for line in vlines:
x1, y1, x2, y2 = line
if len(xarray) == 0:
xarray.append({'x': x1, 'line': [line]})
else:
if xarray[-1]['x'] == x1:
xarray[-1]['line'].append(line)
else:
xarray.append({'x': x1, 'line': [line]})
yarray = []
for line in hlines:
x1, y1, x2, y2 = line
if len(yarray) == 0:
yarray.append({'y': y1, 'line': [line]})
else:
if yarray[-1]['y'] == y1:
yarray[-1]['line'].append(line)
else:
yarray.append({'y': y1, 'line': [line]})
# print len(xarray), len(yarray)
# print xarray
# print yarray
xy_matrix = np.zeros(
(len(xarray), len(yarray)), dtype=np.int64)
for i in range(len(xarray)):
for j in range(len(yarray)):
for vline in xarray[i]['line']:
for hline in yarray[j]['line']:
if vh_intersection(vline, hline):
xy_matrix[i, j] = 1
break
rects = find_rects(xy_matrix)
print rects
# line_img = cv2.imread("%s/%s-%s.png" % (linemap, self.name, cid))
selected_rects = []
for idx, r in enumerate(rects):
a, b, c, d = r
x1 = xarray[a]['x']
y1 = yarray[b]['y']
x2 = xarray[c]['x']
y2 = yarray[d]['y']
if (y2-y1) * (x2-x1) < self.width * self.height * 0.25:
continue
selected_rects.append([x1, y1, x2, y2])
# color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
# cv2.rectangle(line_img, (x1, y1), (x2, y2), color, 2)
# cv2.putText(line_img, str(idx), ((x1+x2)/2, (y1+y2)/2), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2)
if not os.path.exists('%s/%s' % (crop_out, self.name)):
os.mkdir('%s/%s' % (crop_out, self.name))
if not os.path.exists('%s/%s/%s' % (crop_out, self.name, cid)):
os.mkdir('%s/%s/%s' % (crop_out, self.name, cid))
print 'rects', selected_rects
for fidx, f in enumerate(cluster['frames']):
img = self.images[f-1].img
for idx, r in enumerate(selected_rects):
x1, y1, x2, y2 = r
cv2.imwrite("%s/%s/%s/%d_%d.png" % (crop_out,
self.name, cid, f, idx), img[y1:y2, x1:x2])
if fidx == len(cluster['frames']) - 1:
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
cv2.imwrite("%s/%s-%s-rect.png" %
(linemap, self.name, cid), img)
def detect_popup():
from shutil import copyfile
base_folder = "/Volumes/MYWD/Research/VideoAnalytics"
# video_name = "Java Programming Lets Build a Game 1"
video_name = "Intermediate Java Tutorial - 15 - Queue"
# video_name = "Java Programming"
crop_out = "%s/Crops" % base_folder
filename = "%s/%s/0/84_0.png" % (crop_out, video_name)
# filename = "%s/Crops/%s/1/379_0.png" % (base_folder, video_name)
# filename = "%s/Crops/%s/0/64_0.png" % (base_folder, video_name)
# filename = "%s/Images/%s/414.png" % (base_folder, video_name)
# filename = "%s/Images/%s/16.png" % (base_folder, video_name)
img = cv2.imread(filename)
myimg = CImage("test", img)
myimg.find_contours()
# cluster = 0
# crop_out = "%s/Crops" % base_folder
# for filename in os.listdir("%s/%s/%s" % (crop_out, video_name, cluster)):
# filepath = "%s/%s/%d/%s" % (crop_out, video_name, cluster, filename)
# img = cv2.imread(filepath)
# myimg = CImage("test", img)
# # print detect_background(img)
# rects = myimg.find_contours()
# if len(rects) > 0:
# copyfile(filepath, "%s/%s/Popup/%s" % (crop_out, video_name, filename))
# print filename, rects
# break
# else:
# jsonfile = filename.split(".")[0] + ".json"
# copyfile(filepath, "%s/%s/NoPopup/%s" % (crop_out, video_name, filename))
# copyfile("/Users/lingfengbao/Dropbox/VideoAnalytics/GoogleOCR/%s/1/%s" % (video_name, jsonfile), "/Users/lingfengbao/Downloads/JSON/%s" % jsonfile)
# myimg.detect_hv_lines2()
# myimg.show()
def main():
# detect_popup()
base_folder = "/Volumes/MYWD/Research/VideoAnalytics"
video_folder = "%s/Images" % base_folder
# # video_name = "Java Programming"
# # video_name = "Angular 2 Routing & Navigation Basics"
# # video_name = "email"
# # video_name = "Java Tutorial 11 GUI in Java JFrame JPanel JButton JLabel"
# # video_name = "Arrays - Java Tutorial 10"
# # video_name = "Java Programming Lets Build a Game 1"
# # video_name = "JUnit Tutorial 1"
# # video_name = "Java EE (J2EE) Tutorial for beginners Part6"
# video_name = "Intermediate Java Tutorial - 15 - Queue"
# video_name = "Java Threads Tutorial 2 - How to Create Threads in Java by Extending Thread Class"
# frame_folder = video_folder + "/" + video_name
for folder in os.listdir(video_folder):
print folder
frame_folder = video_folder + '/' + folder
if not os.path.exists('%s/frames.txt' % frame_folder):
preprocess(frame_folder)
# video = CVideo(video_name, frame_folder)
# video.cluster_lines()
# video.crop_rects(crop_out="%s/Crops" % base_folder)
# img = cv2.imread(frame_folder + "/16.png")
# cimg = CImage("16", img)
# cimg.preprocess()
# cimg.show()
if __name__ == '__main__':
main()