Exemple #1
0
    def preprocess(self, tile, ilog=None):
        ''' Implements some standard preprocessing for all filters. '''

        # Get the image tile as a numpy matrix.
        if isinstance(tile, pv.Image):
            mat = tile.asMatrix2D()
        else:
            # Assume it is already a matrix
            mat = tile

        # Apply a logarithmic transformation to pixel values.
        if self.norm_log:
            mat = np.log(mat + 1)

        # print "tile is:",mat
        # Transform pixel values to have a mean of zero and unit length
        if self.norm_meanunit:
            mat = pv.meanUnit(mat)
        # print "mat is:",mat

        # Window the input tile to reduce fourier edge effects.
        window = self._resizeWindow(mat.shape)
        if window is not None:
            mat = mat * window

        if ilog != None:
            ilog(pv.Image(mat), label="PREPROCESSED")

        return mat
Exemple #2
0
    def extract(self, img, face_records):
        '''Extract a template that allows the face to be matched.'''
        # Compute the 512D vector that describes the face in img identified by
        #shape.

        im = pv.Image(img[:, :, ::-1])

        for face_record in face_records.face_records:
            rect = pt.rect_proto2pv(face_record.detection.location)
            x, y, w, h = rect.asTuple()

            # Extract view
            rect = pv.Rect()
            cx, cy = x + 0.5 * w, y + 0.5 * h
            tmp = 1.5 * max(w, h)
            cw, ch = tmp, tmp
            crop = pv.AffineFromRect(pv.CenteredRect(cx, cy, cw, ch),
                                     (256, 256))

            pvim = pv.Image(img[:, :, ::-1])  # convert rgb to bgr
            pvim = crop(pvim)
            view = pt.image_pv2proto(pvim)
            face_record.view.CopyFrom(view)

            tile = pvim.resize((224, 224))
            tile = tile.resize((112, 112))

            face_im = tile.asOpenCV2()
            face_im = face_im[:, :, ::-1]  # Convert BGR to RGB

            features = self.fr_model.get_embedding(face_im)
            face_descriptor = pv.meanUnit(features.flatten())

            face_record.template.data.CopyFrom(
                pt.vector_np2proto(face_descriptor))
Exemple #3
0
    def extractTile(self, im):
        import sys
        #import numpy as np
        from keras.preprocessing import image
        #from keras_vggface.vggface import VGGFace
        from keras_vggface import utils

        assert im.shape == (224, 224, 3)
        #tile = tile.resize((224,224))

        #tile.show(delay=1000)

        img = im
        #img = img[:,:,::-1] # Convert BGR to RGB
        #mat_ = cv2.cvtColor(mat,cv2.COLOR_RGB2GRAY)
        #mat = cv2.cvtColor(mat_,cv2.COLOR_GRAY2RGB)

        #img = image.load_img('../image/ajb.jpg', target_size=(224, 224))
        img = image.img_to_array(img)
        img = np.expand_dims(img, axis=0)
        img = utils.preprocess_input(img, version=1)  # or version=2
        #preds = model.predict(x)
        #print('Predicted:', utils.decode_predictions(preds))

        #mat = mat[:,:,::-1]

        #mat.shape = (1,224,224,3)

        # Needed in multithreaded applications
        with self.graph.as_default():
            tmp = self.recognizer.predict(img)

        temp = pv.meanUnit(tmp.flatten())

        return temp
Exemple #4
0
    def computeVector(self, img):
        '''Creates a vector from a face'''
        #face = img.asPIL().crop(rect.box()).resize(self.face_size,ANTIALIAS)
        vec = img.asMatrix2D().flatten()

        if self.norm == PCA_MEAN_STD_NORM:
            vec = pv.meanStd(vec)
        if self.norm == PCA_MEAN_UNIT_NORM:
            vec = pv.meanUnit(vec)
        if self.norm == PCA_UNIT_NORM:
            vec = pv.unit(vec)

        return vec
Exemple #5
0
 def computeVector(self,img):
     '''Creates a vector from a face'''
     #face = img.asPIL().crop(rect.box()).resize(self.face_size,ANTIALIAS)
     vec  = img.asMatrix2D().flatten()
     
     if self.norm == PCA_MEAN_STD_NORM:
         vec = pv.meanStd(vec)
     if self.norm == PCA_MEAN_UNIT_NORM:
         vec = pv.meanUnit(vec)
     if self.norm == PCA_UNIT_NORM:
         vec = pv.unit(vec)
         
     return vec
