Mercurial > hg > nsaunier > traffic-intelligence
annotate scripts/init-tracking.py @ 1188:82fc21f6dfdf
info is on the bitbucket documentation repository at docs/tracking-optimization.md
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 21 Jun 2022 23:22:19 +0200 |
| parents | cdf1773ba89e |
| children |
| rev | line source |
|---|---|
|
998
933670761a57
updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
858
diff
changeset
|
1 #! /usr/bin/env python3 |
|
747
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
2 |
|
1028
cc5cb04b04b0
major update using the trafficintelligence package name and install through pip
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1021
diff
changeset
|
3 import sys, argparse, os.path |
|
858
2faabcbde2c4
minor improvements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
747
diff
changeset
|
4 from shutil import copy |
|
1028
cc5cb04b04b0
major update using the trafficintelligence package name and install through pip
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1021
diff
changeset
|
5 |
|
1102
cdf1773ba89e
corrected bug
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1028
diff
changeset
|
6 from trafficintelligence.cvutils import getImagesFromVideo |
|
747
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
7 from matplotlib.pyplot import imsave |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
8 |
|
1028
cc5cb04b04b0
major update using the trafficintelligence package name and install through pip
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1021
diff
changeset
|
9 from trafficintelligence import storage, utils |
|
cc5cb04b04b0
major update using the trafficintelligence package name and install through pip
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1021
diff
changeset
|
10 |
|
747
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
11 # could try to guess the video |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
12 # check if there is already a tracking.cfg file |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
13 |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
14 parser = argparse.ArgumentParser(description='The program initilizes the files for tracking: copy tracking.cfg, sets up with the video filename, generates a frame image (frame.png) and prints the next commands') |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
15 |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
16 parser.add_argument('-i', dest = 'videoFilename', help = 'filename of the video sequence', required = True) |
|
1021
16932cefabc1
work on paths in line with new configurations from tracker
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
998
diff
changeset
|
17 parser.add_argument('-n', dest = 'nFrames', help = 'number of frames to extract', type = int) |
|
747
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
18 |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
19 args = parser.parse_args() |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
20 |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
21 # assumes tracking.cfg is in the parent directory to the directory of the traffic intelligence python modules |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
22 matchingPaths = [s for s in sys.path if 'traffic-intelligence' in s] |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
23 #if len(matchingPaths) > 1: |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
24 # print('Too many matching paths for Traffic Intelligence modules: {}'.format(matchingPaths)) |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
25 if len(matchingPaths) == 0: |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
26 print('No environment path to Traffic Intelligence modules.\nExiting') |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
27 sys.exit() |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
28 else: |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
29 directoryName = matchingPaths[0] |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
30 if directoryName.endswith('/'): |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
31 directoryName = directoryName[:-1] |
|
1102
cdf1773ba89e
corrected bug
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1028
diff
changeset
|
32 if os.path.exists(directoryName+'/tracking.cfg') and not os.path.exists('./tracking.cfg'): |
|
cdf1773ba89e
corrected bug
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1028
diff
changeset
|
33 f = utils.openCheck(directoryName+'/tracking.cfg') |
|
cdf1773ba89e
corrected bug
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1028
diff
changeset
|
34 out = utils.openCheck('./tracking.cfg', 'w') |
|
747
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
35 for l in f: |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
36 if 'video-filename' in l: |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
37 tmp = l.split('=') |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
38 out.write(tmp[0]+'= '+args.videoFilename+'\n') |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
39 elif 'database-filename' in l: |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
40 tmp = l.split('=') |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
41 out.write(tmp[0]+'= '+utils.removeExtension(args.videoFilename)+'.sqlite\n') |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
42 else: |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
43 out.write(l) |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
44 f.close() |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
45 out.close() |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
46 print('Configuration file tracking.cfg successfully copied to the current directory with video and database filename adapted') |
|
1102
cdf1773ba89e
corrected bug
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1028
diff
changeset
|
47 if os.path.exists(directoryName+'/classifier.cfg') and not os.path.exists('./classifier.cfg'): |
|
cdf1773ba89e
corrected bug
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1028
diff
changeset
|
48 copy(directoryName+'/classifier.cfg', 'classifier.cfg') |
|
858
2faabcbde2c4
minor improvements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
747
diff
changeset
|
49 print('Configuration file classifier.cfg successfully copied to the current directory') |
|
2faabcbde2c4
minor improvements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
747
diff
changeset
|
50 |
|
747
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
51 # extract image from video |
|
1021
16932cefabc1
work on paths in line with new configurations from tracker
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
998
diff
changeset
|
52 if args.nFrames is not None: |
|
16932cefabc1
work on paths in line with new configurations from tracker
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
998
diff
changeset
|
53 image = getImagesFromVideo(args.videoFilename, 0, args.nFrames, saveImage = True, outputPrefix = 'frame') |
|
16932cefabc1
work on paths in line with new configurations from tracker
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
998
diff
changeset
|
54 else: |
|
16932cefabc1
work on paths in line with new configurations from tracker
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
998
diff
changeset
|
55 image = getImagesFromVideo(args.videoFilename, saveImage = True, outputPrefix = 'frame') |
|
747
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
56 print('first video frame successfully copied to the current directory') |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
57 |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
58 # next commands |
|
1021
16932cefabc1
work on paths in line with new configurations from tracker
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
998
diff
changeset
|
59 print('''-------------------------------------- |
|
16932cefabc1
work on paths in line with new configurations from tracker
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
998
diff
changeset
|
60 Here are a sample of the next command to compute the homography, |
|
16932cefabc1
work on paths in line with new configurations from tracker
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
998
diff
changeset
|
61 track features, group them in objects and display object trajectories |
|
16932cefabc1
work on paths in line with new configurations from tracker
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
998
diff
changeset
|
62 --------------------------------------''') |
|
16932cefabc1
work on paths in line with new configurations from tracker
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
998
diff
changeset
|
63 print('compute-homography -i [frame.png] -w [world_image] -n [npoints] -u [unit_per_pixel]') |
|
16932cefabc1
work on paths in line with new configurations from tracker
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
998
diff
changeset
|
64 print('(beware of camera distortion)') |
|
747
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
65 print('feature-based-tracking tracking.cfg --tf') |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
66 print('feature-based-tracking tracking.cfg --gf') |
|
d45ab817ee11
added script to initialize tracking (copying and setting up basic files)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
67 print('display-trajectories --cfg tracking.cfg -t object') |
