def runkNNDigit(percent,trainsize,testsize): trainingData,trainingLabels,testData,testLabels=extractImages("digit",trainsize,testsize) featureFunction = featureFuncLib.basicFeatureExtractorDigit_3 trainFeatures = list(map(featureFunction, trainingData)) testFeatures = list(map(featureFunction,testData)) trainFeatures,trainingLabels=xPercent(percent,trainFeatures,trainingLabels) kvalue = 1 start = time.time() predict=kNN.predict(trainFeatures,trainingLabels,testFeatures,testLabels,kvalue) end = time.time() kNNpredictTimePerImage = abs(end-start) / len(predict) return percentCorrect(testLabels,predict),kNNpredictTimePerImage
def detect_if_lie(self): # get detected features number_of_blinks = self.blink_detector.get_and_reset_number_of_blinks() number_of_blushing_occurred = self.blushing_detector.get_number_of_blushing_occurred_and_reset( ) number_of_lip_pursing_occurred = self.pursed_lips_detector.get_and_reset_number_of_lip_pursing( ) if self.seconds > 0: number_of_blinks_per_second = number_of_blinks / self.seconds else: number_of_blinks_per_second = number_of_blinks to_predict = [ self.person.average_number_of_blinks, number_of_blinks_per_second, number_of_lip_pursing_occurred, number_of_blushing_occurred ] if self.algorithm == "knn": prediction = kNN.predict([to_predict], DATASET_PATH) else: prediction = feedforward_nn.predict([to_predict]) print(prediction) prediction = np.round(prediction[0]) if prediction == 1: prediction = "truth" else: prediction = "lie" self.write_to_file(number_of_blinks, number_of_blushing_occurred, number_of_lip_pursing_occurred, number_of_blinks_per_second, prediction) # reset seconds counter self.seconds = 0
hwLabels = [] trainingFileList = listdir('trainingDigits') #load the training set m = len(trainingFileList) trainingMat = zeros((m, 1024)) for i in range(m): fileNameStr = trainingFileList[i] fileStr = fileNameStr.split('.')[0] #take off .txt classNumStr = int(fileStr.split('_')[0]) hwLabels.append(classNumStr) trainingMat[i, :] = img2vector('trainingDigits/%s' % fileNameStr) testLabels = [] testFileList = listdir('testDigits') #iterate through the test set mTest = len(testFileList) testMat = zeros((mTest, 1024)) for i in range(mTest): fileNameStr = testFileList[i] fileStr = fileNameStr.split('.')[0] #take off .txt classNumStr = int(fileStr.split('_')[0]) testLabels.append(classNumStr) testMat[i, :] = img2vector('testDigits/%s' % fileNameStr) return testMat, testLabels, trainingMat, hwLabels k = 3 testMat, testLabels, trainingMat, hwLabels = loadDataSet() predictions = kNN.predict(testMat, trainingMat, hwLabels, k) accuracy = kNN.accuracy(testLabels, predictions) print("the total number of test obs is: %d" % len(testLabels)) print("\nthe total accuracy rate is: %f" % accuracy)