Exemple #6
0
 def test_2_meanUnit(self):
     '''meanUnit Normalization: norm.mean() = 0.0  and ||norm|| = 1.0....'''
     ilog = None
     if 'ilog' in list(globals().keys()):
         ilog = globals()['ilog']
         
     norm = pv.meanUnit(self.tile)
     
     if ilog != None:
         ilog.log(norm,label="meanUnit_Normalization")
         
     mat = norm.asMatrix2D()
     self.assertAlmostEqual(mat.mean(),0.0)
     length = np.sqrt((mat**2).sum())
     self.assertAlmostEqual(length,1.0,places=4)
Exemple #7
0
    def test_2_meanUnit(self):
        '''meanUnit Normalization: norm.mean() = 0.0  and ||norm|| = 1.0....'''
        ilog = None
        if 'ilog' in list(globals().keys()):
            ilog = globals()['ilog']

        norm = pv.meanUnit(self.tile)

        if ilog != None:
            ilog.log(norm, label="meanUnit_Normalization")

        mat = norm.asMatrix2D()
        self.assertAlmostEqual(mat.mean(), 0.0)
        length = np.sqrt((mat**2).sum())
        self.assertAlmostEqual(length, 1.0, places=4)
def PhaseCorrelation(tile1, tile2, phase_only=True, ilog=None):
    '''
    Uses phase correlation to estimate the best integer displacement to align the images.
    Also fits a quadradic to the correltaion surface to determine a sub pixel estimate of 
    displacement.
    
    Returns four values as a tuple: max_corr, max_displacement, est_corr, est_displacement
    max_corr         - maximum correlation value.
    max_displacement - displacement needed to obtain the maximum correlation. (full pixel)
    est_corr         - estimated corelation value if subpixel displacement is used.
    est_displacement - estimated displacement (subpixel)
    
    see http://en.wikipedia.org/wiki/Phase_correlation
    '''
    if isinstance(tile1,pv.Image):
        tile1 = tile1.asMatrix2D()
    else:
        tile1 = pv.Image(tile1).asMatrix2D()
        raise TypeError("Please pass in a numpy array or a pyvision image.")

    if isinstance(tile2,pv.Image):
        tile2 = tile2.asMatrix2D()
    else:
        tile2 = pv.Image(tile2).asMatrix2D()    
    
    if tile1.shape != tile2.shape:
        raise ValueError("Image tiles must have the same shape. [tile1 %s] != [tile2 %s]"%(tile1.shape,tile2.shape))

    # copy the data
    tile1 = tile1.copy()
    tile2 = tile2.copy()

    # normalize the image tiles
    tile1 = pv.meanUnit(tile1)
    tile2 = pv.meanUnit(tile2)

    # compute the fft
    Ia = np.fft.fft2(tile1)
    Ib = np.conjugate(np.fft.fft2(tile2))

    # build the normalized cross-power spectrum
    ncs = Ia*Ib

    if phase_only:
        ncs = ncs/np.abs(ncs)

    # build the power spectrum
    pc = np.real(np.fft.ifft2(ncs))
    
    if ilog != None:
        ilog.log(pv.Image(tile1),label="Tile1")
        ilog.log(pv.Image(tile2),label="Tile2")
        ilog.log(pv.Image(np.fft.fftshift(pc)),label="Correlation")

    max_corr = pc.max()
    max_elem = (pc == max_corr).nonzero()
    max_elem = max_elem[0][0],max_elem[1][0]

    max_point = list(max_elem)
    if max_elem[0]*2 > tile1.shape[0]:
        max_point[0] = -tile1.shape[0] + max_elem[0]
    if max_elem[1]*2 > tile1.shape[1]:
        max_point[1] = -tile1.shape[1] + max_elem[1]

    est_corr, est_point = QuadradicEstimation(pc,max_point)

    return max_corr, max_point, est_corr, est_point
