Changeset 114


Ignore:
Timestamp:
09/11/2009 04:25:20 PM (10 years ago)
Author:
dave
Message:

added a load of tools for previewing eigenspaces

Location:
foam/trunk/python
Files:
3 added
3 edited

Legend:

Unmodified
Added
Removed
  • foam/trunk/python/build-pca.py

    r113 r114  
    55from faces import * 
    66 
    7 #get_faces("../no-redist/yalefaces/orig/*.png", "../no-redist/yalefaces/processed/") 
    8 # calculate("../no-redist/spacek-large/*.png",20,30,"test.pca") 
     7if sys.argv[1] == "-h": 
     8        print("build_pca inpath/*.png width height outfile") 
    99 
    10 build_pca("../no-redist/yalefaces/processed/*.png",20,30,"yalefaces-expression-20x30.pca") 
     10build_pca(sys.argv[1],int(sys.argv[2]),int(sys.argv[3]),sys.argv[4]) 
  • foam/trunk/python/expression.py

    r113 r114  
    55from faces import * 
    66         
    7 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    8  
    9 #build_expression_pca("../no-redist/yalefaces/processed/*.png",20,30,"expr.pca") 
     7# just try projecting onto vectors that seem 'happy' 
    108 
    119#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     10def print_weight(filename,param,pca,w,h): 
     11        i = Image(filename) 
     12        v = i.Scale(w,h).RGB2GRAY().ToFloatVector() 
     13        p = pca.Project(v) 
     14        print(p[param]) 
    1215 
    13 pca = PCA(1)     
    14 f = OpenFile("expr.pca", "rb") 
    15 pca.Load(f) 
    16 CloseFile(f) 
     16pca = load_pca("yalefaces-30x50.pca") 
     17w = 30 
     18h = 50 
     19filename = "../data/images/faces/dave/expression/normal.png" 
    1720 
    18 emotion_from_expression("../data/images/faces/dave/expression/normal.png",20,30,pca) 
     21print("normal=") 
     22print_weight("../data/images/faces/dave/expression/normal.png",13,pca,w,h) 
     23print("happy=") 
     24print_weight("../data/images/faces/dave/expression/happy.png",13,pca,w,h) 
     25print("angry=") 
     26print_weight("../data/images/faces/dave/expression/angry.png",13,pca,w,h) 
     27print("grimace=") 
     28print_weight("../data/images/faces/dave/expression/grimace.png",13,pca,w,h) 
  • foam/trunk/python/faces.py

    r113 r114  
    33 
    44import glob,string 
     5import numpy as np 
     6import scipy.misc.pilutil as smp 
    57from vision import * 
    68 
     
    8789        s = pca.Synth(p) 
    8890        return s[w*h]>0; 
     91         
     92def load_pca(filename): 
     93        pca = PCA(1)     
     94        f = OpenFile(filename, "rb") 
     95        pca.Load(f) 
     96        CloseFile(f) 
     97        return pca 
     98         
     99def plot_eigenface(image,x,y,w,h,pca,row,gain): 
     100        eigenface = pca.GetEigenTransform().GetRowVector(row)*gain+pca.GetMean() 
     101        c=0 
     102        for iy in range(0,h): 
     103                for ix in range(0,w): 
     104                        v = int(max(min(256*eigenface[c],256),0)) 
     105                        image[iy+y,ix+x] = [v,v,v]  
     106                        c=c+1 
     107 
     108def make_eigenfaces_image(w,h,pca,start,end,gain): 
     109        num_imagesx = end-start;         
     110        image = np.zeros( (h,w*num_imagesx,3), dtype=np.uint8 ) 
     111        for i in range(start, end): 
     112                c=i-start 
     113                plot_eigenface(image,w*c,0,w,h,pca,i,gain) 
     114        return image 
Note: See TracChangeset for help on using the changeset viewer.