Example #1
0
def setup_tracking(moviefile, pre4):
    """Create all tracking objects."""
    # open movie
    testmovie = test_movie_for_known_movie(moviefile)
    movie = movies.Movie(testmovie, interactive=False)

    # create background model calculator
    if pre4:
        bg_model = bg_pre4.BackgroundCalculator(movie)
    else:
        bg_model = bg.BackgroundCalculator(movie)
    bg_img_shape = (movie.get_height(), movie.get_width())
    bg_model.set_buffer_maxnframes()

    # open annotation file
    ann_file = ann.AnnotationFile(annname(moviefile, pre4))
    ann_file.InitializeData()

    # calculate bg
    bg_model.OnCalculate()

    # estimate fly shapes
    havevalue = (not params.maxshape.area == 9999.)
    haveshape = (not num.isinf(params.maxshape.area))
    if (not haveshape) or (not havevalue):
        if pre4:
            params.movie = movie
            ell_pre4.est_shape(bg_model)
            params.movie = None
        else:
            ell.est_shape(bg_model)

    hindsight = hs.Hindsight(ann_file, bg_model)

    return movie, bg_model, ann_file, hindsight
Example #2
0
        self.bw = bw

    def sub_bg(self, frame):
        return (self.dfore[frame], self.bw[frame])


#for t in range(len(tracks)):
#    mpl.imshow(bw[t])
#    for (id,e) in tracks[t].iteritems():
#        est.drawellipse(e,colors[id])
#    mpl.show()

bg = BG(dfore, bw)

# try to fix
hind = hindsight.Hindsight(tracks, bg)
hind.initialize_milestones()
print 'milestones: '
print hind.milestones

print 'tracks: '
print tracks

for t in range(2, len(tracks)):
    hind.fixerrors(t)

mpl.gray()
for t in range(len(tracks)):

    mpl.imshow(bw[t])
    for [id, e] in tracks[t].iteritems():
