├── .gitignore ├── README.md ├── featureExtraction.py ├── featuresColor.py ├── featuresCorners.py ├── featuresDOF.py ├── featuresFace.py ├── featuresHOG.py ├── featuresLBP.py ├── featuresLBP2.py ├── featuresLines.py ├── haarcascade_frontalface_default.xml ├── knnSpeechMusicSpecs ├── sampledata ├── music.melodies_snatch_0081.png ├── s_30_r_335.png └── spectrograms │ ├── music │ ├── hannah_music07.png │ ├── lord_02.png │ ├── m_10_r_32.png │ ├── m_10_r_60.png │ ├── m_30_r_113.png │ ├── m_30_r_125.png │ ├── m_30_r_192.png │ ├── m_30_r_219.png │ ├── m_30_r_231.png │ ├── m_30_r_255.png │ ├── m_30_r_280.png │ ├── m_30_r_285.png │ ├── m_30_r_31.png │ ├── m_30_r_383.png │ ├── m_30_r_391.png │ ├── m_30_r_414.png │ ├── m_30_r_442.png │ ├── m_30_r_444.png │ ├── m_5_r_03.png │ ├── m_5_r_123.png │ ├── m_5_r_139.png │ ├── m_5_r_180.png │ ├── m_5_r_36.png │ ├── m_5_r_89.png │ ├── music.melodies_blackdawn_0243.png │ ├── music.melodies_blackdawn_0254.png │ ├── music.melodies_boogeyman_0196.png │ ├── music.melodies_boogeyman_0199.png │ ├── music.melodies_houseofwax_0222.png │ ├── music.melodies_madhouse_0166.png │ ├── music.melodies_madhouse_0167.png │ ├── music.melodies_madhouse_0182.png │ ├── music.melodies_residentevil2_0152.png │ ├── music.melodies_sincity_0280.png │ ├── music.melodies_snatch_0047.png │ ├── music.melodies_snatch_0055.png │ ├── music.melodies_snatch_0090.png │ ├── music.melodies_snatch_0091.png │ ├── music.melodies_stealth_0107.png │ ├── music.melodies_stealth_0135.png │ ├── music.melodies_stealth_0143.png │ ├── music.melodies_stealth_0145.png │ ├── music.melodies_thecutter_0262.png │ ├── music.melodies_thecutter_0266.png │ ├── music.melodies_underworld_0289.png │ ├── music.melodies_underworld_0295.png │ ├── music.songs_hide&seek_0002.png │ ├── music.songs_stealth_0030.png │ ├── music.songs_stealth_0043.png │ └── music.songs_underworld_0076.png │ └── speech │ ├── 1984_speech10.png │ ├── constantine_speech21.png │ ├── full_s_03.png │ ├── kill_bill_2_speech_16.png │ ├── kill_bill_2_speech_17.png │ ├── repulsion_01.png │ ├── s_10_r_130.png │ ├── s_10_r_148.png │ ├── s_10_r_50.png │ ├── s_10_r_84.png │ ├── s_10_r_86.png │ ├── s_30_r_105.png │ ├── s_30_r_108.png │ ├── s_30_r_133.png │ ├── s_30_r_14.png │ ├── s_30_r_186.png │ ├── s_30_r_212.png │ ├── s_30_r_229.png │ ├── s_30_r_259.png │ ├── s_30_r_277.png │ ├── s_30_r_291.png │ ├── s_30_r_305.png │ ├── s_30_r_322.png │ ├── s_30_r_326.png │ ├── s_30_r_333.png │ ├── s_30_r_339.png │ ├── s_30_r_340.png │ ├── s_30_r_48.png │ ├── s_30_r_72.png │ ├── s_30_r_73.png │ ├── s_5_r_109.png │ ├── s_5_r_115.png │ ├── s_5_r_18.png │ ├── s_5_r_24.png │ ├── s_5_r_28.png │ ├── s_5_r_62.png │ ├── s_5_r_71.png │ ├── s_5_r_93.png │ ├── saw_2_speech10.png │ ├── sm_30_18.png │ ├── sm_30_20.png │ ├── sm_30_30.png │ ├── sm_30_36.png │ ├── sm_30_63.png │ ├── sm_5_01.png │ ├── speech.neutral_hide&seek_0009.png │ ├── speech.neutral_snatch_0129.png │ ├── speech.neutral_stealth_0162.png │ ├── speech.neutral_stealth_0245.png │ └── speech.neutral_stealth_0308.png ├── svmSpeechMusicSpecs ├── svmSpeechMusicSpecs.arff ├── svmSpeechMusicSpecsNORMALIZATION └── train.py /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.pyc 3 | *.tex.backup 4 | *.bbl 5 | *.log 6 | *.aux 7 | *.blg 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sentimagi Python Image Analysis Library 2 | 3 | ## Requirements 4 | sudo apt-get install python-skimage 5 | sudo pip install svgwrite 6 | sudo apt-get install python-pywt 7 | 8 | ## General 9 | This library can be used for general image classification and feature extraction. 10 | 11 | ## Feature extraction: 12 | 13 | ### Extract and plot features from a single file 14 | ``` 15 | python featureExtraction.py -featuresFile sampledata/spectrograms/music/m_5_r_139.png 16 | ``` 17 | 18 | ### Extract features from two files and compare 19 | ``` 20 | python featureExtraction.py -featuresFilesCompare sampledata/spectrograms/music/m_5_r_139.png sampledata/spectrograms/speech/kill_bill_2_speech_17.png 21 | ``` 22 | 23 | ### Extract features from a set of images stored in a folder 24 | ``` 25 | python featureExtraction.py -featuresDir sampledata/spectrograms2/music/ 26 | ``` 27 | 28 | ### Extract features from a set of directories, each one defining an image class 29 | ``` 30 | python featureExtraction.py -featuresDirs spectrograms sampledata/spectrograms/music sampledata/spectrograms/speech 31 | ``` 32 | (Features are stored in file "sectrograms_features") 33 | 34 | ## Training and testing classification - regression models: 35 | 36 | ### Train an image classification model 37 | 38 | Models are trained from samples stored in folders (one folder per class). 39 | 40 | Examples: 41 | 42 | * kNN model training 43 | ``` 44 | python train.py -train knn knnSpeechMusicSpecs sampledata/spectrograms/music sampledata/spectrograms/speech 45 | ``` 46 | The above example trains a kNN classification model, does cross validation to estimate the best parameter (k value) and stores the model in a file (named knn3Classes). 47 | 48 | 49 | * SVM model training 50 | ``` 51 | python train.py -train svm svmSpeechMusicSpecs sampledata/spectrograms/music sampledata/spectrograms/speech 52 | ``` 53 | The above example trains an SVM classification model, does cross validation to estimate the best parameter (C value) and stores the model in a file (named svmSentimentAds). 54 | 55 | 56 | ### Classify an unknown image examples 57 | ``` 58 | python train.py -classifyFile knn knnSpeechMusicSpecs sampledata/music.melodies_snatch_0081.png 59 | python train.py -classifyFile knn knnSpeechMusicSpecs sampledata/s_30_r_335.png 60 | 61 | ``` 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /featureExtraction.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import sys, time, glob, os, ntpath, cv2, numpy, cPickle 3 | import featuresLBP 4 | import featuresLBP2 5 | import featuresColor 6 | import featuresHOG 7 | import featuresLines 8 | import featuresCorners 9 | import featuresFace 10 | import featuresDOF 11 | import scipy.cluster.hierarchy as hier 12 | import scipy.spatial.distance as dist 13 | from matplotlib import pyplot as plt 14 | import sklearn.svm 15 | import sklearn.decomposition 16 | import sklearn.ensemble 17 | 18 | 19 | def resizeFrame(frame, targetWidth): 20 | (Width, Height) = frame.shape[1], frame.shape[0] 21 | 22 | if targetWidth > 0: # Use FrameWidth = 0 for NO frame resizing 23 | ratio = float(Width) / targetWidth 24 | newHeight = int(round(float(Height) / ratio)) 25 | frameFinal = cv2.resize(frame, (targetWidth, newHeight)) 26 | else: 27 | frameFinal = frame; 28 | 29 | return frameFinal 30 | 31 | def blockshaped(arr): 32 | blocks = [] 33 | blocks.append(arr[0:arr.shape[0],0:arr.shape[1]/2]) 34 | blocks.append(arr[0:arr.shape[0],arr.shape[1]/2:-1]) 35 | #blocks.append(arr[arr.shape[0]/2:-1,0:arr.shape[1]/2]) 36 | #blocks.append(arr[arr.shape[0]/2:-1,arr.shape[1]/2:-1]) 37 | return blocks 38 | 39 | def featureExtraction(img, PLOT = False): 40 | start = time.clock() 41 | 42 | 43 | #[fLines, namesLines] = featuresLines.getLineFeatures(img); t1 = time.clock(); #print "{0:.2f} seconds used in line detection".format(t1-start) 44 | #[fFaces, namesFaces] = featuresFace.getFeaturesFace(img); t2 = time.clock(); #print "{0:.2f} seconds used in Face features".format(t3-t2) 45 | #[fLBP, namesLBP] = featuresLBP2.getLBP(img); t3 = time.clock(); #print "{0:.2f} seconds used in LBP".format(t4-t3) 46 | #[fColor, namesColor] = featuresColor.getRGBS(img, PLOT); t4 = time.clock(); #print "{0:.2f} seconds used in Color features".format(t5-t4) 47 | #[fHOG, namesHOG] = featuresHOG.getHOG(img); t5 = time.clock(); #print "{0:.2f} seconds used in HOG".format(t6-t5) 48 | #[fDOF, namesDOF] = featuresDOF.getDepthOfFieldFeature2(img); t6 = time.clock(); 49 | 50 | blocks = blockshaped(img) 51 | [fLines1, namesLines] = featuresLines.getLineFeatures(blocks[0]); 52 | [fLines2, namesLines] = featuresLines.getLineFeatures(blocks[1]); 53 | #[fLines3, namesLines] = featuresLines.getLineFeatures(blocks[2]); 54 | #[fLines4, namesLines] = featuresLines.getLineFeatures(blocks[3]); 55 | t1 = time.clock(); #print "{0:.2f} seconds used in line detection".format(t1-start) 56 | #[fFaces1, namesFaces] = featuresFace.getFeaturesFace(blocks[0]); 57 | #[fFaces2, namesFaces] = featuresFace.getFeaturesFace(blocks[1]); 58 | #[fFaces3, namesFaces] = featuresFace.getFeaturesFace(blocks[2]); 59 | #[fFaces4, namesFaces] = featuresFace.getFeaturesFace(blocks[3]); 60 | [fLBP1, namesLBP] = featuresLBP2.getLBP(blocks[0]); 61 | [fLBP2, namesLBP] = featuresLBP2.getLBP(blocks[1]); 62 | #[fLBP3, namesLBP] = featuresLBP2.getLBP(blocks[2]); 63 | #[fLBP4, namesLBP] = featuresLBP2.getLBP(blocks[3]); 64 | [fColor1, namesColor] = featuresColor.getRGBS(blocks[0], PLOT); 65 | [fColor2, namesColor] = featuresColor.getRGBS(blocks[1], PLOT); 66 | #[fColor3, namesColor] = featuresColor.getRGBS(blocks[2], PLOT); 67 | #[fColor4, namesColor] = featuresColor.getRGBS(blocks[3], PLOT); 68 | [fHOG1, namesHOG] = featuresHOG.getHOG(blocks[0]); 69 | [fHOG2, namesHOG] = featuresHOG.getHOG(blocks[1]); 70 | #[fHOG3, namesHOG] = featuresHOG.getHOG(blocks[2]); 71 | #[fHOG4, namesHOG] = featuresHOG.getHOG(blocks[3]); 72 | #[fDOF1, namesDOF] = featuresDOF.getDepthOfFieldFeature2(blocks[0]); 73 | #[fDOF2, namesDOF] = featuresDOF.getDepthOfFieldFeature2(blocks[1]); 74 | #[fDOF3, namesDOF] = featuresDOF.getDepthOfFieldFeature2(blocks[2]); 75 | #[fDOF4, namesDOF] = featuresDOF.getDepthOfFieldFeature2(blocks[3]); 76 | t6 = time.clock(); 77 | 78 | #print "Total time: {0:.2f}".format(t6-start) 79 | 80 | # WITH BLOCKING 81 | #fv = fLines1 + fLines2 +fLines3 +fLines4 + fFaces1 + fFaces2 + fFaces3 +fFaces4 + fLBP1 + fLBP2 + fLBP3 + fLBP4 + fColor1 + fColor2 + fColor3 + fColor4 + fHOG1 + fHOG2 + fHOG3 + fHOG4 + fDOF1 + fDOF2 + fDOF3 + fDOF4; 82 | #fNames = namesLines + namesLines + namesLines + namesLines + namesFaces + namesFaces + namesFaces + namesFaces + namesLBP + namesLBP + namesLBP + namesLBP + namesColor + namesColor + namesColor + namesColor + namesHOG + namesHOG + namesHOG + namesHOG + namesDOF+ namesDOF+ namesDOF+ namesDOF 83 | #fv = fLines1 + fLines2 +fLines3 +fLines4 + fLBP1 + fLBP2 + fLBP3 + fLBP4 + fColor1 + fColor2 + fColor3 + fColor4 + fHOG1 + fHOG2 + fHOG3 + fHOG4 84 | #fNames = namesLines + namesLines + namesLines + namesLines + namesLBP + namesLBP + namesLBP + namesLBP + namesColor + namesColor + namesColor + namesColor + namesHOG + namesHOG + namesHOG + namesHOG 85 | fv = fLines1 + fLines2 + fLBP1 + fLBP2 + fColor1 + fColor2 + fHOG1 + fHOG2 86 | fNames = namesLines + namesLines + namesLBP + namesLBP + namesColor + namesColor + namesHOG + namesHOG 87 | 88 | # WITHOUT BLOCKING 89 | #fv = fLines + fFaces + fLBP + fColor + fHOG + fDOF; 90 | #fNames = namesLines + namesFaces + namesLBP + namesColor + namesHOG + namesDOF 91 | 92 | #fv = fLines + fLBP + fColor + fHOG + fDOF; 93 | #fNames = namesLines + namesLBP + namesColor + namesHOG + namesDOF 94 | 95 | return fv, fNames 96 | 97 | def getFeaturesFromFile(fileName, PLOT = False): 98 | img = cv2.imread(fileName, cv2.CV_LOAD_IMAGE_COLOR) # read image 99 | #img2 = resizeFrame(img, 128)# resize 100 | 101 | #img2[:,:,0] = img2[:,:,0] + 3.5 * img2.std() * np.random.random([img2.shape[0], img2.shape[1]]) 102 | #img2[:,:,1] = img2[:,:,1] + 3.5 * img2.std() * np.random.random([img2.shape[0], img2.shape[1]]) 103 | #img2[:,:,2] = img2[:,:,2] + 3.5 * img2.std() * np.random.random([img2.shape[0], img2.shape[1]]) 104 | #plt.imshow(img2) 105 | #plt.show() 106 | #[F, N] = featureExtraction(img2, PLOT) # feature extraction 107 | [F, N] = featureExtraction(img, PLOT) # feature extraction 108 | return F, N 109 | 110 | def getFeaturesFromDir(dirName): 111 | types = ('*.jpg', '*.JPG', '*.png') 112 | imageFilesList = [] 113 | for files in types: 114 | imageFilesList.extend(glob.glob(os.path.join(dirName, files))) 115 | 116 | imageFilesList = sorted(imageFilesList) 117 | 118 | Features = []; 119 | for i, imFile in enumerate(imageFilesList): 120 | print "{0:.1f}".format(100.0 * float(i) / len(imageFilesList)) 121 | [F, Names] = getFeaturesFromFile(imFile) 122 | Features.append(F) 123 | 124 | Features = np.matrix(Features) 125 | 126 | return (Features, imageFilesList, Names) 127 | 128 | def getFeaturesFromDirs(dirNames): 129 | features = []; 130 | classNames = [] 131 | fileNames = [] 132 | for i,d in enumerate(dirNames): 133 | print " * * * * * * * * *" 134 | print d 135 | print " * * * * * * * * *" 136 | [f, fn, featureNames] = getFeaturesFromDir(d) 137 | if f.shape[0] > 0: # if at least one audio file has been found in the provided folder: 138 | features.append(f) 139 | fileNames.append(fn) 140 | if d[-1] == "/": 141 | classNames.append(d.split(os.sep)[-2]) 142 | else: 143 | classNames.append(d.split(os.sep)[-1]) 144 | 145 | return features, classNames, fileNames, featureNames 146 | #return (Features, imageFilesList, Names) 147 | 148 | def pcaDimRed(features, nDims): 149 | pca = sklearn.decomposition.PCA(n_components = nDims) 150 | pca.fit(features) 151 | coeff = pca.components_ 152 | featuresNew = [] 153 | for f in features: 154 | ft = f.copy() 155 | ft = numpy.squeeze(numpy.asarray(numpy.dot(f, coeff.T))) 156 | featuresNew.append(ft) 157 | print numpy.array(featuresNew).shape 158 | return (featuresNew, coeff) 159 | 160 | def visualizeFeatures(Features, Files, Names): 161 | y_eig, coeff = pcaDimRed(Features, 2) 162 | plt.close("all") 163 | print y_eig 164 | plt.subplot(2,1,1); 165 | ax = plt.gca() 166 | for i in range(len(Files)): 167 | im = cv2.imread(Files[i], cv2.CV_LOAD_IMAGE_COLOR) 168 | Width = 0.2; Height = 0.2; startX = y_eig[i][0]; startY = y_eig[i][1]; 169 | print startX, startY 170 | myaximage = ax.imshow(cv2.cvtColor(im, cv2.cv.CV_RGB2BGR), extent=(startX-Width/2.0, startX+Width/2.0, startY-Height/2.0, startY+Height/2.0), alpha=1.0, zorder=-1) 171 | plt.axis((-3,3,-3,3)) 172 | # Plot feaures 173 | plt.subplot(2,1,2) 174 | ax = plt.gca() 175 | for i in range(len(Files)): 176 | plt.plot(numpy.array(Features[i,:].T)); 177 | plt.xticks(range(len(Names))) 178 | plt.legend(Files) 179 | ax.set_xticklabels(Names) 180 | plt.setp(plt.xticks()[1], rotation=90) 181 | plt.tick_params(axis='both', which='major', labelsize=8) 182 | plt.tick_params(axis='both', which='minor', labelsize=8) 183 | 184 | plt.show() 185 | 186 | def main(argv): 187 | if argv[1] == "-featuresFile": 188 | if len(argv)==3: 189 | [F, Names] = getFeaturesFromFile(argv[2], True) 190 | plt.plot(F) 191 | ax = plt.gca() 192 | plt.xticks(range(len(Names))) 193 | ax.set_xticklabels(Names) 194 | plt.setp(plt.xticks()[1], rotation=90) 195 | plt.tick_params(axis='both', which='major', labelsize=8) 196 | plt.tick_params(axis='both', which='minor', labelsize=8) 197 | plt.show() 198 | 199 | if argv[1] == "-featuresFilesCompare": 200 | if len(argv)==4: 201 | F1,_ = getFeaturesFromFile(argv[2], False) 202 | F2,Names = getFeaturesFromFile(argv[3], False) 203 | plt.clf() 204 | plt.plot(F1,'g'); 205 | plt.plot(F2,'r'); 206 | plt.legend([ntpath.basename(argv[2]), ntpath.basename(argv[3])]) 207 | ax = plt.gca() 208 | plt.xticks(range(len(Names))) 209 | ax.set_xticklabels(Names) 210 | plt.setp(plt.xticks()[1], rotation=90) 211 | plt.tick_params(axis='both', which='major', labelsize=8) 212 | plt.tick_params(axis='both', which='minor', labelsize=8) 213 | plt.show() 214 | 215 | 216 | elif argv[1] == "-featuresDir": 217 | if len(argv)==3: 218 | (FM, Files, FeatureNames) = getFeaturesFromDir(argv[2]) 219 | visualizeFeatures(FM, Files, FeatureNames) 220 | 221 | elif argv[1] == "-featuresDirs": 222 | if len(argv)>3: 223 | outputfileName = argv[2] 224 | dirNames = argv[3:len(argv)] 225 | (features, classNames, fileNames, featureNames) = getFeaturesFromDirs(dirNames) 226 | fo = open(outputfileName + "_features", "wb") 227 | cPickle.dump(features, fo, protocol = cPickle.HIGHEST_PROTOCOL) 228 | cPickle.dump(classNames, fo, protocol = cPickle.HIGHEST_PROTOCOL) 229 | cPickle.dump(fileNames, fo, protocol = cPickle.HIGHEST_PROTOCOL) 230 | cPickle.dump(featureNames, fo, protocol = cPickle.HIGHEST_PROTOCOL) 231 | fo.close() 232 | 233 | 234 | 235 | if __name__ == '__main__': 236 | main(sys.argv) 237 | -------------------------------------------------------------------------------- /featuresColor.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import cv2 4 | import math 5 | import numpy as np 6 | from matplotlib import pyplot as plt 7 | 8 | def getRGBS(img, PLOT = False): 9 | 10 | image = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 11 | 12 | # grab the image channels, initialize the tuple of colors, 13 | # the figure and the flattened feature vector 14 | chans = cv2.split(image) 15 | colors = ("r", "g", "b") 16 | features = [] 17 | featuresSobel = [] 18 | # print [chans[0].max(), chans[1].max(), chans[2].max()] 19 | # print [chans[0].min(), chans[1].min(), chans[2].min()] 20 | # loop over the image channels 21 | for (chan, color) in zip(chans, colors): 22 | # create a histogram for the current channel and 23 | # concatenate the resulting histograms for each/hist. 24 | # channel 25 | # grad_x = np.abs(cv2.Sobel(chan, cv2.CV_16S, 1, 0, ksize = 3, scale = 1, delta = 0, borderType = cv2.BORDER_DEFAULT)) 26 | # grad_y = np.abs(cv2.Sobel(chan, cv2.CV_16S, 0, 1, ksize = 3, scale = 1, delta = 0, borderType = cv2.BORDER_DEFAULT)) 27 | # abs_grad_x = cv2.convertScaleAbs(grad_x) 28 | # abs_grad_y = cv2.convertScaleAbs(grad_y) 29 | # dst = cv2.addWeighted(abs_grad_x,0.5,abs_grad_y,0.5,0) 30 | # histSobel = cv2.calcHist([dst], [0], None, [8], [0, 256]) 31 | # histSobel = histSobel / histSobel.sum() 32 | hist = cv2.calcHist([chan], [0], None, [8], [0, 256]) 33 | hist = hist/hist.sum() 34 | if PLOT: 35 | plt.subplot(2,1,1) 36 | plt.plot(range(0,256,32),hist, color) 37 | # plt.plot(range(0,256,32),histSobel, color+"--") 38 | features.extend(hist[:,0].tolist()) 39 | # featuresSobel.extend(histSobel[:,0].tolist()) 40 | 41 | 42 | 43 | features.extend(featuresSobel) 44 | Grayscale = cv2.cvtColor(img, cv2.cv.CV_BGR2GRAY) 45 | histG = cv2.calcHist([chan], [0], None, [8], [0, 256]) 46 | histG = histG / histG.sum() 47 | features.extend(histG[:,0].tolist()) 48 | 49 | 50 | grad_x = np.abs(cv2.Sobel(Grayscale, cv2.CV_16S, 1, 0, ksize = 3, scale = 1, delta = 0, borderType = cv2.BORDER_DEFAULT)) 51 | grad_y = np.abs(cv2.Sobel(Grayscale, cv2.CV_16S, 0, 1, ksize = 3, scale = 1, delta = 0, borderType = cv2.BORDER_DEFAULT)) 52 | abs_grad_x = cv2.convertScaleAbs(grad_x) 53 | abs_grad_y = cv2.convertScaleAbs(grad_y) 54 | dst = cv2.addWeighted(abs_grad_x,0.5,abs_grad_y,0.5,0) 55 | histSobel = cv2.calcHist([dst], [0], None, [8], [0, 256]) 56 | histSobel = histSobel / histSobel.sum() 57 | features.extend(histSobel[:,0].tolist()) 58 | 59 | #### CALCULATE HSV HISTOGRAM ######################## 60 | hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) 61 | #hist = cv2.calcHist( [hsv], [0, 1], None, [180, 256], [0, 180, 0, 256] ) 62 | chans = cv2.split(hsv) # take the S channel 63 | S = chans[1] 64 | hist2 = cv2.calcHist([S], [0], None, [8], [0, 256]) 65 | hist2 = hist2/hist2.sum() 66 | features.extend(hist2[:,0].tolist()) 67 | 68 | if PLOT: 69 | plt.subplot(2,1,2); plt.plot(range(0,256,32),hist2) 70 | plt.show() 71 | 72 | Fnames = ["Color-R"+str(i) for i in range(8)] 73 | Fnames.extend(["Color-G"+str(i) for i in range(8)]) 74 | Fnames.extend(["Color-B"+str(i) for i in range(8)]) 75 | # Fnames.extend(["Color-SobelR"+str(i) for i in range(8)]) 76 | # Fnames.extend(["Color-SobelG"+str(i) for i in range(8)]) 77 | # Fnames.extend(["Color-SobelB"+str(i) for i in range(8)]) 78 | Fnames.extend(["Color-Gray"+str(i) for i in range(8)]) 79 | Fnames.extend(["Color-GraySobel"+str(i) for i in range(8)]) 80 | Fnames.extend(["Color-Satur"+str(i) for i in range(8)]) 81 | 82 | return features, Fnames 83 | -------------------------------------------------------------------------------- /featuresCorners.py: -------------------------------------------------------------------------------- 1 | import skimage.feature 2 | from skimage.morphology import octagon 3 | import numpy as np 4 | import cv2 # opencv 2 5 | from matplotlib import pyplot as plt 6 | from matplotlib import pyplot as plt 7 | 8 | def getCornerFeatures(img): 9 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 10 | corners = skimage.feature.corner_peaks(skimage.feature.corner_fast(gray, 14), min_distance=1) 11 | orientations = skimage.feature.corner_orientations(gray, corners, octagon(3, 2)) 12 | corners = np.rad2deg(orientations) 13 | 14 | corners = np.array(corners) 15 | AngleBins = np.arange(0,360,45); 16 | AngleBinsOrientation = np.array([0, 1, 2, 1, 0, 1, 2, 1]) 17 | OrientationHist = np.zeros((3,1)) 18 | for a in corners: 19 | OrientationHist[AngleBinsOrientation[np.argmin(np.abs(a-AngleBins))]] += 1 20 | 21 | if OrientationHist.sum()>0: 22 | OrientationHist = OrientationHist / OrientationHist.sum() 23 | else: 24 | OrientationHist = - 0.01*np.ones((3,1)) 25 | 26 | F = [] 27 | F.extend(OrientationHist[:,0].tolist()) 28 | F.append(100.0*float(len(corners)) / ( gray.shape[0] * gray.shape[1] ) ) 29 | Fnames = ["Corners-Hor", "Corners-Diag", "Corners-Ver", "Corners-Percent"] 30 | return (F, Fnames) 31 | 32 | def cornerDemo2(fileName): 33 | img = cv2.imread(fileName) 34 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 35 | 36 | gray = np.float32(gray) 37 | blockSize = int((gray.shape[0] / 100.0 + gray.shape[0] / 100.0) / 2) 38 | if blockSize<2: 39 | blockSize = 2 40 | 41 | dst = cv2.cornerHarris(gray,blockSize,3,0.04) 42 | 43 | corners = np.zeros(dst.shape) 44 | corners[dst>0.02*dst.max()] = 1; 45 | print float(corners.sum()) / (corners.shape[0]*corners.shape[1]) 46 | #result is dilated for marking the corners, not important 47 | #dst = cv2.dilate(dst,None) 48 | 49 | # Threshold for an optimal value, it may vary depending on the image. 50 | #img[dst>0.02*dst.max()]=[0,0,255] 51 | plt.imshow(corners) 52 | plt.show() 53 | 54 | def cornerDemo(fileName): 55 | img = cv2.imread(fileName, cv2.CV_LOAD_IMAGE_COLOR) 56 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 57 | #C = skimage.feature.corner_peaks(skimage.feature.corner_fast(gray, 14), min_distance=1) 58 | #C = skimage.feature.corner_peaks(skimage.feature.corner_harris(gray), min_distance=1) 59 | w, q = skimage.feature.corner_foerstner(gray) 60 | accuracy_thresh = 0.4 61 | roundness_thresh = 0.2 62 | foerstner = (q > roundness_thresh) * (w > accuracy_thresh) * w 63 | C = skimage.feature.corner_peaks(foerstner, min_distance=1) 64 | orientations = skimage.feature.corner_orientations(gray, C, octagon(3, 2)) 65 | 66 | corners = np.rad2deg(orientations) 67 | AngleBins = np.arange(0,360,45); 68 | AngleBinsOrientation = np.array([0, 1, 2, 1, 0, 1, 2, 1]) 69 | OrientationHist = np.zeros((3,1)) 70 | for a in corners: 71 | OrientationHist[AngleBinsOrientation[np.argmin(np.abs(a-AngleBins))]] += 1 72 | 73 | if OrientationHist.sum()>0: 74 | OrientationHist = OrientationHist / OrientationHist.sum() 75 | else: 76 | OrientationHist = - 0.01*np.ones((3,1)) 77 | 78 | 79 | # plt.subplot(2,1,1) 80 | plt.imshow(img) 81 | plt.plot(C[:,1], C[:,0], '*') 82 | for i in range(len(orientations)): 83 | plt.text(C[i,1], C[i,0], "{0:.2f}".format(corners[i]), fontsize=8); 84 | # plt.subplot(2,1,2) 85 | # plt.hist(corners, 20) 86 | plt.show() 87 | 88 | 89 | print OrientationHist 90 | #cornerDemo("demoData/lineDemo/noPer3.jpg") 91 | #cornerDemo2("demoData/lineDemo/noPer10.jpg") 92 | -------------------------------------------------------------------------------- /featuresDOF.py: -------------------------------------------------------------------------------- 1 | import numpy, cv2, pywt, glob, os 2 | from matplotlib import pyplot as plt 3 | from numpy.lib.stride_tricks import as_strided as ast 4 | 5 | def block_view(A, block= (32, 32)): 6 | # simple shape and strides computations may seem at first strange 7 | # unless one is able to recognize the 'tuple additions' involved ;-) 8 | shape= (A.shape[0]/ block[0], A.shape[1]/ block[1])+ block 9 | strides= (block[0]* A.strides[0], block[1]* A.strides[1])+ A.strides 10 | return ast(A, shape= shape, strides= strides) 11 | 12 | 13 | def getDepthOfFieldFeature(img): 14 | 15 | # ARGUMENT: cv2 bgr image 16 | 17 | Grayscale = cv2.cvtColor(img, cv2.cv.CV_BGR2GRAY) 18 | 19 | # get 2-level wavelets coefs (TODO: 3level???) 20 | coeffs = pywt.wavedec2(Grayscale, 'db1', level=2) 21 | cA2, (cH2, cV2, cD2), (cH1, cV1, cD1) = coeffs 22 | 23 | # compute aggregated coefs 24 | C = numpy.abs(cH2) + numpy.abs(cV2) + numpy.abs(cD2) 25 | print C.shape, img.shape, cH2.shape, cA2.shape, cH1.shape 26 | # block - process (5 x 5 area) 27 | W = block_view(C, ( int(C.shape[0] / 5), int(C.shape[1] / 5))) 28 | 29 | # compute 3 features: 30 | S = 0 31 | for i in range(len(W)): 32 | for j in range(len(W[i])): 33 | S += W[i,j].sum() 34 | 35 | S1 = W[2,2].sum() 36 | S2 = W[2,2].sum() + W[1,2].sum() + W[2,1].sum() + W[2,3].sum() + W[3,2].sum() 37 | S3 = 0 38 | for i in range(1,4): 39 | for j in range(1,4): 40 | S3 += W[i,j].sum() 41 | 42 | S4 = 0 43 | for i in range(0,3): 44 | for j in range(0,5): 45 | S4 += W[i,j].sum() 46 | S5 = 0 47 | for i in range(2,5): 48 | for j in range(0,5): 49 | S5 += W[i,j].sum() 50 | S6 = 0 51 | for i in range(0,5): 52 | for j in range(0,3): 53 | S6 += W[i,j].sum() 54 | S7 = 0 55 | for i in range(0,5): 56 | for j in range(2,5): 57 | S7 += W[i,j].sum() 58 | 59 | F1 = S1 / S 60 | F2 = S2 / S 61 | F3 = S3 / S 62 | F4 = S4 / S 63 | F5 = S5 / S 64 | F6 = S6 / S 65 | F7 = S7 / S 66 | 67 | Fnames = ["DOF-central1", "DOF-central2", "DOF-central3", "DOF-upper", "DOF-lower", "DOF-left", "DOF-right"] 68 | features = [F1, F2, F3, F4, F5, F6, F7] 69 | 70 | # print features 71 | # plt.subplot(2,1,1); plt.imshow(Grayscale) 72 | # plt.subplot(2,1,2); plt.imshow(C) 73 | # plt.show() 74 | 75 | return features, Fnames 76 | 77 | def getDepthOfFieldFeature2(img): 78 | # ARGUMENT: cv2 bgr image 79 | 80 | Grayscale = cv2.cvtColor(img, cv2.cv.CV_BGR2GRAY) 81 | 82 | C = cv2.Canny(Grayscale,100,200) 83 | # block - process (5 x 5 area) 84 | W = block_view(C, ( int(C.shape[0] / 5), int(C.shape[1] / 5))) 85 | 86 | # compute 3 features: 87 | S = 0 88 | for i in range(len(W)): 89 | for j in range(len(W[i])): 90 | S += W[i,j].sum() 91 | 92 | S1 = W[2,2].sum() 93 | S2 = W[2,2].sum() + W[1,2].sum() + W[2,1].sum() + W[2,3].sum() + W[3,2].sum() 94 | S3 = 0 95 | for i in range(1,4): 96 | for j in range(1,4): 97 | S3 += W[i,j].sum() 98 | 99 | S4 = 0 100 | for i in range(0,3): 101 | for j in range(0,5): 102 | S4 += W[i,j].sum() 103 | S5 = 0 104 | for i in range(2,5): 105 | for j in range(0,5): 106 | S5 += W[i,j].sum() 107 | S6 = 0 108 | for i in range(0,5): 109 | for j in range(0,3): 110 | S6 += W[i,j].sum() 111 | S7 = 0 112 | for i in range(0,5): 113 | for j in range(2,5): 114 | S7 += W[i,j].sum() 115 | 116 | F1 = S1 / (S + 0.00000001) 117 | F2 = S2 / (S + 0.00000001) 118 | F3 = S3 / (S + 0.00000001) 119 | F4 = S4 / (S + 0.00000001) 120 | F5 = S5 / (S + 0.00000001) 121 | F6 = S6 / (S + 0.00000001) 122 | F7 = S7 / (S + 0.00000001) 123 | 124 | Fnames = ["DOF-central1", "DOF-central2", "DOF-central3", "DOF-upper", "DOF-lower", "DOF-left", "DOF-right"] 125 | features = [F1, F2, F3, F4, F5, F6, F7] 126 | 127 | # print features 128 | # plt.subplot(2,1,1); plt.imshow(Grayscale) 129 | # plt.subplot(2,1,2); plt.imshow(C) 130 | # plt.show() 131 | 132 | return features, Fnames 133 | 134 | 135 | def scriptDemo(): 136 | dirName = "demoData/dofDemo" 137 | types = ('*.jpg', ) 138 | imageFilesList = [] 139 | for files in types: 140 | imageFilesList.extend(glob.glob(os.path.join(dirName, files))) 141 | 142 | imageFilesList = sorted(imageFilesList) 143 | print imageFilesList 144 | Features = numpy.zeros( (len(imageFilesList), 7) ) 145 | for i, f in enumerate(imageFilesList): 146 | img = cv2.imread(f, cv2.CV_LOAD_IMAGE_COLOR) # read image 147 | [F, names] = getDepthOfFieldFeature2(img) 148 | Features[i,:] = F 149 | # Features[i,j] contains the j-th feature of the i-th file 150 | for i in range(7): 151 | plt.subplot(7,1,i+1); plt.bar(range(Features[:,i].shape[0]), Features[:,i]) 152 | plt.title(names[i]) 153 | plt.show() 154 | print Features 155 | #scriptDemo() 156 | -------------------------------------------------------------------------------- /featuresFace.py: -------------------------------------------------------------------------------- 1 | import cv2, time, sys, glob, os 2 | import matplotlib 3 | import matplotlib.pyplot as plt 4 | import matplotlib.cm as cm 5 | import numpy 6 | 7 | 8 | # face detection-related paths: 9 | HAAR_CASCADE_PATH_FRONTAL = "haarcascade_frontalface_default.xml" 10 | HAAR_CASCADE_PATH_PROFILE = "haarcascade_frontalface_default.xml" 11 | 12 | def intersect_rectangles(r1, r2): 13 | x11 = r1[0]; y11 = r1[1]; x12 = r1[0]+r1[2]; y12 = r1[1]+r1[3]; 14 | x21 = r2[0]; y21 = r2[1]; x22 = r2[0]+r2[2]; y22 = r2[1]+r2[3]; 15 | 16 | X1 = max(x11, x21); X2 = min(x12, x22); 17 | Y1 = max(y11, y21); Y2 = min(y12, y22); 18 | 19 | W = X2 - X1 20 | H = Y2 - Y1 21 | if (H>0) and (W>0): 22 | E = W * H; 23 | else: 24 | E = 0.0; 25 | Eratio = 2.0*E / (r1[2]*r1[3] + r2[2]*r2[3]) 26 | return Eratio 27 | 28 | 29 | def initialize_face(): 30 | cascadeFrontal = cv2.cv.Load(HAAR_CASCADE_PATH_FRONTAL); 31 | cascadeProfile = cv2.cv.Load(HAAR_CASCADE_PATH_PROFILE); 32 | storage = cv2.cv.CreateMemStorage() 33 | return (cascadeFrontal, cascadeProfile, storage) 34 | 35 | def detect_faces(image, cascadeFrontal, cascadeProfile, storage): 36 | facesFrontal = []; facesProfile = [] 37 | detectedFrontal = cv2.cv.HaarDetectObjects(image, cascadeFrontal, storage, 1.3, 2, cv2.cv.CV_HAAR_DO_CANNY_PRUNING, (image.width/20,image.width/20)) 38 | #detectedProfile = cv2.cv.HaarDetectObjects(image, cascadeProfile, storage, 1.3, 2, cv2.cv.CV_HAAR_DO_CANNY_PRUNING, (newWidth/10,newWidth/10)) 39 | if detectedFrontal: 40 | for (x,y,w,h),n in detectedFrontal: 41 | facesFrontal.append((x,y,w,h)) 42 | #if detectedProfile: 43 | # for (x,y,w,h),n in detectedProfile: 44 | # facesProfile.append((x,y,w,h)) 45 | 46 | # remove overlaps: 47 | while (1): 48 | Found = False 49 | for i in range(len(facesFrontal)): 50 | for j in range(len(facesFrontal)): 51 | if i != j: 52 | interRatio = intersect_rectangles(facesFrontal[i], facesFrontal[j]) 53 | if interRatio>0.3: 54 | Found = True; 55 | del facesFrontal[i] 56 | break; 57 | if Found: 58 | break; 59 | 60 | if not Found: # not a single overlap has been detected -> exit loop 61 | break; 62 | 63 | 64 | #return (facesFrontal, facesProfile) 65 | return (facesFrontal) 66 | 67 | 68 | def getFeaturesFace(img): 69 | RGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 70 | (cascadeFrontal, cascadeProfile, storage) = initialize_face() 71 | facesFrontal = detect_faces(cv2.cv.fromarray(RGB), cascadeFrontal, cascadeProfile, storage) 72 | 73 | tempF = 0.0 74 | faceSizes = [] 75 | for f in facesFrontal: 76 | faceSizes.append(f[2] * f[3] / float(img.shape[0] * img.shape[1])) 77 | 78 | F = [] 79 | F.append(len(facesFrontal)) 80 | if len(facesFrontal)>0: 81 | F.append(min(faceSizes)) 82 | F.append(max(faceSizes)) 83 | F.append(numpy.mean(faceSizes)) 84 | else: 85 | F.extend([0, 0, 0]); 86 | 87 | Fnames = ["Faces-Total", "Faces-minSizePer", "Faces-maxSizePer", "Faces-meanSizePer"] 88 | return (F, Fnames) 89 | #print F 90 | #print tempF/len(facesFrontal) 91 | 92 | -------------------------------------------------------------------------------- /featuresHOG.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from itertools import product 4 | from math import floor, pi 5 | import numpy as np 6 | import cv2 # opencv 2 7 | from SimpleCV import * 8 | import skimage 9 | 10 | # TODO: should we do this using scikit-image (http://scikit-image.org/docs/dev/api/skimage.feature.html?highlight=peak_local_max#skimage.feature.local_binary_pattern) 11 | 12 | def normalize(v): 13 | norm=np.linalg.norm(v) 14 | if norm==0: 15 | return v 16 | return v/norm 17 | 18 | def findHOGFeatures(img, n_divs=5, n_bins=4): 19 | """ 20 | **SUMMARY** 21 | Get HOG(Histogram of Oriented Gradients) features from the image. 22 | 23 | 24 | **PARAMETERS** 25 | * *img* - SimpleCV.Image instance 26 | * *n_divs* - the number of divisions(cells). 27 | * *n_bins* - the number of orientation bins. 28 | 29 | **RETURNS** 30 | Returns the HOG vector in a numpy array 31 | 32 | """ 33 | # Size of HOG vector 34 | n_HOG = n_divs * n_divs * n_bins 35 | 36 | # Initialize output HOG vector 37 | # HOG = [0.0]*n_HOG 38 | HOG = np.zeros((n_HOG, 1)) 39 | # Apply sobel on image to find x and y orientations of the image 40 | Icv = img.getNumpyCv2() 41 | Ix = cv2.Sobel(Icv, ddepth=cv.CV_32F, dx=1, dy=0, ksize=3) 42 | Iy = cv2.Sobel(Icv, ddepth=cv.CV_32F, dx=0, dy=1, ksize=3) 43 | 44 | Ix = Ix.transpose(1, 0, 2) 45 | Iy = Iy.transpose(1, 0, 2) 46 | cellx = img.width / n_divs # width of each cell(division) 47 | celly = img.height / n_divs # height of each cell(division) 48 | 49 | #Area of image 50 | img_area = img.height * img.width 51 | 52 | #Range of each bin 53 | BIN_RANGE = (2 * pi) / n_bins 54 | 55 | # m = 0 56 | angles = np.arctan2(Iy, Ix) 57 | magnit = ((Ix ** 2) + (Iy ** 2)) ** 0.5 58 | it = product(xrange(n_divs), xrange(n_divs), xrange(cellx), xrange(celly)) 59 | 60 | for m, n, i, j in it: 61 | # grad value 62 | grad = magnit[m * cellx + i, n * celly + j][0] 63 | # normalized grad value 64 | norm_grad = grad / img_area 65 | # Orientation Angle 66 | angle = angles[m*cellx + i, n*celly+j][0] 67 | # (-pi,pi) to (0, 2*pi) 68 | if angle < 0: 69 | angle += 2 * pi 70 | nth_bin = floor(float(angle/BIN_RANGE)) 71 | HOG[((m * n_divs + n) * n_bins + int(nth_bin))] += norm_grad 72 | 73 | return HOG[:,0].tolist() 74 | 75 | def getHOG(img): 76 | image = Image(img, cv2image=True) # convert it to SimpleCV image 77 | HOG = findHOGFeatures(image, 2, 4) 78 | Fnames = ["HOG"+str(i).zfill(2) for i in range(len(HOG))] 79 | HOG = normalize(HOG) 80 | if type(HOG) is list: 81 | return HOG, Fnames 82 | else: 83 | return HOG.tolist(), Fnames 84 | 85 | 86 | -------------------------------------------------------------------------------- /featuresLBP.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import numpy as np 4 | import cv2 5 | from matplotlib import pyplot as plt 6 | import math 7 | 8 | def normalize(v): 9 | norm=np.linalg.norm(v) 10 | if norm==0: 11 | return v 12 | return v/norm 13 | 14 | def bilinear_interpolation(x, y, img): 15 | x1, y1 = int(x), int(y) 16 | x2, y2 = math.ceil(x), math.ceil(y) 17 | 18 | r1 = (x2 - x) / (x2 - x1) * get_pixel_else_0(img, x1, y1) + (x - x1) / (x2 - x1) * get_pixel_else_0(img, x2, y1) 19 | r2 = (x2 - x) / (x2 - x1) * get_pixel_else_0(img, x1, y2) + (x - x1) / (x2 - x1) * get_pixel_else_0(img, x2, y2) 20 | 21 | return (y2 - y) / (y2 - y1) * r1 + (y - y1) / (y2 - y1) * r2 22 | 23 | def thresholded(center, pixels): 24 | out = [] 25 | for a in pixels: 26 | if a >= center: 27 | out.append(1) 28 | else: 29 | out.append(0) 30 | return out 31 | 32 | def get_pixel_else_0(image, idx, idy): 33 | if idx < int(len(image)) - 1 and idy < len(image[0]): 34 | return image[idx,idy] 35 | else: 36 | return 0 37 | 38 | def find_variations(pixel_values): 39 | prev = pixel_values[-1] 40 | t = 0 41 | for p in range(0, len(pixel_values)): 42 | cur = pixel_values[p] 43 | if cur != prev: 44 | t += 1 45 | prev = cur 46 | return t 47 | 48 | 49 | def getLBP(img): 50 | #img = cv2.imread('../images/mug.jpeg', 0) 51 | img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 52 | transformed_img = img.copy() 53 | 54 | 55 | P = 8 # number of pixels 56 | R = 1 # radius 57 | 58 | for x in range(0, len(img)): 59 | for y in range(0, len(img[0])): 60 | center = img[x,y] 61 | pixels = [] 62 | for point in range(0, P): 63 | r = x + R * math.cos(2 * math.pi * point / P) 64 | c = y - R * math.sin(2 * math.pi * point / P) 65 | if r < 0 or c < 0: 66 | pixels.append(0) 67 | continue 68 | if int(r) == r: 69 | if int(c) != c: 70 | c1 = int(c) 71 | c2 = math.ceil(c) 72 | w1 = (c2 - c) / (c2 - c1) 73 | w2 = (c - c1) / (c2 - c1) 74 | 75 | pixels.append(int((w1 * get_pixel_else_0(img, int(r), int(c)) + \ 76 | w2 * get_pixel_else_0(img, int(r), math.ceil(c))) / (w1 + w2))) 77 | else: 78 | pixels.append(get_pixel_else_0(img, int(r), int(c))) 79 | elif int(c) == c: 80 | r1 = int(r) 81 | r2 = math.ceil(r) 82 | w1 = (r2 - r) / (r2 - r1) 83 | w2 = (r - r1) / (r2 - r1) 84 | pixels.append((w1 * get_pixel_else_0(img, int(r), int(c)) + \ 85 | w2 * get_pixel_else_0(img, math.ceil(r), int(c))) / (w1 + w2)) 86 | else: 87 | pixels.append(bilinear_interpolation(r, c, img)) 88 | values = thresholded(center, pixels) 89 | res = 0 90 | for a in range(0, len(values)): 91 | res += values[a] * (2 ** a) 92 | transformed_img.itemset((x,y), res) 93 | 94 | hist,bins = np.histogram(img.flatten(),16,[0,256]) 95 | 96 | cdf = hist.cumsum() 97 | cdf_normalized = cdf * hist.max()/ cdf.max() 98 | print transformed_img 99 | LBP = plt.hist(transformed_img.flatten(),16,[0,256], color = 'r') 100 | 101 | return normalize(LBP[0]) 102 | 103 | -------------------------------------------------------------------------------- /featuresLBP2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import numpy as np 4 | import cv2 5 | from matplotlib import pyplot as plt 6 | import math 7 | from skimage.feature import local_binary_pattern 8 | from numpy.lib.stride_tricks import as_strided as ast 9 | import matplotlib.cm as cm 10 | 11 | def block_view(A, block= (32, 32)): 12 | # simple shape and strides computations may seem at first strange 13 | # unless one is able to recognize the 'tuple additions' involved ;-) 14 | shape= (A.shape[0]/ block[0], A.shape[1]/ block[1])+ block 15 | strides= (block[0]* A.strides[0], block[1]* A.strides[1])+ A.strides 16 | return ast(A, shape= shape, strides= strides) 17 | 18 | def normalize(v): 19 | norm=np.linalg.norm(v) 20 | if norm==0: 21 | return v 22 | return v/norm 23 | 24 | 25 | def getLBP(img): 26 | img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 27 | radius = 1 28 | n_points = 8 * radius 29 | lbpImage = (local_binary_pattern(img2, n_points, radius)).astype(int)**(1.0/radius) 30 | 31 | # block processing: 32 | lbpImages = block_view(lbpImage, ( int(lbpImage.shape[0] / 2), int(lbpImage.shape[1] / 4))) 33 | 34 | 35 | count = 0 36 | 37 | LBP = np.array([]); 38 | for i in range(lbpImages.shape[0]): # for each block: 39 | for j in range(lbpImages.shape[1]): 40 | count += 1 41 | # plt.subplot(4,2,count) 42 | # plt.imshow(lbpImages[i,j,:,:],cmap = cm.Greys_r) 43 | # plt.subplot(4,2,count+4*2/2) 44 | # print count*2+1 45 | LBPt = cv2.calcHist([lbpImages[i,j,:,:].astype('uint8')], [0], None, [8], [0, 256]) 46 | LBP = np.append(LBP, LBPt[:,0]); 47 | # plt.plot(LBPt) 48 | # plt.show() 49 | 50 | 51 | Fnames = ["LBP"+str(i).zfill(2) for i in range(len(LBP))] 52 | 53 | return normalize(LBP).tolist(), Fnames 54 | 55 | 56 | -------------------------------------------------------------------------------- /featuresLines.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import math, glob, os, ntpath 3 | import numpy as np 4 | from matplotlib import pyplot as plt 5 | import matplotlib.cm as cm 6 | import matplotlib.patches as mpatches 7 | from math import atan2, degrees, pi, sqrt 8 | from mpl_toolkits.mplot3d.axes3d import Axes3D 9 | #from shapely.geometry import LineString 10 | import scipy.cluster.hierarchy as hier 11 | import scipy.spatial.distance as dist 12 | 13 | def temp(): 14 | a = [] 15 | for i in range(100): 16 | x1 = np.random.randn(1) 17 | x2 = np.random.randn(1) 18 | x3 = np.random.randn(1) 19 | x4 = np.random.randn(1) 20 | (A,_) = (angle(x1,x2,x3,x4)) 21 | a.append(int(A)) 22 | plt.plot(a) 23 | plt.show() 24 | 25 | 26 | def perp( a ) : 27 | b = np.empty_like(a) 28 | b[0] = -a[1] 29 | b[1] = a[0] 30 | return b 31 | 32 | # line segment a given by endpoints a1, a2 33 | # line segment b given by endpoints b1, b2 34 | # return 35 | def seg_intersect(a1,a2, b1,b2) : 36 | da = a2-a1 37 | db = b2-b1 38 | dp = a1-b1 39 | dap = perp(da) 40 | denom = np.dot( dap, db) 41 | num = np.dot( dap, dp ) 42 | if denom==0: 43 | return np.NaN + b1 44 | else: 45 | return (num / denom)*db + b1 46 | 47 | def angle(x1,x2,y1,y2): 48 | dx = x2 - x1 49 | dy = y2 - y1 50 | rads = atan2(-dy,dx) 51 | rads %= 2*pi 52 | degs = degrees(rads) 53 | dist = sqrt( (x2 - x1)**2 + (y2 - y1)**2 ) 54 | return degs, dist 55 | 56 | def getLineFeatures(img): # bgr image as argument: 57 | 58 | MIN_LENGTH = (int(np.floor(img.shape[0]/30)) + int(np.floor(img.shape[1]/30)))/2 59 | 60 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 61 | lines = [] 62 | for blurRatio in [40, 50, 60]: 63 | if blurRatio==0: 64 | blur = gray 65 | else: 66 | blurWindow = (int(np.floor(img.shape[0]/blurRatio)) + int(np.floor(img.shape[1]/blurRatio)))/2 67 | if blurWindow % 2 == 0: 68 | blurWindow += 1 69 | blur = cv2.bilateralFilter(gray, blurWindow, 100, 100) 70 | 71 | edges1 = cv2.Canny(blur,100,200) 72 | edges2 = cv2.Canny(blur,50,100) 73 | edges3 = cv2.Canny(blur,50,150) 74 | edges4 = cv2.Canny(blur,100,250) 75 | edges = (edges1+edges2+edges3+edges4)/4 76 | 77 | minLineLength = int( (img.shape[0] / 20 + img.shape[1]/20) / 2 ) 78 | maxLineGap = int( (img.shape[0] / 30 + img.shape[1]/30) / 2 ) 79 | 80 | for param in [50, 100]: 81 | l = cv2.HoughLinesP(edges,1,np.pi/180,param, minLineLength, maxLineGap) 82 | if l==None: 83 | l = [] 84 | else: 85 | l = l[0] 86 | if len(lines)==0: 87 | lines = l; 88 | else: 89 | for nl in l: 90 | D = dist.cdist(lines, np.matrix(nl)) 91 | if D.min() > int( (img.shape[0] / 50 + img.shape[1]/50) / 2 ) : 92 | lines = np.append(lines, [nl], axis = 0 ) 93 | 94 | Angles = []; AnglesTemp = [] 95 | Distances = []; DistancesTemp = [] 96 | finalLines = [] 97 | #plt.subplot(2,1,1); 98 | #plt.imshow(blur,cmap = cm.Greys_r); plt.axis('off'); plt.autoscale() 99 | #plt.subplot(2,1,2); plt.imshow(edges,cmap = cm.Greys_r); plt.axis('off'); plt.autoscale() 100 | 101 | 102 | for (x1,y1,x2,y2) in lines: 103 | a, d = angle(x1,x2,y1,y2) 104 | AnglesTemp.append(a) 105 | DistancesTemp.append(d) 106 | AnglesTemp = np.array(AnglesTemp) 107 | DistancesTemp = np.array(DistancesTemp) 108 | 109 | Angles = AnglesTemp[np.nonzero(DistancesTemp>MIN_LENGTH)] 110 | Distances = DistancesTemp[np.nonzero(DistancesTemp>MIN_LENGTH)] 111 | if len(lines)>0: 112 | finalLines = lines[np.nonzero(DistancesTemp>MIN_LENGTH)] 113 | 114 | if len(finalLines)>100: 115 | ThresholdDist = (np.sort(Distances)[::-1])[100] 116 | Angles =Angles[np.nonzero(Distances>ThresholdDist)] 117 | finalLines = finalLines[np.nonzero(Distances>ThresholdDist)] 118 | Distances = Distances[np.nonzero(Distances>ThresholdDist)] 119 | 120 | #for (x1,y1,x2,y2) in finalLines: 121 | # plt.plot([x1, x2], [y1, y2], color='r') 122 | 123 | # Histogram of orientations: 0 for horizontal, 1 for diagonal and 2 for vertical line orientations (3 features) 124 | Angles = np.array(Angles) 125 | 126 | # acute Threshold: 127 | aThreshold = 40; 128 | if Angles.shape[0]==0: 129 | perAcute = -0.01; 130 | else: 131 | perAcute = (np.nonzero((Angles>0)*(Angles360-aThreshold))[0]).shape[0]/float(Angles.shape[0]) 132 | 133 | AngleBins = np.arange(0,360,45); 134 | AngleBinsOrientation = np.array([0, 1, 2, 1, 0, 1, 2, 1]) 135 | OrientationHist = np.zeros((3,1)) 136 | for a, d in zip(Angles, Distances): 137 | OrientationHist[AngleBinsOrientation[np.argmin(np.abs(a-AngleBins))]] += d 138 | if OrientationHist.sum()>0: 139 | OrientationHist = OrientationHist / OrientationHist.sum() 140 | else: 141 | OrientationHist = - 0.01*np.ones((3,1)) 142 | 143 | # Find intersections: 144 | Xs = []; Ys = [] 145 | 146 | #plt.subplot(2,1,1) 147 | for i,(xa1,ya1,xa2, ya2) in enumerate(finalLines): 148 | for j,(xb1,yb1,xb2,yb2) in enumerate(finalLines): 149 | if (i > j): 150 | [X,Y] = seg_intersect( np.array([float(xa1),float(ya1)]), np.array([float(xa2),float(ya2)]), np.array([float(xb1),float(yb1)]), np.array([float(xb2),float(yb2)])) 151 | Xs.append(X); 152 | Ys.append(Y); 153 | 154 | Colors = ["r","b","g","k","y"] 155 | countParallel = 0 156 | Xoriginal = Xs 157 | Yoriginal = Ys 158 | Xfinal = []; Yfinal = [] 159 | for i in range(len(Xs)): 160 | if (Xs[i] > -0.2*img.shape[1]) and (Xs[i] < 1.2*img.shape[1]) and (Ys[i] > -0.2*img.shape[0]) and (Ys[i] < 1.2*img.shape[0]): 161 | #plt.plot(Xs[i],Ys[i],'r*') 162 | Xfinal.append(Xs[i]) 163 | Yfinal.append(Ys[i]) 164 | else: 165 | countParallel += 1 166 | 167 | # percentage of parallel lines (1 feature) 168 | if len(Xs): 169 | countParallel /= float(len(Xs)) 170 | else: 171 | countParallel = -0.01 172 | 173 | Xs = Xfinal 174 | Ys = Yfinal 175 | countImportantClusters = 0 176 | if len(Xs)>1: 177 | intersectPointMatrx = np.matrix([Xs, Ys]).T 178 | Dists = dist.pdist(intersectPointMatrx) / (np.sqrt(img.shape[0]**2+img.shape[1]**2)) 179 | #cls, means, steps = mlpy.kmeans(intersectPointMatrx, k=3, plus=True) 180 | #STDs = [] 181 | #Weights = [] 182 | #for c in range(3): 183 | # temp = intersectPointMatrx[cls==c, :] 184 | # STDs.append(np.std(dist.pdist(temp)/ (np.sqrt(img.shape[0]**2+img.shape[1]**2)))) 185 | # Weights.append(np.nonzero(np.array(cls)==c)[0].shape[0]) 186 | #Weights = np.array(Weights) 187 | #STDs = np.array(STDs) 188 | #countImportantClusters = STDs[np.argmax(Weights)] 189 | per5 = float(np.nonzero(Dists<0.05)[0].shape[0]) / Dists.shape[0] 190 | per10 = float(np.nonzero(Dists<0.20)[0].shape[0]) / Dists.shape[0] 191 | #for i,(X,Y) in enumerate(zip(Xs, Ys)): 192 | #if (X>-0.2*img.shape[1]) and (X<1.2*img.shape[1]) and (Y<1.2*img.shape[0]) and (Y>-0.2*img.shape[0]): 193 | # plt.plot(X, Y,'*') 194 | #plt.subplot(3,1,3); plt.hist(Dists) 195 | 196 | else: 197 | if countParallel == -0.01: 198 | per5 = -0.01; per10 = -0.01 199 | else: 200 | per5 = 0.0; per10 = 0.0 201 | 202 | #print float(np.nonzero(Dists<0.05)[0].shape[0]) / Dists.shape[0] 203 | #linkageMatrix = hier.linkage(Dists) 204 | #cls = hier.fcluster(linkageMatrix, 5, 'maxclust')-1 205 | 206 | #plt.show() 207 | 208 | F = []; 209 | F.extend([countParallel]) # feature 1 percentage of parallel lines 210 | F.extend(OrientationHist[:,0].tolist()) # features 2-4 line orientation normalized histogram (horizontal, diagonal and vertical respectivelly) 211 | F.extend([per5, per10]) # features 5-6 percentage of very close and close intersection points 212 | F.extend([perAcute]) 213 | 214 | Fnames = ["Lines-Parallel", "Lines-Hor", "Lines-Diag", "Lines-Ver", "Lines-InterPointsClose1", "Lines-InterPointsClose2","Lines-PerAcute"] 215 | 216 | return (F, Fnames) 217 | 218 | def scriptDemo(): 219 | dirName = "demoData/lineDemo" 220 | types = ('*.jpg', ) 221 | imageFilesList = [] 222 | for files in types: 223 | imageFilesList.extend(glob.glob(os.path.join(dirName, files))) 224 | 225 | imageFilesList = sorted(imageFilesList) 226 | 227 | Features = np.zeros( (len(imageFilesList), 7) ) 228 | labels = [] 229 | for i, f in enumerate(imageFilesList): 230 | print f 231 | if ntpath.basename(f)[0:5]=="noPer": 232 | labels.append(0) 233 | else: 234 | labels.append(1) 235 | img = cv2.imread(f, cv2.CV_LOAD_IMAGE_COLOR) # read image 236 | [F, names] = getLineFeatures(img) 237 | Features[i,:] = F 238 | 239 | FeaturesPrespective = Features[:,4:7]; 240 | 241 | fig = plt.figure() 242 | color = ["ro","gx"] 243 | labels = np.array(labels) 244 | ax = fig.add_subplot(111, projection='3d') 245 | 246 | ax.plot(FeaturesPrespective[np.nonzero(labels==0)[0],0], FeaturesPrespective[np.nonzero(labels==0)[0],1], FeaturesPrespective[np.nonzero(labels==0)[0],2], color[0], label='Non Perspective') 247 | ax.plot(FeaturesPrespective[np.nonzero(labels==1)[0],0], FeaturesPrespective[np.nonzero(labels==1)[0],1], FeaturesPrespective[np.nonzero(labels==1)[0],2], color[1], label='Perspective') 248 | 249 | ax.set_xlabel('Close Intersections - 5%') 250 | ax.set_ylabel('Close Intersections - 20%') 251 | ax.set_zlabel('Acute Angles') 252 | 253 | plt.legend(loc='upper left', numpoints=1) 254 | 255 | plt.show() 256 | 257 | for f in range(Features.shape[0]): 258 | print "{0:.4f}\t{1:.4f}\t{2:.4f}".format(Features[f, 4], Features[f, 5], Features[f, 6]) 259 | 260 | 261 | # For generating figures for paper 262 | #scriptDemo() 263 | 264 | # test intersection: 265 | #A1 = np.array([0.0,0.0]) 266 | #A2 = np.array([4.0,2.0]) 267 | #B1 = np.array([1.0,1.2]) 268 | #B2 = np.array([2.0,1.0]) 269 | #plt.plot([A1[0], A2[0]], [A1[1],A2[1]]) 270 | #plt.plot([B1[0], B2[0]], [B1[1],B2[1]]) 271 | #[X, Y] = seg_intersect(A1, A2, B1, B2) 272 | #print X, Y 273 | #plt.plot(X, Y, '*'); 274 | #plt.show() 275 | 276 | 277 | -------------------------------------------------------------------------------- /knnSpeechMusicSpecs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/knnSpeechMusicSpecs -------------------------------------------------------------------------------- /sampledata/music.melodies_snatch_0081.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/music.melodies_snatch_0081.png -------------------------------------------------------------------------------- /sampledata/s_30_r_335.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/s_30_r_335.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/hannah_music07.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/hannah_music07.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/lord_02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/lord_02.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_10_r_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_10_r_32.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_10_r_60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_10_r_60.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_30_r_113.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_30_r_113.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_30_r_125.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_30_r_125.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_30_r_192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_30_r_192.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_30_r_219.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_30_r_219.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_30_r_231.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_30_r_231.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_30_r_255.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_30_r_255.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_30_r_280.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_30_r_280.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_30_r_285.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_30_r_285.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_30_r_31.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_30_r_31.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_30_r_383.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_30_r_383.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_30_r_391.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_30_r_391.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_30_r_414.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_30_r_414.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_30_r_442.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_30_r_442.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_30_r_444.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_30_r_444.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_5_r_03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_5_r_03.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_5_r_123.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_5_r_123.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_5_r_139.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_5_r_139.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_5_r_180.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_5_r_180.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_5_r_36.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_5_r_36.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/m_5_r_89.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/m_5_r_89.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_blackdawn_0243.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_blackdawn_0243.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_blackdawn_0254.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_blackdawn_0254.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_boogeyman_0196.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_boogeyman_0196.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_boogeyman_0199.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_boogeyman_0199.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_houseofwax_0222.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_houseofwax_0222.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_madhouse_0166.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_madhouse_0166.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_madhouse_0167.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_madhouse_0167.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_madhouse_0182.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_madhouse_0182.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_residentevil2_0152.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_residentevil2_0152.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_sincity_0280.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_sincity_0280.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_snatch_0047.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_snatch_0047.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_snatch_0055.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_snatch_0055.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_snatch_0090.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_snatch_0090.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_snatch_0091.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_snatch_0091.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_stealth_0107.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_stealth_0107.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_stealth_0135.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_stealth_0135.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_stealth_0143.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_stealth_0143.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_stealth_0145.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_stealth_0145.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_thecutter_0262.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_thecutter_0262.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_thecutter_0266.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_thecutter_0266.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_underworld_0289.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_underworld_0289.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.melodies_underworld_0295.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.melodies_underworld_0295.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.songs_hide&seek_0002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.songs_hide&seek_0002.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.songs_stealth_0030.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.songs_stealth_0030.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.songs_stealth_0043.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.songs_stealth_0043.png -------------------------------------------------------------------------------- /sampledata/spectrograms/music/music.songs_underworld_0076.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/music/music.songs_underworld_0076.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/1984_speech10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/1984_speech10.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/constantine_speech21.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/constantine_speech21.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/full_s_03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/full_s_03.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/kill_bill_2_speech_16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/kill_bill_2_speech_16.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/kill_bill_2_speech_17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/kill_bill_2_speech_17.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/repulsion_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/repulsion_01.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_10_r_130.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_10_r_130.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_10_r_148.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_10_r_148.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_10_r_50.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_10_r_50.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_10_r_84.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_10_r_84.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_10_r_86.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_10_r_86.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_105.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_105.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_108.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_108.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_133.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_133.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_14.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_186.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_186.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_212.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_212.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_229.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_229.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_259.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_259.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_277.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_277.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_291.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_291.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_305.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_305.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_322.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_322.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_326.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_326.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_333.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_333.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_339.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_339.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_340.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_340.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_48.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_48.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_72.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_72.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_30_r_73.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_30_r_73.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_5_r_109.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_5_r_109.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_5_r_115.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_5_r_115.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_5_r_18.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_5_r_18.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_5_r_24.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_5_r_24.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_5_r_28.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_5_r_28.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_5_r_62.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_5_r_62.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_5_r_71.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_5_r_71.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/s_5_r_93.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/s_5_r_93.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/saw_2_speech10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/saw_2_speech10.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/sm_30_18.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/sm_30_18.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/sm_30_20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/sm_30_20.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/sm_30_30.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/sm_30_30.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/sm_30_36.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/sm_30_36.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/sm_30_63.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/sm_30_63.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/sm_5_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/sm_5_01.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/speech.neutral_hide&seek_0009.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/speech.neutral_hide&seek_0009.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/speech.neutral_snatch_0129.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/speech.neutral_snatch_0129.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/speech.neutral_stealth_0162.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/speech.neutral_stealth_0162.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/speech.neutral_stealth_0245.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/speech.neutral_stealth_0245.png -------------------------------------------------------------------------------- /sampledata/spectrograms/speech/speech.neutral_stealth_0308.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/sampledata/spectrograms/speech/speech.neutral_stealth_0308.png -------------------------------------------------------------------------------- /svmSpeechMusicSpecsNORMALIZATION: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tyiannak/pyImageClassification/HEAD/svmSpeechMusicSpecsNORMALIZATION -------------------------------------------------------------------------------- /train.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import sys, time, glob, os, ntpath, cv2, numpy, cPickle 3 | import featureExtraction 4 | from matplotlib import pyplot as plt 5 | from scipy.spatial import distance 6 | from sklearn import mixture 7 | from sklearn.linear_model import LogisticRegression 8 | from sklearn.neural_network import BernoulliRBM 9 | from sklearn.pipeline import Pipeline 10 | #from nolearn.dbn import DBN 11 | from scipy import linalg as la 12 | #from sklearn.lda import LDA 13 | import random 14 | RBM_NCOMPONENTS = 120; 15 | import sklearn.svm 16 | 17 | 18 | class kNN: 19 | def __init__(self, X, Y, k): 20 | self.X = X 21 | self.Y = Y 22 | self.k = int(k) 23 | def classify(self, testSample): 24 | nClasses = numpy.unique(self.Y).shape[0] 25 | if testSample.shape[0] != 1: 26 | YDist = (distance.cdist(self.X, testSample.reshape(1, testSample.shape[0]), 'euclidean')).T 27 | else: 28 | YDist = (distance.cdist(self.X, testSample, 'euclidean')).T 29 | iSort = numpy.argsort(YDist) 30 | P = numpy.zeros((nClasses,)) 31 | for i in range(nClasses): 32 | P[i] = numpy.nonzero(self.Y[iSort[0][0:self.k]]==i)[0].shape[0] / float(self.k) 33 | return (numpy.argmax(P), P) 34 | 35 | 36 | 37 | def classifierWrapper(classifier, classifierType, testSample): 38 | ''' 39 | This function is used as a wrapper to pattern classification. 40 | ARGUMENTS: 41 | - classifier: a classifier object of type mlpy.LibSvm or kNN (defined in this library) 42 | - classifierType: "svm" or "knn" 43 | - testSample: a feature vector (numpy array) 44 | RETURNS: 45 | - R: class ID 46 | - P: probability estimate 47 | ''' 48 | R = -1; P = -1 49 | if classifierType == "knn": 50 | [R, P] = classifier.classify(testSample) 51 | elif classifierType == "svm": 52 | R = classifier.predict(testSample.reshape(1,-1))[0] 53 | P = classifier.predict_proba(testSample.reshape(1,-1))[0] 54 | elif classifierType == "gmm": 55 | P = [] 56 | array = numpy.matrix(testSample) 57 | for i, gmm in enumerate(classifier): 58 | P.append(gmm.score(array)) 59 | R = numpy.argmax(numpy.array(P)) 60 | P = numpy.array(P) 61 | P[P<0] = 0.0; 62 | if P.sum()>0: 63 | P = P / P.sum() 64 | P = P[:, 0] 65 | elif classifierType == "gmmNoNormalize": 66 | P = [] 67 | array = numpy.matrix(testSample) 68 | for i, gmm in enumerate(classifier): 69 | P.append(gmm.score(array)) 70 | R = numpy.argmax(numpy.array(P)) 71 | P = numpy.array(P) 72 | P[P<0] = 0.0; 73 | P = P[:, 0] 74 | elif classifierType == "rbm": 75 | R = classifier.predict(testSample) 76 | P = 1 77 | elif classifierType == "rbm+svm": 78 | testSample2 = classifier["rbm"].transform(testSample) 79 | R = classifier["svm"].predict(testSample2.reshape(1,-1))[0] 80 | P = classifier["svm"].predict_proba(testSample2.reshape(1,-1))[0] 81 | #elif classifierType == "dbn": 82 | # A =numpy.matrix(testSample) 83 | # R = classifier.predict(A) 84 | return [R, P] 85 | 86 | def randSplitFeatures(features, textSents, ClassNames, partTrain): 87 | """ 88 | def randSplitFeatures(features): 89 | 90 | This function splits a feature set for training and testing. 91 | 92 | ARGUMENTS: 93 | - features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features. 94 | each matrix features[i] of class i is [numOfSamples x numOfDimensions] 95 | - partTrain: percentage 96 | RETURNS: 97 | - featuresTrains: a list of training data for each class 98 | - featuresTest: a list of testing data for each class 99 | """ 100 | 101 | featuresTrain = [] 102 | featuresTest = [] 103 | if ClassNames[0] == "negative": 104 | features = [features[1], features[0]] 105 | 106 | sentimentPTrain = []; sentimentPTest = [] 107 | for i,f in enumerate(features): 108 | [numOfSamples, numOfDims] = f.shape 109 | randperm = numpy.random.permutation(range(numOfSamples)) 110 | nTrainSamples = int(round(partTrain * numOfSamples)); 111 | featuresTrain.append(f[randperm[1:nTrainSamples]]) 112 | featuresTest.append(f[randperm[nTrainSamples+1::]]) 113 | if len(textSents)>i: 114 | sentimentPTrain.append(textSents[i][randperm[1:nTrainSamples]]) 115 | sentimentPTest.append(textSents[i][randperm[nTrainSamples+1::]]) 116 | 117 | return (featuresTrain, featuresTest, sentimentPTrain, sentimentPTest) 118 | 119 | def trainGMM(features, nComp, covType = 'diag'): 120 | Gs = [] 121 | for f in features: 122 | gmm = mixture.GMM(n_components=int(nComp), covariance_type = covType) 123 | Gs.append(gmm.fit(f)) 124 | 125 | return Gs 126 | 127 | def trainKNN(features, K): 128 | ''' 129 | Train a kNN classifier. 130 | ARGUMENTS: 131 | - features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features. 132 | each matrix features[i] of class i is [numOfSamples x numOfDimensions] 133 | - K: parameter K 134 | RETURNS: 135 | - kNN: the trained kNN variable 136 | 137 | ''' 138 | [Xt, Yt] = listOfFeatures2Matrix(features) 139 | knn = kNN(Xt, Yt, K) 140 | return knn 141 | 142 | def trainSVM(features, Cparam): 143 | ''' 144 | Train a multi-class probabilitistic SVM classifier. 145 | Note: This function is simply a wrapper to the mlpy-LibSVM functionality for SVM training 146 | See function trainSVM_feature() to use a wrapper on both the feature extraction and the SVM training (and parameter tuning) processes. 147 | ARGUMENTS: 148 | - features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features. 149 | each matrix features[i] of class i is [numOfSamples x numOfDimensions] 150 | - Cparam: SVM parameter C (cost of constraints violation) 151 | RETURNS: 152 | - svm: the trained SVM variable 153 | 154 | NOTE: 155 | This function trains a linear-kernel SVM for a given C value. For a different kernel, other types of parameters should be provided. 156 | For example, gamma for a polynomial, rbf or sigmoid kernel. Furthermore, Nu should be provided for a nu_SVM classifier. 157 | See MLPY documentation for more details (http://mlpy.sourceforge.net/docs/3.4/svm.html) 158 | ''' 159 | [X, Y] = listOfFeatures2Matrix(features) 160 | svm = sklearn.svm.SVC(C = Cparam, kernel = 'linear', probability = True) 161 | svm.fit(X,Y) 162 | 163 | return svm 164 | 165 | ''' 166 | def trainDBN(features, nComponents): 167 | 168 | [X, Y] = listOfFeatures2Matrix(features) 169 | # TODO !!!!!!!!!!!!!!!!!!!!!!! 170 | X = numpy.concatenate([X, X]) 171 | Y = numpy.concatenate([Y,Y]) 172 | dbn = DBN( 173 | [X.shape[1], int(nComponents), int(nComponents), len(features)], 174 | learn_rates = 0.2, 175 | # learn_rates_pretrain=0.005, 176 | learn_rate_decays = 0.9, 177 | epochs = 200, 178 | verbose = 0) 179 | 180 | dbn.fit(X, Y) 181 | return dbn 182 | ''' 183 | def trainRBM_LR(features, paramC, nComponents): 184 | [X, Y] = listOfFeatures2Matrix(features) 185 | logistic = LogisticRegression(C = paramC) 186 | rbm = BernoulliRBM(n_components = nComponents, n_iter = 100, learning_rate = 0.01, verbose = False) 187 | # NORMALIZATION??? 188 | classifier = Pipeline([("rbm", rbm), ("logistic", logistic)]) 189 | classifier.fit(X, Y) 190 | return classifier 191 | 192 | def trainRBM_SVM(features, Cparam, nComponents): 193 | [X, Y] = listOfFeatures2Matrix(features) 194 | rbm = BernoulliRBM(n_components = nComponents, n_iter = 30, learning_rate = 0.2, verbose = True) 195 | rbm.fit(X,Y) 196 | newX = rbm.transform(X) 197 | # colors = ["r","g","b"] 198 | # for i in range(1,Y.shape[0],5): 199 | # plt.plot(newX[i,:], colors[int(Y[i])]) 200 | # plt.show() 201 | 202 | classifier = {} 203 | classifier["rbm"] = rbm 204 | svm = sklearn.svm.SVC(C = Cparam, kernel = 'linear', probability = True) 205 | svm.fit(newX,Y) 206 | 207 | classifier["svm"] = svm 208 | 209 | return classifier 210 | 211 | 212 | def featureAndTrain(listOfDirs, classifierType, modelName): 213 | ''' 214 | This function is used as a wrapper to segment-based audio feature extraction and classifier training. 215 | ARGUMENTS: 216 | listOfDirs: list of paths of directories. Each directory contains a signle audio class whose samples are stored in seperate WAV files. 217 | modelName: name of the model to be saved 218 | RETURNS: 219 | None. Resulting classifier along with the respective model parameters are saved on files. 220 | ''' 221 | # STEP A: Feature Extraction: 222 | [features, classNames, _, featureNames] = featureExtraction.getFeaturesFromDirs(listOfDirs) 223 | 224 | # write to arff: 225 | writeTrainDataToARFF(modelName, features, classNames, featureNames) 226 | 227 | if len(features)==0: 228 | print "trainSVM_feature ERROR: No data found in any input folder!" 229 | return 230 | for i,f in enumerate(features): 231 | if len(f)==0: 232 | print "trainSVM_feature ERROR: " + listOfDirs[i] + " folder is empty or non-existing!" 233 | return 234 | 235 | # STEP B: Classifier Evaluation and Parameter Selection: 236 | if classifierType == "svm": 237 | classifierParams = numpy.array([0.001, 0.01, 0.25, 0.5, 1.0, 2.0, 5.0]) 238 | elif classifierType == "knn": 239 | classifierParams = numpy.array([1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]); 240 | elif classifierType == "gmm": 241 | classifierParams = numpy.array([2, 3, 4, 5]); 242 | elif classifierType == "rbm": 243 | classifierParams = numpy.array([1500, 2000, 2500, 3000, 3500, 4000, 10000]); 244 | elif classifierType == "rbm+svm": 245 | classifierParams = numpy.array([0.001, 0.01, 0.25, 0.5, 1.0, 2.0, 5.0]); 246 | elif classifierType == "dbn": 247 | classifierParams = numpy.array([10, 50, 100, 150, 160, 170, 180, 200, 500, 1000]); 248 | 249 | # get optimal classifeir parameter: 250 | bestParam = evaluateClassifier(features, [], classNames, 100, classifierType, classifierParams, 0, 0, "") 251 | print "Selected params: {0:.5f}".format(bestParam) 252 | 253 | C = len(classNames) 254 | [featuresNorm, MAX, MIN] = normalizeFeatures(features) # normalize features 255 | MAX = MAX.tolist(); MIN = MIN.tolist() 256 | featuresNew = featuresNorm 257 | 258 | 259 | # STEP C: Save the classifier to file 260 | if classifierType == "svm": 261 | Classifier = trainSVM(featuresNew, bestParam) 262 | #Classifier.save_model(modelName) 263 | with open(modelName, 'wb') as fid: 264 | cPickle.dump(Classifier, fid) 265 | 266 | fo = open(modelName + "NORMALIZATION", "wb") 267 | cPickle.dump(MAX, fo, protocol = cPickle.HIGHEST_PROTOCOL) 268 | cPickle.dump(MIN, fo, protocol = cPickle.HIGHEST_PROTOCOL) 269 | cPickle.dump(classNames, fo, protocol = cPickle.HIGHEST_PROTOCOL) 270 | fo.close() 271 | elif classifierType == "knn": 272 | [X, Y] = listOfFeatures2Matrix(featuresNew) 273 | X = X.tolist(); Y = Y.tolist() 274 | fo = open(modelName, "wb") 275 | cPickle.dump(X, fo, protocol = cPickle.HIGHEST_PROTOCOL) 276 | cPickle.dump(Y, fo, protocol = cPickle.HIGHEST_PROTOCOL) 277 | cPickle.dump(MAX, fo, protocol = cPickle.HIGHEST_PROTOCOL) 278 | cPickle.dump(MIN, fo, protocol = cPickle.HIGHEST_PROTOCOL) 279 | cPickle.dump(classNames, fo, protocol = cPickle.HIGHEST_PROTOCOL) 280 | cPickle.dump(bestParam, fo, protocol = cPickle.HIGHEST_PROTOCOL) 281 | fo.close() 282 | elif classifierType == "gmm": 283 | Classifier = trainGMM(features, bestParam, covType = 'diag') 284 | fo = open(modelName, "wb") 285 | cPickle.dump(classNames, fo, protocol = cPickle.HIGHEST_PROTOCOL) 286 | cPickle.dump(Classifier, fo, protocol = cPickle.HIGHEST_PROTOCOL) 287 | fo.close() 288 | elif classifierType == "rbm": 289 | Classifier = trainRBM_LR(features, bestParam, RBM_NCOMPONENTS) 290 | fo = open(modelName, "wb") 291 | cPickle.dump(classNames, fo, protocol = cPickle.HIGHEST_PROTOCOL) 292 | cPickle.dump(Classifier, fo, protocol = cPickle.HIGHEST_PROTOCOL) 293 | cPickle.dump(MAX, fo, protocol = cPickle.HIGHEST_PROTOCOL) 294 | cPickle.dump(MIN, fo, protocol = cPickle.HIGHEST_PROTOCOL) 295 | cPickle.dump(classNames, fo, protocol = cPickle.HIGHEST_PROTOCOL) 296 | cPickle.dump(bestParam, fo, protocol = cPickle.HIGHEST_PROTOCOL) 297 | fo.close() 298 | elif classifierType == "rbm+svm": 299 | Classifier = trainRBM_SVM(features, bestParam, RBM_NCOMPONENTS) 300 | fo = open(modelName, "wb") 301 | cPickle.dump(classNames, fo, protocol = cPickle.HIGHEST_PROTOCOL) 302 | cPickle.dump(Classifier, fo, protocol = cPickle.HIGHEST_PROTOCOL) 303 | cPickle.dump(MAX, fo, protocol = cPickle.HIGHEST_PROTOCOL) 304 | cPickle.dump(MIN, fo, protocol = cPickle.HIGHEST_PROTOCOL) 305 | cPickle.dump(classNames, fo, protocol = cPickle.HIGHEST_PROTOCOL) 306 | cPickle.dump(bestParam, fo, protocol = cPickle.HIGHEST_PROTOCOL) 307 | fo.close() 308 | ''' 309 | elif classifierType == "dbn": 310 | Classifier = trainDBN(features, bestParam) 311 | fo = open(modelName, "wb") 312 | cPickle.dump(classNames, fo, protocol = cPickle.HIGHEST_PROTOCOL) 313 | cPickle.dump(Classifier, fo, protocol = cPickle.HIGHEST_PROTOCOL) 314 | cPickle.dump(MAX, fo, protocol = cPickle.HIGHEST_PROTOCOL) 315 | cPickle.dump(MIN, fo, protocol = cPickle.HIGHEST_PROTOCOL) 316 | cPickle.dump(classNames, fo, protocol = cPickle.HIGHEST_PROTOCOL) 317 | cPickle.dump(bestParam, fo, protocol = cPickle.HIGHEST_PROTOCOL) 318 | fo.close() 319 | ''' 320 | 321 | def loadGMMModel(gmmModel): 322 | try: 323 | fo = open(gmmModel, "rb") 324 | except IOError: 325 | print "didn't find file" 326 | return 327 | try: 328 | classNames = cPickle.load(fo) 329 | Classifier = cPickle.load(fo) 330 | except: 331 | fo.close() 332 | fo.close() 333 | 334 | return(Classifier, classNames); 335 | 336 | 337 | def loadKNNModel(kNNModelName): 338 | try: 339 | fo = open(kNNModelName, "rb") 340 | except IOError: 341 | print "didn't find file" 342 | return 343 | try: 344 | X = cPickle.load(fo) 345 | Y = cPickle.load(fo) 346 | MAX = cPickle.load(fo) 347 | MIN = cPickle.load(fo) 348 | classNames = cPickle.load(fo) 349 | K = cPickle.load(fo) 350 | except: 351 | fo.close() 352 | fo.close() 353 | 354 | X = numpy.array(X) 355 | Y = numpy.array(Y) 356 | MAX = numpy.array(MAX) 357 | MIN = numpy.array(MIN) 358 | 359 | Classifier = kNN(X, Y, K) # Note: a direct call to the kNN constructor is used here, since the 360 | 361 | return(Classifier, MAX, MIN, classNames); 362 | 363 | def loadSVModel(SVMmodelName): 364 | try: 365 | fo = open(SVMmodelName+"NORMALIZATION", "rb") 366 | except IOError: 367 | print "Load SVM Model: Didn't find file" 368 | return 369 | try: 370 | MAX = cPickle.load(fo) 371 | MIN = cPickle.load(fo) 372 | classNames = cPickle.load(fo) 373 | except: 374 | fo.close() 375 | fo.close() 376 | 377 | MAX = numpy.array(MAX) 378 | MIN = numpy.array(MIN) 379 | 380 | COEFF = [] 381 | 382 | with open(SVMmodelName, 'rb') as fid: 383 | SVM = cPickle.load(fid) 384 | 385 | 386 | return(SVM, MAX, MIN, classNames); 387 | 388 | def loadDBNModel(dbnModelName): 389 | try: 390 | fo = open(dbnModelName, "rb") 391 | except IOError: 392 | print "didn't find file" 393 | return 394 | try: 395 | classNames = cPickle.load(fo) 396 | Classifier = cPickle.load(fo) 397 | MAX = cPickle.load(fo) 398 | MIN = cPickle.load(fo) 399 | classNames = cPickle.load(fo) 400 | bestParam = cPickle.load(fo) 401 | except: 402 | fo.close() 403 | fo.close() 404 | 405 | return(Classifier, classNames, MAX, MIN); 406 | 407 | def evaluateClassifier(features, fileNames, ClassNames, nExp, ClassifierName, Params, parameterMode, method, dimReduction = ""): 408 | ''' 409 | ARGUMENTS: 410 | features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features. 411 | each matrix features[i] of class i is [numOfSamples x numOfDimensions] 412 | fileNames: 413 | ClassNames: 414 | nExp: number of experiments 415 | ClassifierName: "svm" or "knn" 416 | Params: list of classifier params value. For the case of svm+knn combination two values are needed per element 417 | RETURNS: 418 | bestParam: the value of the input parameter that optimizes the selected performance measure 419 | ''' 420 | 421 | textSents = [] 422 | for c in fileNames: 423 | t = [] 424 | for f in c: 425 | t.append(text.classifyFile(1, "NBmodelSmall", f)) 426 | textSents.append(numpy.array(t)) 427 | 428 | # feature normalization: 429 | (featuresNorm, MAX, MIN) = normalizeFeatures(features) 430 | nClasses = len(features) 431 | CAll = []; acAll = []; F1All = [] 432 | PrecisionClassesAll = []; RecallClassesAll = []; ClassesAll = []; F1ClassesAll = [] 433 | CMsAll = [] 434 | 435 | for Ci, C in enumerate(Params): # for each Nu value 436 | 437 | CM = numpy.zeros((nClasses, nClasses)) 438 | for e in range(nExp): # for each cross-validation iteration: 439 | # split features: 440 | if (ClassifierName=="svm") or (ClassifierName=="knn") or (ClassifierName=="rbm") or (ClassifierName=="rbm+svm") or (ClassifierName=="dbn") or (ClassifierName=="svm+knn"): 441 | featuresTrain, featuresTest, sentPTrain, sentPTest = randSplitFeatures(featuresNorm, textSents, ClassNames, 0.80) 442 | elif ClassifierName=="gmm": 443 | featuresTrain, featuresTest, sentPTrain, sentPTest = randSplitFeatures(features, textSents, ClassNames, 0.80) 444 | # train multi-class svms: 445 | if ClassifierName=="svm": 446 | Classifier = trainSVM(featuresTrain, C) 447 | elif ClassifierName=="knn": 448 | Classifier = trainKNN(featuresTrain, C) 449 | elif ClassifierName=="gmm": 450 | Classifier = trainGMM(featuresTrain, C) 451 | elif ClassifierName=="rbm": 452 | Classifier = trainRBM_LR(featuresTrain, C, RBM_NCOMPONENTS) 453 | elif ClassifierName=="rbm+svm": 454 | Classifier = trainRBM_SVM(featuresTrain, C, RBM_NCOMPONENTS) 455 | elif ClassifierName=="dbn": 456 | Classifier = trainDBN(featuresTrain, C) 457 | elif ClassifierName=="svm+knn": 458 | Classifier1 = trainSVM(featuresTrain, C[0]) 459 | Classifier2 = trainKNN(featuresTrain, C[1]) 460 | 461 | CMt = numpy.zeros((nClasses, nClasses)) 462 | for c1 in range(nClasses): 463 | #Results = Classifier.pred(featuresTest[c1]) 464 | nTestSamples = len(featuresTest[c1]) 465 | Results = numpy.zeros((nTestSamples,1)) 466 | for ss in range(nTestSamples): 467 | if ClassifierName == "svm+knn": 468 | [Rtemp, P1] = classifierWrapper(Classifier1, "svm", featuresTest[c1][ss]) 469 | [Rtemp, P2] = classifierWrapper(Classifier2, "knn", featuresTest[c1][ss]) 470 | P = [(P1[pp]+P2[pp])/2.0 for pp in range(len(P1))] 471 | Results[ss] = numpy.argmax(numpy.array(P)) 472 | else: 473 | [Results[ss], P] = classifierWrapper(Classifier, ClassifierName, featuresTest[c1][ss]) 474 | for c2 in range(nClasses): 475 | CMt[c1][c2] = float(len(numpy.nonzero(Results==c2)[0])) 476 | CM = CM + CMt 477 | CM = CM + 0.0000000010 478 | Rec = numpy.zeros((CM.shape[0],)) 479 | Pre = numpy.zeros((CM.shape[0],)) 480 | 481 | for ci in range(CM.shape[0]): 482 | Rec[ci] = CM[ci,ci] / numpy.sum(CM[ci,:]); 483 | Pre[ci] = CM[ci,ci] / numpy.sum(CM[:,ci]); 484 | PrecisionClassesAll.append(Pre) 485 | RecallClassesAll.append(Rec) 486 | F1 = 2 * Rec * Pre / (Rec + Pre) 487 | F1ClassesAll.append(F1) 488 | acAll.append(numpy.sum(numpy.diagonal(CM)) / numpy.sum(CM)) 489 | 490 | CMsAll.append(CM) 491 | F1All.append(numpy.mean(F1)) 492 | 493 | print ("\t\t"), 494 | for i,c in enumerate(ClassNames): 495 | if i==len(ClassNames)-1: print "{0:s}\t\t".format(c), 496 | else: print "{0:s}\t\t\t".format(c), 497 | print ("OVERALL") 498 | print ("\tC"), 499 | if ClassifierName == "svm+knn": 500 | print "\t", 501 | for c in ClassNames: print "\tPRE\tREC\tF1", 502 | print "\t{0:s}\t{1:s}".format("ACC","F1") 503 | bestAcInd = numpy.argmax(acAll) 504 | bestF1Ind = numpy.argmax(F1All) 505 | for i in range(len(PrecisionClassesAll)): 506 | if ClassifierName == "svm+knn": 507 | print "\t{0:.3f} + {1:.3f}".format(Params[i][0], Params[i][1]), 508 | else: 509 | print "\t{0:.3f}".format(Params[i]), 510 | for c in range(len(PrecisionClassesAll[i])): 511 | print "\t{0:.1f}\t{1:.1f}\t{2:.1f}".format(100.0*PrecisionClassesAll[i][c], 100.0*RecallClassesAll[i][c], 100.0*F1ClassesAll[i][c]), 512 | print "\t{0:.1f}\t{1:.1f}".format(100.0*acAll[i], 100.0*F1All[i]), 513 | if i == bestF1Ind: print "\t best F1", 514 | if i == bestAcInd: print "\t best Acc", 515 | print 516 | 517 | if parameterMode==0: # keep parameters that maximize overall classification accuracy: 518 | print "Confusion Matrix:" 519 | printConfusionMatrix(CMsAll[bestAcInd], ClassNames) 520 | return Params[bestAcInd] 521 | elif parameterMode==1: # keep parameters that maximize overall F1 measure: 522 | print "Confusion Matrix:" 523 | printConfusionMatrix(CMsAll[bestF1Ind], ClassNames) 524 | return Params[bestF1Ind] 525 | 526 | def printConfusionMatrix(CM, ClassNames): 527 | ''' 528 | This function prints a confusion matrix for a particular classification task. 529 | ARGUMENTS: 530 | CM: a 2-D numpy array of the confusion matrix 531 | (CM[i,j] is the number of times a sample from class i was classified in class j) 532 | ClassNames: a list that contains the names of the classes 533 | ''' 534 | 535 | if CM.shape[0] != len(ClassNames): 536 | print "printConfusionMatrix: Wrong argument sizes\n" 537 | return 538 | 539 | for c in ClassNames: 540 | if len(c)>4: 541 | c = c[0:3] 542 | print "\t{0:s}".format(c), 543 | print 544 | 545 | for i, c in enumerate(ClassNames): 546 | if len(c)>4: c = c[0:3] 547 | print "{0:s}".format(c), 548 | for j in range(len(ClassNames)): 549 | print "\t{0:.1f}".format(100.0*CM[i][j] / numpy.sum(CM)), 550 | print 551 | 552 | def normalizeFeatures(features): 553 | ''' 554 | This function nromalizes a feature set to 0-mean and 1-std. 555 | Used in most classifier trainning cases. 556 | 557 | ARGUMENTS: 558 | - features: list of feature matrices (each one of them is a numpy matrix) 559 | RETURNS: 560 | - featuresNorm: list of NORMALIZED feature matrices 561 | - MAX: mean vector 562 | - MIN: std vector 563 | ''' 564 | X = numpy.array([]) 565 | 566 | for count,f in enumerate(features): 567 | if f.shape[0]>0: 568 | if count==0: 569 | X = f 570 | else: 571 | X = numpy.vstack((X, f)) 572 | count += 1 573 | 574 | # MAX = numpy.mean(X, axis = 0) 575 | # MIN = numpy.std( X, axis = 0) 576 | 577 | # featuresNorm = [] 578 | # for f in features: 579 | # ft = f.copy() 580 | # for nSamples in range(f.shape[0]): 581 | # ft[nSamples,:] = (ft[nSamples,:] - MAX) / MIN 582 | # featuresNorm.append(ft) 583 | 584 | MIN = numpy.min(X, axis = 0) 585 | MAX = numpy.max(X, axis = 0) 586 | 587 | featuresNorm = [] 588 | for f in features: 589 | ft = f.copy() 590 | for nSamples in range(f.shape[0]): 591 | ft[nSamples,:] = (ft[nSamples,:] - MIN) / (MAX - MIN + 0.000001) 592 | featuresNorm.append(ft) 593 | 594 | 595 | return (featuresNorm, MAX, MIN) 596 | 597 | def listOfFeatures2Matrix(features): 598 | ''' 599 | listOfFeatures2Matrix(features) 600 | 601 | This function takes a list of feature matrices as argument and returns a single concatenated feature matrix and the respective class labels. 602 | 603 | ARGUMENTS: 604 | - features: a list of feature matrices 605 | 606 | RETURNS: 607 | - X: a concatenated matrix of features 608 | - Y: a vector of class indeces 609 | ''' 610 | 611 | X = numpy.array([]) 612 | Y = numpy.array([]) 613 | for i,f in enumerate(features): 614 | if i==0: 615 | X = f 616 | Y = i * numpy.ones((len(f), 1)) 617 | else: 618 | X = numpy.vstack((X, f)) 619 | Y = numpy.append(Y, i * numpy.ones((len(f), 1))) 620 | return (X, Y) 621 | 622 | def listOfFeatures2MatrixRegression(features, Ys): 623 | ''' 624 | listOfFeatures2MatrixRegression(features) 625 | 626 | Same as MatrixRegression but used for regression (also takes real values as arguments) 627 | 628 | ARGUMENTS: 629 | - features: a list of feature matrices 630 | - Ys: a list of respective real values 631 | 632 | RETURNS: 633 | - X: a concatenated matrix of features 634 | - Y: a vector of class indeces 635 | ''' 636 | 637 | X = numpy.array([]) 638 | Y = numpy.array([]) 639 | for i,f in enumerate(features): 640 | if i==0: 641 | X = f 642 | Y = Ys[i] * numpy.ones((len(f), 1)) 643 | else: 644 | X = numpy.vstack((X, f)) 645 | Y = numpy.append(Y, Ys[i] * numpy.ones((len(f), 1))) 646 | return (X, Y) 647 | 648 | 649 | def fileClassification(inputFile, modelName, modelType): 650 | # Load classifier: 651 | if modelType!="svm+knn": 652 | if not os.path.isfile(modelName): 653 | print "fileClassification: input modelName not found!" 654 | return (-1,-1, -1) 655 | 656 | if not os.path.isfile(inputFile): 657 | print "fileClassification: input modelType not found!" 658 | return (-1,-1, -1) 659 | 660 | 661 | if modelType=='svm': 662 | [Classifier, MAX, MIN, classNames] = loadSVModel(modelName) 663 | elif modelType=='knn': 664 | [Classifier, MAX, MIN, classNames] = loadKNNModel(modelName) 665 | elif modelType=="gmm" or modelType=="gmmNoNormalize": 666 | [Classifier, classNames] = loadGMMModel(modelName) 667 | elif modelType=='dbn': 668 | [Classifier, classNames, MAX, MIN] = loadDBNModel(modelName) 669 | elif modelType=='svm+knn': 670 | [Classifier1, MAX, MIN, classNames] = loadSVModel(modelName[0]) 671 | [Classifier2, MAX, MIN, classNames] = loadKNNModel(modelName[1]) 672 | 673 | [curFV, _] = featureExtraction.getFeaturesFromFile(inputFile, False) 674 | 675 | if modelType=='svm' or modelType=='knn' or modelType=="rbm" or modelType=="svm+knn": 676 | curFV = (curFV - MIN) / (MAX-MIN+0.00000001); # normalization 677 | if modelType!='svm+knn': 678 | [Result, P] = classifierWrapper(Classifier, modelType, curFV) # classification 679 | else: 680 | [R1, P1] = classifierWrapper(Classifier1, "svm", curFV) 681 | [Re, P2] = classifierWrapper(Classifier2, "knn", curFV) 682 | P = (P1 + P2) / 2.0 683 | print P1 684 | print P2 685 | print P 686 | Result = numpy.argmax(numpy.array(P)) 687 | 688 | # check if text file exists: 689 | inputFileText = inputFile[0:-4] + ".txt" 690 | print inputFileText 691 | if os.path.isfile("text" + os.sep + inputFileText): 692 | Ptext = text.classifyFile(1, "NBmodelSmall", inputFileText) 693 | print Ptext 694 | P = (1.3 * P + 0.7 * numpy.array([Ptext, 1-Ptext])) / 2.0 695 | return P, classNames 696 | 697 | def dirClassificationSentiment(dirName, modelName, modelType): 698 | types = ('*.jpg', '*.png',) 699 | filesList = [] 700 | for files in types: 701 | filesList.extend(glob.glob(os.path.join(dirName, files))) 702 | 703 | filesList = sorted(filesList) 704 | print filesList 705 | Features = [] 706 | plt.close('all'); 707 | ax = plt.gca() 708 | plt.hold(True) 709 | for fi in filesList: 710 | P, classNames = fileClassification(fi, modelName, modelType) 711 | im = cv2.imread(fi, cv2.CV_LOAD_IMAGE_COLOR) 712 | Width = 0.1; Height = 0.1; startX = P[classNames.index("positive")]; startY = 0; 713 | myaximage = ax.imshow(cv2.cvtColor(im, cv2.cv.CV_RGB2BGR), extent=(startX-Width/2.0, startX+Width/2.0, startY-Height/2.0, startY+Height/2.0), alpha=1.0, zorder=-1) 714 | plt.axis((0,1,-0.1,0.1)) 715 | plt.show(block = False); 716 | plt.draw() 717 | plt.show(block = True); 718 | 719 | def writeTrainDataToARFF(modelName, features, classNames, featureNames): 720 | f = open(modelName + ".arff", 'w') 721 | f.write('@RELATION ' + modelName + '\n'); 722 | for fn in featureNames: 723 | f.write('@ATTRIBUTE ' + fn + ' NUMERIC\n') 724 | f.write('@ATTRIBUTE class {') 725 | for c in range(len(classNames)-1): 726 | f.write(classNames[c] + ',') 727 | f.write(classNames[-1] + '}\n\n') 728 | f.write('@DATA\n') 729 | for c, fe in enumerate(features): 730 | for i in range(fe.shape[0]): 731 | for j in range(fe.shape[1]): 732 | f.write("{0:f},".format(fe[i,j])) 733 | f.write(classNames[c]+"\n") 734 | f.close() 735 | 736 | def temp(features): 737 | [featuresNorm, MAX, MIN] = normalizeFeatures(features) 738 | [X, Y] = listOfFeatures2Matrix(featuresNorm) 739 | rbm = BernoulliRBM(n_components = 10, n_iter = 1000, learning_rate = 0.01, verbose = False) 740 | X1 = X[0::2] 741 | X2 = X[1::2] 742 | Y1 = Y[0::2] 743 | Y2 = Y[1::2] 744 | rbm.fit(X1,Y1) 745 | YY = rbm.transform(X1) 746 | 747 | for i in range(10):plt.plot(YY[i,:],'r') 748 | for i in range(10):plt.plot(YY[i+10,:],'g') 749 | for i in range(10):plt.plot(YY[i+20,:],'b') 750 | plt.show() 751 | 752 | def main(argv): 753 | if argv[1] == "-train": 754 | if len(argv)>=5: 755 | modelType = argv[2] 756 | modelName = argv[3] 757 | dirNames = argv[4:len(argv)] 758 | #(features, classNames, fileNames) = featureExtraction.getFeaturesFromDirs(dirNames) 759 | featureAndTrain(dirNames, modelType, modelName) 760 | 761 | 762 | if argv[1] == "-evaluateClassifierFromFile": # evaluate a classifier for features stored in file 763 | # to be used after featureExtraction's getFeaturesFromDirs functionality. Eg: 764 | # python featureExtraction.py -featuresDirs 4classes data/beach/ data/building/ data/faces/ daa/groups/ 765 | # python train.py -evaluateClassifierFromFile 4classes_features svm 10 0.1 1 2 766 | if len(argv)>=8: 767 | featureFile = argv[2] 768 | classifierType = argv[3] 769 | nExp = int(argv[4]) 770 | dimRedFile = argv[5] 771 | method = int(argv[6]) 772 | if classifierType=="svm+knn": 773 | cParams = [float(c) for c in argv[7::]] 774 | if len(cParams) % 2 != 0: 775 | print "Number of classifier Params must be event for the svm+knn combination!" 776 | return 777 | else: 778 | cParams1 = cParams[0:len(cParams)/2] 779 | cParams2 = cParams[len(cParams)/2::] 780 | cParams = [] 781 | for p1 in cParams1: 782 | for p2 in cParams2: 783 | cParams.append([p1, p2]) 784 | else: 785 | cParams = [float(c) for c in argv[7::]] 786 | 787 | 788 | fo = open(featureFile, "rb") 789 | features = cPickle.load(fo) 790 | classNames = cPickle.load(fo) 791 | fileNames = cPickle.load(fo) 792 | featureNames = cPickle.load(fo) 793 | fo.close() 794 | 795 | #features = features[0:2] 796 | #classNames = classNames[0:2] 797 | #print classNames 798 | evaluateClassifier(features, fileNames, classNames, nExp, classifierType, cParams, 0, method, dimRedFile) 799 | 800 | if argv[1] == "-classifyFile": 801 | if len(argv)>=5: 802 | modelType = argv[2] 803 | if modelType=="svm+knn": 804 | modelName = [argv[3], argv[4]] 805 | fileToClassify = argv[5] 806 | else: 807 | modelName = argv[3] 808 | fileToClassify = argv[4] 809 | [P, C] = fileClassification(fileToClassify, modelName, modelType) 810 | for c, p in zip(C,P): 811 | print "{0:20s} \t {1:15.1f}%".format(c,100*p); 812 | 813 | if argv[1] == "-classifyFileMultiLabel": 814 | if len(argv)==4: 815 | Threshold = 0.050 816 | modelName = argv[2] 817 | # modelName = argv[3] 818 | fileToClassify = argv[3] 819 | 820 | [P, C] = fileClassification(fileToClassify, modelName, "gmmNoNormalize") 821 | 822 | for c, p in zip(C,P): 823 | if p > Threshold: 824 | print "{0:20s} \t {1:15.1f}%".format(c,p); 825 | 826 | 827 | if argv[1] == "-classifyFolderSentiment": 828 | if len(argv)==5: 829 | modelType = argv[2] 830 | modelName = argv[3] 831 | folder = argv[4] 832 | #print modelType, modelName, fileToClassify 833 | dirClassificationSentiment(folder, modelName, modelType) 834 | #[P, C] = fileClassification(fileToClassify, modelName, modelType) 835 | 836 | #P = P[0] 837 | #for c, p in zip(C,P): 838 | # print "{0:20s} \t {1:15.1f}%".format(c,100*p); 839 | 840 | 841 | if __name__ == '__main__': 842 | main(sys.argv) 843 | --------------------------------------------------------------------------------