-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathretrack.py
1355 lines (1051 loc) · 33.4 KB
/
retrack.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import division
import os
import sys
import time
import numpy as np
import scipy.ndimage
import cv2
import json
from multiprocessing.pool import ThreadPool
from collections import deque
import pprint; pp = pprint.pprint
from mosse import MOSSE
from opencv_common import RectSelector
import ffwriter
from cachingvideoreader import RateChangedVideo
########################################################################
class VideoSource(object):
def __init__(self, vid, numcache=100, numstep=25, equalize=False):
self.vid = vid
self.index = -1 # just for relative addressing
self.numcache = numcache
self.numstep = numstep
self.equalize = equalize
self.cache = {} # index -> frame
#self.stripes = {} # index -> row
self.mru = [] # oldest -> newest
def cache_range(self, start, stop, delta=None):
"cache given range, possibly some more context"
# reversed playback?
if delta is not None:
if delta < 0:
# check if previous 5 frames are ok...
do_prefetch = not all(i in self.cache for i in xrange(start-5, start+1))
if do_prefetch:
start -= self.numstep
elif delta > 0:
stop += self.numstep
if start < 0:
start = 0
if stop >= totalframes:
stop = totalframes-1
assert 0 <= start <= stop < totalframes
# earlierst+last uncached frame?
original_requested = range(start, stop+1)
requested = [i for i in original_requested if i not in self.cache]
if not requested:
return
start = min(requested)
stop = max(requested)
requested = range(start, stop+1)
# do caching
vidpos = self.vid.tell()
if start != vidpos:
print "cache_range: seeking from {0} to {1}".format(vidpos, start)
assert start >= 0
self.vid.seek(start)
for i in requested:
rv = self.vid.grab()
assert rv
if i not in self.cache:
(rv, frame) = self.vid.retrieve()
assert rv
if rv:
if self.equalize:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = cv2.equalizeHist(frame)
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
self.cache[i] = frame
self.mru = [i for i in self.mru if i not in original_requested] + original_requested
self.mru = self.mru[-self.numcache:]
self.cache = { i: frame for i,frame in self.cache.iteritems() if i in self.mru }
def read(self, newindex=None):
if newindex is None:
newindex = self.index + 1
delta = newindex - self.index
if not (0 <= newindex < totalframes):
return None
self.cache_range(newindex, newindex)
assert newindex in self.cache
self.index = newindex
return self.cache[self.index]
########################################################################
keyframe_dtype = np.dtype({
'names': 'x y rx ry xy roi'.split(),
'formats': 'f4 f4 f4 f4 2f4 2f4'.split(),
'offsets': [0, 4, 8, 12, 0, 8]
})
def make_keyframe(*a, **kw):
result = np.empty((), dtype=keyframe_dtype)
result['x'] = result['y'] = np.nan
result['roi'] = (0,0)
if a:
result['xy'] = a[:2]
if a[2:]:
result['roi'] = a[2:]
if kw:
if 'xy' in kw:
result['xy'] = kw['xy']
if 'roi' in kw:
result['roi'] = kw['roi']
return result
def x0wh2rect(x0, wh):
return np.hstack([x0, x0 + wh])
def cxwh2rect(cx, wh):
wh2 = wh / 2
return np.hstack([cx - wh2, cx + wh2])
def rect2x0wh(rect):
x0 = rect[0:2]
wh = rect[2:4] - x0
return (x0, wh)
def rect2cxwh(rect):
wh = rect[2:4] - rect[0:2]
cx = (rect[0:2] + rect[2:4]) / 2
return (cx, wh)
########################################################################
VK_LEFT = 2424832
VK_RIGHT = 2555904
VK_SPACE = 32
VK_PGUP = 2162688
VK_PGDN = 2228224
VK_POS1 = 2359296
VK_END = 2293760
def iround(x):
return int(round(x))
def sgn(x):
return (x > 0) - (x < 0)
def fix8(a):
if isinstance(a, (int, float)):
return a * 256
else:
return tuple((np.array(a) * 256).round().astype(np.int32))
def redraw_display():
#print "redraw"
if mousedown:
anchorcolor = (255, 0, 0)
else:
anchorcolor = (255, 255, 0)
# anchor is animated
(xmin, xmax) = meta['anchor_x_range']
(ymin, ymax) = meta['anchor_y_range']
# anchor within bounds
(ax, ay) = canchor = np.clip(anchor, [xmin, ymin], [xmax, ymax])
# anchor cross will be updated
cpos = np.float32(position) + (anchor - canchor) * meta['scale']
# *scale to compensate the offset in screen space
Anchor = np.matrix([
[1, 0, -ax],
[0, 1, -ay],
[0, 0, 1.0],
])
InvAnchor = np.linalg.inv(Anchor)
scale = meta['scale']
Scale = np.matrix([
[scale, 0, 0],
[0, scale, 0],
[0, 0, 1.0]
])
# position is fixed in meta
Translate = np.matrix([
[1, 0, position[0]],
[0, 1, position[1]],
[0, 0, 1.0]
])
M = Translate * Scale * Anchor
InvM = np.linalg.inv(M)
if draw_output:
if curframe is None:
surface = np.zeros((screenh, screenw, 3), dtype=np.uint8)
else:
surface = cv2.warpAffine(curframe, M[0:2,:], (screenw, screenh), flags=cv2.INTER_AREA)
cv2.line(surface,
fix8(cpos + (+10, +10)),
fix8(cpos - (+10, +10)),
anchorcolor, thickness=iround(1/dispscale), shift=8, lineType=cv2.LINE_AA)
cv2.line(surface,
fix8(cpos + (+10, -10)),
fix8(cpos - (+10, -10)),
anchorcolor, thickness=iround(1/dispscale), shift=8, lineType=cv2.LINE_AA)
timepos = (screenw * src.index / totalframes)
cv2.line(surface,
fix8([timepos, 0]),
fix8([timepos, 20]),
(255, 255, 0), thickness=iround(2/dispscale), shift=8, lineType=cv2.LINE_AA)
cv2.imshow("output", surface)
if draw_input and curframe is not None:
source = curframe.copy()
cv2.line(source,
fix8(anchor - 10),
fix8(anchor + 10),
anchorcolor, thickness=iround(1/dispscale), shift=8, lineType=cv2.LINE_AA)
cv2.line(source,
fix8(anchor + (+10, -10)),
fix8(anchor - (+10, -10)),
anchorcolor, thickness=iround(1/dispscale), shift=8, lineType=cv2.LINE_AA)
TL = InvM * np.matrix([[0, 0, 1]]).T
BR = InvM * np.matrix([[screenw, screenh, 1]]).T
cv2.rectangle(source,
fix8(np.array(TL)[0:2,0]),
fix8(np.array(BR)[0:2,0]),
(255, 0, 0), thickness=iround(1/dispscale), shift=8, lineType=cv2.LINE_AA)
secs = src.index / framerate
hours, secs = divmod(secs, 3600)
mins, secs = divmod(secs, 60)
cv2.rectangle(source,
(0, srch),
(srcw, srch-70),
(0,0,0), cv2.FILLED)
text = "{h:.0f}:{m:02.0f}:{s:06.3f} / frame {frame}".format(h=hours, m=mins, s=secs, frame=src.index)
cv2.putText(source,
text,
(10, srch-10), cv2.FONT_HERSHEY_PLAIN, 4, (255,255,255), 3)
if use_faces:
# faces are in source coordinates and scale
if faces_roi is not None:
cv2.rectangle(source,
fix8(faces_roi[0:2]), fix8(faces_roi[2:4]),
(0, 0, 160), thickness=iround(1/dispscale), shift=8, lineType=cv2.LINE_AA)
for face in faces:
facewh = face[2:4] - face[0:2]
fanchor = face[0:2] + facewh * face_anchor
cv2.polylines(source,
fix8([
[fanchor + facewh * 0.05, fanchor - facewh * 0.05],
[fanchor + facewh * (+1,-1) * 0.05, fanchor - facewh * (+1,-1) * 0.05]
]),
False,
(0, 255, 0), thickness=iround(1/dispscale), shift=8, lineType=cv2.LINE_AA)
cv2.rectangle(source,
fix8(face[0:2]), fix8(face[2:4]),
(0, 255, 0), thickness=iround(1/dispscale), shift=8, lineType=cv2.LINE_AA)
if use_tracker:
tracker_rectsel.draw(source)
if (keyframes[src.index]['roi'] > 0).all():
radius = keyframes[src.index]['roi'] // 2
cv2.rectangle(source,
fix8(anchor - radius), fix8(anchor + radius),
(0, 255, 0), thickness=iround(1/dispscale), shift=8, lineType=cv2.LINE_AA)
if tracker:
# scale to source resolution
tracker.draw_state(source, 1 / trackerscale)
cv2.imshow("source", source)
if tracker and draw_tracker:
cv2.imshow('tracker state', tracker.state_vis)
if draw_graph:
global graphbg, graphbg_head, graphbg_indices
# graphslices/2 is midpoint
# draw this range
imax = src.index + graphslices//2
imin = imax - graphslices
indices = range(imax, imin, -1)
if graphbg is None: # full redraw
t0 = time.clock()
graphbg = [
src.cache[i][np.clip(get_keyframe(i)[1], 0, srch-1)] if (i in src.cache) else emptyrow
for i in indices
]
t1 = time.clock()
graphbg = np.array(graphbg, dtype=np.uint8)
t2 = time.clock()
graphbg_head = imax
graphbg_indices = set(indices) & set(src.cache)
print "graphbg redraw {0:.3f} {1:.3f}".format(t1-t0, t2-t1)
if graphbg_head != imax: # scrolling to current position
shift = imax - graphbg_head
graphbg = np.roll(graphbg, shift, axis=0)
oldhead = graphbg_head
graphbg_head = imax
# replace rolled-over lines
ashift = min(graphslices, abs(shift))
if shift > 0:
#import pdb; pdb.set_trace()
newindices = xrange(imax, imax-ashift, -1)
graphbg_indices = set(i for i in graphbg_indices if i > imin)
elif shift < 0:
#import pdb; pdb.set_trace()
newindices = xrange(imin+ashift, imin, -1)
graphbg_indices = set(i for i in graphbg_indices if i <= imax)
replacements = [
src.cache[i][np.clip(get_keyframe(i)[1], 0, srch-1)] if (i in src.cache) else emptyrow
for i in newindices
]
graphbg_indices.update( set(newindices) & set(src.cache) )
if shift > 0:
graphbg[:ashift] = replacements
elif shift < 0:
graphbg[-ashift:] = replacements
updates = (set(indices) & set(src.cache)) - graphbg_indices
if updates:
for i in updates:
graphbg[graphbg_head - i] = src.cache[i][np.clip(get_keyframe(i)[1], 0, srch-1)]
graphbg_indices.update(updates)
graph = cv2.resize(graphbg, (srcw, graphheight), interpolation=cv2.INTER_NEAREST)
lineindices = [
i for i in range(imin, imax+1)
if (0 <= i < totalframes) and keyframe_is_valid(i)
]
lines = np.int32([
fix8([ keyframes[index]['x'], (imax - index) * graphscale ])
for index in lineindices
])
now = (imax - src.index) * graphscale
cv2.line(graph,
fix8([0, now]),
fix8([srcw, now]),
(255, 255, 255), thickness=1, shift=8, lineType=cv2.LINE_AA)
if len(lines) > 0:
cv2.polylines(
graph,
[lines],
False,
(255, 255, 0),
thickness=1,
shift=8, lineType=cv2.LINE_AA
)
for i,pos in zip(lineindices, lines):
x,y = pos
points = np.array(map(get_keyframe, [i-1, i, i+1]))
d2 = (points[0]+points[2])/2.0 - points[1]
d2 *= 100
spread = d2[0]
spread = np.array([max(-spread, 0), max(spread, 0)]) + 5
spread += abs(d2[1])
thickness = 1
color = (0, 255, 255)
if graphsel_start is not None:
if graphsel_start <= i <= graphsel_stop:
thickness = 3
color = (255,255,255)
spread += 3
cv2.line(
graph,
(x-int(spread[0] * 256), y), (x+int(spread[1] * 256), y),
color,
thickness=thickness, shift=8, lineType=cv2.LINE_AA)
secs = src.index / framerate
hours, secs = divmod(secs, 3600)
mins, secs = divmod(secs, 60)
#cv2.rectangle(graph,
# (0, screenh), (screenw, screenh-70),
# (0,0,0),
# cv2.FILLED
# )
text = "{h:.0f}:{m:02.0f}:{s:06.3f} / frame {frame}".format(h=hours, m=mins, s=secs, frame=src.index)
cv2.putText(graph,
text,
(10, graphheight-10), cv2.FONT_HERSHEY_PLAIN, 4, (0,0,255), 3)
cv2.imshow("graph", graph)
# TODO: onmouse_* scroll wheel events
def CV_GET_WHEEL_DELTA(flags):
return (flags >> 16) / 120.0
def onmouse_scroll(event, x, y, flags, userdata):
# 'EVENT_MOUSEWHEEL'
delta = -CV_GET_WHEEL_DELTA(flags)
print "scroll {0:+.1f}".format(delta)
load_delta_frame(int(delta))
def onmouse_source(event, x, y, flags, userdata):
global mousedown, redraw
if event == cv2.EVENT_MOUSEWHEEL:
onmouse_scroll(event, x, y, flags, userdata)
if use_tracker:
tracker_rectsel.onmouse(event, x, y, flags, userdata)
redraw = True
return
# TODO: reorder this to allow repositioning the tracker without resizing tracker rect
# probably need to get tracker_rectsel. if wh=0, it's a click
if event == cv2.EVENT_MOUSEMOVE:
#print "move", event, (x,y), flags
if flags == cv2.EVENT_FLAG_LBUTTON:
#print "onmouse move lbutton", (x,y), flags, userdata
set_anchor([x,y], roi=0)
elif event == cv2.EVENT_LBUTTONDOWN:
#print "onmouse buttondown", (x,y), flags, userdata
mousedown = True
set_anchor([x,y], roi=0)
elif event == cv2.EVENT_LBUTTONUP:
#print "onmouse buttonup", (x,y), flags, userdata
set_anchor([x,y], roi=0)
mousedown = False
def onmouse_output(event, x, y, flags, userdata):
if event == cv2.EVENT_MOUSEWHEEL:
onmouse_scroll(event, x, y, flags, userdata)
if (event == cv2.EVENT_LBUTTONDOWN) or (event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON):
newindex = iround(totalframes * x / screenw)
load_this_frame(newindex)
def onmouse_graph(event, x, y, flags, userdata):
global redraw
curindex = graphbg_head - iround(y / graphscale)
if curindex < 0:
curindex = 0
elif curindex >= totalframes:
curindex = totalframes - 1
global graphsel_start, graphsel_stop
if event == cv2.EVENT_MOUSEWHEEL:
onmouse_scroll(event, x, y, flags, userdata)
# implement some selection dragging (for smoothing and deleting)
if event in (cv2.EVENT_MBUTTONDOWN, cv2.EVENT_RBUTTONDOWN):
graphsel_start = curindex
graphsel_stop = curindex
redraw = True
elif event == cv2.EVENT_MOUSEMOVE and flags in (cv2.EVENT_FLAG_MBUTTON, cv2.EVENT_FLAG_RBUTTON):
graphsel_stop = curindex
redraw = True
elif graphsel_start is not None and event in (cv2.EVENT_MBUTTONUP, cv2.EVENT_RBUTTONUP):
graphsel_stop = curindex
redraw = True
indices = range(graphsel_start, graphsel_stop+1)
# prepare to undo this
oldkeyframes = {
i: keyframes[i]
for i in indices
if keyframe_is_valid(i)
}
def undo():
keyframes[indices] = empty_keyframe
for i in oldkeyframes:
keyframes[i] = oldkeyframes[i]
undoqueue.append(undo)
while len(undoqueue) > 100:
undoqueue.pop(0)
graphsel_start = None
### graph smoothing
if event is cv2.EVENT_RBUTTONUP:
updates = { i: smoothed_keyframe(i) for i in indices }
for i in indices:
keyframes[i] = updates[i]
### graph smoothing
if event is cv2.EVENT_MBUTTONUP:
keyframes[indices] = make_keyframe()
if graphdraw:
if (event == cv2.EVENT_LBUTTONDOWN) or (event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON):
(ax,ay) = get_keyframe(curindex) # interpolated if needed
ax = x
keyframes[curindex] = make_keyframe(ax, ay)
redraw = True
else:
if (event == cv2.EVENT_LBUTTONDOWN):
load_this_frame(curindex)
smoothing_radius = 2
smoothing_kernel = range(-smoothing_radius, +smoothing_radius+1)
def smoothed_keyframe(i):
#import pdb; pdb.set_trace()
return np.sum([get_keyframe(i+j) for j in smoothing_kernel], axis=0, dtype=np.float32) / len(smoothing_kernel)
def set_anchor(newanchor, roi):
global anchor, redraw
newanchor = np.float32(newanchor)
anchor = newanchor
keyframes[src.index] = make_keyframe(anchor[0], anchor[1], roi=roi or 0)
redraw = True
#print "set anchor", anchor
def save_meta(do_query=False):
# meta file
output = json.dumps(meta, indent=2, sort_keys=True)
do_write = True
exists = os.path.exists(metafile)
if exists:
do_write &= (open(metafile).read() != output)
if do_query and do_write:
do_write &= (raw_input("write meta file? (y/n) ").lower().startswith('y'))
if do_write:
if exists:
bakfile = "{0}.bak".format(metafile)
if os.path.exists(bakfile):
os.unlink(bakfile)
os.rename(metafile, bakfile)
open(metafile, "w").write(output)
print "wrote metafile"
# TODO: this is ugly, wrap it or poke numpy to do it
def save_keyframes(do_query=False):
global keyframes
if not meta['keyframes'].endswith('.bin'):
(filebase, fileext) = os.path.splitext(meta['keyframes'])
filename = "{0}.bin".format(filebase)
meta['keyframes'] = filename
print "changing keyframe file extension to .bin"
save_meta(do_query=True)
filename = os.path.join(
os.path.dirname(metafile), meta['keyframes'])
if isinstance(keyframes, np.memmap):
keyframes.flush()
print "keyframes flushed to disk."
else:
mapped = np.memmap(filename, shape=keyframes.shape, dtype=keyframe_dtype, mode='w+')
mapped[:] = keyframes
mapped.flush()
keyframes = mapped
print "keyframes saved and reloaded as mmap"
def load_keyframes(count):
global keyframes
filename = os.path.join(
os.path.dirname(metafile), meta['keyframes'])
do_save = False
if not os.path.exists(filename):
keyframes = np.empty((count,), dtype=keyframe_dtype)
keyframes[:] = make_keyframe()
do_save = True
else:
if filename.endswith('.json'):
keyframes = np.array([
make_keyframe(xy=keyframe if (keyframe is not None) else np.nan)
for keyframe
in json.load(open(filename))
], dtype=keyframe_dtype)
do_save = True
elif filename.endswith('.bin'):
keyframes = np.memmap(filename, dtype=keyframe_dtype, mode='r+')
if len(keyframes) < count:
oldcount = keyframes.shape[0]
keyframes.resize((count,))
keyframes[oldcount:count] = make_keyframe()
do_save = True
assert keyframes.dtype == keyframe_dtype, (keyframes.dtype, keyframe_dtype)
if do_save:
save_keyframes()
def save(do_query=False):
save_meta(do_query)
save_keyframes(do_query)
def scan_nonempty(pos, step):
if step < 0:
while pos >= 0 and step < 0:
if keyframe_is_valid(pos):
return pos
pos -= 1
step += 1
else:
return None
elif step > 0:
while pos < totalframes and step > 0:
if keyframe_is_valid(pos):
return pos
pos += 1
step -= 1
else:
return None
return None
def keyframe_is_valid(index=None):
if index is None:
return ~np.isnan(keyframes['xy']).any(axis=1)
if not (0 <= index < totalframes):
return False
return not np.isnan(keyframes[index]['xy']).any()
def get_keyframe(index):
if not (0 <= index < totalframes):
return np.float32(meta['anchor'])
if keyframe_is_valid(index):
return keyframes[index]['xy']
else:
prev = scan_nonempty(index-1, -100)
next = scan_nonempty(index+1, +100)
if prev is None and next is None:
return np.float32(meta['anchor'])
if prev is None:
return np.float32(keyframes[next]['xy'])
else:
kprev = keyframes[prev]['xy']
if next is None:
return np.float32(keyframes[prev]['xy'])
else:
knext = keyframes[next]['xy']
alpha = (index - prev) / (next-prev)
#print "alpha", alpha, index, prev, next
u = kprev
v = knext
return np.float32(0.5 + u + alpha * (v-u))
def on_tracker_rect(rect):
print "rect selected:", rect
init_tracker(rect)
def init_tracker(rect):
global tracker
tracker = MOSSE(curframe_gray, tracker_downscale(rect))
set_anchor(tracker_upscale(tracker.pos), roi=tracker_upscale(tracker.size))
print "tracked initialized"
def load_delta_frame(tdelta):
global redraw
result = None # True ~ stop
if tdelta in (-1, +1):
oldanchor = anchor
load_this_frame(src.index + tdelta, False) # changes anchor
if curframe is not None:
newanchor = oldanchor.copy()
if tracker:
xydelta = tracker.track(curframe_gray, pos=(oldanchor * trackerscale)) / trackerscale
if tracker.good:
newanchor += xydelta
else:
result = True # stop
print "tracking bad, aborting"
if use_faces: # and tracker and tracker.good:
global faces_roi # will be set
if tracker:
trackersize = np.float32(tracker.size) / trackerscale # from tracker scale to source scale
faces_roi = np.hstack([
newanchor + trackersize * faces_rel_roi[0:2],
newanchor + trackersize * faces_rel_roi[2:4]
])
else:
faces_roi = None
global faces
faces = detect_faces(subrect=faces_roi)
if tracker and tracker.good and len(faces) >= 1:
faces.sort(key=(lambda face:
np.linalg.norm(
newanchor - \
(face[0:2] + (face[2:4] - face[0:2]) * face_anchor))))
face = faces[0]
facesize = face[2:4] - face[0:2]
faceanchor = face[0:2] + facesize * face_anchor
newanchor += (faceanchor - newanchor) * face_attract_rate
redraw = True
if tracker and tracker.good:
# use (dx,dy) from above, possibly updated by face pos
tracker.adapt(curframe_gray, rate=tracker_adapt_rate, pos=(newanchor * trackerscale))
set_anchor(newanchor, roi=tracker_upscale(tracker.size))
# update xt
if draw_graph:
graphbg[graphbg_head - src.index] = \
src.cache[src.index][ np.clip(newanchor[1], 0, srch-1) ]
else: # big jump
load_this_frame(src.index + tdelta, bool(tracker))
if curframe is None:
return True # stop
if (tdelta > 0) and (graphbg_head is not None) and (draw_graph):
imax = graphbg_head
imin = imax - graphslices//2
src.cache_range(imin, imax)
return result
def load_this_frame(index=None, update_tracker=True, only_decode=False):
global curframe, curframe_gray, redraw, anchor
if index is None:
index = src.index
if index < 0:
index = 0
if index >= totalframes:
index = totalframes-1
assert 0 <= index < totalframes
delta = index - src.index
curframe = src.read(index) # sets src.index
assert curframe is not None
if not only_decode:
curframe_gray = cv2.resize(
cv2.cvtColor(curframe, cv2.COLOR_BGR2GRAY),
dsize=None,
fx=trackerscale, fy=trackerscale,
interpolation=cv2.INTER_AREA)
anchor = get_keyframe(src.index)
#print "frame", src.index, "anchor {0:8.3f} x {1:8.3f}".format(*anchor)
if update_tracker and tracker and not only_decode:
print "set tracker to", tracker.pos
tracker.pos = tracker_downscale(anchor)
if abs(delta) > 1 and use_tracker and tracker and (tracker_rectsel.drag_radius is not None):
(tx,ty) = anchor
if (keyframes[src.index]['roi'] > 0).all():
(rx, ry) = keyframes[src.index]['roi'] // 2
else:
(rx, ry) = tracker_rectsel.drag_radius
if rx > 0 and ry > 0:
print "resetting tracker"
newrect = (tx-rx, ty-ry, tx+rx, ty+ry)
init_tracker(newrect)
redraw = True
def tracker_upscale(point):
return tuple(v / trackerscale for v in point)
def tracker_downscale(point):
return tuple(v * trackerscale for v in point)
def interpolate_nans(arr, goodmask):
badmask = ~goodmask
arr[badmask] = np.interp(
np.flatnonzero(badmask),
np.flatnonzero(goodmask),
arr[goodmask])
def dump_video(videodest):
output = np.zeros((totalframes, 2), dtype=np.float32)
prevgood = None
nextgood = None
output[:] = keyframes['xy']
mask = keyframe_is_valid()
interpolate_nans(output[:,0], mask)
interpolate_nans(output[:,1], mask)
(xmin, xmax) = meta['anchor_x_range']
(ymin, ymax) = meta['anchor_y_range']
output[output[:,0] < xmin, 0] = xmin
output[output[:,0] > xmax, 0] = xmax
output[output[:,1] < ymin, 1] = ymin
output[output[:,1] > ymax, 1] = ymax
sigma = meta.get('sigma', 0)
if sigma > 0:
sigma *= framerate
output[:,0] = scipy.ndimage.filters.gaussian_filter(output[:,0], sigma)
output[:,1] = scipy.ndimage.filters.gaussian_filter(output[:,1], sigma)
do_pieces = ('%' in videodest)
outseq = 1
outvid = None
for i,k in enumerate(output):
if do_pieces and (i % int(600 * framerate) == 0) and (outvid is not None):
outvid.release()
outvid = None
outseq += 1
if outvid is None:
if i == 0:
fourcc = -1 # user config
else:
fourcc = cv2.VideoWriter_fourcc(*"X264")
outvid = ffwriter.FFWriter(
videodest,
framerate, (screenw, screenh),
codec='libx264', pixfmt='bgr24',
moreflags='-loglevel 32 -pix_fmt yuv420p -crf 15 -preset ultrafast')
#outvid = cv2.VideoWriter(videodest % outseq, fourcc, framerate, (screenw, screenh))
#assert outvid.isOpened()
load_this_frame(i, only_decode=True)
# anchor is animated
(ax,ay) = k
Anchor = np.matrix([
[1, 0, -ax],
[0, 1, -ay],
[0, 0, 1.0],
])
InvAnchor = np.linalg.inv(Anchor)
scale = meta['scale']
Scale = np.matrix([
[scale, 0, 0],
[0, scale, 0],
[0, 0, 1.0]
])
# position is fixed in meta
Translate = np.matrix([
[1, 0, position[0]],
[0, 1, position[1]],
[0, 0, 1.0]
])
M = Translate * Scale * Anchor
InvM = np.linalg.inv(M)
#surface = cv2.warpAffine(curframe, M[0:2,:], (screenw, screenh), flags=cv2.INTER_CUBIC)
surface = cv2.warpAffine(curframe, M[0:2,:], (screenw, screenh), flags=cv2.INTER_LINEAR)
outvid.write(surface)
if i % 10 == 0:
#sys.stdout.write("\rframe {0} of {1} written ({2:.3f}%)".format(i, totalframes, 100.0 * i/totalframes))
sys.stdout.flush()
cv2.imshow("rendered", cv2.pyrDown(surface))
key = cv2.waitKey(1)
if key == 27: break
cv2.destroyWindow("rendered")
outvid.release()
print "done"
def detect_faces(subrect=None):
# http://docs.opencv.org/modules/objdetect/doc/cascade_classification.html#cascadeclassifier-detectmultiscale
# expects U8 input (gray)
facesize = minfacesize # full region
image = curframe_gray
if subrect is not None:
cliprect = np.clip(subrect, [0,0,0,0], [srcw, srch, srcw, srch])
trackrect = cliprect * trackerscale
(x0,y0,x1,y1) = trackrect.round().astype(np.int32)
facesize = min(facesize, int(min(x1-x0, y1-y0) * 0.3))
image = image[y0:y1, x0:x1]
if meta.get('face_flip', False):
image = np.fliplr(image)
faces = face_cascade.detectMultiScale(
image,
scaleFactor=1.3, minNeighbors=2, minSize=(facesize, facesize), flags=cv2.CASCADE_SCALE_IMAGE)
if len(faces) == 0:
return []
if meta.get('face_flip', False):
faces[:,0] = image.shape[1] - faces[:,0] - faces[:,2]
if subrect is not None:
faces[:,0:2] += (x0,y0)
faces[:,2:4] += faces[:,0:2]
faces = faces / trackerscale
return list(faces)
draw_input = True
draw_output = True
draw_graph = True
draw_tracker = True
dispscale = 0.5
graphbg = None
graphbg_head = None
graphbg_indices = set()
graphdraw = False
graphsel_start = None