コード例 #1
0
    def __init__(self,
                 log,
                 pipeline,
                 sigma,
                 outdir,
                 tracks_filename,
                 interval=None,
                 min_led_distance=0,
                 weight_others=0,
                 detect_smooth_sigma=1.0,
                 detect_neighbors=10,
                 write_png=False):

        # get configuration
        blink_detect_config = get_blink_config(log)

        # create trackers
        frequencies = blink_detect_config.get_frequencies()
        self.trackers = []
        for f in frequencies:
            others = list(frequencies)
            others.remove(f)
            tracker = TrackerFixedFreq(f,
                                       others=others,
                                       others_weight=weight_others,
                                       sigma=sigma,
                                       interval=interval)
            self.trackers.append(tracker)

        # start track log


#        tracks_filename = os.path.splitext(log)[0] + '.%s.tracks' % suffix
        self.tracklog = AERTrackLogWriter(tracks_filename)
        params = dict(frequencies=frequencies,
                      sigma=sigma,
                      weight_others=weight_others,
                      interval=interval)
        self.tracklog.write_comment('params: %s' % params)

        # open files
        if False:
            raw_sequence = aer_load_log_generic(log)
            self.transitions = aer_pipeline_transitions1(
                raw_sequence, pipeline)
        else:
            self.transitions = aer_pipeline_transitions1_all(log, pipeline)

        # save params needed later
        self.detect_smooth_sigma = detect_smooth_sigma
        self.detect_neighbors = detect_neighbors
        self.min_led_distance = int(min_led_distance)
        self.write_png = write_png
        self.outdir = outdir
コード例 #2
0
ファイル: rcl_detect_meat.py プロジェクト: AndreaCensi/rcl
    def __init__(self, log, pipeline, sigma, outdir, tracks_filename,
               interval=None,
               min_led_distance=0, weight_others=0,
               detect_smooth_sigma=1.0,
               detect_neighbors=10, write_png=False):
        
        # get configuration
        blink_detect_config = get_blink_config(log)
        
        # create trackers    
        frequencies = blink_detect_config.get_frequencies()
        self.trackers = []
        for f in frequencies:
            others = list(frequencies)
            others.remove(f)
            tracker = TrackerFixedFreq(f, others=others,
                                          others_weight=weight_others,
                                          sigma=sigma, interval=interval)
            self.trackers.append(tracker)

        # start track log
#        tracks_filename = os.path.splitext(log)[0] + '.%s.tracks' % suffix
        self.tracklog = AERTrackLogWriter(tracks_filename)
        params = dict(frequencies=frequencies,
                      sigma=sigma, weight_others=weight_others,
                      interval=interval)
        self.tracklog.write_comment('params: %s' % params)

        # open files
        if False:
            raw_sequence = aer_load_log_generic(log)
            self.transitions = aer_pipeline_transitions1(raw_sequence, pipeline)
        else:
            self.transitions = aer_pipeline_transitions1_all(log, pipeline)

        # save params needed later
        self.detect_smooth_sigma = detect_smooth_sigma
        self.detect_neighbors = detect_neighbors
        self.min_led_distance = int(min_led_distance)
        self.write_png = write_png
        self.outdir = outdir
コード例 #3
0
ファイル: rcl_detect_meat.py プロジェクト: AndreaCensi/rcl
class MultipleDetector(object):
    def __init__(self, log, pipeline, sigma, outdir, tracks_filename,
               interval=None,
               min_led_distance=0, weight_others=0,
               detect_smooth_sigma=1.0,
               detect_neighbors=10, write_png=False):
        
        # get configuration
        blink_detect_config = get_blink_config(log)
        
        # create trackers    
        frequencies = blink_detect_config.get_frequencies()
        self.trackers = []
        for f in frequencies:
            others = list(frequencies)
            others.remove(f)
            tracker = TrackerFixedFreq(f, others=others,
                                          others_weight=weight_others,
                                          sigma=sigma, interval=interval)
            self.trackers.append(tracker)

        # start track log
