Mercurial > hg > nsaunier > traffic-intelligence
annotate scripts/dltrack.py @ 1230:c582b272108f
(minor) work in progress
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Mon, 21 Aug 2023 15:49:32 -0400 |
| parents | 759d76d6d20c |
| children | 6487ef10c0e0 |
| 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 |
|
1229
759d76d6d20c
minor variable name update
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1221
diff
changeset
|
5 from trafficintelligence.moving import cocoTypeNames |
|
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 |
|
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
|
7 |
|
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 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
|
9 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
|
10 # 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
|
11 # 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
|
12 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
|
13 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
|
14 |
|
1230
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
15 # required functionality? |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
16 # # 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
|
17 # video-filename = laurier.avi |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
18 # # filename of the database where results are saved |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
19 # database-filename = laurier.sqlite |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
20 # # filename of the homography matrix |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
21 # homography-filename = laurier-homography.txt |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
22 # # filename of the camera intrinsic matrix |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
23 # intrinsic-camera-filename = intrinsic-camera.txt |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
24 # # -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
|
25 # distortion-coefficients = -0.11759321 |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
26 # distortion-coefficients = 0.0148536 |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
27 # distortion-coefficients = 0.00030756 |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
28 # distortion-coefficients = -0.00020578 |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
29 # distortion-coefficients = -0.00091816 |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
30 # # undistorted image multiplication |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
31 # undistorted-size-multiplication = 1.31 |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
32 # # 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
|
33 # interpolation-method = 1 |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
34 # # filename of the mask image (where features are detected) |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
35 # mask-filename = none |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
36 # # undistort the video for feature tracking |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
37 # undistort = false |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
38 # # load features from database |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
39 # load-features = false |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
40 # # display trajectories on the video |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
41 # display = false |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
42 # # original video frame rate (number of frames/s) |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
43 # video-fps = 29.97 |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
44 # # 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
|
45 # # measurement-precision = 3 |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
46 # # first frame to process |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
47 # frame1 = 0 |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
48 # # 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
|
49 # nframes = 0 |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
50 |
|
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
51 # 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
|
52 |
|
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
|
53 # Load a model |
|
1221
5a207c838323
correcting recursive errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1219
diff
changeset
|
54 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
|
55 # 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
|
56 # 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
|
57 |
|
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 # 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
|
59 if args.display: |
|
1229
759d76d6d20c
minor variable name update
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1221
diff
changeset
|
60 results = model.track(source=args.videoFilename, tracker="/home/nicolas/Research/Data/classification-models/bytetrack.yaml", classes=list(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
|
61 else: |
|
1229
759d76d6d20c
minor variable name update
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1221
diff
changeset
|
62 results = model.track(source=args.videoFilename, tracker="/home/nicolas/Research/Data/classification-models/bytetrack.yaml", classes=list(cocoTypeNames.keys()), stream=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
|
63 for result in results: |
|
1230
c582b272108f
(minor) work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1229
diff
changeset
|
64 print(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
|
65 for box in result.boxes: |
|
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
|
66 print(box.xyxy) |
