Welcome to mirror list, hosted at ThFree Co, Russian Federation.

space_clip_editor_autotracker.py - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1cd20886a041ea98107f6366c52dd57a0dc7558f (plain)
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
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

bl_info = {
    "name": "Autotrack",
    "author": "Miika Puustinen, Matti Kaihola, Stephen Leger",
    "version": (0, 1, 1),
    "blender": (2, 78, 0),
    "location": "Movie clip Editor > Tools Panel > Autotrack",
    "description": "Motion Tracking with automatic feature detection.",
    "warning": "",
    "wiki_url": "https://github.com/miikapuustinen/blender_autotracker",
    "category": "Motion Tracking",
    }

import bpy
import bgl
import blf
from bpy.types import (
        Operator,
        Panel,
        PropertyGroup,
        WindowManager,
        )
from bpy.props import (
        BoolProperty,
        FloatProperty,
        IntProperty,
        EnumProperty,
        PointerProperty,
        )

# for debug purposes
import time

# set to True to enable debug prints
DEBUG = False


# pass variables just like for the regular prints
def debug_print(*args, **kwargs):
    global DEBUG
    if DEBUG:
        print(*args, **kwargs)


# http://blenderscripting.blogspot.ch/2011/07/bgl-drawing-with-opengl-onto-blender-25.html
class GlDrawOnScreen():
    black = (0.0, 0.0, 0.0, 0.7)
    white = (1.0, 1.0, 1.0, 0.5)
    progress_colour = (0.2, 0.7, 0.2, 0.5)

    def String(self, text, x, y, size, colour):
        ''' my_string : the text we want to print
            pos_x, pos_y : coordinates in integer values
            size : font height.
            colour : used for definining the colour'''
        dpi, font_id = 72, 0   # dirty fast assignment
        bgl.glColor4f(*colour)
        blf.position(font_id, x, y, 0)
        blf.size(font_id, size, dpi)
        blf.draw(font_id, text)

    def _end(self):
        bgl.glEnd()
        bgl.glPopAttrib()
        bgl.glLineWidth(1)
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glColor4f(0.0, 0.0, 0.0, 1.0)

    def _start_line(self, colour, width=2, style=bgl.GL_LINE_STIPPLE):
        bgl.glPushAttrib(bgl.GL_ENABLE_BIT)
        bgl.glLineStipple(1, 0x9999)
        bgl.glEnable(style)
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glColor4f(*colour)
        bgl.glLineWidth(width)
        bgl.glBegin(bgl.GL_LINE_STRIP)

    def Rectangle(self, x0, y0, x1, y1, colour, width=2, style=bgl.GL_LINE):
        self._start_line(colour, width, style)
        bgl.glVertex2i(x0, y0)
        bgl.glVertex2i(x1, y0)
        bgl.glVertex2i(x1, y1)
        bgl.glVertex2i(x0, y1)
        bgl.glVertex2i(x0, y0)
        self._end()

    def Polygon(self, pts, colour):
        bgl.glPushAttrib(bgl.GL_ENABLE_BIT)
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glColor4f(*colour)
        bgl.glBegin(bgl.GL_POLYGON)
        for pt in pts:
            x, y = pt
            bgl.glVertex2f(x, y)
        self._end()

    def ProgressBar(self, x, y, width, height, start, percent):
        x1, y1 = x + width, y + height
        # progress from current point to either start or end
        xs = x + (x1 - x) * float(start)
        if percent > 0:
            # going forward
            xi = xs + (x1 - xs) * float(percent)
        else:
            # going backward
            xi = xs - (x - xs) * float(percent)
        self.Polygon([(xs, y), (xs, y1), (xi, y1), (xi, y)], self.progress_colour)
        self.Rectangle(x, y, x1, y1, self.white, width=1)


def draw_callback(self, context):
    self.gl.ProgressBar(10, 24, 200, 16, self.start, self.progress)
    self.gl.String(str(int(100 * abs(self.progress))) + "% ESC to Stop", 14, 28, 10, self.gl.white)


