Mercurial > hg > nsaunier > traffic-intelligence
comparison python/objectsmoothing.py @ 661:dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Mon, 18 May 2015 13:53:25 +0200 |
| parents | d74e8c175d6b |
| children | 15e244d2a1b5 |
comparison
equal
deleted
inserted
replaced
| 660:994dd644f6ab | 661:dc70d9e711f5 |
|---|---|
| 12 else: | 12 else: |
| 13 dist[f]= moving.Point.distanceNorm2(feat.getPositionAtInstant(t-1),f.getPositionAtInstant(t)) | 13 dist[f]= moving.Point.distanceNorm2(feat.getPositionAtInstant(t-1),f.getPositionAtInstant(t)) |
| 14 return min(dist, key=dist.get) # = utils.argmaxDict(dist) | 14 return min(dist, key=dist.get) # = utils.argmaxDict(dist) |
| 15 | 15 |
| 16 def getFeatures(obj, featureID): | 16 def getFeatures(obj, featureID): |
| 17 first = obj.features[featureID].getFirstInstant() | 17 currentFeature = obj.getFeature(featureID) |
| 18 last = obj.features[featureID].getLastInstant() | 18 first = currentFeature.getFirstInstant() |
| 19 featureList=[[obj.features[featureID],first,last,moving.Point(0,0)]] | 19 last = currentFeature.getLastInstant() |
| 20 featureList=[[currentFeature,first,last,moving.Point(0,0)]] | |
| 20 # find the features to fill in the beginning of the object existence | 21 # find the features to fill in the beginning of the object existence |
| 21 currentFeature = obj.features[featureID] | |
| 22 while first != obj.getFirstInstant(): | 22 while first != obj.getFirstInstant(): |
| 23 delta=featureList[-1][3] | 23 delta=featureList[-1][3] |
| 24 featureSet = [f for f in obj.features if f.existsAtInstant(first-1)] | 24 featureSet = [f for f in obj.getFeatures() if f.existsAtInstant(first-1)] |
| 25 feat = findNearest(currentFeature,featureSet,first-1,reverse=True) | 25 feat = findNearest(currentFeature,featureSet,first-1,reverse=True) |
| 26 if feat.existsAtInstant(first): | 26 if feat.existsAtInstant(first): |
| 27 featureList.append([feat,feat.getFirstInstant(),first-1,(currentFeature.getPositionAtInstant(first)-feat.getPositionAtInstant(first))+delta]) | 27 featureList.append([feat,feat.getFirstInstant(),first-1,(currentFeature.getPositionAtInstant(first)-feat.getPositionAtInstant(first))+delta]) |
| 28 else: | 28 else: |
| 29 featureList.append([feat,feat.getFirstInstant(),first-1,(currentFeature.getPositionAtInstant(first)-feat.getPositionAtInstant(first-1))+delta]) | 29 featureList.append([feat,feat.getFirstInstant(),first-1,(currentFeature.getPositionAtInstant(first)-feat.getPositionAtInstant(first-1))+delta]) |
| 30 currentFeature = feat | 30 currentFeature = feat |
| 31 first= feat.getFirstInstant() | 31 first= feat.getFirstInstant() |
| 32 # find the features to fill in the end of the object existence | 32 # find the features to fill in the end of the object existence |
| 33 delta=moving.Point(0,0) | 33 delta=moving.Point(0,0) |
| 34 currentFeature = obj.features[featureID] | 34 currentFeature = obj.getFeature(featureID) # need to reinitialize |
| 35 while last!= obj.getLastInstant(): | 35 while last!= obj.getLastInstant(): |
| 36 featureSet = [f for f in obj.features if f.existsAtInstant(last+1)] | 36 featureSet = [f for f in obj.getFeatures() if f.existsAtInstant(last+1)] |
| 37 feat = findNearest(currentFeature,featureSet,last+1,reverse=False) | 37 feat = findNearest(currentFeature,featureSet,last+1,reverse=False) |
| 38 if feat.existsAtInstant(last): | 38 if feat.existsAtInstant(last): |
| 39 featureList.append([feat,last+1,feat.getLastInstant(),(currentFeature.getPositionAtInstant(last)-feat.getPositionAtInstant(last))+delta]) | 39 featureList.append([feat,last+1,feat.getLastInstant(),(currentFeature.getPositionAtInstant(last)-feat.getPositionAtInstant(last))+delta]) |
| 40 else: | 40 else: |
| 41 featureList.append([feat,last+1,feat.getLastInstant(),(currentFeature.getPositionAtInstant(last)-feat.getPositionAtInstant(last+1))+delta]) | 41 featureList.append([feat,last+1,feat.getLastInstant(),(currentFeature.getPositionAtInstant(last)-feat.getPositionAtInstant(last+1))+delta]) |
| 118 results=[] | 118 results=[] |
| 119 bearing={} | 119 bearing={} |
| 120 if create: | 120 if create: |
| 121 feature = buildFeature(obj, featureID , num=1) # why num=1 | 121 feature = buildFeature(obj, featureID , num=1) # why num=1 |
| 122 else: | 122 else: |
| 123 feature = obj.features[featureID] | 123 feature = obj.getFeature(featureID) |
| 124 for t in feature.getTimeInterval(): | 124 for t in feature.getTimeInterval(): |
| 125 p1= feature.getPositionAtInstant(t) | 125 p1= feature.getPositionAtInstant(t) |
| 126 p2= obj.getPositionAtInstant(t) | 126 p2= obj.getPositionAtInstant(t) |
| 127 if t!=feature.getLastInstant(): | 127 if t!=feature.getLastInstant(): |
| 128 p3= feature.getPositionAtInstant(t+1) | 128 p3= feature.getPositionAtInstant(t+1) |
| 178 '''Computes a smoother trajectory for the object | 178 '''Computes a smoother trajectory for the object |
| 179 and optionnally smoother velocities | 179 and optionnally smoother velocities |
| 180 | 180 |
| 181 The object should have its features in obj.features | 181 The object should have its features in obj.features |
| 182 TODO: check whether features are necessary''' | 182 TODO: check whether features are necessary''' |
| 183 if len(obj.features) == 0: | 183 if not obj.hasFeatures(): |
| 184 print('Object {} has an empty list of features: please load and add them using obj.setFeatures(features)'.format(obj.getNum())) | 184 print('Object {} has an empty list of features: please load and add them using obj.setFeatures(features)'.format(obj.getNum())) |
| 185 from sys import exit | 185 from sys import exit |
| 186 exit() | 186 exit() |
| 187 | 187 |
| 188 featureList=[i for i,f in enumerate(obj.features) if f.length() >= minLengthParam*obj.length()] | 188 featureList=[i for i,f in enumerate(obj.getFeatures()) if f.length() >= minLengthParam*obj.length()] |
| 189 if featureList==[]: | 189 if featureList==[]: |
| 190 featureList.append(utils.argmaxDict({i:f.length() for i,f in enumerate(obj.features)})) | 190 featureList.append(utils.argmaxDict({i:f.length() for i,f in enumerate(obj.getFeatures())})) |
| 191 create = True | 191 create = True |
| 192 newObjects = [] | 192 newObjects = [] |
| 193 for featureID in featureList: # featureID should be the index in the list of obj.features | 193 for featureID in featureList: # featureID should be the index in the list of obj.features |
| 194 newObjects.append(smoothObjectTrajectory(obj, featureID, newNum, smoothing = smoothing, halfWidth = halfWidth, create = create)) | 194 newObjects.append(smoothObjectTrajectory(obj, featureID, newNum, smoothing = smoothing, halfWidth = halfWidth, create = create)) |
| 195 | 195 |
| 230 csj4= sumSquaredJerk(newObj,fromPosition=False) | 230 csj4= sumSquaredJerk(newObj,fromPosition=False) |
| 231 if csj4<=csj3: | 231 if csj4<=csj3: |
| 232 newObj.velocities= obj.velocities | 232 newObj.velocities= obj.velocities |
| 233 | 233 |
| 234 newObj.featureNumbers=obj.featureNumbers | 234 newObj.featureNumbers=obj.featureNumbers |
| 235 newObj.features=obj.features | 235 newObj.features=obj.getFeatures() |
| 236 newObj.userType=obj.userType | 236 newObj.userType=obj.userType |
| 237 | 237 |
| 238 if plotResults: | 238 if plotResults: |
| 239 plt.figure() | 239 plt.figure() |
| 240 plt.title('objects_id = {}'.format(obj.num)) | 240 plt.title('objects_id = {}'.format(obj.num)) |
| 241 for i in featureList: | 241 for i in featureList: |
| 242 obj.features[i].plot('cx-') | 242 obj.getFeature(i).plot('cx-') |
| 243 obj.plot('rx-') | 243 obj.plot('rx-') |
| 244 newObj.plot('gx-') | 244 newObj.plot('gx-') |
| 245 return newObj | 245 return newObj |