Example #3
0
    def Track(self):
        """Run the m-tracker."""

        ## initialization ##

        if DEBUG: print "Tracking from frame %d..." % self.start_frame
        if DEBUG:
            print "Last frame tracked = %d" % self.ann_file.lastframetracked

        print "YL:"
        print " bg_type", self.bg_imgs.bg_type
        print " norm_type", self.bg_imgs.norm_type
        print " thresh", params.n_bg_std_thresh, params.n_bg_std_thresh_low
        print " area", params.maxshape.area, params.minshape.area
        print " max_jump", params.max_jump, params.max_jump_split
        print " use_shadow_detector", params.use_shadow_detector
        print " recalc_bg_minutes", params.recalc_bg_minutes
        print " recalc_n_frames", self.movie.recalc_n_frames()
        print " fps %.1f" % self.movie.get_fps()
        strt = time.clock()

        if params.use_shadow_detector:
            fx, fy = self.matchTemplate()
            print " fx, fy", fx, fy
            bx = (fx[0] + fx[1]) / 2

        # maximum number of frames we will look back to fix errors
        self.maxlookback = max(params.lostdetection_length,
                               params.spuriousdetection_length,
                               params.mergeddetection_length,
                               params.splitdetection_length)

        if params.interactive:
            wx.Yield()

        # initialize hindsight data structures
        self.hindsight = hindsight.Hindsight(self.ann_file, self.bg_imgs)

        self.break_flag = False

        if DEBUG: print "Initializing buffer for tracking"
        self.ann_file.InitializeBufferForTracking(self.start_frame)

        # initialize dfore and connected component buffer
        self.bg_imgs.set_buffer_maxnframes()

        rc, rnf, bgs, nf = 0, self.movie.recalc_n_frames(
        ), [], self.movie.get_n_frames()

        def appendBg():
            bgRw = self.bg_imgs.centers if self.bg_imgs.varying_bg else [
                self.bg_imgs.center
            ]
            bgs.append(
                dict(bgs=[bg.astype(num.float32) for bg in bgRw],
                     varying_bg=self.bg_imgs.varying_bg,
                     mean_separator=self.bg_imgs.mean_separator))

        appendBg()
        for self.start_frame in range(self.start_frame, nf):

            # KB 20120109 added last_frame command-line option
            if self.start_frame >= self.last_frame:
                break

            if DEBUG_LEVEL > 0:
                print "Tracking frame %d / %d" % (self.start_frame, nf - 1)

            #if DEBUG:
            #    break

            if self.break_flag:
                break

            last_time = time.time()

            # recalculate background?
            rc += 1
            if rnf > 0 and rc > rnf:
                if nf - self.start_frame > rnf / 2:
                    assert self.start_frame % rnf == 0
                    # note: makes it easy to calculate which background was used;
                    #  not required for tracking
                    self.bg_imgs.bg_firstframe = self.start_frame
                    self.bg_imgs.bg_lastframe = self.start_frame + rnf - 1
                    self.OnComputeBg()
                    appendBg()
                rc = 1

            # perform background subtraction
            #try:
            (self.dfore,self.isfore,self.cc,self.ncc) = \
                self.bg_imgs.sub_bg( self.start_frame, dobuffer=True )
            #except:
            #    # catch all error types here, and just break out of loop
            #    break

            # write to sbfmf
            if self.dowritesbfmf:
                self.movie.writesbfmf_writeframe(self.isfore,
                                                 self.bg_imgs.curr_im,
                                                 self.bg_imgs.curr_stamp,
                                                 self.start_frame)

            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break

            # find observations
            self.ellipses = ell.find_ellipses(self.dfore, self.cc, self.ncc)

            # shadow detector
            if params.use_shadow_detector:
                ne = 2 * [0]  # idx: left, right (chamber)
                bei, bdist, bgood = 2 * [-1], 2 * [1000], 2 * [None]  # best
                for ei, e in enumerate(self.ellipses):
                    if e.area < params.minshape.area:
                        continue
                    good = e.area >= params.shadow_detector_minarea and (
                        not e.merged_areas or any(a >= params.minshape.area
                                                  for a in e.merged_areas))
                    i = e.center.x > bx
                    ne[i] += 1
                    dist = num.sqrt((e.center.x - fx[i])**2 +
                                    (e.center.y - fy[i])**2)
                    if bei[i] < 0 or good and not bgood[
                            i] or dist < bdist[i] and good == bgood[i]:
                        bei[i], bdist[i], bgood[i] = ei, dist, good
                #print "l:%d r:%d" %(ne)

                # keep only non-shadow ellipses
                numEll = len(self.ellipses)
                self.ellipses = [self.ellipses[i] for i in bei if i >= 0]
                if len(self.ellipses) < numEll:
                    print ">>> kept only", bei

            #if params.DOBREAK:
            #    print 'Exiting at frame %d'%self.start_frame
            #    sys.exit(1)

            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break

            # match target identities to observations
            if len(self.ann_file) > 1:
                flies = ell.find_flies(self.ann_file[-2], self.ann_file[-1],
                                       self.ellipses, self.ann_file)
            elif len(self.ann_file) == 1:
                flies = ell.find_flies(self.ann_file[-1], self.ann_file[-1],
                                       self.ellipses, self.ann_file)
            else:
                flies = ell.TargetList()
                for i, obs in enumerate(self.ellipses):
                    if obs.isEmpty():
                        if DEBUG: print 'empty observation'
                    else:
                        newid = self.ann_file.GetNewId()
                        obs.identity = newid
                        flies.append(obs)

            if DEBUG_LEVEL > 0:
                print "Done with frame %d, appending to ann_file" % self.start_frame

            # save to ann_data
            self.ann_file.append(flies)

            if DEBUG_LEVEL > 0:
                print "Added to ann_file, now running fixerrors"

            # fix any errors using hindsight
            self.hindsight.fixerrors()
            #print 'time to fix errors: '+str(time.time() - last_time)

            # draw?
            if self.request_refresh or (self.do_refresh and (
                (self.start_frame % self.framesbetweenrefresh) == 0)):
                if params.interactive:
                    if self.start_frame:
                        self.ShowCurrentFrame()
                else:
                    on = ("on " if self.bg_imgs.on else
                          "off ") if self.bg_imgs.varying_bg else ""
                    print "    Frame %d / %d %s[%ds]" \
                        %(self.start_frame, nf, on, time.clock()-strt)
                self.request_refresh = False

            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break

            if (self.start_frame %
                    100) == 0 and self.has('diagnostics_filename'):
                self.write_diagnostics()  # save ongoing

        self.saveBackgrounds(bgs)
        self.Finish()
