diff scripts/classify-objects.py @ 812:21f10332c72b

moved the classification parameters from tracking.cfg to a new classifier.cfg and made all classification parameters apparent
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 10 Jun 2016 17:07:36 -0400
parents 52aa03260f03
children b9ec0cc2677d
line wrap: on
line diff
--- a/scripts/classify-objects.py	Fri Jun 10 15:44:08 2016 -0400
+++ b/scripts/classify-objects.py	Fri Jun 10 17:07:36 2016 -0400
@@ -12,8 +12,6 @@
 
 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 = cv2.SVM_RBF, type = long)
-parser.add_argument('--svm', dest = 'svmType', help = 'SVM type', default = cv2.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)
@@ -22,6 +20,7 @@
 
 args = parser.parse_args()
 params = storage.ProcessParameters(args.configFilename)
+classifierParams = storage.ClassifierParameters(params.classifierFilename)
 
 if args.videoFilename is not None:
     videoFilename = args.videoFilename
@@ -32,31 +31,31 @@
 else:
     databaseFilename = params.databaseFilename
 
-params.convertToFrames(3.6)
+classifierParams.convertToFrames(params.videoFrameRate, 3.6) # conversion from km/h to m/s
 if params.homography is not None:
     invHomography = np.linalg.inv(params.homography)
 else:
     invHomography = None
 
-if params.speedAggregationMethod == 'median':
+if classifierParams.speedAggregationMethod == 'median':
     speedAggregationFunc = np.median
-elif params.speedAggregationMethod == 'mean':
+elif classifierParams.speedAggregationMethod == 'mean':
     speedAggregationFunc = np.mean
-elif params.speedAggregationMethod == 'quantile':
+elif classifierParams.speedAggregationMethod == 'quantile':
     speedAggregationFunc = lambda speeds: np.percentile(speeds, args.speedAggregationQuantile)
 else:
-    print('Unknown speed aggregation method: {}. Exiting'.format(params.speedAggregationMethod))
+    print('Unknown speed aggregation method: {}. Exiting'.format(classifierParams.speedAggregationMethod))
     sys.exit()
 
-pedBikeCarSVM = ml.SVM(args.svmType, args.kernelType)
-pedBikeCarSVM.load(params.pedBikeCarSVMFilename)
-bikeCarSVM = ml.SVM(args.svmType, args.kernelType)
-bikeCarSVM.load(params.bikeCarSVMFilename)
+pedBikeCarSVM = ml.SVM()
+pedBikeCarSVM.load(classifierParams.pedBikeCarSVMFilename)
+bikeCarSVM = ml.SVM()
+bikeCarSVM.load(classifierParams.bikeCarSVMFilename)
 
 # log logistic for ped and bik otherwise ((pedBeta/pedAlfa)*((sMean/pedAlfa)**(pedBeta-1)))/((1+(sMean/pedAlfa)**pedBeta)**2.)
-speedProbabilities = {'car': lambda s: norm(params.meanVehicleSpeed, params.stdVehicleSpeed).pdf(s),
-                      'pedestrian': lambda s: norm(params.meanPedestrianSpeed, params.stdPedestrianSpeed).pdf(s), 
-                      'bicycle': lambda s: lognorm(params.scaleCyclistSpeed, loc = 0., scale = np.exp(params.locationCyclistSpeed)).pdf(s)} # numpy lognorm shape, loc, scale: shape for numpy is scale (std of the normal) and scale for numpy is location (mean of the normal)
+speedProbabilities = {'car': lambda s: norm(classifierParams.meanVehicleSpeed, classifierParams.stdVehicleSpeed).pdf(s),
+                      'pedestrian': lambda s: norm(classifierParams.meanPedestrianSpeed, classifierParams.stdPedestrianSpeed).pdf(s), 
+                      'bicycle': lambda s: lognorm(classifierParams.scaleCyclistSpeed, loc = 0., scale = np.exp(classifierParams.locationCyclistSpeed)).pdf(s)} # numpy lognorm shape, loc, scale: shape for numpy is scale (std of the normal) and scale for numpy is location (mean of the normal)
 
 if args.plotSpeedDistribution:
     import matplotlib.pyplot as plt
@@ -96,25 +95,26 @@
         if ret:
             if frameNum%50 == 0:
                 print('frame number: {}'.format(frameNum))
-                currentObjects = []
-                for obj in objects:
-                    if obj.getLastInstant() < frameNum:
-                        obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = params.minSpeedEquiprobable, speedProbabilities = speedProbabilities)
+            if params.undistort:
+                img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)
+            currentObjects = []
+            for obj in objects:
+                inter = obj.getTimeInterval()
+                if inter.contains(frameNum):
+                    if inter.first == frameNum:
+                        obj.initClassifyUserTypeHoGSVM(speedAggregationFunc, pedBikeCarSVM, bikeCarSVM, classifierParams.maxPedestrianSpeed, classifierParams.maxCyclistSpeed, classifierParams.nFramesIgnoreAtEnds)
+                        currentObjects.append(obj)
+                    elif inter.last == frameNum:
+                        obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = classifierParams.minSpeedEquiprobable, speedProbabilities = speedProbabilities)
                         pastObjects.append(obj)
                     else:
+                        obj.classifyUserTypeHoGSVMAtInstant(img, frameNum, invHomography, width, height, classifierParams.percentIncreaseCrop, classifierParams.percentIncreaseCrop, classifierParams.minNPixels, classifierParams.hogRescaleSize, classifierParams.hogNOrientations, classifierParams.hogNPixelsPerCell, classifierParams.hogNCellsPerBlock)
                         currentObjects.append(obj)
-                objects = currentObjects
-            if params.undistort:
-                img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)
-            for obj in objects:
-                if obj.existsAtInstant(frameNum):
-                    if obj.getFirstInstant() == frameNum:
-                        obj.initClassifyUserTypeHoGSVM(speedAggregationFunc, pedBikeCarSVM, bikeCarSVM, params.maxPedestrianSpeed, params.maxCyclistSpeed, params.nFramesIgnoreAtEnds)
-                    obj.classifyUserTypeHoGSVMAtInstant(img, frameNum, invHomography, width, height, 0.2, 0.2, 800) # px, py, pixelThreshold
+            objects = currentObjects
         frameNum += 1
     
     for obj in objects:
-        obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = params.minSpeedEquiprobable, speedProbabilities = speedProbabilities)
+        obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = classifierParams.minSpeedEquiprobable, speedProbabilities = speedProbabilities)
         pastObjects.append(obj)
     print('Saving user types')
     storage.setRoadUserTypes(databaseFilename, pastObjects)