Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/classify-objects.py @ 788:5b970a5bc233 dev
updated classifying code to OpenCV 3.x (bug in function to load classification models)
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Thu, 24 Mar 2016 16:37:37 -0400 |
| parents | de278c5e65f6 |
| children | 1158a6e2d28e |
comparison
equal
deleted
inserted
replaced
| 787:0a428b449b80 | 788:5b970a5bc233 |
|---|---|
| 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 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.) |
| 42 speedAggregationFunc = lambda speeds: np.percentile(speeds, args.speedAggregationQuantile) | 44 speedAggregationFunc = lambda speeds: np.percentile(speeds, args.speedAggregationQuantile) |
| 43 else: | 45 else: |
| 44 print('Unknown speed aggregation method: {}. Exiting'.format(params.speedAggregationMethod)) | 46 print('Unknown speed aggregation method: {}. Exiting'.format(params.speedAggregationMethod)) |
| 45 sys.exit() | 47 sys.exit() |
| 46 | 48 |
| 47 pedBikeCarSVM = ml.SVM() | 49 pedBikeCarSVM = ml.SVM(args.svmType, args.kernelType) |
| 48 pedBikeCarSVM.load(params.pedBikeCarSVMFilename) | 50 pedBikeCarSVM.load(params.pedBikeCarSVMFilename) |
| 49 bikeCarSVM = ml.SVM() | 51 bikeCarSVM = ml.SVM(args.svmType, args.kernelType) |
| 50 bikeCarSVM.load(params.bikeCarSVMFilename) | 52 bikeCarSVM.load(params.bikeCarSVMFilename) |
| 51 | 53 |
| 52 # log logistic for ped and bik otherwise ((pedBeta/pedAlfa)*((sMean/pedAlfa)**(pedBeta-1)))/((1+(sMean/pedAlfa)**pedBeta)**2.) | 54 # 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), | 55 speedProbabilities = {'car': lambda s: norm(params.meanVehicleSpeed, params.stdVehicleSpeed).pdf(s), |
| 54 'pedestrian': lambda s: norm(params.meanPedestrianSpeed, params.stdPedestrianSpeed).pdf(s), | 56 'pedestrian': lambda s: norm(params.meanPedestrianSpeed, params.stdPedestrianSpeed).pdf(s), |
| 73 #obj.setFeatures(features) | 75 #obj.setFeatures(features) |
| 74 intervals.append(obj.getTimeInterval()) | 76 intervals.append(obj.getTimeInterval()) |
| 75 timeInterval = moving.unionIntervals(intervals) | 77 timeInterval = moving.unionIntervals(intervals) |
| 76 | 78 |
| 77 capture = cv2.VideoCapture(videoFilename) | 79 capture = cv2.VideoCapture(videoFilename) |
| 78 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) | 80 width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) |
| 79 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) | 81 height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
| 80 | 82 |
| 81 pastObjects = [] | 83 pastObjects = [] |
| 82 if params.undistort: # setup undistortion | 84 if params.undistort: # setup undistortion |
| 83 [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients) | 85 [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients) |
| 84 if capture.isOpened(): | 86 if capture.isOpened(): |
| 85 ret = True | 87 ret = True |
| 86 frameNum = timeInterval.first | 88 frameNum = timeInterval.first |
| 87 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) | 89 capture.set(cv2.CAP_PROP_POS_FRAMES, frameNum) |
| 88 lastFrameNum = timeInterval.last | 90 lastFrameNum = timeInterval.last |
| 89 | 91 |
| 90 while ret and frameNum <= lastFrameNum: | 92 while ret and frameNum <= lastFrameNum: |
| 91 ret, img = capture.read() | 93 ret, img = capture.read() |
| 92 if ret: | 94 if ret: |
