source: foam/trunk/python/faces.py @ 114

Revision 114, 2.8 KB checked in by dave, 10 years ago (diff)

added a load of tools for previewing eigenspaces

Line 
1# faces.py
2# bunch of scripts for doing things with faces
3
4import glob,string
5import numpy as np
6import scipy.misc.pilutil as smp
7from vision import *
8
9#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10
11def filename_from_path(path):
12        return path[string.rfind(path,"/")+1:]
13
14#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15# returns a vector with the extra parameter appended
16
17def add_parameter(v,p):
18        vp = FloatVector(v.Size()+1)
19        for i in range(0,v.Size()):
20                vp[i]=v[i]
21        vp[v.Size()]=p
22        return vp
23
24#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25# builds a eigenmatrix from all the images and saves the pca file
26
27def build_pca(imagepath,w,h,filename):
28        images = glob.glob(imagepath)
29        pca = PCA(w*h)
30       
31        for image in images:
32                im = Image(image)
33                v = im.Scale(w,h).RGB2GRAY().ToFloatVector()
34                pca.AddFeature(v)
35       
36        pca.Calculate()
37       
38        f = OpenFile(filename, "wb")
39        pca.Save(f)
40        CloseFile(f)
41
42#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
43# do facefinding on all the images and save the cropped faces
44
45def get_faces(imagepath,outpath):
46        images = glob.glob(imagepath)
47        finder = FaceFinder()
48        for image in images:
49                i = Image(image)
50                rects = finder.Find(i,False)
51                for rect in rects:
52                        face = i.SubImage(rect)                 
53                        face.Save(outpath+filename_from_path(image))
54
55#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56# attempts to build an expression eigenspace with emotion
57# parameter tacked on (based on the image filename)
58
59def build_expression_pca(imagepath,w,h,filename):
60        images = glob.glob(imagepath)
61       
62        pca = PCA(w*h+1)
63       
64        for imagename in images:
65                emotion=0
66                if "happy" in imagename:
67                        emotion=1
68                if "sad" in imagename:
69                        emotion=-1
70                print(imagename+" is "+str(emotion))
71       
72                im = Image(imagename)
73                v = im.Scale(w,h).RGB2GRAY().ToFloatVector()
74                pca.AddFeature(add_parameter(v,emotion))
75       
76        pca.Calculate()
77       
78        f = OpenFile(filename, "wb")
79        pca.Save(f)
80        CloseFile(f)
81
82#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
83# takes an image and attempts to calculate it's extra parameter
84
85def emotion_from_expression(filename,w,h,pca):
86        i = Image(filename)
87        v = i.Scale(w,h).RGB2GRAY().ToFloatVector()
88        p = pca.Project(add_parameter(v,0))
89        s = pca.Synth(p)
90        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 TracBrowser for help on using the repository browser.