class OP_Tracking_auto_tracker(Operator):
    bl_idname = "tracking.auto_track"
    bl_label = "AutoTracking"
    bl_description = ("Start Autotracking, Press Esc to Stop \n"
                      "When stopped, the added Track Markers will be kept")

    _timer = None
    _draw_handler = None

    gl = GlDrawOnScreen()
    progress = 0
    limits = 0
    t = 0

    def find_track_start(self, track):
        for m in track.markers:
            if not m.mute:
                return m.frame
        return track.markers[0].frame

    def find_track_end(self, track):
        for m in reversed(track.markers):
            if not m.mute:
                return m.frame
        return track.markers[-1].frame - 1

    def find_track_length(self, track):
        tstart = self.find_track_start(track)
        tend = self.find_track_end(track)
        return tend - tstart

    def show_tracks(self, context):
        clip = context.area.spaces.active.clip
        tracks = clip.tracking.tracks
        for track in tracks:
            track.hide = False

    def get_vars_from_context(self, context):
        scene = context.scene
        props = context.window_manager.autotracker_props
        clip = context.area.spaces.active.clip
        tracks = clip.tracking.tracks
        current_frame = scene.frame_current
        clip_end = clip.frame_start + clip.frame_duration
        clip_start = clip.frame_start
        if props.track_backwards:
            last_frame = min(clip_end, current_frame + props.frame_separation)
        else:
            last_frame = max(clip_start, current_frame - props.frame_separation)
        return scene, props, clip, tracks, current_frame, last_frame

    def delete_tracks(self, to_delete):
        bpy.ops.clip.select_all(action='DESELECT')
        for track in to_delete:
            track.select = True
        bpy.ops.clip.delete_track()

    # DETECT FEATURES
    def auto_features(self, context):
        """
            Detect features
        """
        t = time.time()

        scene, props, clip, tracks, current_frame, last_frame = self.get_vars_from_context(context)

        selected = []
        old = []
        to_delete = []
        width = clip.size[0]
        delete_threshold = float(props.delete_threshold) / 100.0

        bpy.ops.clip.select_all(action='DESELECT')

        # Detect Features
        bpy.ops.clip.detect_features(
                threshold=props.df_threshold,
                min_distance=props.df_distance / 100.0 * width,
                margin=props.df_margin / 100.0 * width,
                placement=props.placement_list
                )

        # filter new and old tracks
        for track in tracks:
            if track.hide or track.lock:
                continue
            marker = track.markers.find_frame(current_frame)
            if marker is not None:
                if (not track.select) and (not marker.mute):
                    old.append(track)
                if track.select:
                    selected.append(track)

        added_tracks = len(selected)

        # Select overlapping new markers
        for track_new in selected:
            marker0 = track_new.markers.find_frame(current_frame)
            for track_old in old:
                marker1 = track_old.markers.find_frame(current_frame)
                distance = (marker1.co - marker0.co).length
                if distance < delete_threshold:
                    to_delete.append(track_new)
                    added_tracks -= 1
                    break

        # Delete Overlapping Markers
        self.delete_tracks(to_delete)
        debug_print("auto_features %.4f seconds, add: %s tracks" % (time.time() - t, added_tracks))

    # AUTOTRACK FRAMES
    def track_frames_backward(self):
        # INVOKE_DEFAULT to show progress and take account of frame_limit
        t = time.time()
        res = bpy.ops.clip.track_markers('INVOKE_DEFAULT', backwards=True, sequence=True)
        debug_print("track_frames_backward %.2f seconds %s" % (time.time() - t, res))

    def track_frames_forward(self):
        # INVOKE_DEFAULT to show progress and take account of frame_limit
        t = time.time()
        res = bpy.ops.clip.track_markers('INVOKE_DEFAULT', backwards=False, sequence=True)
        debug_print("track_frames_forward %.2f seconds %s" % (time.time() - t, res))

    def get_active_tracks(self, context):
        scene, props, clip, tracks, current_frame, last_frame = self.get_vars_from_context(context)

        active_tracks = []
        for track in tracks:
            if track.hide or track.lock:
                continue
            if len(track.markers) < 2:
                active_tracks.append(track)
            else:
                marker = track.markers.find_frame(current_frame)
                if (marker is not None) and (not marker.mute):
                    active_tracks.append(track)
        return active_tracks

    def select_active_tracks(self, context):
        t = time.time()
        scene, props, clip, tracks, current_frame, last_frame = self.get_vars_from_context(context)
        # Select active trackers for tracking
        bpy.ops.clip.select_all(action='DESELECT')
        selected = self.get_active_tracks(context)
        for track in selected:
            track.select = True
        debug_print("select_active_tracks %.2f seconds,"
                    " selected: %s" % (time.time() - t, len(selected)))
        return selected

    def estimate_motion(self, context, last, frame):
        """
            compute mean pixel motion for current frame
            TODO: use statistic here to make filtering more efficient
            last : last frame number
            frame: current frame number
            return mean pixel distance error
        """
        scene, props, clip, tracks, current_frame, last_frame = self.get_vars_from_context(context)
        nbtracks = 0
        distance = 0.0
        for track in tracks:
            if track.hide or track.lock:
                continue
            marker0 = track.markers.find_frame(frame)
            marker1 = track.markers.find_frame(last)
            if marker0 is not None and marker1 is not None:
                d = (marker0.co - marker1.co).length
                # skip fixed tracks
                if d > 0:
                    distance += d
                    nbtracks += 1
        if nbtracks > 0:
            mean = distance / nbtracks
        else:
            # arbitrary set to prevent division by 0 error
            mean = 10

        return mean

    # REMOVE SMALL TRACKS
    def remove_small(self, context):
        t = time.time()
        scene, props, clip, tracks, current_frame, last_frame = self.get_vars_from_context(context)
        to_delete = []
        bpy.ops.clip.select_all(action='DESELECT')
        for track in tracks:
            if track.hide or track.lock:
                continue
            if len(track.markers) > 1:
                marker = track.markers.find_frame(current_frame)
                if marker is None and self.find_track_length(track) < props.small_tracks:
                    to_delete.append(track)
        deleted_tracks = len(to_delete)
        self.delete_tracks(to_delete)
        debug_print("remove_small %.4f seconds, %s tracks deleted" % (time.time() - t, deleted_tracks))

    def split_track(self, context, track, split_frame, skip=0):
        scene, props, clip, tracks, current_frame, last_frame = self.get_vars_from_context(context)
        if props.track_backwards:
            end = scene.frame_start
            step = -1
        else:
            end = scene.frame_end
            step = 1
        new_track = tracks.new(frame=split_frame)

        for frame in range(split_frame, end, step):
            marker = track.markers.find_frame(frame)
            if marker is None:
                return
            # add new marker on new track for frame
            if abs(frame - split_frame) >= skip:
                new_marker = new_track.markers.find_frame(frame)
                if new_marker is None:
                    new_marker = new_track.markers.insert_frame(frame)
                new_marker.co = marker.co
            # remove marker on track for frame
            if frame == split_frame:
                track.hide = True
            else:
                track.markers.delete_frame(frame)
            marker.mute = True

    # REMOVE JUMPING MARKERS
    def remove_jumping(self, context):

        t = time.time()
        scene, props, clip, tracks, current_frame, last_frame = self.get_vars_from_context(context)

        if props.track_backwards:
            step = -1
        else:
            step = 1

        to_split = [None for track in tracks]
        for frame in range(last_frame, current_frame, step):

            last = frame - step

            # mean motion (normalized [0-1]) distance for tracks between last and current frame
            mean = self.estimate_motion(context, last, frame)

            # how much a track is allowed to move
            allowed = mean * props.jump_cut

            for i, track in enumerate(tracks):
                if track.hide or track.lock:
                    continue
                marker0 = track.markers.find_frame(frame)
                marker1 = track.markers.find_frame(last)
                if marker0 is not None and marker1 is not None:
                    distance = (marker0.co - marker1.co).length
                    # Jump Cut threshold
                    if distance > allowed:
                        if to_split[i] is None:
                            to_split[i] = [frame, frame]
                        else:
                            to_split[i][1] = frame

        jumping = 0
        for i, split in enumerate(to_split):
            if split is not None:
                self.split_track(context, tracks[i], split[0], abs(split[0] - split[1]))
                jumping += 1

        debug_print("remove_jumping: %.4f seconds, %s tracks cut." % (time.time() - t, jumping))

    def get_frame_range(self, context):
        """
            get tracking frames range
            use clip limits when clip shorter than scene
            else use scene limits
        """
        scene, props, clip, tracks, current_frame, last_frame = self.get_vars_from_context(context)
        frame_start = max(scene.frame_start, clip.frame_start)
        frame_end = min(scene.frame_end, clip.frame_start + clip.frame_duration)
        frame_duration = frame_end - frame_start
        return frame_start, frame_end, frame_duration

    def modal(self, context, event):

        if event.type in {'ESC'}:
            self.report({'INFO'},
                        "Stopping, up to now added Markers will be kept. Autotracking Finished")
            self.cancel(context)
            return {'FINISHED'}

        scene, props, clip, tracks, current_frame, last_frame = self.get_vars_from_context(context)
        frame_start, frame_end, frame_duration = self.get_frame_range(context)

        if (((not props.track_backwards) and current_frame >= frame_end) or
                 (props.track_backwards and current_frame <= frame_start)):

            self.report({'INFO'},
                        "Reached the end of the Clip. Autotracking Finished")
            self.cancel(context)
            return {'FINISHED'}

        # do not run this modal while tracking operator runs
        # Known issue, you'll have to keep ESC pressed
        if event.type not in {'TIMER'} or context.scene.frame_current != self.next_frame:
            return {'PASS_THROUGH'}

        # prevent own TIMER event while running
        self.stop_timer(context)

        if props.track_backwards:
            self.next_frame = scene.frame_current - props.frame_separation
            total = self.start_frame - frame_start
        else:
            self.next_frame = scene.frame_current + props.frame_separation
            total = frame_end - self.start_frame

        if total > 0:
            self.progress = (current_frame - self.start_frame) / total
        else:
            self.progress = 0

        debug_print("Tracking frame %s" % (scene.frame_current))

        # Remove bad tracks before adding new ones
        self.remove_small(context)
        self.remove_jumping(context)

        # add new tracks
        self.auto_features(context)

        # Select active trackers for tracking
        active_tracks = self.select_active_tracks(context)

        # finish if there is nothing to track
        if len(active_tracks) == 0:
            self.report({'INFO'},
                         "No new tracks created, nothing to track. Autotrack Finished")
            self.cancel(context)
            return {'FINISHED'}

        # setup frame_limit on tracks
        for track in active_tracks:
            track.frames_limit = 0
        active_tracks[0].frames_limit = props.frame_separation

        # Forwards or backwards tracking
        if props.track_backwards:
            self.track_frames_backward()
        else:
            self.track_frames_forward()

        # setup a timer to broadcast a TIMER event to force modal to
        # re-run as fast as possible (not waiting for any mouse or keyboard event)
        self.start_timer(context)

        return {'RUNNING_MODAL'}

    def invoke(self, context, event):
        scene = context.scene
        frame_start, frame_end, frame_duration = self.get_frame_range(context)

        if scene.frame_current > frame_end:
            scene.frame_current = frame_end
        elif scene.frame_current < frame_start:
            scene.frame_current = frame_start

        self.start_frame = scene.frame_current
        self.start = (scene.frame_current - frame_start) / (frame_duration)
        self.progress = 0

        # keep track of frame at witch we should detect new features and filter tracks
        self.next_frame = scene.frame_current

        # draw progress
        args = (self, context)
        self._draw_handler = bpy.types.SpaceClipEditor.draw_handler_add(
                                                        draw_callback, args,
                                                        'WINDOW', 'POST_PIXEL'
                                                        )
        self.start_timer(context)
        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}

    def __init__(self):
        self.t = time.time()

    def __del__(self):
        debug_print("AutoTrack %.2f seconds" % (time.time() - self.t))

    def execute(self, context):
        debug_print("Autotrack execute called")
        return {'FINISHED'}

    def stop_timer(self, context):
        context.window_manager.event_timer_remove(self._timer)

    def start_timer(self, context):
        self._timer = context.window_manager.event_timer_add(time_step=0.1, window=context.window)

    def cancel(self, context):
        self.stop_timer(context)
        self.show_tracks(context)
        bpy.types.SpaceClipEditor.draw_handler_remove(self._draw_handler, 'WINDOW')

    @classmethod
    def poll(cls, context):
        return (context.area.spaces.active.clip is not None)


class AutotrackerSettings(PropertyGroup):
    """Create properties"""
    df_margin = FloatProperty(
            name="Detect Features Margin",
            description="Only consider features from pixels located outside\n"
                        "the defined margin from the clip borders",
            subtype='PERCENTAGE',
            default=5,
            min=0,
            max=100
            )
    df_threshold = FloatProperty(
            name="Detect Features Threshold",
            description="Threshold level to deem a feature being good enough for tracking",
            default=0.3,
            min=0.0,
            max=1.0
            )
    # Note: merge this one with delete_threshold
    df_distance = FloatProperty(
            name="Detect Features Distance",
            description="Minimal acceptable distance between two features",
            subtype='PERCENTAGE',
            default=8,
            min=1,
            max=100
            )
    delete_threshold = FloatProperty(
            name="New Marker Threshold",
            description="Threshold of how close a new features can appear during tracking",
            subtype='PERCENTAGE',
            default=8,
            min=1,
            max=100
            )
    small_tracks = IntProperty(
            name="Minimum track length",
            description="Delete tracks shorter than this number of frames\n"
                        "Note: set to 0 for keeping all tracks",
            default=50,
            min=1,
            max=1000
            )
    frame_separation = IntProperty(
            name="Frame Separation",
            description="How often new features are generated",
            default=5,
            min=1,
            max=100
            )
    jump_cut = FloatProperty(
            name="Jump Cut",
            description="How much distance a marker can travel before it is considered "
                        "to be a bad track and cut.\nA new track wil be added "
                        "(factor relative to mean motion)",
            default=5.0,
            min=0.0,
            max=50.0
            )
    track_backwards = BoolProperty(
            name="AutoTrack Backwards",
            description="Track from the last frame of the selected clip",
            default=False
            )
    # Dropdown menu
    list_items = [
            ("FRAME", "Whole Frame", "", 1),
            ("INSIDE_GPENCIL", "Inside Grease Pencil", "", 2),
            ("OUTSIDE_GPENCIL", "Outside Grease Pencil", "", 3),
            ]
    placement_list = EnumProperty(
            name="Placement",
            description="Feature Placement",
            items=list_items
            )


