comparison scripts/dltrack.py @ 1247:439207b6c146

bug corrected
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 12 Feb 2024 16:47:33 -0500
parents 2397de73770d
children 2aa56b101041
comparison
equal deleted inserted replaced
1246:2397de73770d 1247:439207b6c146
56 elif args.configFilename is not None: 56 elif args.configFilename is not None:
57 lastFrameNum = params.lastFrameNum 57 lastFrameNum = params.lastFrameNum
58 else: 58 else:
59 lastFrameNum = inf 59 lastFrameNum = inf
60 60
61 # TODO use mask 61 # TODO use mask, remove short objects, smooth
62
62 # TODO add option to refine position with mask for vehicles, to save different positions 63 # TODO add option to refine position with mask for vehicles, to save different positions
63 # TODO work with optical flow (farneback or RAFT) https://pytorch.org/vision/main/models/raft.html 64 # TODO work with optical flow (farneback or RAFT) https://pytorch.org/vision/main/models/raft.html
64 65
65 # use 2 x bytetrack track buffer to remove objects from existing ones 66 # use 2 x bytetrack track buffer to remove objects from existing ones
66 67
151 twCost.append(nmatches/tw.nBBoxes) 152 twCost.append(nmatches/tw.nBBoxes)
152 costs.append(twCost) 153 costs.append(twCost)
153 154
154 costs = -np.array(costs) 155 costs = -np.array(costs)
155 156
157 if costs.size > 0:
156 # before matching, scan for pedestrians with good non-overlapping temporal match with different bikes 158 # before matching, scan for pedestrians with good non-overlapping temporal match with different bikes
157 for pedInd in range(costs.shape[1]): 159 for pedInd in range(costs.shape[1]):
158 nMatchedBikes = (costs[:,pedInd] < -args.cyclistMatchingProportion).sum() 160 nMatchedBikes = (costs[:,pedInd] < -args.cyclistMatchingProportion).sum()
159 if nMatchedBikes == 0: # peds that have no bike matching: see if they have been classified as bikes sometimes 161 if nMatchedBikes == 0: # peds that have no bike matching: see if they have been classified as bikes sometimes
160 userTypeStats = Counter(obj.userTypes) 162 userTypeStats = Counter(obj.userTypes)
161 if (moving.userType2Num['cyclist'] in userTypeStats or (moving.userType2Num['motorcyclist'] in userTypeStats and moving.userType2Num['cyclist'] in userTypeStats and userTypeStats[moving.userType2Num['motorcyclist']]<=userTypeStats[moving.userType2Num['cyclist']])) and userTypeStats[moving.userType2Num['motorcyclist']]+userTypeStats[moving.userType2Num['cyclist']] > args.bikeProportion*userTypeStats.total(): # verif if not turning all motorbike into cyclists 163 if (moving.userType2Num['cyclist'] in userTypeStats or (moving.userType2Num['motorcyclist'] in userTypeStats and moving.userType2Num['cyclist'] in userTypeStats and userTypeStats[moving.userType2Num['motorcyclist']]<=userTypeStats[moving.userType2Num['cyclist']])) and userTypeStats[moving.userType2Num['motorcyclist']]+userTypeStats[moving.userType2Num['cyclist']] > args.bikeProportion*userTypeStats.total(): # verif if not turning all motorbike into cyclists
162 obj.setUserType(moving.userType2Num['cyclist']) 164 obj.setUserType(moving.userType2Num['cyclist'])
163 elif nMatchedBikes > 1: # try to merge bikes first 165 elif nMatchedBikes > 1: # try to merge bikes first
164 twIndices = np.nonzero(costs[:,pedInd] < -args.cyclistMatchingProportion)[0] 166 twIndices = np.nonzero(costs[:,pedInd] < -args.cyclistMatchingProportion)[0]
165 # we have to compute temporal overlaps of all 2 wheels among themselves, then remove the ones with the most overlap (sum over column) one by one until there is little left 167 # we have to compute temporal overlaps of all 2 wheels among themselves, then remove the ones with the most overlap (sum over column) one by one until there is little left
166 nTwoWheels = len(twIndices) 168 nTwoWheels = len(twIndices)
167 twTemporalOverlaps = np.zeros((nTwoWheels,nTwoWheels)) 169 twTemporalOverlaps = np.zeros((nTwoWheels,nTwoWheels))
168 for i in range(nTwoWheels): 170 for i in range(nTwoWheels):
169 for j in range(i): 171 for j in range(i):
170 twi = objects[twowheels[twIndices[i]]] 172 twi = objects[twowheels[twIndices[i]]]
171 twj = objects[twowheels[twIndices[j]]] 173 twj = objects[twowheels[twIndices[j]]]
172 twTemporalOverlaps[i,j] = len(set(twi.bboxes).intersection(set(twj.bboxes)))/max(len(twi.bboxes), len(twj.bboxes)) 174 twTemporalOverlaps[i,j] = len(set(twi.bboxes).intersection(set(twj.bboxes)))/max(len(twi.bboxes), len(twj.bboxes))
173 #twTemporalOverlaps[j,i] = twTemporalOverlaps[i,j] 175 #twTemporalOverlaps[j,i] = twTemporalOverlaps[i,j]
174 tw2merge = list(range(nTwoWheels)) 176 tw2merge = list(range(nTwoWheels))
175 while len(tw2merge)>0 and (twTemporalOverlaps[np.ix_(tw2merge, tw2merge)] > args.maxTemporalOverlap).sum(0).max() >= 2: 177 while len(tw2merge)>0 and (twTemporalOverlaps[np.ix_(tw2merge, tw2merge)] > args.maxTemporalOverlap).sum(0).max() >= 2:
176 i = (twTemporalOverlaps[np.ix_(tw2merge, tw2merge)] > args.maxTemporalOverlap).sum(0).argmax() 178 i = (twTemporalOverlaps[np.ix_(tw2merge, tw2merge)] > args.maxTemporalOverlap).sum(0).argmax()
177 del tw2merge[i] 179 del tw2merge[i]
178 twIndices = [twIndices[i] for i in tw2merge] 180 twIndices = [twIndices[i] for i in tw2merge]
179 tw1 = objects[twowheels[twIndices[0]]] 181 tw1 = objects[twowheels[twIndices[0]]]
180 twCost = costs[twIndices[0],:]*tw1.nBBoxes 182 twCost = costs[twIndices[0],:]*tw1.nBBoxes
181 nBBoxes = tw1.nBBoxes 183 nBBoxes = tw1.nBBoxes
182 for twInd in twIndices[1:]: 184 for twInd in twIndices[1:]:
183 mergeObjects(tw1, objects[twowheels[twInd]]) 185 mergeObjects(tw1, objects[twowheels[twInd]])
184 twCost = twCost + costs[twInd,:]*objects[twowheels[twInd]].nBBoxes 186 twCost = twCost + costs[twInd,:]*objects[twowheels[twInd]].nBBoxes
185 nBBoxes += objects[twowheels[twInd]].nBBoxes 187 nBBoxes += objects[twowheels[twInd]].nBBoxes
186 twIndicesToKeep = list(range(costs.shape[0])) 188 twIndicesToKeep = list(range(costs.shape[0]))
187 for twInd in twIndices[1:]: 189 for twInd in twIndices[1:]:
188 twIndicesToKeep.remove(twInd) 190 twIndicesToKeep.remove(twInd)
189 del objects[twowheels[twInd]] 191 del objects[twowheels[twInd]]
190 twowheels = [twowheels[i] for i in twIndicesToKeep] 192 twowheels = [twowheels[i] for i in twIndicesToKeep]
191 costs = costs[twIndicesToKeep,:] 193 costs = costs[twIndicesToKeep,:]
192 194
193 twIndices, matchingPedIndices = linear_sum_assignment(costs) 195 twIndices, matchingPedIndices = linear_sum_assignment(costs)
194 for twInd, pedInd in zip(twIndices, matchingPedIndices): # caution indices in the cost matrix 196 for twInd, pedInd in zip(twIndices, matchingPedIndices): # caution indices in the cost matrix
195 if -costs[twInd, pedInd] >= args.cyclistMatchingProportion: 197 if -costs[twInd, pedInd] >= args.cyclistMatchingProportion:
196 tw = objects[twowheels[twInd]] 198 tw = objects[twowheels[twInd]]
197 ped = objects[pedestrians[pedInd]] 199 ped = objects[pedestrians[pedInd]]
198 mergeObjects(tw, ped) 200 mergeObjects(tw, ped)
199 del objects[pedestrians[pedInd]] 201 del objects[pedestrians[pedInd]]
200 #TODO Verif overlap piéton vélo : si long hors overlap, changement mode (trouver exemples) 202 #TODO Verif overlap piéton vélo : si long hors overlap, changement mode (trouver exemples)
201 203
202 # interpolate and save image coordinates 204 # interpolate and save image coordinates
203 for num, obj in objects.items(): 205 for num, obj in objects.items():
204 for f in obj.getFeatures(): 206 for f in obj.getFeatures():
205 if f.length() != len(f.tmpPositions): # interpolate 207 if f.length() != len(f.tmpPositions): # interpolate