コード例 #1
0
def Example1(ilog):

    # This illustrates how images keep weak refs through affine
    # transformation to avoid an accumulation of errors through multiple
    # transformations.
    fname = os.path.join(pv.__path__[0], 'data', 'nonface', 'NONFACE_13.jpg')
    im = pv.Image(fname)
    im = pv.Image(im.asPIL().resize((320, 240)))
    im.annotateLabel(pv.Point(10, 10), "ScaleTest: Original image.")
    ilog.log(im)
    w, h = im.size

    # Create a small version of the image
    aff_small = pv.AffineScale(0.2, (w / 5, h / 5))
    tmp1 = aff_small.transformImage(im)
    tmp1.annotateLabel(pv.Point(10, 10), "Small")
    ilog.log(tmp1)

    # Scale the image back to its original size without using the original
    aff_big = pv.AffineScale(5.0, (w, h))
    tmp2 = aff_big.transformImage(tmp1, use_orig=False)
    tmp2.annotateLabel(pv.Point(10, 10), "ScaleTest: use_orig=False")
    tmp2.annotateLabel(pv.Point(20, 20), "This image should be blurry.")
    ilog.log(tmp2)

    # Use the affine class to produce a transform that maps the original
    # directly to the final image and therefore keeps most of the detail.
    tmp3 = (aff_big * aff_small).transformImage(im)
    tmp3.annotateLabel(pv.Point(10, 10), "ScaleTest: aff_big*aff_small")
    tmp3.annotateLabel(pv.Point(20, 20), "This image should be sharp.")
    ilog.log(tmp3)

    # Scale the image back to its original size using the original
    tmp4 = aff_big.transformImage(tmp1, use_orig=True)
    tmp4.annotateLabel(pv.Point(10, 10), "ScaleTest: use_orig=True")
    tmp4.annotateLabel(pv.Point(20, 20), "This image should be sharp.")
    ilog.log(tmp4)

    # Now remove the reverence to the im instance.  The weak references within
    # tmp1 do not hold onto the original data so now there is no choice but to
    # use the scaled down image.
    del im
    tmp5 = aff_big.transformImage(tmp1, use_orig=True)
    tmp5.annotateLabel(pv.Point(10, 10), "ScaleTest: use_orig=True")
    tmp5.annotateLabel(pv.Point(20, 20), "This image should be blurry")
    tmp5.annotateLabel(pv.Point(20, 30), "because the original has be")
    tmp5.annotateLabel(pv.Point(20, 40), "removed from memory.")
    ilog.log(tmp5)
コード例 #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_scale(self):
        transform = pv.AffineScale(1.5, (640, 480))
        _ = transform.transformImage(self.test_image)
        #im_a.show()

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

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

        # Try scaling down and then scaling back up
        tmp1 = pv.AffineScale(0.1, (w / 10, h / 10)).transformImage(im_a)
        #tmp1.show()

        tmp2 = pv.AffineScale(10.0, (w, h)).transformImage(tmp1,
                                                           use_orig=False)
        tmp2.annotateLabel(pv.Point(10, 10), "This image should be blurry.")
        #tmp2.show()

        tmp3 = pv.AffineScale(10.0, (w, h)).transformImage(tmp1, use_orig=True)
        tmp3.annotateLabel(pv.Point(10, 10), "This image should be sharp.")
        #tmp3.show()

        del im_a

        tmp4 = pv.AffineScale(10.0, (w, h)).transformImage(tmp1, use_orig=True)
        tmp4.annotateLabel(pv.Point(10, 10), "This image should be blurry.")
コード例 #6
0
ファイル: pyvision_banner.py プロジェクト: mdqyy/pyvision
import os.path
from Image import composite, LINEAR
import pyvision as pv
from pyvision.edge.sobel import sobel
#from pyvision.edge.canny import canny
from pyvision.point.DetectorSURF import DetectorSURF
import cv

if __name__ == '__main__':
    ilog = pv.ImageLog()
    source_name = os.path.join(pv.__path__[0], 'data', 'misc', 'p5240019.jpg')

    #Load source image and resize to smaller scale
    im = pv.Image(source_name)
    print "Size before affine scale: %s" % str(im.size)
    im = pv.AffineScale(0.25, (320, 240)).transformImage(im)
    print "Size after scaling: %s" % str(im.size)
    ilog.log(im, 'Input')
    #im.show(window='Input', pos=(0,0))

    #Generate edge image using sobel edge detector
    edges = sobel(im, 1, 0, 3, 0)
    ilog.log(edges, 'Edges')
    #edges.show(window='Edges', pos=(360,0))

    #Generate threshold mask, shows numpy integration
    mat = im.asMatrix2D()
    high = mat > 180
    low = mat < 50
    mask = high  #+low
    ilog.log(pv.Image(1.0 * mask), 'Mask')
コード例 #7
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")
コード例 #8
0
    if options.log_dir != None:
        print("Creating Image Log...")
        ilog = pv.ImageLog(options.log_dir)
    
    # For each image run face and eye detection
    face_detect = CascadeDetector(image_scale=1.3*options.scale)
    locate_eyes = FilterEyeLocator()#locator_filename)
    
    c = 0
    for pathname in image_names:
        c += 1
        
        im = pv.Image(pathname)

        scale = options.log_scale
        log_im = pv.AffineScale(scale,(int(scale*im.width),int(scale*im.height))).transformImage(im)
        
            
 
        results = processFaces(im,face_detect,locate_eyes)

        if options.rotate:
            
            rot_image = pv.Image(im.asPIL().transpose(PIL.Image.ROTATE_90))
            more_results = processFaces(rot_image,face_detect,locate_eyes)
            for face,eye1,eye2 in more_results:
                results.append([pv.Rect(im.width-face.y-face.h, face.x, face.h, face.w),
                               pv.Point(im.width-eye1.Y(),eye1.X()),
                               pv.Point(im.width-eye2.Y(),eye2.X())])

            rot_image = pv.Image(im.asPIL().transpose(PIL.Image.ROTATE_180))