comparison scripts/classify-objects.py @ 680:da1352b89d02 dev

classification is working
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 05 Jun 2015 02:25:30 +0200
parents 95276d310972
children fbe29be25501
comparison
equal deleted inserted replaced
678:97c305108460 680:da1352b89d02
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('-u', dest = 'undistort', help = 'undistort the video (because features have been extracted that way)', action = 'store_true')
12 # parser.add_argument('-o', dest = 'homographyFilename', help = 'name of the image to world homography file') 16 #parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', type = int)
13 # need a classification config file for speed distribution parameters, svm models, frequency parameters, area parameters etc 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)
14 #parser.add_argument('--cfg', dest = 'svmType', help = 'SVM type', default = SVM_C_SVC, type = long) 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)
15 19 # parser.add_argument('--speed-aggregation', dest = 'speedAggregationMethod', help = 'method to aggregate road user speed', type = str, choices = ['median', 'mean', 'quantile'], default = 'median')
16 20 # parser.add_argument('--speed-aggregation-quantile', dest = 'speedAggregationQuantile', help = 'quantile for the speed aggregation, if quantile is chosen', type = int, default = 50)
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 21
22 args = parser.parse_args() 22 args = parser.parse_args()
23 params = storage.ProcessParameters(args.configFilename) 23 params = storage.ProcessParameters(args.configFilename)
24
25 params.convertToFrames(3.6)
26 invHomography = np.linalg.inv(params.homography)
27
28 if params.speedAggregationMethod == 'median':
29 speedAggregationFunc = np.median
30 elif params.speedAggregationMethod == 'mean':
31 speedAggregationFunc = np.mean
32 elif params.speedAggregationMethod == 'quantile':
33 speedAggregationFunc = lambda speeds: np.percentile(speeds, args.speedAggregationQuantile)
34 else:
35 print('Unknown speed aggregation method: {}. Exiting'.format(params.speedAggregationMethod))
36 from sys import exit
37 exit()
38
39 pedBikeCarSVM = ml.SVM()
40 pedBikeCarSVM.load(params.pedBikeCarSVMFilename)
41 bikeCarSVM = ml.SVM()
42 bikeCarSVM.load(params.bikeCarSVMFilename)
43
44 # 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),
46 '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
48
49 objects = storage.loadTrajectoriesFromSqlite(params.databaseFilename, 'object')
50 features = storage.loadTrajectoriesFromSqlite(params.databaseFilename, 'feature')
51 intervals = []
52 for obj in objects:
53 obj.setFeatures(features)
54 intervals.append(obj.getTimeInterval())
55 timeInterval = moving.unionIntervals(intervals)
56
57 capture = cv2.VideoCapture(params.videoFilename)
58 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
59 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
60
61 if params.undistort: # setup undistortion
62 [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients)
63 if capture.isOpened():
64 ret = True
65 frameNum = timeInterval.first
66 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum)
67 lastFrameNum = timeInterval.last
68
69 while ret and frameNum <= lastFrameNum:
70 ret, img = capture.read()
71 if ret:
72 if frameNum%50 == 0:
73 print('frame number: {}'.format(frameNum))
74 if params.undistort:
75 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)
76 for obj in objects:
77 if obj.existsAtInstant(frameNum):
78 if obj.getFirstInstant() == frameNum:
79 print 'first frame for obj {}'.format(obj.getNum())
80 obj.initClassifyUserTypeHoGSVM(speedAggregationFunc, pedBikeCarSVM, bikeCarSVM, params.maxPedestrianSpeed, params.maxCyclistSpeed, params.nFramesIgnoreAtEnds)
81 obj.classifyUserTypeHoGSVMAtInstant(img, frameNum, invHomography, width, height, 0.2, 0.2, 800) # px, py, pixelThreshold
82 frameNum += 1
83
84 for obj in objects:
85 obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = params.minSpeedEquiprobable, speedProbabilities = speedProbabilities)