Mercurial Hosting > traffic-intelligence
annotate scripts/dltrack.py @ 1231:6487ef10c0e0
work in progress
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Thu, 24 Aug 2023 17:06:16 -0400 |
parents | c582b272108f |
children | d5695e0b59d9 |
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 |
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
|
4 |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
5 from trafficintelligence import cvutils, moving, storage |
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
|
6 from ultralytics import YOLO |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
7 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
|
8 |
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 parser = argparse.ArgumentParser(description='The program tracks objects following the ultralytics yolo executable.')#, epilog = 'Either the configuration filename or the other parameters (at least video and database filenames) need to be provided.') |
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
|
10 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (overrides the configuration file)') |
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
|
11 # detect 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
|
12 # tracker 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
|
13 parser.add_argument('--display', dest = 'display', help = 'show the results (careful with long videos, risk of running out of memory)', action = 'store_true') |
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
|
14 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
|
15 |
1230
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
16 # required functionality? |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
17 # # filename of the video to process (can be images, eg image%04d.png) |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
18 # video-filename = laurier.avi |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
19 # # filename of the database where results are saved |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
20 # database-filename = laurier.sqlite |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
21 # # filename of the homography matrix |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
22 # homography-filename = laurier-homography.txt |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
23 # # filename of the camera intrinsic matrix |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
24 # intrinsic-camera-filename = intrinsic-camera.txt |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
25 # # -0.11759321 0.0148536 0.00030756 -0.00020578 -0.00091816 |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
26 # distortion-coefficients = -0.11759321 |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
27 # distortion-coefficients = 0.0148536 |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
28 # distortion-coefficients = 0.00030756 |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
29 # distortion-coefficients = -0.00020578 |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
30 # distortion-coefficients = -0.00091816 |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
31 # # undistorted image multiplication |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
32 # undistorted-size-multiplication = 1.31 |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
33 # # Interpolation method for remapping image when correcting for distortion: 0 for INTER_NEAREST - a nearest-neighbor interpolation; 1 for INTER_LINEAR - a bilinear interpolation (used by default); 2 for INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood; 3 for INTER_LANCZOS4 |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
34 # interpolation-method = 1 |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
35 # # filename of the mask image (where features are detected) |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
36 # mask-filename = none |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
37 # # undistort the video for feature tracking |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
38 # undistort = false |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
39 # # load features from database |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
40 # load-features = false |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
41 # # display trajectories on the video |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
42 # display = false |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
43 # # original video frame rate (number of frames/s) |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
44 # video-fps = 29.97 |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
45 # # number of digits of precision for all measurements derived from video |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
46 # # measurement-precision = 3 |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
47 # # first frame to process |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
48 # frame1 = 0 |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
49 # # number of frame to process: 0 means processing all frames |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
50 # nframes = 0 |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
51 |
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
52 # 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
|
53 |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
54 # 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
|
55 |
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
|
56 # Load a model |
1221
5a207c838323
correcting recursive errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1219
diff
changeset
|
57 model = YOLO('/home/nicolas/Research/Data/classification-models/yolov8x.pt') # 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
|
58 # 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
|
59 # 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
|
60 |
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
|
61 # 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
|
62 if args.display: |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
63 results = model.track(source=args.videoFilename, tracker="/home/nicolas/Research/Data/classification-models/bytetrack.yaml", classes=list(moving.cocoTypeNames.keys()), show=True) # , save_txt=True |
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
|
64 else: |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
65 windowName = 'frame' |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
66 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
67 |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
68 results = model.track(source=args.videoFilename, tracker="/home/nicolas/Research/Data/classification-models/bytetrack.yaml", classes=list(moving.cocoTypeNames.keys()), stream=True) |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
69 objects = [] |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
70 currentObjects = {} |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
71 featureNum = 0 |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
72 # create object with user type and list of 3 features (bottom ones and middle) + projection |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
73 for frameNum, result in enumerate(results): |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
74 print(frameNum, len(result.boxes)) |
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
|
75 for box in result.boxes: |
1231
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
76 num = int(box.id) |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
77 xyxy = box.xyxy[0].tolist() |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
78 if num in currentObjects: |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
79 currentObjects[num].timeInterval.last = frameNum |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
80 features = currentObjects[num].features |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
81 features[0].getPositions().addPositionXY(xyxy[0],xyxy[1]) |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
82 features[1].getPositions().addPositionXY(xyxy[2],xyxy[3]) |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
83 else: |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
84 currentObjects[num] = moving.MovingObject(num, moving.TimeInterval(frameNum,frameNum), userType = moving.coco2Types[int(box.cls)]) |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
85 currentObjects[num].features = [moving.MovingObject(featureNum, moving.TimeInterval(frameNum, frameNum), moving.Trajectory([[xyxy[0]],[xyxy[1]]])), |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
86 moving.MovingObject(featureNum+1, moving.TimeInterval(frameNum, frameNum), moving.Trajectory([[xyxy[2]],[xyxy[3]]]))] |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
87 currentObjects[num].featureNumbers = [featureNum, featureNum+1] |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
88 featureNum += 2 |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
89 print(box.cls, box.xyxy) |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
90 cvutils.cvImshow(windowName, result.plot()) # original image in orig_img |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
91 key = cv2.waitKey() |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
92 if cvutils.quitKey(key): |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
93 break |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
94 |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
95 storage.saveTrajectoriesToSqlite('test.sqlite', list(currentObjects.values()), 'object') |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
96 |
6487ef10c0e0
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1230
diff
changeset
|
97 # todo save bbox and mask to study localization / representation |