Mercurial Hosting > traffic-intelligence
comparison scripts/classify-objects.py @ 708:a37c565f4b68
merged dev
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 22 Jul 2015 14:17:44 -0400 |
parents | de278c5e65f6 |
children | 5b970a5bc233 |
comparison
equal
deleted
inserted
replaced
707:7efa36b9bcfd | 708:a37c565f4b68 |
---|---|
1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
2 | |
3 import cvutils, moving, ml, storage | |
2 | 4 |
3 import numpy as np | 5 import numpy as np |
4 import sys, argparse | 6 import sys, argparse |
5 from cv2 import SVM_RBF, SVM_C_SVC | 7 #from cv2 import SVM_RBF, SVM_C_SVC |
8 import cv2 | |
9 from scipy.stats import norm, lognorm | |
6 | 10 |
7 import cvutils, moving, ml | 11 # TODO add mode detection live |
8 | 12 |
9 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') |
10 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) |
11 parser.add_argument('-d', dest = 'directoryName', help = 'name of the parent directory containing the videos and extracted trajectories to process', required = True) | 15 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file (overrides the configuration file)') |
12 # parser.add_argument('-o', dest = 'homographyFilename', help = 'name of the image to world homography file') | 16 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (overrides the configuration file)') |
13 # need a classification config file for speed distribution parameters, svm models, frequency parameters, area parameters etc | 17 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to classify', type = int, default = None) |
14 #parser.add_argument('--cfg', dest = 'svmType', help = 'SVM type', default = SVM_C_SVC, type = long) | 18 parser.add_argument('--plot-speed-distributions', dest = 'plotSpeedDistribution', help = 'simply plots the distributions used for each user type', action = 'store_true') |
15 | 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.) |
16 | |
17 #parser.add_argument('-s', dest = 'rescaleSize', help = 'rescale size of image samples', default = 64, type = int) | |
18 #parser.add_argument('-o', dest = 'nOrientations', help = 'number of orientations in HoG', default = 9, type = int) | |
19 #parser.add_argument('-p', dest = 'nPixelsPerCell', help = 'number of pixels per cell', default = 8, type = int) | |
20 #parser.add_argument('-c', dest = 'nCellsPerBlock', help = 'number of cells per block', default = 2, type = int) | |
21 | 20 |
22 args = parser.parse_args() | 21 args = parser.parse_args() |
23 params = storage.ProcessParameters(args.configFilename) | 22 params = storage.ProcessParameters(args.configFilename) |
23 | |
24 if args.videoFilename is not None: | |
25 videoFilename = args.videoFilename | |
26 else: | |
27 videoFilename = params.videoFilename | |
28 if args.databaseFilename is not None: | |
29 databaseFilename = args.databaseFilename | |
30 else: | |
31 databaseFilename = params.databaseFilename | |
32 | |
33 params.convertToFrames(3.6) | |
34 if params.homography is not None: | |
35 invHomography = np.linalg.inv(params.homography) | |
36 | |
37 if params.speedAggregationMethod == 'median': | |
38 speedAggregationFunc = np.median | |
39 elif params.speedAggregationMethod == 'mean': | |
40 speedAggregationFunc = np.mean | |
41 elif params.speedAggregationMethod == 'quantile': | |
42 speedAggregationFunc = lambda speeds: np.percentile(speeds, args.speedAggregationQuantile) | |
43 else: | |
44 print('Unknown speed aggregation method: {}. Exiting'.format(params.speedAggregationMethod)) | |
45 sys.exit() | |
46 | |
47 pedBikeCarSVM = ml.SVM() | |
48 pedBikeCarSVM.load(params.pedBikeCarSVMFilename) | |
49 bikeCarSVM = ml.SVM() | |
50 bikeCarSVM.load(params.bikeCarSVMFilename) | |
51 | |
52 # log logistic for ped and bik otherwise ((pedBeta/pedAlfa)*((sMean/pedAlfa)**(pedBeta-1)))/((1+(sMean/pedAlfa)**pedBeta)**2.) | |
53 speedProbabilities = {'car': lambda s: norm(params.meanVehicleSpeed, params.stdVehicleSpeed).pdf(s), | |
54 'pedestrian': lambda s: norm(params.meanPedestrianSpeed, params.stdPedestrianSpeed).pdf(s), | |
55 '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) | |
56 | |
57 if args.plotSpeedDistribution: | |
58 import matplotlib.pyplot as plt | |
59 plt.figure() | |
60 for k in speedProbabilities: | |
61 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) | |
62 plt.xlabel('Speed (km/h)') | |
63 plt.ylabel('Probability') | |
64 plt.legend() | |
65 plt.title('Probability Density Function') | |
66 plt.show() | |
67 sys.exit() | |
68 | |
69 objects = storage.loadTrajectoriesFromSqlite(databaseFilename, 'object', args.nObjects, withFeatures = True) | |
70 #features = storage.loadTrajectoriesFromSqlite(databaseFilename, 'feature') | |
71 intervals = [] | |
72 for obj in objects: | |
73 #obj.setFeatures(features) | |
74 intervals.append(obj.getTimeInterval()) | |
75 timeInterval = moving.unionIntervals(intervals) | |
76 | |
77 capture = cv2.VideoCapture(videoFilename) | |
78 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) | |
79 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) | |
80 | |
81 pastObjects = [] | |
82 if params.undistort: # setup undistortion | |
83 [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients) | |
84 if capture.isOpened(): | |
85 ret = True | |
86 frameNum = timeInterval.first | |
87 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) | |
88 lastFrameNum = timeInterval.last | |
89 | |
90 while ret and frameNum <= lastFrameNum: | |
91 ret, img = capture.read() | |
92 if ret: | |
93 if frameNum%50 == 0: | |
94 print('frame number: {}'.format(frameNum)) | |
95 currentObjects = [] | |
96 for obj in objects: | |
97 if obj.getLastInstant() < frameNum: | |
98 obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = params.minSpeedEquiprobable, speedProbabilities = speedProbabilities) | |
99 pastObjects.append(obj) | |
100 else: | |
101 currentObjects.append(obj) | |
102 objects = currentObjects | |
103 if params.undistort: | |
104 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) | |
105 for obj in objects: | |
106 if obj.existsAtInstant(frameNum): | |
107 if obj.getFirstInstant() == frameNum: | |
108 obj.initClassifyUserTypeHoGSVM(speedAggregationFunc, pedBikeCarSVM, bikeCarSVM, params.maxPedestrianSpeed, params.maxCyclistSpeed, params.nFramesIgnoreAtEnds) | |
109 obj.classifyUserTypeHoGSVMAtInstant(img, frameNum, invHomography, width, height, 0.2, 0.2, 800) # px, py, pixelThreshold | |
110 frameNum += 1 | |
111 | |
112 for obj in objects: | |
113 obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = params.minSpeedEquiprobable, speedProbabilities = speedProbabilities) | |
114 pastObjects.append(obj) | |
115 print('Saving user types') | |
116 storage.setRoadUserTypes(databaseFilename, pastObjects) |