comparison scripts/classify-objects.py @ 795:a34ec862371f

merged with dev branch
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 09 May 2016 15:33:11 -0400
parents 8eb8a6bd70e8
children 52aa03260f03
comparison
equal deleted inserted replaced
758:0a05883216cf 795:a34ec862371f
2 2
3 import cvutils, moving, ml, storage 3 import cvutils, moving, ml, storage
4 4
5 import numpy as np 5 import numpy as np
6 import sys, argparse 6 import sys, argparse
7 #from cv2 import SVM_RBF, SVM_C_SVC 7 from cv2.ml import SVM_RBF, SVM_C_SVC
8 import cv2 8 import cv2
9 from scipy.stats import norm, lognorm 9 from scipy.stats import norm, lognorm
10 10
11 # TODO add mode detection live 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 = SVM_RBF, type = long)
16 parser.add_argument('--svm', dest = 'svmType', help = 'SVM type', default = SVM_C_SVC, type = long)
15 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file (overrides the configuration file)') 17 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file (overrides the configuration file)')
16 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (overrides the configuration file)') 18 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (overrides the configuration file)')
17 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to classify', type = int, default = None) 19 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to classify', type = int, default = None)
18 parser.add_argument('--plot-speed-distributions', dest = 'plotSpeedDistribution', help = 'simply plots the distributions used for each user type', action = 'store_true') 20 parser.add_argument('--plot-speed-distributions', dest = 'plotSpeedDistribution', help = 'simply plots the distributions used for each user type', action = 'store_true')
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.) 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.)
31 databaseFilename = params.databaseFilename 33 databaseFilename = params.databaseFilename
32 34
33 params.convertToFrames(3.6) 35 params.convertToFrames(3.6)
34 if params.homography is not None: 36 if params.homography is not None:
35 invHomography = np.linalg.inv(params.homography) 37 invHomography = np.linalg.inv(params.homography)
38 else:
39 invHomography = None
36 40
37 if params.speedAggregationMethod == 'median': 41 if params.speedAggregationMethod == 'median':
38 speedAggregationFunc = np.median 42 speedAggregationFunc = np.median
39 elif params.speedAggregationMethod == 'mean': 43 elif params.speedAggregationMethod == 'mean':
40 speedAggregationFunc = np.mean 44 speedAggregationFunc = np.mean
42 speedAggregationFunc = lambda speeds: np.percentile(speeds, args.speedAggregationQuantile) 46 speedAggregationFunc = lambda speeds: np.percentile(speeds, args.speedAggregationQuantile)
43 else: 47 else:
44 print('Unknown speed aggregation method: {}. Exiting'.format(params.speedAggregationMethod)) 48 print('Unknown speed aggregation method: {}. Exiting'.format(params.speedAggregationMethod))
45 sys.exit() 49 sys.exit()
46 50
47 pedBikeCarSVM = ml.SVM() 51 pedBikeCarSVM = ml.SVM(args.svmType, args.kernelType)
48 pedBikeCarSVM.load(params.pedBikeCarSVMFilename) 52 pedBikeCarSVM.load(params.pedBikeCarSVMFilename)
49 bikeCarSVM = ml.SVM() 53 bikeCarSVM = ml.SVM(args.svmType, args.kernelType)
50 bikeCarSVM.load(params.bikeCarSVMFilename) 54 bikeCarSVM.load(params.bikeCarSVMFilename)
51 55
52 # log logistic for ped and bik otherwise ((pedBeta/pedAlfa)*((sMean/pedAlfa)**(pedBeta-1)))/((1+(sMean/pedAlfa)**pedBeta)**2.) 56 # 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), 57 speedProbabilities = {'car': lambda s: norm(params.meanVehicleSpeed, params.stdVehicleSpeed).pdf(s),
54 'pedestrian': lambda s: norm(params.meanPedestrianSpeed, params.stdPedestrianSpeed).pdf(s), 58 'pedestrian': lambda s: norm(params.meanPedestrianSpeed, params.stdPedestrianSpeed).pdf(s),
70 #features = storage.loadTrajectoriesFromSqlite(databaseFilename, 'feature') 74 #features = storage.loadTrajectoriesFromSqlite(databaseFilename, 'feature')
71 intervals = [] 75 intervals = []
72 for obj in objects: 76 for obj in objects:
73 #obj.setFeatures(features) 77 #obj.setFeatures(features)
74 intervals.append(obj.getTimeInterval()) 78 intervals.append(obj.getTimeInterval())
75 timeInterval = moving.unionIntervals(intervals) 79 timeInterval = moving.TimeInterval.unionIntervals(intervals)
76 80
77 capture = cv2.VideoCapture(videoFilename) 81 capture = cv2.VideoCapture(videoFilename)
78 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) 82 width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
79 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) 83 height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
80 84
81 pastObjects = [] 85 pastObjects = []
82 if params.undistort: # setup undistortion 86 if params.undistort: # setup undistortion
83 [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients) 87 [map1, map2] = cvutils.computeUndistortMaps(width, height, params.undistortedImageMultiplication, params.intrinsicCameraMatrix, params.distortionCoefficients)
84 if capture.isOpened(): 88 if capture.isOpened():
85 ret = True 89 ret = True
86 frameNum = timeInterval.first 90 frameNum = timeInterval.first
87 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) 91 capture.set(cv2.CAP_PROP_POS_FRAMES, frameNum)
88 lastFrameNum = timeInterval.last 92 lastFrameNum = timeInterval.last
89 93
90 while ret and frameNum <= lastFrameNum: 94 while ret and frameNum <= lastFrameNum:
91 ret, img = capture.read() 95 ret, img = capture.read()
92 if ret: 96 if ret: