def getEben( self ):
     i = 20
     tr = 500
     out =    "C:\\Users\\Valerie\\Desktop\\output\\20-500-1" 
     ### END OF NEED?
     fh = FileHelper( i, tr, out, self.fPts, False) 
     trainImg = fh.readInEbenImg()
     trainPts = fh.readInOneDude( '000_1_1.pts')  
     return trainImg, ActiveShape( trainPts )
def process( fPts, fImgs, train, method, maxPx, fSize ):
    params ={ "nIters" : 7, 
              "nTrain" : 500,
              "filterPts" : fPts,
              "filterImgs" : fImgs,
              "trainASM" : train, 
              "writePCA" : False,
              "maxPx" : maxPx,
              "fSize" : fSize,
              "method" : method
              }


    params = OrderedDict( sorted( params.items(), key = lambda t : len(t[0]) ) )
    ##Methods: "SSD", "NCorr", or "grad"
    ASMout = getASMfolder( params )
    appASMout = getMethodSubFolder( params, ASMout )

    fh = FileHelper( params["nIters"], params["nTrain"], ASMout, params["filterPts"], params["filterImgs"] )

    if params["filterPts"]:
        asm = ActiveShapeModel( [43,35] ) 
    else: 
        asm = ActiveShapeModel( [36,31] )    

    if params["trainASM"]:
        asm = fh.readInPoints( asm )
        asm = ShapeAligner( asm, params["nIters"], ASMout ).alignTrainingSet( )
        fh.writeOutASM( asm ) #write out to read in later
    else: 
        asm = fh.readInASM( asm )

    ### Calculate Principal components

    asm.PCA()

    if params["trainASM"] or params["writePCA"]:
        ### Draw PCA stuffs
        if not os.path.exists( os.path.join( ASMout, "PCA" ) ) :
            os.mkdir( os.path.join( ASMout, "PCA" ) )
        wu = WriteUp( asm, fh )
        wu.PCAresults()

    if not params["trainASM"] :
        ### Apply ASM to image
        img = cv2.imread( "C:\Users\Valerie\Desktop\MicroExpress\CASME2\CASME2_RAW\CASME2-RAW\sub01\EP02_01f\img1.jpg")
        img =  cv2.cvtColor( img, cv2.COLOR_BGR2GRAY)
        appASM = ApplyASM( asm, params["nIters"], params["nTrain"],
                          appASMout, img, params['method'], 
                          params['maxPx'], params['fSize'], params["filterPts"]  )
        appASM.applyASM()
Esempio n. 3
0
def process(fPts, fImgs, train, method, maxPx, fSize):
    params = {
        "nIters": 7,
        "nTrain": 500,
        "filterPts": fPts,
        "filterImgs": fImgs,
        "trainASM": train,
        "writePCA": False,
        "maxPx": maxPx,
        "fSize": fSize,
        "method": method
    }

    params = OrderedDict(sorted(params.items(), key=lambda t: len(t[0])))
    ##Methods: "SSD", "NCorr", or "grad"
    ASMout = getASMfolder(params)
    appASMout = getMethodSubFolder(params, ASMout)

    fh = FileHelper(params["nIters"], params["nTrain"], ASMout,
                    params["filterPts"], params["filterImgs"])

    if params["filterPts"]:
        asm = ActiveShapeModel([43, 35])
    else:
        asm = ActiveShapeModel([36, 31])

    if params["trainASM"]:
        asm = fh.readInPoints(asm)
        asm = ShapeAligner(asm, params["nIters"], ASMout).alignTrainingSet()
        fh.writeOutASM(asm)  #write out to read in later
    else:
        asm = fh.readInASM(asm)

    ### Calculate Principal components

    asm.PCA()

    if params["trainASM"] or params["writePCA"]:
        ### Draw PCA stuffs
        if not os.path.exists(os.path.join(ASMout, "PCA")):
            os.mkdir(os.path.join(ASMout, "PCA"))
        wu = WriteUp(asm, fh)
        wu.PCAresults()

    if not params["trainASM"]:
        ### Apply ASM to image
        img = cv2.imread(
            "C:\Users\Valerie\Desktop\MicroExpress\CASME2\CASME2_RAW\CASME2-RAW\sub01\EP02_01f\img1.jpg"
        )
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        appASM = ApplyASM(asm, params["nIters"], params["nTrain"], appASMout,
                          img, params['method'], params['maxPx'],
                          params['fSize'], params["filterPts"])
        appASM.applyASM()
    row = rows.flatten()
    col = cols.flatten()
    
    return map( lambda x: Vector( x[0], x[1] ), zip( row, col )  )

def coordOffset( pt, n ):
    y = pt.y   #row
    x = pt.x   #col
    return x - ( n - 1)/2, y - (n-1)/1 

#def run( ):
i = 20
tr = 500
out =    "C:\\Users\\Valerie\\Desktop\\output\\20-500-1" 

fh = FileHelper( i, tr, out )

ebenFace  =  fh.readInImage()
ebenPoints = fh.readInOneDude( '000_1_1.pts')  
ebenShape = ActiveShape( ebenPoints )
 
#### MATCHING PROCESS

# Read in image
I = cv2.imread( "C:\Users\Valerie\Desktop\MicroExpress\CASME2\CASME2_RAW\CASME2-RAW\sub01\EP02_01f\img1.jpg")
I = cv2.cvtColor( I, cv2.COLOR_BGR2GRAY)

# Align shape
asm = ActiveShapeModel( [36,31] )
asm = fh.readInASM( asm )
asm.PCA()  
from helpers.DrawFace import DrawFace
from shapes.ActiveShape import ActiveShape
from active_shape_models.ActiveShapeModel import ActiveShapeModel
from active_shape_models.ApplyASM import ApplyASM
from shapes.Vector import Vector
from matplotlib import pyplot as plt
from matplotlib import gridspec
import numpy as np
import math
from image_processing.TemplateMatcher import TemplateMatcher

i = 20
tr = 500
out =    "C:\\Users\\Valerie\\Desktop\\output\\ASMTraining-MessingAround\\20-500-1" 

fh = FileHelper( i, tr, out, False, False )

ebenFace  =  fh.readInImage()
ebenPoints = fh.readInOneDude( '000_1_1.pts')  
ebenShape = ActiveShape( ebenPoints )

## draw indices
DrawFace( ebenShape, plt).labelIndices()
plt.imshow( ebenFace )
plt.set_cmap( "gray" )
plt.gca().axes.xaxis.set_ticks([])
plt.gca().axes.yaxis.set_ticks([])
plt.show()


fh = FileHelper( i, tr, out, True, False )
if not os.path.exists( os.path.join( output, study ) ) and (train or align ):
    os.mkdir( os.path.join( output, study ) )
    output = os.path.join( output, study )
else:                       
    while os.path.exists( os.path.join( output, study )):
        i += 1
        study = "%d-%d-%d" % ( iters, training, i )
        if train or align:
            os.mkdir( os.path.join( output, study ) )
        else: 
            study = "%d-%d-%d" % ( iters, training, i )
        output =  os.path.join( output, study )



fh = FileHelper( iters, training , output )

if train:
    asm = ActiveShapeModel( [36,31] )
    asm = fh.readInPoints( asm )

### Align Shapes
if align:
    asm = ShapeAligner( asm, iters, output ).alignTrainingSet( )
    fh.writeOutASM( asm ) #write out to read in later


d = map( lambda x: x.shapeDist( asm.normMeanShape ), asm.allShapes )
d1 = np.mean( d, 1 )

min = np.min( d1 )
i = 0
study = "%d-%d-%d" % (iters, training, i)
if not os.path.exists(os.path.join(output, study)) and (train or align):
    os.mkdir(os.path.join(output, study))
    output = os.path.join(output, study)
else:
    while os.path.exists(os.path.join(output, study)):
        i += 1
        study = "%d-%d-%d" % (iters, training, i)
        if train or align:
            os.mkdir(os.path.join(output, study))
        else:
            study = "%d-%d-%d" % (iters, training, i)
        output = os.path.join(output, study)

fh = FileHelper(iters, training, output)

if train:
    asm = ActiveShapeModel([36, 31])
    asm = fh.readInPoints(asm)

### Align Shapes
if align:
    asm = ShapeAligner(asm, iters, output).alignTrainingSet()
    fh.writeOutASM(asm)  #write out to read in later

d = map(lambda x: x.shapeDist(asm.normMeanShape), asm.allShapes)
d1 = np.mean(d, 1)

min = np.min(d1)
mn = np.mean(d1)