Mercurial Hosting > traffic-intelligence
changeset 1247:439207b6c146
bug corrected
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 12 Feb 2024 16:47:33 -0500 |
parents | 2397de73770d |
children | c4c50678c856 |
files | scripts/dltrack.py |
diffstat | 1 files changed, 46 insertions(+), 44 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/dltrack.py Fri Feb 09 17:47:33 2024 -0500 +++ b/scripts/dltrack.py Mon Feb 12 16:47:33 2024 -0500 @@ -58,7 +58,8 @@ else: lastFrameNum = inf -# TODO use mask +# TODO use mask, remove short objects, smooth + # TODO add option to refine position with mask for vehicles, to save different positions # TODO work with optical flow (farneback or RAFT) https://pytorch.org/vision/main/models/raft.html @@ -153,51 +154,52 @@ costs = -np.array(costs) +if costs.size > 0: # before matching, scan for pedestrians with good non-overlapping temporal match with different bikes -for pedInd in range(costs.shape[1]): - nMatchedBikes = (costs[:,pedInd] < -args.cyclistMatchingProportion).sum() - if nMatchedBikes == 0: # peds that have no bike matching: see if they have been classified as bikes sometimes - userTypeStats = Counter(obj.userTypes) - 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 - obj.setUserType(moving.userType2Num['cyclist']) - elif nMatchedBikes > 1: # try to merge bikes first - twIndices = np.nonzero(costs[:,pedInd] < -args.cyclistMatchingProportion)[0] - # 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 - nTwoWheels = len(twIndices) - twTemporalOverlaps = np.zeros((nTwoWheels,nTwoWheels)) - for i in range(nTwoWheels): - for j in range(i): - twi = objects[twowheels[twIndices[i]]] - twj = objects[twowheels[twIndices[j]]] - twTemporalOverlaps[i,j] = len(set(twi.bboxes).intersection(set(twj.bboxes)))/max(len(twi.bboxes), len(twj.bboxes)) - #twTemporalOverlaps[j,i] = twTemporalOverlaps[i,j] - tw2merge = list(range(nTwoWheels)) - while len(tw2merge)>0 and (twTemporalOverlaps[np.ix_(tw2merge, tw2merge)] > args.maxTemporalOverlap).sum(0).max() >= 2: - i = (twTemporalOverlaps[np.ix_(tw2merge, tw2merge)] > args.maxTemporalOverlap).sum(0).argmax() - del tw2merge[i] - twIndices = [twIndices[i] for i in tw2merge] - tw1 = objects[twowheels[twIndices[0]]] - twCost = costs[twIndices[0],:]*tw1.nBBoxes - nBBoxes = tw1.nBBoxes - for twInd in twIndices[1:]: - mergeObjects(tw1, objects[twowheels[twInd]]) - twCost = twCost + costs[twInd,:]*objects[twowheels[twInd]].nBBoxes - nBBoxes += objects[twowheels[twInd]].nBBoxes - twIndicesToKeep = list(range(costs.shape[0])) - for twInd in twIndices[1:]: - twIndicesToKeep.remove(twInd) - del objects[twowheels[twInd]] - twowheels = [twowheels[i] for i in twIndicesToKeep] - costs = costs[twIndicesToKeep,:] + for pedInd in range(costs.shape[1]): + nMatchedBikes = (costs[:,pedInd] < -args.cyclistMatchingProportion).sum() + if nMatchedBikes == 0: # peds that have no bike matching: see if they have been classified as bikes sometimes + userTypeStats = Counter(obj.userTypes) + 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 + obj.setUserType(moving.userType2Num['cyclist']) + elif nMatchedBikes > 1: # try to merge bikes first + twIndices = np.nonzero(costs[:,pedInd] < -args.cyclistMatchingProportion)[0] + # 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 + nTwoWheels = len(twIndices) + twTemporalOverlaps = np.zeros((nTwoWheels,nTwoWheels)) + for i in range(nTwoWheels): + for j in range(i): + twi = objects[twowheels[twIndices[i]]] + twj = objects[twowheels[twIndices[j]]] + twTemporalOverlaps[i,j] = len(set(twi.bboxes).intersection(set(twj.bboxes)))/max(len(twi.bboxes), len(twj.bboxes)) + #twTemporalOverlaps[j,i] = twTemporalOverlaps[i,j] + tw2merge = list(range(nTwoWheels)) + while len(tw2merge)>0 and (twTemporalOverlaps[np.ix_(tw2merge, tw2merge)] > args.maxTemporalOverlap).sum(0).max() >= 2: + i = (twTemporalOverlaps[np.ix_(tw2merge, tw2merge)] > args.maxTemporalOverlap).sum(0).argmax() + del tw2merge[i] + twIndices = [twIndices[i] for i in tw2merge] + tw1 = objects[twowheels[twIndices[0]]] + twCost = costs[twIndices[0],:]*tw1.nBBoxes + nBBoxes = tw1.nBBoxes + for twInd in twIndices[1:]: + mergeObjects(tw1, objects[twowheels[twInd]]) + twCost = twCost + costs[twInd,:]*objects[twowheels[twInd]].nBBoxes + nBBoxes += objects[twowheels[twInd]].nBBoxes + twIndicesToKeep = list(range(costs.shape[0])) + for twInd in twIndices[1:]: + twIndicesToKeep.remove(twInd) + del objects[twowheels[twInd]] + twowheels = [twowheels[i] for i in twIndicesToKeep] + costs = costs[twIndicesToKeep,:] -twIndices, matchingPedIndices = linear_sum_assignment(costs) -for twInd, pedInd in zip(twIndices, matchingPedIndices): # caution indices in the cost matrix - if -costs[twInd, pedInd] >= args.cyclistMatchingProportion: - tw = objects[twowheels[twInd]] - ped = objects[pedestrians[pedInd]] - mergeObjects(tw, ped) - del objects[pedestrians[pedInd]] - #TODO Verif overlap piéton vélo : si long hors overlap, changement mode (trouver exemples) + twIndices, matchingPedIndices = linear_sum_assignment(costs) + for twInd, pedInd in zip(twIndices, matchingPedIndices): # caution indices in the cost matrix + if -costs[twInd, pedInd] >= args.cyclistMatchingProportion: + tw = objects[twowheels[twInd]] + ped = objects[pedestrians[pedInd]] + mergeObjects(tw, ped) + del objects[pedestrians[pedInd]] + #TODO Verif overlap piéton vélo : si long hors overlap, changement mode (trouver exemples) # interpolate and save image coordinates for num, obj in objects.items():