Mercurial Hosting > traffic-intelligence
annotate scripts/dltrack.py @ 1243:88eedf79f16a
correct classifier.cfg and info classification
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 07 Feb 2024 11:53:00 -0500 |
parents | 4cd8ace3552f |
children | 371c718e57d7 |
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 |
1233
d5695e0b59d9
saving results from ultralytics works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1231
diff
changeset
|
4 from copy import copy |
1236
100fe098abe9
progress on classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1235
diff
changeset
|
5 from collections import Counter |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
6 import numpy as np |
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
7 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
|
8 from ultralytics import YOLO |
1236
100fe098abe9
progress on classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1235
diff
changeset
|
9 from torch import cat |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
10 from torchvision.ops import box_iou |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
11 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
|
12 |
1233
d5695e0b59d9
saving results from ultralytics works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1231
diff
changeset
|
13 from trafficintelligence import cvutils, moving, storage, utils |
d5695e0b59d9
saving results from ultralytics works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1231
diff
changeset
|
14 |
1240
bb14f919d1cb
cleaned use of centile (np only) and added info in classify-objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1238
diff
changeset
|
15 parser = argparse.ArgumentParser(description='The program tracks objects using the ultralytics models and trakcers.') |
1234
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
16 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file', required = True) |
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
17 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True) |
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
18 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
|
19 parser.add_argument('-t', dest = 'trackerFilename', help = 'name of the tracker file', required = True) |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
20 parser.add_argument('-o', dest = 'homographyFilename', help = 'filename of the homography matrix', default = 'homography.txt') |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
21 parser.add_argument('-k', dest = 'maskFilename', help = 'name of the mask file') |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
22 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
|
23 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
|
24 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
|
25 parser.add_argument('--display', dest = 'display', help = 'show 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
|
26 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to process', type = int, default = 0) |
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
27 parser.add_argument('-l', dest = 'lastFrameNum', help = 'number of last frame number to process', type = int, default = float('Inf')) |
1242
4cd8ace3552f
major update for classification, allowing the use of neural network classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
28 parser.add_argument('--conf', dest = 'confindence', 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
|
29 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
|
30 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
|
31 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
|
32 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) |
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
|
33 args = parser.parse_args() |
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
|
34 |
1230
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
35 # TODO add option to refine position with mask for vehicles |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
36 |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
37 # 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
|
38 |
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
|
39 # Load a model |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
40 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
|
41 # seg could be used on cropped image... if can be loaded and kept in memory |
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
|
42 # model = YOLO('/home/nicolas/Research/Data/classification-models/yolo_nas_l.pt ') # AttributeError: 'YoloNAS_L' object has no attribute 'get' |
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 |
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
|
44 # 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
|
45 if args.display: |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
46 windowName = 'frame' |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
47 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) |
1234
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
48 |
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
49 capture = cv2.VideoCapture(args.videoFilename) |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
50 objects = {} |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
51 featureNum = 1 |
1234
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
52 frameNum = args.firstFrameNum |
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
53 capture.set(cv2.CAP_PROP_POS_FRAMES, frameNum) |
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
54 lastFrameNum = args.lastFrameNum |
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
55 |
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
56 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
|
57 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
|
58 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
|
59 import sys; sys.exit() |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
60 results = model.track(frame, tracker=args.trackerFilename, classes=list(moving.cocoTypeNames.keys()), persist=True, verbose=False) |
1233
d5695e0b59d9
saving results from ultralytics works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1231
diff
changeset
|
61 # create object with user type and list of 3 features (bottom ones and middle) + projection |
1234
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
62 while capture.isOpened() and success and frameNum <= lastFrameNum: |
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
63 #for frameNum, result in enumerate(results): |
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
64 result = results[0] |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
65 if frameNum %10 == 0: |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
66 print(frameNum, len(result.boxes), 'objects') |
1233
d5695e0b59d9
saving results from ultralytics works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1231
diff
changeset
|
67 for box in result.boxes: |
d5695e0b59d9
saving results from ultralytics works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1231
diff
changeset
|
68 #print(box.cls, box.id, box.xyxy) |
d5695e0b59d9
saving results from ultralytics works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1231
diff
changeset
|
69 if box.id is not None: # None are objects with low confidence |
1236
100fe098abe9
progress on classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1235
diff
changeset
|
70 num = int(box.id.item()) |
100fe098abe9
progress on classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1235
diff
changeset
|
71 #xyxy = box.xyxy[0].tolist() |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
72 if num in objects: |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
73 objects[num].timeInterval.last = frameNum |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
74 objects[num].features[0].timeInterval.last = frameNum |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
75 objects[num].features[1].timeInterval.last = frameNum |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
76 objects[num].bboxes[frameNum] = copy(box.xyxy) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
77 objects[num].userTypes.append(moving.coco2Types[int(box.cls.item())]) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
78 objects[num].features[0].tmpPositions[frameNum] = moving.Point(box.xyxy[0,0].item(), box.xyxy[0,1].item()) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
79 objects[num].features[1].tmpPositions[frameNum] = moving.Point(box.xyxy[0,2].item(), box.xyxy[0,3].item()) |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
80 else: |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
81 inter = moving.TimeInterval(frameNum, frameNum) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
82 objects[num] = moving.MovingObject(num, inter) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
83 objects[num].bboxes = {frameNum: copy(box.xyxy)} |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
84 objects[num].userTypes = [moving.coco2Types[int(box.cls.item())]] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
85 objects[num].features = [moving.MovingObject(featureNum, copy(inter)), moving.MovingObject(featureNum+1, copy(inter))] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
86 objects[num].featureNumbers = [featureNum, featureNum+1] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
87 objects[num].features[0].tmpPositions = {frameNum: moving.Point(box.xyxy[0,0].item(), box.xyxy[0,1].item())} |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
88 objects[num].features[1].tmpPositions = {frameNum: moving.Point(box.xyxy[0,2].item(), box.xyxy[0,3].item())} |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
89 featureNum += 2 |
1233
d5695e0b59d9
saving results from ultralytics works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1231
diff
changeset
|
90 if args.display: |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
91 cvutils.cvImshow(windowName, result.plot()) # original image in orig_img |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
92 key = cv2.waitKey() |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
93 if cvutils.quitKey(key): |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
94 break |
1234
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
95 frameNum += 1 |
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
96 success, frame = capture.read() |
dd969637381e
work on tracker interface
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1233
diff
changeset
|
97 results = model.track(frame, persist=True) |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
98 |
1236
100fe098abe9
progress on classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1235
diff
changeset
|
99 # classification |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
100 for num, obj in objects.items(): |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
101 obj.setUserType(utils.mostCommon(obj.userTypes)) # improve? mix with speed? |
1236
100fe098abe9
progress on classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1235
diff
changeset
|
102 |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
103 # add quality control: avoid U-turns |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
104 |
1236
100fe098abe9
progress on classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1235
diff
changeset
|
105 # merge bikes and people |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
106 twowheels = [num for num, obj in objects.items() if obj.getUserType() in (3,4)] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
107 pedestrians = [num for num, obj in objects.items() if obj.getUserType() == 2] |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
108 |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
109 def mergeObjects(obj1, obj2): |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
110 obj1.features = obj1.features+obj2.features |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
111 obj1.featureNumbers = obj1.featureNumbers+obj2.featureNumbers |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
112 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
|
113 |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
114 costs = [] |
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
115 for twInd in twowheels: |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
116 tw = objects[twInd] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
117 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
|
118 twCost = [] |
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
119 for pedInd in pedestrians: |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
120 ped = objects[pedInd] |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
121 nmatches = 0 |
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
122 for t in tw.bboxes: |
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
123 if t in ped.bboxes: |
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
124 #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
|
125 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
|
126 nmatches += 1 |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
127 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
|
128 costs.append(twCost) |
1236
100fe098abe9
progress on classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1235
diff
changeset
|
129 |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
130 costs = -np.array(costs) |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
131 |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
132 # before matching, scan for pedestrians with good non-overlapping temporal match with different bikes |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
133 for pedInd in range(costs.shape[1]): |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
134 nMatchedBikes = (costs[:,pedInd] < -args.cyclistMatchingProportion).sum() |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
135 if nMatchedBikes == 0: # peds that have no bike matching: see if they have been classified as bikes sometimes |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
136 userTypeStats = Counter(obj.userTypes) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
137 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) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
138 obj.setUserType(4) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
139 elif nMatchedBikes > 1: # try to merge bikes first |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
140 twIndices = np.nonzero(costs[:,pedInd] < -args.cyclistMatchingProportion)[0] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
141 # 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 |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
142 nTwoWheels = len(twIndices) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
143 twTemporalOverlaps = np.zeros((nTwoWheels,nTwoWheels)) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
144 for i in range(nTwoWheels): |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
145 for j in range(i): |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
146 twi = objects[twowheels[twIndices[i]]] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
147 twj = objects[twowheels[twIndices[j]]] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
148 twTemporalOverlaps[i,j] = len(set(twi.bboxes).intersection(set(twj.bboxes)))/max(len(twi.bboxes), len(twj.bboxes)) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
149 #twTemporalOverlaps[j,i] = twTemporalOverlaps[i,j] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
150 tw2merge = list(range(nTwoWheels)) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
151 while len(tw2merge)>0 and (twTemporalOverlaps[np.ix_(tw2merge, tw2merge)] > args.maxTemporalOverlap).sum(0).max() >= 2: |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
152 i = (twTemporalOverlaps[np.ix_(tw2merge, tw2merge)] > args.maxTemporalOverlap).sum(0).argmax() |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
153 del tw2merge[i] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
154 twIndices = [twIndices[i] for i in tw2merge] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
155 tw1 = objects[twowheels[twIndices[0]]] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
156 twCost = costs[twIndices[0],:]*tw1.nBBoxes |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
157 nBBoxes = tw1.nBBoxes |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
158 for twInd in twIndices[1:]: |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
159 mergeObjects(tw1, objects[twowheels[twInd]]) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
160 twCost = twCost + costs[twInd,:]*objects[twowheels[twInd]].nBBoxes |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
161 nBBoxes += objects[twowheels[twInd]].nBBoxes |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
162 twIndicesToKeep = list(range(costs.shape[0])) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
163 for twInd in twIndices[1:]: |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
164 twIndicesToKeep.remove(twInd) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
165 del objects[twowheels[twInd]] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
166 twowheels = [twowheels[i] for i in twIndicesToKeep] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
167 costs = costs[twIndicesToKeep,:] |
1237
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
168 |
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
169 twIndices, matchingPedIndices = linear_sum_assignment(costs) |
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
170 for twInd, pedInd in zip(twIndices, matchingPedIndices): # caution indices in the cost matrix |
31a441efca6c
ped-bike grouping working, bike merging left todo
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1236
diff
changeset
|
171 if -costs[twInd, pedInd] >= args.cyclistMatchingProportion: |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
172 tw = objects[twowheels[twInd]] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
173 ped = objects[pedestrians[pedInd]] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
174 mergeObjects(tw, ped) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
175 del objects[pedestrians[pedInd]] |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
176 #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
|
177 |
1238
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
178 # interpolate and generate velocity (?) for the features (bboxes) before saving |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
179 for num, obj in objects.items(): |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
180 #obj.features[1].timeInterval = copy(obj.getTimeInterval()) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
181 for f in obj.getFeatures(): |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
182 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
|
183 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
|
184 #obj.features[1].positions = moving.Trajectory.fromPointDict(obj.features[1].tmpPositions) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
185 else: |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
186 f.positions = moving.Trajectory.fromPointList(list(f.tmpPositions.values())) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
187 #obj.features[1].positions = moving.Trajectory.fromPointList(list(obj.features[1].tmpPositions.values())) |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
188 |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
189 storage.saveTrajectoriesToSqlite(args.databaseFilename, list(objects.values()), 'object') |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
190 |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
191 # 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
|
192 # 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
|
193 |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
194 # def mergeBBoxes(tw, ped): |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
195 # '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
|
196 # 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
|
197 # for t in timeInstants: |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
198 # 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
|
199 # 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
|
200 # 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
|
201 # 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
|
202 # 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
|
203 # elif t in ped.bboxes: |
b684135d817f
version 1 of dltrack without coordinate projection
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1237
diff
changeset
|
204 # 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
|
205 # 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
|
206 # tw.timeInterval = moving.TimeInterval(min(tw.getFirstInstant(), ped.getFirstInstant()), max(tw.getLastInstant(), ped.getLastInstant())) |