Exemple #9
0
def PhaseCorrelation(tile1, tile2, phase_only=True, ilog=None):
    '''
    Uses phase correlation to estimate the best integer displacement to align the images.
    Also fits a quadradic to the correltaion surface to determine a sub pixel estimate of 
    displacement.
    
    Returns four values as a tuple: max_corr, max_displacement, est_corr, est_displacement
    max_corr         - maximum correlation value.
    max_displacement - displacement needed to obtain the maximum correlation. (full pixel)
    est_corr         - estimated corelation value if subpixel displacement is used.
    est_displacement - estimated displacement (subpixel)
    
    see http://en.wikipedia.org/wiki/Phase_correlation
    '''
    if isinstance(tile1, pv.Image):
        tile1 = tile1.asMatrix2D()
    else:
        tile1 = pv.Image(tile1).asMatrix2D()
        raise TypeError("Please pass in a numpy array or a pyvision image.")

    if isinstance(tile2, pv.Image):
        tile2 = tile2.asMatrix2D()
    else:
        tile2 = pv.Image(tile2).asMatrix2D()

    if tile1.shape != tile2.shape:
        raise ValueError(
            "Image tiles must have the same shape. [tile1 %s] != [tile2 %s]" %
            (tile1.shape, tile2.shape))

    # copy the data
    tile1 = tile1.copy()
    tile2 = tile2.copy()

    # normalize the image tiles
    tile1 = pv.meanUnit(tile1)
    tile2 = pv.meanUnit(tile2)

    # compute the fft
    Ia = np.fft.fft2(tile1)
    Ib = np.conjugate(np.fft.fft2(tile2))

    # build the normalized cross-power spectrum
    ncs = Ia * Ib

    if phase_only:
        ncs = ncs / np.abs(ncs)

    # build the power spectrum
    pc = np.real(np.fft.ifft2(ncs))

    if ilog != None:
        ilog.log(pv.Image(tile1), label="Tile1")
        ilog.log(pv.Image(tile2), label="Tile2")
        ilog.log(pv.Image(np.fft.fftshift(pc)), label="Correlation")

    max_corr = pc.max()
    max_elem = (pc == max_corr).nonzero()
    max_elem = max_elem[0][0], max_elem[1][0]

    max_point = list(max_elem)
    if max_elem[0] * 2 > tile1.shape[0]:
        max_point[0] = -tile1.shape[0] + max_elem[0]
    if max_elem[1] * 2 > tile1.shape[1]:
        max_point[1] = -tile1.shape[1] + max_elem[1]

    est_corr, est_point = QuadradicEstimation(pc, max_point)

    return max_corr, max_point, est_corr, est_point
Exemple #10
0
    def extract(self, img, face_records):
        '''Extract a template that allows the face to be matched.'''
        # Compute the 128D vector that describes the face in img identified by
        # shape.  In general, if two face descriptor vectors have a Euclidean
        # distance between them less than 0.6 then they are from the same
        # person, otherwise they are from different people. Here we just print
        # the vector to the screen.

        im = pv.Image(img[:, :, ::-1])

        for face_record in face_records.face_records:
            rect = pt.rect_proto2pv(face_record.detection.location)
            x, y, w, h = rect.asTuple()

            # Extract view
            rect = pv.Rect()
            cx, cy = x + 0.5 * w, y + 0.5 * h
            tmp = 1.5 * max(w, h)
            cw, ch = tmp, tmp
            crop = pv.AffineFromRect(pv.CenteredRect(cx, cy, cw, ch),
                                     (256, 256))

            pvim = pv.Image(img[:, :, ::-1])  # convert rgb to bgr
            pvim = crop(pvim)
            view = pt.image_pv2proto(pvim)
            face_record.view.CopyFrom(view)

            # Extract landmarks
            l, t, r, b = [int(tmp) for tmp in [x, y, x + w, y + h]]
            d = dlib.rectangle(l, t, r, b)
            shape = self.shape_pred(img, d)

            for i in range(len(shape.parts())):
                loc = shape.parts()[i]
                landmark = face_record.landmarks.add()
                landmark.landmark_id = "point_%02d" % i
                landmark.location.x = loc.x
                landmark.location.y = loc.y

            # Get detection rectangle and crop the face
            #rect = pt.rect_proto2pv(face_record.detection.location).rescale(1.5)
            #tile = im.crop(rect)
            tile = pvim.resize((224, 224))

            #tile.show(delay=1000)

            face_im = tile.asOpenCV2()
            face_im = face_im[:, :, ::-1]  # Convert BGR to RGB
            #mat_ = cv2.cvtColor(mat,cv2.COLOR_RGB2GRAY)
            #mat = cv2.cvtColor(mat_,cv2.COLOR_GRAY2RGB)

            #img = image.load_img('../image/ajb.jpg', target_size=(224, 224))

            from keras_vggface import utils
            from keras.preprocessing import image

            face_im = image.img_to_array(face_im)
            face_im = np.expand_dims(face_im, axis=0)
            face_im = utils.preprocess_input(face_im,
                                             version=2)  # or version=2

            # Needed in multithreaded applications
            with self.graph.as_default():
                tmp = self.recognizer.predict(face_im)

            face_descriptor = pv.meanUnit(tmp.flatten())

            #print('shape:',face_records.face_records[0].landmarks)
            #face_descriptor = self.face_rec.compute_face_descriptor(img, shape, JITTER_COUNT)
            #face_descriptor = np.array(face_descriptor)

            #vec = face_descriptor.flatten()
            face_record.template.data.CopyFrom(
                pt.vector_np2proto(face_descriptor))