#
source:
libs/magicsquares/python/faces.py
@
213

Revision 213, 3.0 KB checked in by dave, 10 years ago (diff) |
---|

Line | |
---|---|

1 | # faces.py |

2 | # bunch of scripts for doing things with faces |

3 | |

4 | import glob,string |

5 | import numpy as np |

6 | import scipy.misc.pilutil as smp |

7 | from magicsquares import * |

8 | |

9 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |

10 | |

11 | def filename_from_path(path): |

12 | return path[string.rfind(path,"/")+1:] |

13 | |

14 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |

15 | # returns a vector with the extra parameter appended |

16 | |

17 | def 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 | |

27 | def 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 | |

45 | def 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 | |

59 | def 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 | |

85 | def 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 | |

92 | def load_pca(filename): |

93 | pca = PCA(1) |

94 | f = OpenFile(filename, "rb") |

95 | pca.Load(f) |

96 | CloseFile(f) |

97 | return pca |

98 | |

99 | def vec2npimg(w,h,v): |

100 | c=0 |

101 | image = np.zeros( (h,w,3), dtype=np.uint8 ) |

102 | for iy in range(0,h): |

103 | for ix in range(0,w): |

104 | i = int(max(min(256*v[c],256),0)) |

105 | image[iy,ix] = [i,i,i] |

106 | c=c+1 |

107 | return image |

108 | |

109 | def plot_eigenface(image,x,y,w,h,pca,row,gain): |

110 | eigenface = pca.GetEigenTransform().GetRowVector(row)*gain+pca.GetMean() |

111 | c=0 |

112 | for iy in range(0,h): |

113 | for ix in range(0,w): |

114 | v = int(max(min(256*eigenface[c],256),0)) |

115 | image[iy+y,ix+x] = [v,v,v] |

116 | c=c+1 |

117 | |

118 | def make_eigenfaces_image(w,h,pca,start,end,gain): |

119 | num_imagesx = end-start; |

120 | image = np.zeros( (h,w*num_imagesx,3), dtype=np.uint8 ) |

121 | for i in range(start, end): |

122 | c=i-start |

123 | plot_eigenface(image,w*c,0,w,h,pca,i,gain) |

124 | return image |

**Note:**See TracBrowser for help on using the repository browser.