Mercurial Hosting > traffic-intelligence
changeset 788:5b970a5bc233 dev
updated classifying code to OpenCV 3.x (bug in function to load classification models)
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Thu, 24 Mar 2016 16:37:37 -0400 |
parents | 0a428b449b80 |
children | 3666342dabe2 1158a6e2d28e |
files | python/cvutils.py python/ml.py scripts/classify-objects.py scripts/train-object-classification.py |
diffstat | 4 files changed, 35 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/python/cvutils.py Thu Mar 17 16:01:19 2016 -0400 +++ b/python/cvutils.py Thu Mar 24 16:37:37 2016 -0400 @@ -602,6 +602,6 @@ inputData.append(features) nImages = len(inputData) - return array(inputData, dtype = float32), array([classLabel]*nImages, dtype = float32) + return array(inputData, dtype = float32), array([classLabel]*nImages)
--- a/python/ml.py Thu Mar 17 16:01:19 2016 -0400 +++ b/python/ml.py Thu Mar 24 16:37:37 2016 -0400 @@ -11,6 +11,7 @@ import matplotlib.pyplot as plt from scipy.cluster.vq import kmeans, whiten, vq from sklearn import mixture +import cv2 import utils @@ -18,7 +19,7 @@ # OpenCV ML models ##################### -class Model(object): +class StatModel(object): '''Abstract class for loading/saving model''' def load(self, filename): if path.exists(filename): @@ -29,16 +30,21 @@ def save(self, filename): self.model.save(filename) -class SVM(Model): +class SVM(StatModel): '''wrapper for OpenCV SimpleVectorMachine algorithm''' + def __init__(self, svmType = cv2.ml.SVM_C_SVC, kernelType = cv2.ml.SVM_RBF, degree = 0, gamma = 1, coef0 = 0, Cvalue = 1, nu = 0, p = 0): + self.model = cv2.ml.SVM_create() + self.model.setType(svmType) + self.model.setKernel(kernelType) + self.model.setDegree(degree) + self.model.setGamma(gamma) + self.model.setCoef0(coef0) + self.model.setC(Cvalue) + self.model.setNu(nu) + self.model.setP(p) - def __init__(self): - import cv2 - self.model = cv2.SVM() - - def train(self, samples, responses, svm_type, kernel_type, degree = 0, gamma = 1, coef0 = 0, Cvalue = 1, nu = 0, p = 0): - self.params = dict(svm_type = svm_type, kernel_type = kernel_type, degree = degree, gamma = gamma, coef0 = coef0, Cvalue = Cvalue, nu = nu, p = p) - self.model.train(samples, responses, params = self.params) + def train(self, samples, layout, responses): + self.model.train(samples, layout, responses) def predict(self, hog): return self.model.predict(hog)
--- a/scripts/classify-objects.py Thu Mar 17 16:01:19 2016 -0400 +++ b/scripts/classify-objects.py Thu Mar 24 16:37:37 2016 -0400 @@ -4,14 +4,16 @@ import numpy as np import sys, argparse -#from cv2 import SVM_RBF, SVM_C_SVC +from cv2 import SVM_RBF, SVM_C_SVC import cv2 from scipy.stats import norm, lognorm -# TODO add mode detection live +# TODO add mode detection live, add choice of kernel and svm type (to be saved in future classifier format) parser = argparse.ArgumentParser(description='The program processes indicators for all pairs of road users in the scene') parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file', required = True) +parser.add_argument('--kernel', dest = 'kernelType', help = 'kernel type for the support vector machine (SVM)', default = SVM_RBF, type = long) +parser.add_argument('--svm', dest = 'svmType', help = 'SVM type', default = SVM_C_SVC, type = long) parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file (overrides the configuration file)') parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (overrides the configuration file)') parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to classify', type = int, default = None) @@ -44,9 +46,9 @@ print('Unknown speed aggregation method: {}. Exiting'.format(params.speedAggregationMethod)) sys.exit() -pedBikeCarSVM = ml.SVM() +pedBikeCarSVM = ml.SVM(args.svmType, args.kernelType) pedBikeCarSVM.load(params.pedBikeCarSVMFilename) -bikeCarSVM = ml.SVM() +bikeCarSVM = ml.SVM(args.svmType, args.kernelType) bikeCarSVM.load(params.bikeCarSVMFilename) # log logistic for ped and bik otherwise ((pedBeta/pedAlfa)*((sMean/pedAlfa)**(pedBeta-1)))/((1+(sMean/pedAlfa)**pedBeta)**2.) @@ -75,8 +77,8 @@ timeInterval = moving.unionIntervals(intervals) capture = cv2.VideoCapture(videoFilename) -width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) -height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) +width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) +height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) pastObjects = [] if params.undistort: # setup undistortion @@ -84,7 +86,7 @@ if capture.isOpened(): ret = True frameNum = timeInterval.first - capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) + capture.set(cv2.CAP_PROP_POS_FRAMES, frameNum) lastFrameNum = timeInterval.last while ret and frameNum <= lastFrameNum:
--- a/scripts/train-object-classification.py Thu Mar 17 16:01:19 2016 -0400 +++ b/scripts/train-object-classification.py Thu Mar 24 16:37:37 2016 -0400 @@ -2,7 +2,7 @@ import numpy as np import sys, argparse -from cv2 import SVM_RBF, SVM_C_SVC +from cv2.ml import SVM_RBF, SVM_C_SVC, ROW_SAMPLE import cvutils, moving, ml @@ -10,6 +10,7 @@ parser.add_argument('-d', dest = 'directoryName', help = 'parent directory name for the directories containing the samples for the different road users', required = True) parser.add_argument('--kernel', dest = 'kernelType', help = 'kernel type for the support vector machine (SVM)', default = SVM_RBF, type = long) parser.add_argument('--svm', dest = 'svmType', help = 'SVM type', default = SVM_C_SVC, type = long) +# TODO make other SVM parameters apparent: C, C0, Nu, etc. parser.add_argument('-s', dest = 'rescaleSize', help = 'rescale size of image samples', default = 64, type = int) parser.add_argument('-o', dest = 'nOrientations', help = 'number of orientations in HoG', default = 9, type = int) parser.add_argument('-p', dest = 'nPixelsPerCell', help = 'number of pixels per cell', default = 8, type = int) @@ -24,7 +25,6 @@ 'bicycle': args.directoryName + "/Cyclists/", 'car': args.directoryName + "/Vehicles/"} -#directory_model = args.directoryName trainingSamplesPBV = {} trainingLabelsPBV = {} trainingSamplesBV = {} @@ -47,21 +47,21 @@ # Training the Support Vector Machine print "Training Pedestrian-Cyclist-Vehicle Model" -model = ml.SVM() -model.train(np.concatenate(trainingSamplesPBV.values()), np.concatenate(trainingLabelsPBV.values()), args.svmType, args.kernelType) +model = ml.SVM(args.svmType, args.kernelType) +model.train(np.concatenate(trainingSamplesPBV.values()), ROW_SAMPLE, np.concatenate(trainingLabelsPBV.values())) model.save(args.directoryName + "/modelPBV.xml") print "Training Cyclist-Vehicle Model" -model = ml.SVM() -model.train(np.concatenate(trainingSamplesBV.values()), np.concatenate(trainingLabelsBV.values()), args.svmType, args.kernelType) +model = ml.SVM(args.svmType, args.kernelType) +model.train(np.concatenate(trainingSamplesBV.values()), ROW_SAMPLE, np.concatenate(trainingLabelsBV.values())) model.save(args.directoryName + "/modelBV.xml") print "Training Pedestrian-Cyclist Model" -model = ml.SVM() -model.train(np.concatenate(trainingSamplesPB.values()), np.concatenate(trainingLabelsPB.values()), args.svmType, args.kernelType) +model = ml.SVM(args.svmType, args.kernelType) +model.train(np.concatenate(trainingSamplesPB.values()), ROW_SAMPLE, np.concatenate(trainingLabelsPB.values())) model.save(args.directoryName + "/modelPB.xml") print "Training Pedestrian-Vehicle Model" -model = ml.SVM() -model.train(np.concatenate(trainingSamplesPV.values()), np.concatenate(trainingLabelsPV.values()), args.svmType, args.kernelType) +model = ml.SVM(args.svmType, args.kernelType) +model.train(np.concatenate(trainingSamplesPV.values()), ROW_SAMPLE, np.concatenate(trainingLabelsPV.values())) model.save(args.directoryName + "/modelPV.xml")