Mercurial Hosting > traffic-intelligence
comparison scripts/classify-objects.py @ 683:f3a0b652b590 dev
added function to display the speed distributions and optimize a little going through objects
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 05 Jun 2015 11:04:06 +0200 |
parents | fbe29be25501 |
children | 6d2ece4aed7c |
comparison
equal
deleted
inserted
replaced
682:fbe29be25501 | 683:f3a0b652b590 |
---|---|
10 | 10 |
11 # TODO add mode detection live | 11 # TODO add mode detection live |
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('--plot-speed-distributions', dest = 'plotSpeedDistribution', help = 'simply plots the distributions used for each user type', action = 'store_true') | |
16 parser.add_argument('--max-speed-distribution-plot', dest = 'maxSpeedDistributionPlot', help = 'if plotting the user distributions, the maximum speed to display', type = float, default = 50.) | |
15 #parser.add_argument('-u', dest = 'undistort', help = 'undistort the video (because features have been extracted that way)', action = 'store_true') | 17 #parser.add_argument('-u', dest = 'undistort', help = 'undistort the video (because features have been extracted that way)', action = 'store_true') |
16 #parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', type = int) | 18 #parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', type = int) |
17 #parser.add_argument('--last-frame', dest = 'lastFrameNum', help = 'number of last frame number to save (for image saving, no display is made)', type = int) | 19 #parser.add_argument('--last-frame', dest = 'lastFrameNum', help = 'number of last frame number to save (for image saving, no display is made)', type = int) |
18 # parser.add_argument('--min-speed-equiprobable', dest = 'minSpeedEquiprobable', help = 'speed value below which all classes are equiprobable (distributions give odd values there) (km/h)', type = float, default = 3.33) | 20 # parser.add_argument('--min-speed-equiprobable', dest = 'minSpeedEquiprobable', help = 'speed value below which all classes are equiprobable (distributions give odd values there) (km/h)', type = float, default = 3.33) |
19 # parser.add_argument('--speed-aggregation', dest = 'speedAggregationMethod', help = 'method to aggregate road user speed', type = str, choices = ['median', 'mean', 'quantile'], default = 'median') | 21 # parser.add_argument('--speed-aggregation', dest = 'speedAggregationMethod', help = 'method to aggregate road user speed', type = str, choices = ['median', 'mean', 'quantile'], default = 'median') |
21 | 23 |
22 args = parser.parse_args() | 24 args = parser.parse_args() |
23 params = storage.ProcessParameters(args.configFilename) | 25 params = storage.ProcessParameters(args.configFilename) |
24 | 26 |
25 params.convertToFrames(3.6) | 27 params.convertToFrames(3.6) |
26 invHomography = np.linalg.inv(params.homography) | 28 if params.homography is not None: |
29 invHomography = np.linalg.inv(params.homography) | |
27 | 30 |
28 if params.speedAggregationMethod == 'median': | 31 if params.speedAggregationMethod == 'median': |
29 speedAggregationFunc = np.median | 32 speedAggregationFunc = np.median |
30 elif params.speedAggregationMethod == 'mean': | 33 elif params.speedAggregationMethod == 'mean': |
31 speedAggregationFunc = np.mean | 34 speedAggregationFunc = np.mean |
32 elif params.speedAggregationMethod == 'quantile': | 35 elif params.speedAggregationMethod == 'quantile': |
33 speedAggregationFunc = lambda speeds: np.percentile(speeds, args.speedAggregationQuantile) | 36 speedAggregationFunc = lambda speeds: np.percentile(speeds, args.speedAggregationQuantile) |
34 else: | 37 else: |
35 print('Unknown speed aggregation method: {}. Exiting'.format(params.speedAggregationMethod)) | 38 print('Unknown speed aggregation method: {}. Exiting'.format(params.speedAggregationMethod)) |
36 from sys import exit | 39 sys.exit() |
37 exit() | |
38 | 40 |
39 pedBikeCarSVM = ml.SVM() | 41 pedBikeCarSVM = ml.SVM() |
40 pedBikeCarSVM.load(params.pedBikeCarSVMFilename) | 42 pedBikeCarSVM.load(params.pedBikeCarSVMFilename) |
41 bikeCarSVM = ml.SVM() | 43 bikeCarSVM = ml.SVM() |
42 bikeCarSVM.load(params.bikeCarSVMFilename) | 44 bikeCarSVM.load(params.bikeCarSVMFilename) |
44 # log logistic for ped and bik otherwise ((pedBeta/pedAlfa)*((sMean/pedAlfa)**(pedBeta-1)))/((1+(sMean/pedAlfa)**pedBeta)**2.) | 46 # log logistic for ped and bik otherwise ((pedBeta/pedAlfa)*((sMean/pedAlfa)**(pedBeta-1)))/((1+(sMean/pedAlfa)**pedBeta)**2.) |
45 speedProbabilities = {'car': lambda s: norm(params.meanVehicleSpeed, params.stdVehicleSpeed).pdf(s), | 47 speedProbabilities = {'car': lambda s: norm(params.meanVehicleSpeed, params.stdVehicleSpeed).pdf(s), |
46 'pedestrian': lambda s: norm(params.meanPedestrianSpeed, params.stdPedestrianSpeed).pdf(s), | 48 'pedestrian': lambda s: norm(params.meanPedestrianSpeed, params.stdPedestrianSpeed).pdf(s), |
47 'bicycle': lambda s: lognorm(params.scaleCyclistSpeed, loc = 0., scale = np.exp(params.locationCyclistSpeed)).pdf(s)} # lognorm shape, loc, scale | 49 'bicycle': lambda s: lognorm(params.scaleCyclistSpeed, loc = 0., scale = np.exp(params.locationCyclistSpeed)).pdf(s)} # lognorm shape, loc, scale |
48 | 50 |
49 def plotSpeedProbabilities(): | 51 if args.plotSpeedDistribution: |
52 import matplotlib.pyplot as plt | |
53 plt.figure() | |
50 for k in speedProbabilities: | 54 for k in speedProbabilities: |
51 plot(arange(0.1, 50, 0.1), [speedProbabilities[k](s/3.6/25) for s in arange(0.1, 50, 0.1)]) | 55 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) |
56 plt.xlabel('Speed (km/h)') | |
57 plt.ylabel('Probability') | |
58 plt.legend() | |
59 plt.title('Probability Density Function') | |
60 plt.show() | |
61 sys.exit() | |
52 | 62 |
53 objects = storage.loadTrajectoriesFromSqlite(params.databaseFilename, 'object') | 63 objects = storage.loadTrajectoriesFromSqlite(params.databaseFilename, 'object') |
54 features = storage.loadTrajectoriesFromSqlite(params.databaseFilename, 'feature') | 64 features = storage.loadTrajectoriesFromSqlite(params.databaseFilename, 'feature') |
55 intervals = [] | 65 intervals = [] |
56 for obj in objects: | 66 for obj in objects: |
73 while ret and frameNum <= lastFrameNum: | 83 while ret and frameNum <= lastFrameNum: |
74 ret, img = capture.read() | 84 ret, img = capture.read() |
75 if ret: | 85 if ret: |
76 if frameNum%50 == 0: | 86 if frameNum%50 == 0: |
77 print('frame number: {}'.format(frameNum)) | 87 print('frame number: {}'.format(frameNum)) |
88 currentObjects = [] | |
89 for obj in objects: | |
90 if obj.getLastFrameNum() < frameNum: | |
91 obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = params.minSpeedEquiprobable, speedProbabilities = speedProbabilities) | |
92 else: | |
93 currentObjects.append(obj) | |
94 objects = currentObjects | |
78 if params.undistort: | 95 if params.undistort: |
79 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) | 96 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) |
80 for obj in objects: | 97 for obj in objects: |
81 if obj.existsAtInstant(frameNum): | 98 if obj.existsAtInstant(frameNum): |
82 if obj.getFirstInstant() == frameNum: | 99 if obj.getFirstInstant() == frameNum: |
83 print 'first frame for obj {}'.format(obj.getNum()) | |
84 obj.initClassifyUserTypeHoGSVM(speedAggregationFunc, pedBikeCarSVM, bikeCarSVM, params.maxPedestrianSpeed, params.maxCyclistSpeed, params.nFramesIgnoreAtEnds) | 100 obj.initClassifyUserTypeHoGSVM(speedAggregationFunc, pedBikeCarSVM, bikeCarSVM, params.maxPedestrianSpeed, params.maxCyclistSpeed, params.nFramesIgnoreAtEnds) |
85 obj.classifyUserTypeHoGSVMAtInstant(img, frameNum, invHomography, width, height, 0.2, 0.2, 800) # px, py, pixelThreshold | 101 obj.classifyUserTypeHoGSVMAtInstant(img, frameNum, invHomography, width, height, 0.2, 0.2, 800) # px, py, pixelThreshold |
86 frameNum += 1 | 102 frameNum += 1 |
87 | 103 |
88 for obj in objects: | 104 for obj in objects: |