#        tracks_filename = os.path.splitext(log)[0] + '.%s.tracks' % suffix
        self.tracklog = AERTrackLogWriter(tracks_filename)
        params = dict(frequencies=frequencies,
                      sigma=sigma, weight_others=weight_others,
                      interval=interval)
        self.tracklog.write_comment('params: %s' % params)

        # open files
        if False:
            raw_sequence = aer_load_log_generic(log)
            self.transitions = aer_pipeline_transitions1(raw_sequence, pipeline)
        else:
            self.transitions = aer_pipeline_transitions1_all(log, pipeline)

        # save params needed later
        self.detect_smooth_sigma = detect_smooth_sigma
        self.detect_neighbors = detect_neighbors
        self.min_led_distance = int(min_led_distance)
        self.write_png = write_png
        self.outdir = outdir
            
    def go(self):    
        for f in self.transitions:
            # check if this event makes any of those trigger
            triggered = []
            for tracker in self.trackers:
                res = tracker.integrate(f)
                if res is not None:
                    triggered.append(tracker)
                    
            # Check that all trackers have produced a frame
            # if so, start two-phase estimation
            if triggered and all([t.has_frame() for t in self.trackers]):
                self.two_phase_estimation(f['timestamp'], triggered)
        
        self.tracklog.done()
    
    def two_phase_estimation(self, timestamp, triggered):
        # first get the current spots
        first = []
        for t in self.trackers:
            td = peak_detect(t.get_accum(),
                             sigma=self.detect_smooth_sigma,
                             neighbors=self.detect_neighbors)
            first.append(dict(tracker=t, detection=td))
        
        # now let's order by quality
        first.sort(key=lambda x: (-x['detection']['quality']))

        # one by one
        second = []
        occupied = [] 
        while first:
            f = first.pop(0)
            f_accum = remove_occupied(accum=f['tracker'].get_accum(),
                                      occupied=occupied, distance=self.min_led_distance)
            f_d = peak_detect(f_accum, sigma=self.detect_smooth_sigma,
                                        neighbors=self.detect_neighbors)
            if f_d is None:
                logger.debug('No track for %s' % f['tracker'])
            else:
                c = f_d['centroid']
                occupied.append((int(c[0]), int(c[1])))
                second.append(dict(tracker=f['tracker'], detection=f_d))

        d = lambda a, b: np.linalg.norm(np.array(a, 'float') - np.array(b))
        distances = [d(p1, p2) for p1, p2 in itertools.combinations(occupied, 2)]
        if distances:
            min_distance = np.min(distances)
            assert min_distance >= self.min_led_distance
        
        for s in second: 
            if s['tracker'] in triggered:
                triggered.remove(s['tracker'])
                self.output(timestamp, s['tracker'], s['detection'])
        
        # ones that are invalid
        for t in triggered:
            if s['tracker'] == t:
                s['detection']['centroid'] = (5, 5)
                self.output(timestamp, s['tracker'], s['detection'])
        
                
    
    def output(self, timestamp, tracker, detection):
        if self.write_png:
            rcl_detect_write_png(tracker, detection, timestamp, self.outdir)
        else:
                pass
        # print('%14s %s' % (timestamp, tracker.freq))
        center = detection['centroid'] 
        quality = detection['centroid_nevents']
        id_track = int(tracker.freq)
        self.tracklog.write(timestamp=timestamp, id_track=id_track,
                            x=center[0], y=center[1], quality=quality)
