def weight_set(self, measurement, particle_set):
        pos_mat    = ut.list_mat_to_mat(particle_set, axis=1)
        gauss      = pr.Gaussian(m = measurement, v = self.cov)
        f          = gauss.pdf_mat()
        weight_mat = f(pos_mat)

        def pair_weights(tup):
            part, idx = tup
            return (part, weight_mat[idx])
        return map(pair_weights, it.izip(particle_set, xrange(len(particle_set))))
Exemple #2
0
def learn_run(exposure = LaserPointerDetector.SUN_EXPOSURE, num_examples_to_collect=200, display_during_run = True):
    hg.cvNamedWindow("video",       1)
    hg.cvNamedWindow("thresholded", 1)
    hg.cvNamedWindow('motion',      1)
    hg.cvNamedWindow('intensity',   1)

    hg.cvMoveWindow("video",       0,   0)
    hg.cvMoveWindow("thresholded", 800, 0)

    hg.cvMoveWindow("intensity",   0,   600)
    hg.cvMoveWindow("motion",      800, 600)

    video     = cam.VidereStereo(0, gain=96, exposure=exposure)
    frames    = video.next()
    detector  = LaserPointerDetector(frames[0], exposure=exposure, 
                                    dataset=PatchClassifier.DEFAULT_DATASET_FILE,
                                    use_color=False, use_learning=False)
    detector2 = LaserPointerDetector(frames[1], exposure=exposure, 
                                    dataset=PatchClassifier.DEFAULT_DATASET_FILE,
                                    use_color=False, use_learning=False)

    def append_examples_to_file(dataset, file = PatchClassifier.DEFAULT_DATASET_FILE):
        try:
            loaded_set = load_pickle(file)
            dataset.append(loaded_set)
        except IOError:
            pass
        dump_pickle(dataset, file)
        print 'Saved examples!'

    #Gather positive examples from laser detector
    if confirmation_prompt('gather positive examples?'):
        print 'Lets gather some positive examples... we need', num_examples_to_collect, 'examples'
        positive_examples_for_classifier = []
        count_down(0)
        for i in xrange(10):
            frames = video.next()
            detector.detect(frames[0])
            detector2.detect(frames[1])

        for img in video:
            image                 = None
            combined              = None
            motion                = None
            intensity             = None
            laser_blob            = None
            intensity_motion_blob = None

            for raw_image, detect in zip(img, [detector, detector2]):
                before = time.time()
                image, combined, laser_blob, intensity_motion_blob = detect.detect(raw_image)
                diff = time.time() - before
                #print 'took %.2f seconds to run or %.2f fps' % (diff, 1.0/diff)
                if laser_blob != None:
                    instance = blob_to_input_instance(image, laser_blob)
                    if instance is not None:
                        positive_examples_for_classifier.append(instance)
                        print 'got', len(positive_examples_for_classifier), 'instances'
                motion, intensity = detect.get_motion_intensity_images()

            show_processed(image, [combined, motion, intensity], laser_blob, intensity_motion_blob, detector2)
            if len(positive_examples_for_classifier) > num_examples_to_collect:
                break
        positive_instances_dataset = matrix_to_dataset(ut.list_mat_to_mat(positive_examples_for_classifier, axis=1))
        append_examples_to_file(positive_instances_dataset)

    if confirmation_prompt('gather negative examples?'):
        #Gather negative examples from laser detector
        print 'lets gather some negative examples... we need', num_examples_to_collect,' examples'
        negative_examples_for_classifier = []
        count_down(10)
        for i in xrange(10):
            frames = video.next()
            detector.detect(frames[0])
            detector2.detect(frames[1])

        for img in video:
            image                 = None
            combined              = None
            motion                = None
            intensity             = None
            laser_blob            = None
            intensity_motion_blob = None
            for raw_image, detect in zip(img, [detector, detector2]):
                image, combined, laser_blob, intensity_motion_blob = detect.detect(raw_image)
                if laser_blob != None:
                    instance = blob_to_input_instance(image, laser_blob)
                    if instance is not None:
                        negative_examples_for_classifier.append(instance)
                        print 'got', len(negative_examples_for_classifier), 'instances'
                motion, intensity = detect.get_motion_intensity_images()

            show_processed(image, [combined, motion, intensity], laser_blob, intensity_motion_blob, detector2)
            if len(negative_examples_for_classifier) > (num_examples_to_collect*2):
                break
        negative_instances_dataset = matrix_to_dataset(ut.list_mat_to_mat(negative_examples_for_classifier, axis=1))
        append_examples_to_file(negative_instances_dataset)

    if confirmation_prompt('run classifier?'):
        run(exposure, video = video, display=display_during_run)