Mercurial Hosting > traffic-intelligence
annotate scripts/dltrack.py @ 1250:77fbd0e2ba7d
dltrack works with moving average filtering
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Thu, 15 Feb 2024 22:04:35 -0500 |
parents | 2aa56b101041 |
children | dff5b678a33a |
rev | line source |
---|---|
1219
8a626226793e
update where optimization uses either nomad-parameter file depending on optimizing 1 or 2 steps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
1 #! /usr/bin/env python3 |
8a626226793e
update where optimization uses either nomad-parameter file depending on optimizing 1 or 2 steps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
2 # from https://docs.ultralytics.com/modes/track/ |
8a626226793e
update where optimization uses either nomad-parameter file depending on optimizing 1 or 2 steps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
3 import sys, argparse |
1246
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
4 from math import inf |
1233
d5695e0b59d9
saving results from ultralytics works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1231
diff
changeset
|
5 from copy import copy |
1236
100fe098abe9
progress on classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1235
diff
changeset
|
6 from collections import Counter |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
7 import numpy as np |
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
8 from scipy.optimize import linear_sum_assignment |
1219
8a626226793e
update where optimization uses either nomad-parameter file depending on optimizing 1 or 2 steps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
9 from ultralytics import YOLO |
1236
100fe098abe9
progress on classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1235
diff
changeset
|
10 from torch import cat |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
11 from torchvision.ops import box_iou |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
12 import cv2 |
1219
8a626226793e
update where optimization uses either nomad-parameter file depending on optimizing 1 or 2 steps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
13 |
1233
d5695e0b59d9
saving results from ultralytics works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1231
diff
changeset
|
14 from trafficintelligence import cvutils, moving, storage, utils |
d5695e0b59d9
saving results from ultralytics works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1231
diff
changeset
|
15 |
1246
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
16 parser = argparse.ArgumentParser(description='The program tracks objects using the ultralytics models and trackers.', |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
17 epilog= '''The models can be found in the Ultralytics model zoo, |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
18 eg YOLOv8 (https://docs.ultralytics.com/models/yolov8/). |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
19 The tracking models can be found also online |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
20 (https://github.com/ultralytics/ultralytics/tree/main/ultralytics/cfg/trackers). |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
21 The choice is to project the middle of the bottom line for persons, |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
22 and the bounding box center otherwise.''') |
1245
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
23 parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file') |
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
24 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file (overrides the configuration file)') |
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
25 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (overrides the configuration file)') |
1234
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
26 parser.add_argument('-m', dest = 'detectorFilename', help = 'name of the detection model file', required = True) |
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
27 parser.add_argument('-t', dest = 'trackerFilename', help = 'name of the tracker file', required = True) |
1246
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
28 parser.add_argument('-o', dest = 'homographyFilename', help = 'filename of the homography matrix') |
1249
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
29 parser.add_argument('-k', dest = 'maskFilename', help = 'name of the mask file') |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
30 parser.add_argument('--undistort', dest = 'undistort', help = 'undistort the video', action = 'store_true') |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
31 parser.add_argument('--intrinsic', dest = 'intrinsicCameraMatrixFilename', help = 'name of the intrinsic camera file') |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
32 parser.add_argument('--distortion-coefficients', dest = 'distortionCoefficients', help = 'distortion coefficients', nargs = '*', type = float) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
33 parser.add_argument('--display', dest = 'display', help = 'show the raw detection and tracking results', action = 'store_true') |
1245
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
34 parser.add_argument('--no-image-coordinates', dest = 'notSavingImageCoordinates', help = 'not saving the raw detection and tracking results', action = 'store_true') |
1234
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
35 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to process', type = int, default = 0) |
1249
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
36 parser.add_argument('-l', dest = 'lastFrameNum', help = 'number of last frame number to process', type = int, default = inf) |
1246
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
37 parser.add_argument('--conf', dest = 'confidence', help = 'object confidence threshold for detection', type = float, default = 0.25) |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
38 parser.add_argument('--bike-prop', dest = 'bikeProportion', help = 'minimum proportion of time a person classified as bike or motorbike to be classified as cyclist', type = float, default = 0.2) |
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
39 parser.add_argument('--cyclist-iou', dest = 'cyclistIou', help = 'IoU threshold to associate a bike and ped bounding box', type = float, default = 0.15) |
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
40 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) |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
41 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) |
1245
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
42 |
1219
8a626226793e
update where optimization uses either nomad-parameter file depending on optimizing 1 or 2 steps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
43 args = parser.parse_args() |
1246
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
44 params, videoFilename, databaseFilename, homography, invHomography, intrinsicCameraMatrix, distortionCoefficients, undistortedImageMultiplication, undistort, firstFrameNum = storage.processVideoArguments(args) |
1245
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
45 |
1246
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
46 if args.homographyFilename is not None: |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
47 homography = np.loadtxt(args.homographyFilename) |
1245
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
48 if args.intrinsicCameraMatrixFilename is not None: |
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
49 intrinsicCameraMatrix = loadtxt(args.intrinsicCameraMatrixFilename) |
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
50 if args.distortionCoefficients is not None: |
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
51 distortionCoefficients = args.distortionCoefficients |
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
52 if args.firstFrameNum is not None: |
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
53 firstFrameNum = args.firstFrameNum |
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
54 if args.lastFrameNum is not None: |
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
55 lastFrameNum = args.lastFrameNum |
1246
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
56 elif args.configFilename is not None: |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
57 lastFrameNum = params.lastFrameNum |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
58 else: |
1249
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
59 lastFrameNum = args.lastFrameNum |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
60 if args.maskFilename is not None: |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
61 mask = cv2.imread(args.maskFilename, cv2.IMREAD_GRAYSCALE) |
1250
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
62 elif params is not None and params.maskFilename is not None: |
1249
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
63 mask = cv2.imread(params.maskFilename, cv2.IMREAD_GRAYSCALE) |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
64 else: |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
65 mask = None |
1250
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
66 if params is not None: |
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
67 smoothingHalfWidth = params.smoothingHalfWidth |
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
68 else: |
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
69 smoothingHalfWidth = None |
1219
8a626226793e
update where optimization uses either nomad-parameter file depending on optimizing 1 or 2 steps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
70 |
1247
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
71 # TODO use mask, remove short objects, smooth |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
72 |
1246
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
73 # TODO add option to refine position with mask for vehicles, to save different positions |
1245
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
74 # TODO work with optical flow (farneback or RAFT) https://pytorch.org/vision/main/models/raft.html |
1230
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
75 |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
76 # use 2 x bytetrack track buffer to remove objects from existing ones |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
77 |
1219
8a626226793e
update where optimization uses either nomad-parameter file depending on optimizing 1 or 2 steps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
78 # Load a model |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
79 model = YOLO(args.detectorFilename) # seg yolov8x-seg.pt |
1219
8a626226793e
update where optimization uses either nomad-parameter file depending on optimizing 1 or 2 steps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
80 # seg could be used on cropped image... if can be loaded and kept in memory |
1246
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
81 # model = YOLOX('/home/nicolas/Research/Data/classification-models/yolo_nas_l.pt ') # AttributeError: 'YoloNAS_L' object has no attribute 'get' |
1219
8a626226793e
update where optimization uses either nomad-parameter file depending on optimizing 1 or 2 steps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
82 |
8a626226793e
update where optimization uses either nomad-parameter file depending on optimizing 1 or 2 steps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
83 # Track with the model |
8a626226793e
update where optimization uses either nomad-parameter file depending on optimizing 1 or 2 steps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
84 if args.display: |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
85 windowName = 'frame' |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
86 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) |
1234
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
87 |
1246
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
88 capture = cv2.VideoCapture(videoFilename) |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
89 objects = {} |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
90 featureNum = 1 |
1246
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
91 frameNum = firstFrameNum |
1234
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
92 capture.set(cv2.CAP_PROP_POS_FRAMES, frameNum) |
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
93 |
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
94 success, frame = capture.read() |
1240
bb14f919d1cb
cleaned use of centile (np only) and added info in classify-objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1238
diff
changeset
|
95 if not success: |
bb14f919d1cb
cleaned use of centile (np only) and added info in classify-objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1238
diff
changeset
|
96 print('Input {} could not be read. Exiting'.format(args.videoFilename)) |
bb14f919d1cb
cleaned use of centile (np only) and added info in classify-objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1238
diff
changeset
|
97 import sys; sys.exit() |
1245
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
98 |
1249
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
99 results = model.track(source=frame, tracker=args.trackerFilename, classes=list(moving.cocoTypeNames.keys()), conf=args.confidence, persist=True, verbose=False) |
1234
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
100 while capture.isOpened() and success and frameNum <= lastFrameNum: |
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
101 result = results[0] |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
102 if frameNum %10 == 0: |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
103 print(frameNum, len(result.boxes), 'objects') |
1233
d5695e0b59d9
saving results from ultralytics works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1231
diff
changeset
|
104 for box in result.boxes: |
1249
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
105 if box.id is not None:# None are objects with low confidence |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
106 xyxy = copy(box.xyxy) |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
107 minPoint = moving.Point(xyxy[0,0].item(), xyxy[0,1].item()) |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
108 maxPoint = moving.Point(xyxy[0,2].item(), xyxy[0,3].item()) |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
109 center = (minPoint+maxPoint).divide(2.).asint() |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
110 if mask is None or mask[center.y, center.x] > 0: |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
111 num = int(box.id.item()) |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
112 if num in objects: |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
113 objects[num].timeInterval.last = frameNum |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
114 objects[num].features[0].timeInterval.last = frameNum |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
115 objects[num].features[1].timeInterval.last = frameNum |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
116 objects[num].bboxes[frameNum] = xyxy |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
117 objects[num].userTypes.append(moving.coco2Types[int(box.cls.item())]) |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
118 objects[num].features[0].tmpPositions[frameNum] = minPoint # min |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
119 objects[num].features[1].tmpPositions[frameNum] = maxPoint # max |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
120 else: |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
121 inter = moving.TimeInterval(frameNum, frameNum) |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
122 objects[num] = moving.MovingObject(num, inter) |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
123 objects[num].bboxes = {frameNum: copy(xyxy)} |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
124 objects[num].userTypes = [moving.coco2Types[int(box.cls.item())]] |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
125 objects[num].features = [moving.MovingObject(featureNum, copy(inter)), moving.MovingObject(featureNum+1, copy(inter))] |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
126 objects[num].featureNumbers = [featureNum, featureNum+1] |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
127 objects[num].features[0].tmpPositions = {frameNum: minPoint} |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
128 objects[num].features[1].tmpPositions = {frameNum: maxPoint} |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
129 featureNum += 2 |
1233
d5695e0b59d9
saving results from ultralytics works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1231
diff
changeset
|
130 if args.display: |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
131 cvutils.cvImshow(windowName, result.plot()) # original image in orig_img |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
132 key = cv2.waitKey() |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
133 if cvutils.quitKey(key): |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
134 break |
1234
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
135 frameNum += 1 |
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
136 success, frame = capture.read() |
1249
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
137 results = model.track(source=frame, persist=True) |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
138 capture.release() |
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
139 cv2.destroyAllWindows() |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
140 |
1236
100fe098abe9
progress on classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1235
diff
changeset
|
141 # classification |
1250
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
142 shortObjectNumbers = [] |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
143 for num, obj in objects.items(): |
1250
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
144 if obj.length() < 3: |
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
145 shortObjectNumbers.append(num) |
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
146 else: |
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
147 obj.setUserType(utils.mostCommon(obj.userTypes)) # improve? mix with speed? |
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
148 for num in shortObjectNumbers: |
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
149 del objects[num] |
1246
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
150 # TODO add quality control: avoid U-turns |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
151 |
1236
100fe098abe9
progress on classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1235
diff
changeset
|
152 # merge bikes and people |
1245
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
153 twowheels = [num for num, obj in objects.items() if obj.getUserType() in (moving.userType2Num['motorcyclist'],moving.userType2Num['cyclist'])] |
371c718e57d7
interface updates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1242
diff
changeset
|
154 pedestrians = [num for num, obj in objects.items() if obj.getUserType() == moving.userType2Num['pedestrian']] |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
155 |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
156 def mergeObjects(obj1, obj2): |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
157 obj1.features = obj1.features+obj2.features |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
158 obj1.featureNumbers = obj1.featureNumbers+obj2.featureNumbers |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
159 obj1.timeInterval = moving.TimeInterval(min(obj1.getFirstInstant(), obj2.getFirstInstant()), max(obj1.getLastInstant(), obj2.getLastInstant())) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
160 |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
161 costs = [] |
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
162 for twInd in twowheels: |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
163 tw = objects[twInd] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
164 tw.nBBoxes = len(tw.bboxes) |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
165 twCost = [] |
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
166 for pedInd in pedestrians: |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
167 ped = objects[pedInd] |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
168 nmatches = 0 |
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
169 for t in tw.bboxes: |
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
170 if t in ped.bboxes: |
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
171 #print(tw.num, ped.num, t, box_iou(tw.bboxes[t], ped.bboxes[t])) |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
172 if not tw.commonTimeInterval(ped).empty() and box_iou(tw.bboxes[t], ped.bboxes[t]).item() > args.cyclistIou: |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
173 nmatches += 1 |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
174 twCost.append(nmatches/tw.nBBoxes) |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
175 costs.append(twCost) |
1236
100fe098abe9
progress on classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1235
diff
changeset
|
176 |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
177 costs = -np.array(costs) |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
178 |
1247
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
179 if costs.size > 0: |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
180 # before matching, scan for pedestrians with good non-overlapping temporal match with different bikes |
1247
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
181 for pedInd in range(costs.shape[1]): |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
182 nMatchedBikes = (costs[:,pedInd] < -args.cyclistMatchingProportion).sum() |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
183 if nMatchedBikes == 0: # peds that have no bike matching: see if they have been classified as bikes sometimes |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
184 userTypeStats = Counter(obj.userTypes) |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
185 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 |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
186 obj.setUserType(moving.userType2Num['cyclist']) |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
187 elif nMatchedBikes > 1: # try to merge bikes first |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
188 twIndices = np.nonzero(costs[:,pedInd] < -args.cyclistMatchingProportion)[0] |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
189 # 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 |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
190 nTwoWheels = len(twIndices) |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
191 twTemporalOverlaps = np.zeros((nTwoWheels,nTwoWheels)) |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
192 for i in range(nTwoWheels): |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
193 for j in range(i): |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
194 twi = objects[twowheels[twIndices[i]]] |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
195 twj = objects[twowheels[twIndices[j]]] |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
196 twTemporalOverlaps[i,j] = len(set(twi.bboxes).intersection(set(twj.bboxes)))/max(len(twi.bboxes), len(twj.bboxes)) |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
197 #twTemporalOverlaps[j,i] = twTemporalOverlaps[i,j] |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
198 tw2merge = list(range(nTwoWheels)) |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
199 while len(tw2merge)>0 and (twTemporalOverlaps[np.ix_(tw2merge, tw2merge)] > args.maxTemporalOverlap).sum(0).max() >= 2: |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
200 i = (twTemporalOverlaps[np.ix_(tw2merge, tw2merge)] > args.maxTemporalOverlap).sum(0).argmax() |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
201 del tw2merge[i] |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
202 twIndices = [twIndices[i] for i in tw2merge] |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
203 tw1 = objects[twowheels[twIndices[0]]] |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
204 twCost = costs[twIndices[0],:]*tw1.nBBoxes |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
205 nBBoxes = tw1.nBBoxes |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
206 for twInd in twIndices[1:]: |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
207 mergeObjects(tw1, objects[twowheels[twInd]]) |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
208 twCost = twCost + costs[twInd,:]*objects[twowheels[twInd]].nBBoxes |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
209 nBBoxes += objects[twowheels[twInd]].nBBoxes |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
210 twIndicesToKeep = list(range(costs.shape[0])) |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
211 for twInd in twIndices[1:]: |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
212 twIndicesToKeep.remove(twInd) |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
213 del objects[twowheels[twInd]] |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
214 twowheels = [twowheels[i] for i in twIndicesToKeep] |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
215 costs = costs[twIndicesToKeep,:] |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
216 |
1247
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
217 twIndices, matchingPedIndices = linear_sum_assignment(costs) |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
218 for twInd, pedInd in zip(twIndices, matchingPedIndices): # caution indices in the cost matrix |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
219 if -costs[twInd, pedInd] >= args.cyclistMatchingProportion: |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
220 tw = objects[twowheels[twInd]] |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
221 ped = objects[pedestrians[pedInd]] |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
222 mergeObjects(tw, ped) |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
223 del objects[pedestrians[pedInd]] |
439207b6c146
bug corrected
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1246
diff
changeset
|
224 #TODO Verif overlap piéton vélo : si long hors overlap, changement mode (trouver exemples) |
1236
100fe098abe9
progress on classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1235
diff
changeset
|
225 |
1246
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
226 # interpolate and save image coordinates |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
227 for num, obj in objects.items(): |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
228 for f in obj.getFeatures(): |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
229 if f.length() != len(f.tmpPositions): # interpolate |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
230 f.positions = moving.Trajectory.fromPointDict(f.tmpPositions) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
231 else: |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
232 f.positions = moving.Trajectory.fromPointList(list(f.tmpPositions.values())) |
1246
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
233 if not args.notSavingImageCoordinates: |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
234 storage.saveTrajectoriesToSqlite(utils.removeExtension(args.databaseFilename)+'-bb.sqlite', list(objects.values()), 'object') |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
235 # project, smooth and save |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
236 for num, obj in objects.items(): |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
237 features = obj.getFeatures() |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
238 if moving.userTypeNames[obj.getUserType()] == 'pedestrian': |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
239 assert len(features) == 2 |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
240 t1 = features[0].getPositions() |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
241 t2 = features[1].getPositions() |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
242 t = [[(p1.x+p2.x)/2., max(p1.y, p2.y)] for p1, p2 in zip(t1, t2)] |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
243 else: |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
244 t = [] |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
245 for instant in obj.getTimeInterval(): |
1249
2aa56b101041
added mask functionality for dltrack
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1247
diff
changeset
|
246 points = [f.getPositionAtInstant(instant) for f in features if f.existsAtInstant(instant)] |
1246
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
247 t.append(moving.Point.agg(points, np.mean).aslist()) |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
248 #t = sum([f.getPositions().asArray() for f in features])/len(features) |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
249 #t = (moving.Trajectory.add(t1, t2)*0.5).asArray() |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
250 projected = cvutils.imageToWorldProject(np.array(t).T, intrinsicCameraMatrix, distortionCoefficients, homography) |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
251 featureNum = features[0].getNum() |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
252 obj.features=[moving.MovingObject(featureNum, obj.getTimeInterval(), moving.Trajectory(projected.tolist()))] |
2397de73770d
dltrack saves after projecting coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1245
diff
changeset
|
253 obj.featureNumbers = [featureNum] |
1250
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
254 if smoothingHalfWidth is not None: # smoothing |
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
255 for num, obj in objects.items(): |
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
256 for f in obj.getFeatures(): |
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
257 f.positions = f.getPositions().filterMovingWindow(smoothingHalfWidth) |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
258 storage.saveTrajectoriesToSqlite(args.databaseFilename, list(objects.values()), 'object') |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
259 |
1250
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
260 |
77fbd0e2ba7d
dltrack works with moving average filtering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1249
diff
changeset
|
261 |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
262 # todo save bbox and mask to study localization / representation |
1233
d5695e0b59d9
saving results from ultralytics works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1231
diff
changeset
|
263 # apply quality checks deviation and acceleration bounds? |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
264 |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
265 # def mergeBBoxes(tw, ped): |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
266 # 'merges ped into tw (2nd obj into first obj)' |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
267 # timeInstants = set(tw.bboxes).union(set(ped.bboxes)) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
268 # for t in timeInstants: |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
269 # if t in tw.bboxes and t in ped.bboxes: |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
270 # tw.features[0].tmpPositions[t] = moving.Point(min(tw.features[0].tmpPositions[t].x, ped.features[0].tmpPositions[t].x), |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
271 # min(tw.features[0].tmpPositions[t].y, ped.features[0].tmpPositions[t].y)) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
272 # tw.features[1].tmpPositions[t] = moving.Point(max(tw.features[1].tmpPositions[t].x, ped.features[1].tmpPositions[t].x), |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
273 # max(tw.features[1].tmpPositions[t].y, ped.features[1].tmpPositions[t].y)) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
274 # elif t in ped.bboxes: |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
275 # tw.features[0].tmpPositions[t] = ped.features[0].tmpPositions[t] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
276 # tw.features[1].tmpPositions[t] = ped.features[1].tmpPositions[t] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
277 # tw.timeInterval = moving.TimeInterval(min(tw.getFirstInstant(), ped.getFirstInstant()), max(tw.getLastInstant(), ped.getLastInstant())) |