コード例 #4
0
class MultipleDetector(object):
    def __init__(self,
                 log,
                 pipeline,
                 sigma,
                 outdir,
                 tracks_filename,
                 interval=None,
                 min_led_distance=0,
                 weight_others=0,
                 detect_smooth_sigma=1.0,
                 detect_neighbors=10,
                 write_png=False):

        # get configuration
        blink_detect_config = get_blink_config(log)

        # create trackers
        frequencies = blink_detect_config.get_frequencies()
        self.trackers = []
        for f in frequencies:
            others = list(frequencies)
            others.remove(f)
            tracker = TrackerFixedFreq(f,
                                       others=others,
                                       others_weight=weight_others,
                                       sigma=sigma,
                                       interval=interval)
            self.trackers.append(tracker)

        # start track log


#        tracks_filename = os.path.splitext(log)[0] + '.%s.tracks' % suffix
        self.tracklog = AERTrackLogWriter(tracks_filename)
        params = dict(frequencies=frequencies,
                      sigma=sigma,
                      weight_others=weight_others,
                      interval=interval)
        self.tracklog.write_comment('params: %s' % params)

        # open files
        if False:
            raw_sequence = aer_load_log_generic(log)
            self.transitions = aer_pipeline_transitions1(
                raw_sequence, pipeline)
        else:
            self.transitions = aer_pipeline_transitions1_all(log, pipeline)

        # save params needed later
        self.detect_smooth_sigma = detect_smooth_sigma
        self.detect_neighbors = detect_neighbors
        self.min_led_distance = int(min_led_distance)
        self.write_png = write_png
        self.outdir = outdir

    def go(self):
        for f in self.transitions:
            # check if this event makes any of those trigger
            triggered = []
            for tracker in self.trackers:
                res = tracker.integrate(f)
                if res is not None:
                    triggered.append(tracker)

            # Check that all trackers have produced a frame
            # if so, start two-phase estimation
            if triggered and all([t.has_frame() for t in self.trackers]):
                self.two_phase_estimation(f['timestamp'], triggered)

        self.tracklog.done()

    def two_phase_estimation(self, timestamp, triggered):
        # first get the current spots
        first = []
        for t in self.trackers:
            td = peak_detect(t.get_accum(),
                             sigma=self.detect_smooth_sigma,
                             neighbors=self.detect_neighbors)
            first.append(dict(tracker=t, detection=td))

        # now let's order by quality
        first.sort(key=lambda x: (-x['detection']['quality']))

        # one by one
        second = []
        occupied = []
        while first:
            f = first.pop(0)
            f_accum = remove_occupied(accum=f['tracker'].get_accum(),
                                      occupied=occupied,
                                      distance=self.min_led_distance)
            f_d = peak_detect(f_accum,
                              sigma=self.detect_smooth_sigma,
                              neighbors=self.detect_neighbors)
            if f_d is None:
                logger.debug('No track for %s' % f['tracker'])
            else:
                c = f_d['centroid']
                occupied.append((int(c[0]), int(c[1])))
                second.append(dict(tracker=f['tracker'], detection=f_d))

        d = lambda a, b: np.linalg.norm(np.array(a, 'float') - np.array(b))
        distances = [
            d(p1, p2) for p1, p2 in itertools.combinations(occupied, 2)
        ]
        if distances:
            min_distance = np.min(distances)
            assert min_distance >= self.min_led_distance

        for s in second:
            if s['tracker'] in triggered:
                triggered.remove(s['tracker'])
                self.output(timestamp, s['tracker'], s['detection'])

        # ones that are invalid
        for t in triggered:
            if s['tracker'] == t:
                s['detection']['centroid'] = (5, 5)
                self.output(timestamp, s['tracker'], s['detection'])

    def output(self, timestamp, tracker, detection):
        if self.write_png:
            rcl_detect_write_png(tracker, detection, timestamp, self.outdir)
        else:
            pass
        # print('%14s %s' % (timestamp, tracker.freq))
        center = detection['centroid']
        quality = detection['centroid_nevents']
        id_track = int(tracker.freq)
        self.tracklog.write(timestamp=timestamp,
                            id_track=id_track,
                            x=center[0],
                            y=center[1],
                            quality=quality)