Example #4
0
    def Track(self):
        """Run the tracker."""

        ## initialization ##
        if DEBUG: print "Tracking from frame %d..." % self.start_frame
        if DEBUG:
            print "Last frame tracked = %d" % self.ann_file.lastframetracked

        # maximum number of frames we will look back to fix errors
        self.maxlookback = max(params.lostdetection_length,
                               params.spuriousdetection_length,
                               params.mergeddetection_length,
                               params.splitdetection_length)

        if params.interactive:
            wx.Yield()

        # initialize hindsight data structures
        self.hindsight = hindsight.Hindsight(self.ann_file, self.bg_imgs)

        # initialize dfore and connected component buffer
        self.bg_imgs.set_buffer_maxnframes()

        self.track_timer_start_time = time.time()
        self.update_track_time()

        self.break_flag = False

        for self.start_frame in range(self.start_frame,
                                      self.movie.get_n_frames()):
            if DEBUG or DEBUG_TRACKINGSETTINGS: print "frame", self.start_frame

            # KB 20120109 added last_frame command-line option
            if self.start_frame >= self.last_frame:
                break

            if DEBUG_LEVEL > 0:
                print "Tracking frame %d / %d" % (
                    self.start_frame, self.movie.get_n_frames() - 1)

            if self.break_flag:
                break

            last_time = time.time()

            # perform background subtraction
            try:
                (dfore, isfore, cc,
                 ncc) = self.bg_imgs.sub_bg(self.start_frame)
            except IOError:
                if self.movie.type == 'cavi' and self.start_frame >= self.movie.get_n_frames(
                ) - 2:
                    # last frame or so wasn't present, no problem
                    print "ignoring compressed AVI IOError reading last frame"
                else:
                    traceback.print_exc()
                break
            except:
                # catch all error types here, and just break out of the loop
                traceback.print_exc()
                break
            if DEBUG_LEVEL > 1: print ncc, "connected components"

            # write to sbfmf
            if self.dowritesbfmf:
                self.movie.writesbfmf_writeframe(isfore, self.bg_imgs.curr_im,
                                                 self.bg_imgs.curr_stamp,
                                                 self.start_frame)

            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break

            # find observations
            ellipses = ell.find_ellipses(dfore, cc, ncc)
            if DEBUG_LEVEL > 1: print len(ellipses), "ellipses"

            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break

            # match target identities to observations
            if DEBUG_LEVEL > 0: print "matching identities"
            if len(self.ann_file) > 1:
                flies = ell.find_flies(self.ann_file[-2], self.ann_file[-1],
                                       ellipses, self.ann_file)
            elif len(self.ann_file) == 1:
                flies = ell.find_flies(self.ann_file[-1], self.ann_file[-1],
                                       ellipses, self.ann_file)
            else:
                flies = ell.TargetList()
                for i, obs in enumerate(ellipses):
                    if obs.isEmpty():
                        if DEBUG: print 'empty observation'
                    else:
                        newid = self.ann_file.GetNewId()
                        obs.identity = newid
                        flies.append(obs)
            if DEBUG_LEVEL > 1: print len(flies), "flies"

            if DEBUG_LEVEL > 0:
                print "Done with frame %d, appending to ann_file" % self.start_frame

            # save to ann_data
            self.ann_file.append(flies)

            # fix any errors using hindsight
            if DEBUG_LEVEL > 0:
                print "Added to ann_file, now running fixerrors"
            self.hindsight.fixerrors()

            # draw?
            if self.request_refresh or (self.do_refresh and (
                (self.start_frame % self.framesbetweenrefresh) == 0)):
                if params.interactive:
                    if self.start_frame:
                        self.ShowCurrentFrame()
                else:
                    print "    Frame %d / %d" % (self.start_frame,
                                                 self.movie.get_n_frames() - 1)
                self.request_refresh = False
            if DEBUG_LEVEL > 1:
                print len(self.ann_file[-1]), "flies in last frame"

            # process gui events
            if params.interactive:
                wx.Yield()
            if self.break_flag:
                break

            if (self.start_frame %
                    100) == 0 and self.has('diagnostics_filename'):
                self.write_diagnostics()  # save ongoing

        if hasattr(self, 'timer'):
            # timer.cancel() should work, but in case it doesn't, set a flag too
            self.timer.cancel()
            self.track_timer_cancel = True

        self.Finish()