"""
    NOTE:
    All size properties are in percent of the clip size,
    so presets do not depend on the clip size
"""


class AutotrackerPanel(Panel):
    """Creates a Panel in the Render Layer properties window"""
    bl_label = "Autotrack"
    bl_idname = "autotrack"
    bl_space_type = 'CLIP_EDITOR'
    bl_region_type = 'TOOLS'
    bl_category = "Track"

    @classmethod
    def poll(cls, context):
        return (context.area.spaces.active.clip is not None)

    def draw(self, context):
        layout = self.layout
        wm = context.window_manager

        row = layout.row()
        row.scale_y = 1.5
        row.operator("tracking.auto_track", text="Autotrack!     ", icon='PLAY')

        row = layout.row()
        row.prop(wm.autotracker_props, "track_backwards")

        row = layout.row()
        col = layout.column(align=True)
        col.prop(wm.autotracker_props, "delete_threshold")
        col.prop(wm.autotracker_props, "small_tracks")
        col.prop(wm.autotracker_props, "frame_separation", text="Frame Separation")
        col.prop(wm.autotracker_props, "jump_cut", text="Jump Threshold")

        row = layout.row()
        row.label(text="Detect Features Settings:")
        col = layout.column(align=True)
        col.prop(wm.autotracker_props, "df_margin", text="Margin:")
        col.prop(wm.autotracker_props, "df_distance", text="Distance:")
        col.prop(wm.autotracker_props, "df_threshold", text="Threshold:")

        row = layout.row()
        row.label(text="Feature Placement:")
        col = layout.column(align=True)
        col.prop(wm.autotracker_props, "placement_list", text="")


def register():
    bpy.utils.register_class(AutotrackerSettings)
    WindowManager.autotracker_props = PointerProperty(
                                            type=AutotrackerSettings
                                            )
    bpy.utils.register_module(__name__)


def unregister():
    bpy.utils.unregister_class(AutotrackerSettings)
    bpy.utils.unregister_module(__name__)
    del WindowManager.autotracker_props


if __name__ == "__main__":
    register()