Mercurial > hg > nsaunier > traffic-intelligence
comparison python/objectsmoothing.py @ 644:e54751e71d61
more cleanup
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 10 Apr 2015 16:09:13 +0200 |
| parents | 9fe254f11743 |
| children | d74e8c175d6b |
comparison
equal
deleted
inserted
replaced
| 643:bfaa6b95dae2 | 644:e54751e71d61 |
|---|---|
| 11 dist[f]= moving.Point.distanceNorm2(feat.getPositionAtInstant(t+1),f.getPositionAtInstant(t)) | 11 dist[f]= moving.Point.distanceNorm2(feat.getPositionAtInstant(t+1),f.getPositionAtInstant(t)) |
| 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,features,featureID): | 16 def getFeatures(obj, featureID): |
| 17 t1,t3 = features[featureID].getFirstInstant(), features[featureID].getLastInstant() | 17 first = obj.features[featureID].getFirstInstant() |
| 18 listFeatures=[[features[featureID],t1,t3,moving.Point(0,0)]] | 18 last = obj.features[featureID].getLastInstant() |
| 19 featureList=[[obj.features[featureID],first,last,moving.Point(0,0)]] | |
| 19 # find the features to fill in the beginning of the object existence | 20 # find the features to fill in the beginning of the object existence |
| 20 currentFeature = features[featureID] | 21 currentFeature = obj.features[featureID] |
| 21 while t1!=obj.getFirstInstant(): | 22 while first != obj.getFirstInstant(): |
| 22 delta=listFeatures[-1][3] | 23 delta=featureList[-1][3] |
| 23 featureSet = [f for f in obj.features if f.existsAtInstant(t1-1)] | 24 featureSet = [f for f in obj.features if f.existsAtInstant(first-1)] |
| 24 feat = findNearest(currentFeature,featureSet,t1-1,reverse=True) | 25 feat = findNearest(currentFeature,featureSet,first-1,reverse=True) |
| 25 if feat.existsAtInstant(t1): | 26 if feat.existsAtInstant(first): |
| 26 listFeatures.append([feat,feat.getFirstInstant(),t1-1,(currentFeature.getPositionAtInstant(t1)-feat.getPositionAtInstant(t1))+delta]) | 27 featureList.append([feat,feat.getFirstInstant(),first-1,(currentFeature.getPositionAtInstant(first)-feat.getPositionAtInstant(first))+delta]) |
| 27 else: | 28 else: |
| 28 listFeatures.append([feat,feat.getFirstInstant(),t1-1,(currentFeature.getPositionAtInstant(t1)-feat.getPositionAtInstant(t1-1))+delta]) | 29 featureList.append([feat,feat.getFirstInstant(),first-1,(currentFeature.getPositionAtInstant(first)-feat.getPositionAtInstant(first-1))+delta]) |
| 29 currentFeature = feat | 30 currentFeature = feat |
| 30 t1= feat.getFirstInstant() | 31 first= feat.getFirstInstant() |
| 31 # 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 |
| 32 delta=moving.Point(0,0) | 33 delta=moving.Point(0,0) |
| 33 currentFeature = features[featureID] | 34 currentFeature = obj.features[featureID] |
| 34 while t3!= obj.getLastInstant(): | 35 while last!= obj.getLastInstant(): |
| 35 featureSet = [f for f in obj.features if f.existsAtInstant(t3+1)] | 36 featureSet = [f for f in obj.features if f.existsAtInstant(last+1)] |
| 36 feat = findNearest(currentFeature,featureSet,t3+1,reverse=False) | 37 feat = findNearest(currentFeature,featureSet,last+1,reverse=False) |
| 37 if feat.existsAtInstant(t3): | 38 if feat.existsAtInstant(last): |
| 38 listFeatures.append([feat,t3+1,feat.getLastInstant(),(currentFeature.getPositionAtInstant(t3)-feat.getPositionAtInstant(t3))+delta]) | 39 featureList.append([feat,last+1,feat.getLastInstant(),(currentFeature.getPositionAtInstant(last)-feat.getPositionAtInstant(last))+delta]) |
| 39 else: | 40 else: |
| 40 listFeatures.append([feat,t3+1,feat.getLastInstant(),(currentFeature.getPositionAtInstant(t3)-feat.getPositionAtInstant(t3+1))+delta]) | 41 featureList.append([feat,last+1,feat.getLastInstant(),(currentFeature.getPositionAtInstant(last)-feat.getPositionAtInstant(last+1))+delta]) |
| 41 currentFeature = feat | 42 currentFeature = feat |
| 42 t3= feat.getLastInstant() | 43 last= feat.getLastInstant() |
| 43 delta=listFeatures[-1][3] | 44 delta=featureList[-1][3] |
| 44 return listFeatures | 45 return featureList |
| 45 | 46 |
| 46 def buildFeature(obj,features,featureID,num=1): | 47 def buildFeature(obj, featureID, num = 1): |
| 47 listFeatures= getFeatures(obj,features,featureID) | 48 featureList= getFeatures(obj, featureID) |
| 48 tmp={} | 49 tmp={} |
| 49 delta={} | 50 delta={} |
| 50 for i in listFeatures: | 51 for i in featureList: |
| 51 for t in xrange(i[1],i[2]+1): | 52 for t in xrange(i[1],i[2]+1): |
| 52 tmp[t]=[i[0],i[3]] | 53 tmp[t]=[i[0],i[3]] |
| 53 newTraj = moving.Trajectory() | 54 newTraj = moving.Trajectory() |
| 54 | 55 |
| 55 for instant in obj.getTimeInterval(): | 56 for instant in obj.getTimeInterval(): |
| 111 t=0 | 112 t=0 |
| 112 for i in sorted(jerk.keys()): | 113 for i in sorted(jerk.keys()): |
| 113 t+= jerk[i]* jerk[i] | 114 t+= jerk[i]* jerk[i] |
| 114 return t | 115 return t |
| 115 | 116 |
| 116 def smoothObjectTrajectory(obj,features,featureID,newNum,smoothing=False,halfWidth=3,create=False): | 117 def smoothObjectTrajectory(obj, featureID,newNum,smoothing=False,halfWidth=3,create=False): |
| 117 results=[] | 118 results=[] |
| 118 bearing={} | 119 bearing={} |
| 119 if create: | 120 if create: |
| 120 feature = buildFeature(obj,features,featureID,num=1) | 121 feature = buildFeature(obj, featureID , num=1) # why num=1 |
| 121 else: | 122 else: |
| 122 feature = features[featureID] | 123 feature = obj.features[featureID] |
| 123 for t in feature.getTimeInterval(): | 124 for t in feature.getTimeInterval(): |
| 124 p1= feature.getPositionAtInstant(t) | 125 p1= feature.getPositionAtInstant(t) |
| 125 p2= obj.getPositionAtInstant(t) | 126 p2= obj.getPositionAtInstant(t) |
| 126 if t!=feature.getLastInstant(): | 127 if t!=feature.getLastInstant(): |
| 127 p3= feature.getPositionAtInstant(t+1) | 128 p3= feature.getPositionAtInstant(t+1) |
| 171 translated.setPosition(-i-1,p2) | 172 translated.setPosition(-i-1,p2) |
| 172 | 173 |
| 173 newObj= moving.MovingObject(newNum,timeInterval=feature.getTimeInterval(),positions=translated) | 174 newObj= moving.MovingObject(newNum,timeInterval=feature.getTimeInterval(),positions=translated) |
| 174 return newObj | 175 return newObj |
| 175 | 176 |
| 176 def smoothObject(obj,features,newNum,minLengthParam=0.7,smoothing=False,plotResults=True,halfWidth=3, _computeVelocities=True,optimize=True,create=False): | 177 def smoothObject(obj, newNum, minLengthParam = 0.7, smoothing = False, plotResults = True, halfWidth = 3, _computeVelocities = True, optimize = True, create = False): |
| 177 '''Computes a smoother trajectory for the object | 178 '''Computes a smoother trajectory for the object |
| 178 and optionnally smoother velocities | 179 and optionnally smoother velocities |
| 179 | 180 |
| 181 The object should have its features in obj.features | |
| 180 TODO: check whether features are necessary''' | 182 TODO: check whether features are necessary''' |
| 181 featureList=[i.num for i in obj.features if i.length() >= minLengthParam*obj.length()] | 183 if len(obj.features) == 0: |
| 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 | |
| 186 exit() | |
| 187 | |
| 188 featureList=[i for i,f in enumerate(obj.features) if f.length() >= minLengthParam*obj.length()] | |
| 182 if featureList==[]: | 189 if featureList==[]: |
| 183 featureList.append(utils.argmaxDict({f.getNum():f.length() for f in obj.features})) | 190 featureList.append(utils.argmaxDict({i:f.length() for i,f in enumerate(obj.features)})) |
| 184 create = True | 191 create = True |
| 185 newObjects = [] | 192 newObjects = [] |
| 186 for featureID in featureList: | 193 for featureID in featureList: # featureID should be the index in the list of obj.features |
| 187 newObjects.append(smoothObjectTrajectory(obj,features,featureID,newNum,smoothing=smoothing,halfWidth=halfWidth,create=create)) | 194 newObjects.append(smoothObjectTrajectory(obj, featureID, newNum, smoothing = smoothing, halfWidth = halfWidth, create = create)) |
| 188 | 195 |
| 189 newTranslated = moving.Trajectory() | 196 newTranslated = moving.Trajectory() |
| 190 newInterval = [] | 197 newInterval = [] |
| 191 for t in obj.getTimeInterval(): | 198 for t in obj.getTimeInterval(): |
| 192 xCoord=[] | 199 xCoord=[] |
| 230 | 237 |
| 231 if plotResults: | 238 if plotResults: |
| 232 plt.figure() | 239 plt.figure() |
| 233 plt.title('objects_id = {}'.format(obj.num)) | 240 plt.title('objects_id = {}'.format(obj.num)) |
| 234 for i in featureList: | 241 for i in featureList: |
| 235 features[i].plot('cx-') | 242 obj.features[i].plot('cx-') |
| 236 obj.plot('rx-') | 243 obj.plot('rx-') |
| 237 newObj.plot('gx-') | 244 newObj.plot('gx-') |
| 238 return newObj | 245 return newObj |
