comparison 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
comparison
equal deleted inserted replaced
810:082a5c2685f4 812:21f10332c72b
10 10
11 # TODO add mode detection live, add choice of kernel and svm type (to be saved in future classifier format) 11 # TODO add mode detection live, add choice of kernel and svm type (to be saved in future classifier format)
12 12
13 parser = argparse.ArgumentParser(description='The program processes indicators for all pairs of road users in the scene') 13 parser = argparse.ArgumentParser(description='The program processes indicators for all pairs of road users in the scene')
14 parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file', required = True) 14 parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file', required = True)
15 parser.add_argument('--kernel', dest = 'kernelType', help = 'kernel type for the support vector machine (SVM)', default = cv2.SVM_RBF, type = long)
16 parser.add_argument('--svm', dest = 'svmType', help = 'SVM type', default = cv2.SVM_C_SVC, type = long)
17 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file (overrides the configuration file)') 15 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file (overrides the configuration file)')
18 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (overrides the configuration file)') 16 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (overrides the configuration file)')
19 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to classify', type = int, default = None) 17 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to classify', type = int, default = None)
20 parser.add_argument('--plot-speed-distributions', dest = 'plotSpeedDistribution', help = 'simply plots the distributions used for each user type', action = 'store_true') 18 parser.add_argument('--plot-speed-distributions', dest = 'plotSpeedDistribution', help = 'simply plots the distributions used for each user type', action = 'store_true')
21 parser.add_argument('--max-speed-distribution-plot', dest = 'maxSpeedDistributionPlot', help = 'if plotting the user distributions, the maximum speed to display', type = float, default = 50.) 19 parser.add_argument('--max-speed-distribution-plot', dest = 'maxSpeedDistributionPlot', help = 'if plotting the user distributions, the maximum speed to display', type = float, default = 50.)
22 20
23 args = parser.parse_args() 21 args = parser.parse_args()
24 params = storage.ProcessParameters(args.configFilename) 22 params = storage.ProcessParameters(args.configFilename)
23 classifierParams = storage.ClassifierParameters(params.classifierFilename)
25 24
26 if args.videoFilename is not None: 25 if args.videoFilename is not None:
27 videoFilename = args.videoFilename 26 videoFilename = args.videoFilename
28 else: 27 else:
29 videoFilename = params.videoFilename 28 videoFilename = params.videoFilename
30 if args.databaseFilename is not None: 29 if args.databaseFilename is not None:
31 databaseFilename = args.databaseFilename 30 databaseFilename = args.databaseFilename
32 else: 31 else:
33 databaseFilename = params.databaseFilename 32 databaseFilename = params.databaseFilename
34 33
35 params.convertToFrames(3.6) 34 classifierParams.convertToFrames(params.videoFrameRate, 3.6) # conversion from km/h to m/s
36 if params.homography is not None: 35 if params.homography is not None:
37 invHomography = np.linalg.inv(params.homography) 36 invHomography = np.linalg.inv(params.homography)
38 else: 37 else:
39 invHomography = None 38 invHomography = None
40 39
41 if params.speedAggregationMethod == 'median': 40 if classifierParams.speedAggregationMethod == 'median':
42 speedAggregationFunc = np.median 41 speedAggregationFunc = np.median
43 elif params.speedAggregationMethod == 'mean': 42 elif classifierParams.speedAggregationMethod == 'mean':
44 speedAggregationFunc = np.mean 43 speedAggregationFunc = np.mean
45 elif params.speedAggregationMethod == 'quantile': 44 elif classifierParams.speedAggregationMethod == 'quantile':
46 speedAggregationFunc = lambda speeds: np.percentile(speeds, args.speedAggregationQuantile) 45 speedAggregationFunc = lambda speeds: np.percentile(speeds, args.speedAggregationQuantile)
47 else: 46 else:
48 print('Unknown speed aggregation method: {}. Exiting'.format(params.speedAggregationMethod)) 47 print('Unknown speed aggregation method: {}. Exiting'.format(classifierParams.speedAggregationMethod))
49 sys.exit() 48 sys.exit()
50 49
51 pedBikeCarSVM = ml.SVM(args.svmType, args.kernelType) 50 pedBikeCarSVM = ml.SVM()
52 pedBikeCarSVM.load(params.pedBikeCarSVMFilename) 51 pedBikeCarSVM.load(classifierParams.pedBikeCarSVMFilename)
53 bikeCarSVM = ml.SVM(args.svmType, args.kernelType) 52 bikeCarSVM = ml.SVM()
54 bikeCarSVM.load(params.bikeCarSVMFilename) 53 bikeCarSVM.load(classifierParams.bikeCarSVMFilename)
55 54
56 # log logistic for ped and bik otherwise ((pedBeta/pedAlfa)*((sMean/pedAlfa)**(pedBeta-1)))/((1+(sMean/pedAlfa)**pedBeta)**2.) 55 # log logistic for ped and bik otherwise ((pedBeta/pedAlfa)*((sMean/pedAlfa)**(pedBeta-1)))/((1+(sMean/pedAlfa)**pedBeta)**2.)
57 speedProbabilities = {'car': lambda s: norm(params.meanVehicleSpeed, params.stdVehicleSpeed).pdf(s), 56 speedProbabilities = {'car': lambda s: norm(classifierParams.meanVehicleSpeed, classifierParams.stdVehicleSpeed).pdf(s),
58 'pedestrian': lambda s: norm(params.meanPedestrianSpeed, params.stdPedestrianSpeed).pdf(s), 57 'pedestrian': lambda s: norm(classifierParams.meanPedestrianSpeed, classifierParams.stdPedestrianSpeed).pdf(s),
59 '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) 58 '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)
60 59
61 if args.plotSpeedDistribution: 60 if args.plotSpeedDistribution:
62 import matplotlib.pyplot as plt 61 import matplotlib.pyplot as plt
63 plt.figure() 62 plt.figure()
64 for k in speedProbabilities: 63 for k in speedProbabilities:
94 while ret and frameNum <= lastFrameNum: 93 while ret and frameNum <= lastFrameNum:
95 ret, img = capture.read() 94 ret, img = capture.read()
96 if ret: 95 if ret:
97 if frameNum%50 == 0: 96 if frameNum%50 == 0:
98 print('frame number: {}'.format(frameNum)) 97 print('frame number: {}'.format(frameNum))
99 currentObjects = [] 98 if params.undistort:
100 for obj in objects: 99 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)
101 if obj.getLastInstant() < frameNum: 100 currentObjects = []
102 obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = params.minSpeedEquiprobable, speedProbabilities = speedProbabilities) 101 for obj in objects:
102 inter = obj.getTimeInterval()
103 if inter.contains(frameNum):
104 if inter.first == frameNum:
105 obj.initClassifyUserTypeHoGSVM(speedAggregationFunc, pedBikeCarSVM, bikeCarSVM, classifierParams.maxPedestrianSpeed, classifierParams.maxCyclistSpeed, classifierParams.nFramesIgnoreAtEnds)
106 currentObjects.append(obj)
107 elif inter.last == frameNum:
108 obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = classifierParams.minSpeedEquiprobable, speedProbabilities = speedProbabilities)
103 pastObjects.append(obj) 109 pastObjects.append(obj)
104 else: 110 else:
111 obj.classifyUserTypeHoGSVMAtInstant(img, frameNum, invHomography, width, height, classifierParams.percentIncreaseCrop, classifierParams.percentIncreaseCrop, classifierParams.minNPixels, classifierParams.hogRescaleSize, classifierParams.hogNOrientations, classifierParams.hogNPixelsPerCell, classifierParams.hogNCellsPerBlock)
105 currentObjects.append(obj) 112 currentObjects.append(obj)
106 objects = currentObjects 113 objects = currentObjects
107 if params.undistort:
108 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)
109 for obj in objects:
110 if obj.existsAtInstant(frameNum):
111 if obj.getFirstInstant() == frameNum:
112 obj.initClassifyUserTypeHoGSVM(speedAggregationFunc, pedBikeCarSVM, bikeCarSVM, params.maxPedestrianSpeed, params.maxCyclistSpeed, params.nFramesIgnoreAtEnds)
113 obj.classifyUserTypeHoGSVMAtInstant(img, frameNum, invHomography, width, height, 0.2, 0.2, 800) # px, py, pixelThreshold
114 frameNum += 1 114 frameNum += 1
115 115
116 for obj in objects: 116 for obj in objects:
117 obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = params.minSpeedEquiprobable, speedProbabilities = speedProbabilities) 117 obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = classifierParams.minSpeedEquiprobable, speedProbabilities = speedProbabilities)
118 pastObjects.append(obj) 118 pastObjects.append(obj)
119 print('Saving user types') 119 print('Saving user types')
120 storage.setRoadUserTypes(databaseFilename, pastObjects) 120 storage.setRoadUserTypes(databaseFilename, pastObjects)