Mercurial Hosting > traffic-intelligence
changeset 1245:371c718e57d7
interface updates
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Thu, 08 Feb 2024 16:10:54 -0500 |
parents | 00b71da2baac |
children | 2397de73770d |
files | scripts/display-trajectories.py scripts/dltrack.py trafficintelligence/cvutils.py trafficintelligence/moving.py trafficintelligence/storage.py |
diffstat | 5 files changed, 32 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/display-trajectories.py Thu Feb 08 15:04:56 2024 -0500 +++ b/scripts/display-trajectories.py Thu Feb 08 16:10:54 2024 -0500 @@ -1,6 +1,7 @@ #! /usr/bin/env python3 import sys, argparse +from math import inf from numpy.linalg import inv from numpy import loadtxt @@ -18,7 +19,7 @@ parser.add_argument('--undistorted-multiplication', dest = 'undistortedImageMultiplication', help = 'undistorted image multiplication', type = float) parser.add_argument('-u', dest = 'undistort', help = 'undistort the video (because features have been extracted that way)', action = 'store_true') parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', type = int) -parser.add_argument('-l', dest = 'lastFrameNum', help = 'number of last frame number to save (for image saving, no display is made)', type = int) +parser.add_argument('-l', dest = 'lastFrameNum', help = 'number of last frame number to save (for image saving, no display is made)', type = int, default = inf) parser.add_argument('-r', dest = 'rescale', help = 'rescaling factor for the displayed image', default = 1., type = float) parser.add_argument('-s', dest = 'nFramesStep', help = 'number of frames between each display', default = 1, type = int) parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to display', type = int) @@ -26,7 +27,6 @@ parser.add_argument('--nzeros', dest = 'nZerosFilenameArg', help = 'number of digits in filenames', type = int) args = parser.parse_args() - params, videoFilename, databaseFilename, invHomography, intrinsicCameraMatrix, distortionCoefficients, undistortedImageMultiplication, undistort, firstFrameNum = storage.processVideoArguments(args) if args.homographyFilename is not None: @@ -39,6 +39,8 @@ undistortedImageMultiplication = args.undistortedImageMultiplication if args.firstFrameNum is not None: firstFrameNum = args.firstFrameNum +if args.lastFrameNum is not None: + lastFrameNum = args.lastFrameNum if args.nObjects is not None: nObjects = args.nObjects else: @@ -46,4 +48,4 @@ objects = storage.loadTrajectoriesFromSqlite(databaseFilename, args.trajectoryType, nObjects) boundingBoxes = storage.loadBoundingBoxTableForDisplay(databaseFilename) -cvutils.displayTrajectories(videoFilename, objects, boundingBoxes, invHomography, firstFrameNum, args.lastFrameNum, rescale = args.rescale, nFramesStep = args.nFramesStep, saveAllImages = args.saveAllImages, nZerosFilenameArg = args.nZerosFilenameArg, undistort = (undistort or args.undistort), intrinsicCameraMatrix = intrinsicCameraMatrix, distortionCoefficients = distortionCoefficients, undistortedImageMultiplication = undistortedImageMultiplication) +cvutils.displayTrajectories(videoFilename, objects, boundingBoxes, invHomography, firstFrameNum, lastFrameNum, rescale = args.rescale, nFramesStep = args.nFramesStep, saveAllImages = args.saveAllImages, nZerosFilenameArg = args.nZerosFilenameArg, undistort = (undistort or args.undistort), intrinsicCameraMatrix = intrinsicCameraMatrix, distortionCoefficients = distortionCoefficients, undistortedImageMultiplication = undistortedImageMultiplication)
--- a/scripts/dltrack.py Thu Feb 08 15:04:56 2024 -0500 +++ b/scripts/dltrack.py Thu Feb 08 16:10:54 2024 -0500 @@ -13,8 +13,9 @@ from trafficintelligence import cvutils, moving, storage, utils parser = argparse.ArgumentParser(description='The program tracks objects using the ultralytics models and trakcers.') -parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file', required = True) -parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True) +parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file') +parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file (overrides the configuration file)') +parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (overrides the configuration file)') parser.add_argument('-m', dest = 'detectorFilename', help = 'name of the detection model file', required = True) parser.add_argument('-t', dest = 'trackerFilename', help = 'name of the tracker file', required = True) parser.add_argument('-o', dest = 'homographyFilename', help = 'filename of the homography matrix', default = 'homography.txt') @@ -23,6 +24,7 @@ parser.add_argument('--intrinsic', dest = 'intrinsicCameraMatrixFilename', help = 'name of the intrinsic camera file') parser.add_argument('--distortion-coefficients', dest = 'distortionCoefficients', help = 'distortion coefficients', nargs = '*', type = float) parser.add_argument('--display', dest = 'display', help = 'show the raw detection and tracking results', action = 'store_true') +parser.add_argument('--no-image-coordinates', dest = 'notSavingImageCoordinates', help = 'not saving the raw detection and tracking results', action = 'store_true') parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to process', type = int, default = 0) parser.add_argument('-l', dest = 'lastFrameNum', help = 'number of last frame number to process', type = int, default = float('Inf')) parser.add_argument('--conf', dest = 'confindence', help = 'object confidence threshold for detection', type = float, default = 0.25) @@ -30,9 +32,21 @@ parser.add_argument('--cyclist-iou', dest = 'cyclistIou', help = 'IoU threshold to associate a bike and ped bounding box', type = float, default = 0.15) parser.add_argument('--cyclist-match-prop', dest = 'cyclistMatchingProportion', help = 'minimum proportion of time a bike exists and is associated with a pedestrian to be merged as cyclist', type = float, default = 0.3) parser.add_argument('--max-temp-overal', dest = 'maxTemporalOverlap', help = 'maximum proportion of time to merge 2 bikes associated with same pedestrian', type = float, default = 0.05) + args = parser.parse_args() +params, videoFilename, databaseFilename, invHomography, intrinsicCameraMatrix, distortionCoefficients, undistortedImageMultiplication, undistort, firstFrameNum = storage.processVideoArguments(args) + +if args.intrinsicCameraMatrixFilename is not None: + intrinsicCameraMatrix = loadtxt(args.intrinsicCameraMatrixFilename) +if args.distortionCoefficients is not None: + distortionCoefficients = args.distortionCoefficients +if args.firstFrameNum is not None: + firstFrameNum = args.firstFrameNum +if args.lastFrameNum is not None: + lastFrameNum = args.lastFrameNum # TODO add option to refine position with mask for vehicles +# TODO work with optical flow (farneback or RAFT) https://pytorch.org/vision/main/models/raft.html # use 2 x bytetrack track buffer to remove objects from existing ones @@ -57,10 +71,9 @@ if not success: print('Input {} could not be read. Exiting'.format(args.videoFilename)) import sys; sys.exit() + results = model.track(frame, tracker=args.trackerFilename, classes=list(moving.cocoTypeNames.keys()), persist=True, verbose=False) -# create object with user type and list of 3 features (bottom ones and middle) + projection while capture.isOpened() and success and frameNum <= lastFrameNum: -#for frameNum, result in enumerate(results): result = results[0] if frameNum %10 == 0: print(frameNum, len(result.boxes), 'objects') @@ -103,8 +116,8 @@ # add quality control: avoid U-turns # merge bikes and people -twowheels = [num for num, obj in objects.items() if obj.getUserType() in (3,4)] -pedestrians = [num for num, obj in objects.items() if obj.getUserType() == 2] +twowheels = [num for num, obj in objects.items() if obj.getUserType() in (moving.userType2Num['motorcyclist'],moving.userType2Num['cyclist'])] +pedestrians = [num for num, obj in objects.items() if obj.getUserType() == moving.userType2Num['pedestrian']] def mergeObjects(obj1, obj2): obj1.features = obj1.features+obj2.features @@ -134,8 +147,8 @@ 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 (4 in userTypeStats or (3 in userTypeStats and 4 in userTypeStats and userTypeStats[3]<=userTypeStats[4])) and userTypeStats[3]+userTypeStats[4] > args.bikeProportion*userTypeStats.total(): # 3 is motorcycle and 4 is cyclist (verif if not turning all motorbike into cyclists) - obj.setUserType(4) + 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
--- a/trafficintelligence/cvutils.py Thu Feb 08 15:04:56 2024 -0500 +++ b/trafficintelligence/cvutils.py Thu Feb 08 16:10:54 2024 -0500 @@ -4,7 +4,7 @@ from sys import stdout from os import listdir from subprocess import run -from math import floor, log10, ceil +from math import floor, log10, ceil, inf from time import time from numpy import dot, array, append, float32, loadtxt, savetxt, append, zeros, ones, identity, abs as npabs, logical_and, unravel_index, sum as npsum, isnan, mgrid, median, floor as npfloor, ceil as npceil, nonzero, dtype @@ -312,7 +312,7 @@ else: run(cmd) - def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1., nFramesStep = 1, saveAllImages = False, nZerosFilenameArg = None, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1., annotations = [], gtMatches = {}, toMatches = {}, colorBlind = False, **kwargs): + def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNum = None, printFrames = True, rescale = 1., nFramesStep = 1, saveAllImages = False, nZerosFilenameArg = None, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1., annotations = [], gtMatches = {}, toMatches = {}, colorBlind = False, **kwargs): '''Displays the objects overlaid frame by frame over the video ''' if colorBlind: colorType = 'colorblind' @@ -334,12 +334,8 @@ ret = True frameNum = firstFrameNum capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum) - if lastFrameNumArg is None: - lastFrameNum = float("inf") - else: - lastFrameNum = lastFrameNumArg if nZerosFilenameArg is None: - if lastFrameNumArg is None: + if lastFrameNum is None or lastFrameNum == inf: nZerosFilename = int(ceil(log10(objects[-1].getLastInstant()))) else: nZerosFilename = int(ceil(log10(lastFrameNum)))
--- a/trafficintelligence/moving.py Thu Feb 08 15:04:56 2024 -0500 +++ b/trafficintelligence/moving.py Thu Feb 08 16:10:54 2024 -0500 @@ -1400,7 +1400,7 @@ userTypeNames = ['unknown', 'car', 'pedestrian', - 'motorcycle', + 'motorcyclist', 'cyclist', 'bus', 'truck', @@ -1847,7 +1847,7 @@ arrow(p.x, p.y, v.x, v.y, width=arrowWidth) def play(self, videoFilename, homography = None, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1.): - cvutils.displayTrajectories(videoFilename, [self], homography = homography, firstFrameNum = self.getFirstInstant(), lastFrameNumArg = self.getLastInstant(), undistort = undistort, intrinsicCameraMatrix = intrinsicCameraMatrix, distortionCoefficients = distortionCoefficients, undistortedImageMultiplication = undistortedImageMultiplication) + cvutils.displayTrajectories(videoFilename, [self], homography = homography, firstFrameNum = self.getFirstInstant(), lastFrameNum = self.getLastInstant(), undistort = undistort, intrinsicCameraMatrix = intrinsicCameraMatrix, distortionCoefficients = distortionCoefficients, undistortedImageMultiplication = undistortedImageMultiplication) def speedDiagnostics(self, framerate = 1., display = False, nInstantsIgnoredAtEnds=0): speeds = framerate*self.getSpeeds(nInstantsIgnoredAtEnds)
--- a/trafficintelligence/storage.py Thu Feb 08 15:04:56 2024 -0500 +++ b/trafficintelligence/storage.py Thu Feb 08 16:10:54 2024 -0500 @@ -1690,8 +1690,7 @@ undistort = False firstFrameNum = 0 - # override video and database filenames if present on command line - # if not absolute, make all filenames relative to the location of the configuration filename + # override video and database filenames if present on command line (path is relative to command line location) if args.videoFilename is not None: videoFilename = args.videoFilename else: