コード例 #1
0
 def _initialize(self,frame,rect):
     '''
     A private method that initializes the filter and prepares this 
     instance of a MOSSETrack to receive new frames.
     
     @param frame: A source video frame to _initialize from.
     @type frame: pv.Image
     @param rect: A rectangle defining the object to be tracked in "frame".
     @type rect: pv.Rect 
     '''
     start = time.time()
     self.rect = copy.deepcopy(rect)
     affine = pv.AffineFromRect(rect,self.tile_size)
     pt = affine.transformPoint(self.rect.center())
     if self.strategy == INIT_ONE_TRANSFORM:
         tmp = affine.transformImage(frame)
         self.filter.addTraining(tmp,pt)
         self.input = tmp
     elif self.strategy == INIT_NINE_TRANSFORM:
         for scale in [-1,0,1]:
             scale = pv.AffineRotate(scale*self.dscale,self.tile_size,center=pt)
             for rotate in [-1,0,1]:
                 rot = pv.AffineRotate(rotate*self.drotate,self.tile_size,center=pt)
                 pt = (scale*rot*affine).transformPoint(self.rect.center())
                 tmp = (scale*rot*affine).transformImage(frame)
                 self.filter.addTraining(tmp,pt)
         self.input = tmp
     self.corr = np.zeros(self.tile_size,dtype=np.float64)
     self.frame = 0
     stop = time.time()
     self.update_time = stop-start
     self.best_estimate = rect.center()
コード例 #2
0
    def generateTransforms(self, detection):
        # Transform the face
        affine = pv.AffineFromRect(detection, self.tile_size)

        w, h = self.tile_size

        if self.perturbations:
            # Randomly rotate, translate and scale the images
            center = pv.AffineTranslate(-0.5 * w, -0.5 * h, self.tile_size)
            rotate = pv.AffineRotate(random.uniform(-pi / 8, pi / 8),
                                     self.tile_size)
            scale = pv.AffineScale(random.uniform(0.9, 1.1), self.tile_size)
            translate = pv.AffineTranslate(random.uniform(-0.05 * w, 0.05 * w),
                                           random.uniform(-0.05 * h, 0.05 * h),
                                           self.tile_size)
            inv_center = pv.AffineTranslate(0.5 * w, 0.5 * h, self.tile_size)

            affine = inv_center * translate * scale * rotate * center * affine
            #affine = affine*center*rotate*scale*translate*inv_center

        lx = self.left_center.X() - self.subtile_size[0] / 2
        ly = self.left_center.Y() - self.subtile_size[1] / 2
        rx = self.right_center.X() - self.subtile_size[0] / 2
        ry = self.right_center.Y() - self.subtile_size[1] / 2

        laffine = pv.AffineFromRect(
            pv.Rect(lx, ly, self.subtile_size[0], self.subtile_size[1]),
            self.subtile_size) * affine
        raffine = pv.AffineFromRect(
            pv.Rect(rx, ry, self.subtile_size[0], self.subtile_size[1]),
            self.subtile_size) * affine
        return laffine, raffine
コード例 #3
0
    def addTraining(self, left_eye, right_eye, im):
        '''Train an eye detector givin a full image and the eye coordinates.'''

        # determine the face rect
        true_rect = face_from_eyes(left_eye, right_eye)

        # run the face detector
        rects = self.face_detector.detect(im)

        # find the best detection if there is one
        for pred_rect in rects:
            if is_success(pred_rect, true_rect):
                # Transform the face
                affine = pv.AffineFromRect(pred_rect, self.tile_size)

                w, h = self.tile_size

                if self.perturbations:
                    # Randomly rotate, translate and scale the images
                    center = pv.AffineTranslate(-0.5 * w, -0.5 * h,
                                                self.tile_size)
                    rotate = pv.AffineRotate(random.uniform(-pi / 8, pi / 8),
                                             self.tile_size)
                    scale = pv.AffineScale(random.uniform(0.9, 1.1),
                                           self.tile_size)
                    translate = pv.AffineTranslate(
                        random.uniform(-0.05 * w, 0.05 * w),
                        random.uniform(-0.05 * h, 0.05 * h), self.tile_size)
                    inv_center = pv.AffineTranslate(0.5 * w, 0.5 * h,
                                                    self.tile_size)

                    affine = inv_center * translate * scale * rotate * center * affine
                    #affine = affine*center*rotate*scale*translate*inv_center

                cropped = affine.transformImage(im)
                cropped = pv.meanStd(cropped)

                # Mark the eyes
                leye = affine.transformPoint(left_eye)
                reye = affine.transformPoint(right_eye)

                # Add training data to locators
                self.training_labels.append((leye, reye))

                self.normalize.addTraining(0.0, cropped)
                #self.left_locator.addTraining(cropped,leye)
                #self.right_locator.addTraining(cropped,reye)

                # Just use the first success
                return

        # The face was not detected
        self.detection_failures += 1
コード例 #4
0
    def test_rotation(self):
        transform = pv.AffineRotate(3.14 / 8, (640, 480))
        _ = transform.transformImage(self.test_image)
        # im_a.show()

        pt = transform.transformPoint(pv.Point(320, 240))
        self.assertAlmostEqual(pt.X(), 203.86594448424472)
        self.assertAlmostEqual(pt.Y(), 344.14920700118842)

        pt = transform.invertPoint(pv.Point(320, 240))
        self.assertAlmostEqual(pt.X(), 387.46570317672939)
        self.assertAlmostEqual(pt.Y(), 99.349528744542198)
コード例 #5
0
    def test_prev_ref3(self):
        fname = os.path.join(pv.__path__[0], 'data', 'nonface',
                             'NONFACE_13.jpg')
        torig = tprev = im_a = pv.Image(fname)
        #im_a.show()
        w, h = im_a.size

        # Scale
        aff = pv.AffineScale(0.5, (w / 2, h / 2))
        accu = aff
        torig = aff.transformImage(torig)
        tprev = aff.transformImage(tprev, use_orig=False)
        taccu = accu.transformImage(im_a)

        torig.annotateLabel(pv.Point(10, 10), "use_orig = True")
        tprev.annotateLabel(pv.Point(10, 10), "use_orig = False")
        taccu.annotateLabel(pv.Point(10, 10), "accumulated")

        #torig.show()
        #tprev.show()
        #taccu.show()

        # Translate
        aff = pv.AffineTranslate(20, 20, (w / 2, h / 2))
        accu = aff * accu
        torig = aff.transformImage(torig)
        tprev = aff.transformImage(tprev, use_orig=False)
        taccu = accu.transformImage(im_a)

        torig.annotateLabel(pv.Point(10, 10), "use_orig = True")
        tprev.annotateLabel(pv.Point(10, 10), "use_orig = False")
        taccu.annotateLabel(pv.Point(10, 10), "accumulated")

        #torig.show()
        #tprev.show()
        #taccu.show()

        # Rotate
        aff = pv.AffineRotate(np.pi / 4, (w / 2, h / 2))
        accu = aff * accu
        torig = aff.transformImage(torig)
        tprev = aff.transformImage(tprev, use_orig=False)
        taccu = accu.transformImage(im_a)

        torig.annotateLabel(pv.Point(10, 10), "use_orig = True")
        tprev.annotateLabel(pv.Point(10, 10), "use_orig = False")
        taccu.annotateLabel(pv.Point(10, 10), "accumulated")

        #torig.show()
        #tprev.show()
        #taccu.show()

        # Translate
        aff = pv.AffineTranslate(100, -10, (w / 2, h / 2))
        accu = aff * accu
        torig = aff.transformImage(torig)
        tprev = aff.transformImage(tprev, use_orig=False)
        taccu = accu.transformImage(im_a)

        torig.annotateLabel(pv.Point(10, 10), "use_orig = True")
        tprev.annotateLabel(pv.Point(10, 10), "use_orig = False")
        taccu.annotateLabel(pv.Point(10, 10), "accumulated")

        #torig.show()
        #tprev.show()
        #taccu.show()

        # Scale
        aff = pv.AffineScale(2.0, (w, h))
        accu = aff * accu
        torig = aff.transformImage(torig)
        tprev = aff.transformImage(tprev, use_orig=False)
        taccu = accu.transformImage(im_a)

        torig.annotateLabel(pv.Point(10, 10), "use_orig = True")
        tprev.annotateLabel(pv.Point(10, 10), "use_orig = False")
        taccu.annotateLabel(pv.Point(10, 10), "accumulated")