diff scripts/classify-objects.py @ 854:33d296984dd8

rework and more info on speed probabilities for classification
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 22 Sep 2016 17:50:35 -0400
parents b9ec0cc2677d
children ff92801e5c54
line wrap: on
line diff
--- a/scripts/classify-objects.py	Thu Sep 22 16:49:43 2016 -0400
+++ b/scripts/classify-objects.py	Thu Sep 22 17:50:35 2016 -0400
@@ -16,11 +16,12 @@
 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)
 parser.add_argument('--plot-speed-distributions', dest = 'plotSpeedDistribution', help = 'simply plots the distributions used for each user type', action = 'store_true')
-parser.add_argument('--max-speed-distribution-plot', dest = 'maxSpeedDistributionPlot', help = 'if plotting the user distributions, the maximum speed to display', type = float, default = 50.)
+parser.add_argument('--max-speed-distribution-plot', dest = 'maxSpeedDistributionPlot', help = 'if plotting the user distributions, the maximum speed to display (km/h)', type = float, default = 50.)
 
 args = parser.parse_args()
 params = storage.ProcessParameters(args.configFilename)
 classifierParams = storage.ClassifierParameters(params.classifierFilename)
+classifierParams.convertToFrames(params.videoFrameRate, 3.6) # conversion from km/h to m/frame
 
 if args.videoFilename is not None:
     videoFilename = args.videoFilename
@@ -31,7 +32,6 @@
 else:
     databaseFilename = params.databaseFilename
 
-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:
@@ -53,15 +53,24 @@
 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(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)
+carNorm = norm(classifierParams.meanVehicleSpeed, classifierParams.stdVehicleSpeed)
+pedNorm = norm(classifierParams.meanPedestrianSpeed, classifierParams.stdPedestrianSpeed)
+# numpy lognorm shape, loc, scale: shape for numpy is scale (std of the normal) and scale for numpy is exp(location) (loc=mean of the normal)
+bicLogNorm = lognorm(classifierParams.scaleCyclistSpeed, loc = 0., scale = np.exp(classifierParams.locationCyclistSpeed))
+speedProbabilities = {'car': lambda s: carNorm.pdf(s),
+                      'pedestrian': lambda s: pedNorm.pdf(s), 
+                      'bicycle': lambda s: bicLogNorm.pdf(s)}
 
 if args.plotSpeedDistribution:
     import matplotlib.pyplot as plt
     plt.figure()
     for k in speedProbabilities:
-        plt.plot(np.arange(0.1, args.maxSpeedDistributionPlot, 0.1), [speedProbabilities[k](s/3.6/25) for s in np.arange(0.1, args.maxSpeedDistributionPlot, 0.1)], label = k)
+        plt.plot(np.arange(0.1, args.maxSpeedDistributionPlot, 0.1), [speedProbabilities[k](s/(3.6*params.videoFrameRate)) for s in np.arange(0.1, args.maxSpeedDistributionPlot, 0.1)], label = k)
+    maxProb = -1.
+    for k in speedProbabilities:
+        maxProb = max(maxProb, np.max([speedProbabilities[k](s/(3.6*params.videoFrameRate)) for s in np.arange(0.1, args.maxSpeedDistributionPlot, 0.1)]))
+    plt.plot([classifierParams.minSpeedEquiprobable*3.6*params.videoFrameRate]*2, [0., maxProb], 'k-')
+    plt.text(classifierParams.minSpeedEquiprobable*3.6*params.videoFrameRate, maxProb, 'threshold for equiprobable class')
     plt.xlabel('Speed (km/h)')
     plt.ylabel('Probability')
     plt.legend()