1 '''
2 This package contains some standard tests that can be performed with
3 the test data distributed with pyvision.
4 '''
5 import pyvision as pv
6 import os
7 from pyvision.analysis.FaceAnalysis.EyesFile import EyesFile
8 import csv
9 import numpy as np
10
11 SCRAPS_EYES = EyesFile(os.path.join(pv.__path__[0],'data','csuScrapShots','coords.txt'))
12 ''' The path to a text file containing coordinates to the scrapshots dataset. '''
13
14
15
16
17 AIRPLANE = os.path.join(pv.__path__[0],'data','misc','airplane.jpg')
18 ''' The path to a commonly use test image of a jet airplane.'''
19
20 BABOON = os.path.join(pv.__path__[0],'data','misc','baboon.jpg')
21 ''' The path to a commonly use test image of a baboon face.'''
22
23 FRUITS = os.path.join(pv.__path__[0],'data','misc','fruits.jpg')
24 ''' The path to a commonly use test image of fruit.'''
25
26 LENA = os.path.join(pv.__path__[0],'data','misc','lena.jpg')
27 ''' The path to a commonly used "Lena" test image.'''
28
29 LOGO = os.path.join(pv.__path__[0],'data','misc','logo.jpg')
30 ''' The path to a commonly use test image of a butterfly used as the pyvision logo.'''
31
32 TAZ_IMAGE = os.path.join(pv.__path__[0],'data','test','TAZ_0010.jpg')
33 ''' The path to a commonly use test image of a butterfly used as the pyvision logo.'''
34
35 TAZ_VIDEO = os.path.join(pv.__path__[0],'data','test','TazSample.m4v')
36 ''' The path to a test video of a Loony Tunes Taz stuffed animal.'''
37
38 CAR_VIDEO = os.path.join(pv.__path__[0],'data','test','toy_car.m4v')
39 ''' The path to a test video of a small moving car.'''
40
41 BUGS_VIDEO = os.path.join(pv.__path__[0],'data','test','BugsSample.m4v')
42 ''' The path to a test video sequence of bugs crawling through weeds.'''
43
44
45 FONT_ARIAL = os.path.join(pv.__path__[0],'config','Arial.ttf')
46 ''' The path to a file containing the Arial true type font. '''
47
48 IRIS_PATH = os.path.join(pv.__path__[0],'data','ml','iris.csv')
49 IRIS_DATA = np.array(list(csv.reader(open(IRIS_PATH,'rb'))))
50 IRIS_LABELS = IRIS_DATA[1:,5]
51 IRIS_DATA = np.array(IRIS_DATA[1:,1:5],dtype=np.float32)
52
53
54
56 '''
57 genderClassifier takes a classifier as an argument and will use the
58 csuScrapShot data to perform a gender classification test on that
59 classifier.
60
61 These three functions will be called::
62
63 for im in training_images:
64 clsfy.addTraining(label,im,ilog=ilog)
65
66 clsfy.train(ilog=ilog)
67
68 for im in testing_images:
69 clsfy.predict(im,ilog=ilog)
70
71 label = 0 or 1 (0=Female,1=Male)
72
73 im is a 64x64 pyvision image that is normalized to crop the face
74
75 Output of predict should be a class label (0 or 1)
76
77 @returns: the success rate for the testing set.
78 '''
79 filename = os.path.join(pv.__path__[0],'data','csuScrapShots','gender.txt')
80 f = open(filename,'r')
81 image_cache = []
82 examples = []
83 for line in f:
84 im_name, class_name = line.split()
85 if class_name == 'F':
86 class_name = 0
87 else:
88 class_name = 1
89 long_name = os.path.join(pv.__path__[0],'data','csuScrapShots',im_name)
90 leye,reye = SCRAPS_EYES.getEyes(im_name)[0]
91 im = pv.Image(long_name)
92 image_cache.append(im)
93 im = pv.AffineFromPoints(leye,reye,pv.Point(22,27),pv.Point(42,27),(64,64)).transformImage(im)
94
95 examples.append([class_name,im,im_name])
96
97 training = examples[:103]
98 testing = examples[103:]
99
100 for each in training[:103]:
101 clsfy.addTraining(each[0],each[1],ilog=ilog)
102
103 clsfy.train(ilog=ilog)
104
105 table = pv.Table()
106
107
108 correct = 0
109 total = 0
110 for each in testing:
111 label = clsfy.predict(each[1],ilog=ilog)
112 total += 1
113 if label == each[0]:
114 correct += 1
115
116 rate = float(correct)/total
117
118 if ilog: ilog.table(table)
119 return rate
120
121 if __name__ == "__main__":
122 from pyvision.vector.SVM import SVM
123
124 svm = SVM(kernel='LINEAR',random_seed=30)
125 ilog = pv.ImageLog()
126 print "SVM rate:",genderClassifier(svm,ilog=None)
127
128 svm = SVM(kernel='RBF',random_seed=30)
129 ilog = pv.ImageLog()
130 print "SVM rate:",genderClassifier(svm,ilog=None)
131
132 ilog.show()
133