-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouthAFLW_ALL.py
134 lines (99 loc) · 3.97 KB
/
mouthAFLW_ALL.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
"""
Created on Tue May 7 17:29:01 2013
@author: attale00
"""
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 12 15:24:39 2013
This script classifies the multipie pictures with the random forest classifer learned on the aflw database pics
. It was trained on hog features only on the down scaled images. see the accompanying info file to the classfier for details
@author: attale00
"""
import utils
import featureGeneration as fg
import cv2
import numpy as np
import pickle
import sys
import plottingUtils
import classifierUtils
from sklearn import svm
def main(mode):
path = '/local/attale00/AFLW_ALL'
path_ea = path+'/color256/'
fileNames = utils.getAllFiles(path_ea);
labs=utils.parseLabelFiles(path+'/labels/labels','mouth_opening',fileNames,cutoffSeq='.png',suffix='_face0.labels')
testSet = fg.dataContainer(labs)
roi=(88,165,150,362)
#roi=(44,84,88,168)
# eM=np.load('/home/attale00/Desktop/mouthMask.npy')
# m=cv2.resize(np.uint8(eM),(256,256));
# strel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
# dil = cv2.dilate(m,strel)
#
# m=dil>0;
fg.getHogFeature(testSet,roi,path=path_ea,ending='.png',extraMask = None,orientations = 4, cells_per_block=(26,9),maskFromAlpha=False)
fg.getColorHistogram(testSet,roi,path=path_ea,ending='.png',colorspace='lab',bins=20)
testSet.targetNum=map(utils.mapMouthLabels2Two,testSet.target)
rf=classifierUtils.standardRF(max_features = np.sqrt(len(testSet.data[0])),min_split=5,max_depth=40)
print len(testSet.data)
if mode in ['s','v']:
print 'Classifying with loaded classifier'
_classifyWithOld(path,testSet,mode)
elif mode in ['c']:
print 'cross validation of data'
#classifierUtils.standardCrossvalidation(rf,testSet,n_jobs=5)
classifierUtils.dissectedCV(rf,testSet)
elif mode in ['save']:
print 'saving new classifier'
_saveRF(testSet)
else:
print 'not doing anything'
def _saveRF(testSet,rf):
rf.fit(testSet.data,testSet.targetNum)
pickle.dump(rf,open('/home/attale00/Desktop/classifiers/RandomForestMouthclassifier_nTHogColorNoMask','w'))
f=open('/home/attale00/Desktop/classifiers/RandomForestMouthclassifier_nTVVVHogColorNoMask.txt','w')
f.write('Source Images: AFLW, but only original dataset of 900 pics')
f.write('attribute: Mouth')
f.write('Features: Hog\n')
f.write('Features: getHogFeature(orientations = 4, cells_per_block=(26,9),maskFromAlpha=False \n')
f.write('ROI:(88,165,150,362)\n')
f.write('labels: none: 0, light,thick: 1\n')
f.close()
def _classifyWithOld(path,testSet,mode):
#f=file('/home/attale00/Desktop/classifiers/RandomForestMouthclassifier_1','r')
f=file('/home/attale00/Desktop/classifiers/SVMMouth_1','r')
clf = pickle.load(f)
testSet.classifiedAs=clf.predict(testSet.data)
testSet.hasBeenClassified = True
if mode =='s':
_score(clf,testSet)
else:
_view(clf,testSet,path+'Multi-PIE/extracted/')
_score(clf,testSet)
def _score(clf,testSet):
score = clf.score(testSet.data,testSet.targetNum)
testSet.hasBeenClassified = True
classifierUtils.evaluateClassification(testSet,{0:'closed or narrow',1:'open or wide open'})
print 'Overall Score: {:.3f}'.format(score)
return
def _view(clf,testSet,path):
viewer = plottingUtils.ClassifiedImViewer(path,testSet)
viewer.view(comparer=plottingUtils.MouthTwo2FourComparer)
if __name__=='__main__':
if len(sys.argv)==2:
if sys.argv[1] in ['s','Score']:
m = 's'
elif sys.argv[1] in ['v','View']:
m='v'
elif sys.argv[1] in ['c']:
m='c'
elif sys.argv[1] in ['save']:
m='save'
else:
print 'Option not supported, valid options are s,v. Now just scoring'
m='s'
else:
m='s'
main(m)