Mercurial Hosting > traffic-intelligence
comparison python/ubc_utils.py @ 47:e27598865af3
modified loadtrajectories to load objects and prototypes
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Thu, 23 Sep 2010 17:12:09 -0400 |
parents | be3ae926e4e8 |
children | 8aed225f71d8 |
comparison
equal
deleted
inserted
replaced
46:b5d007612e16 | 47:e27598865af3 |
---|---|
6 | 6 |
7 __metaclass__ = type | 7 __metaclass__ = type |
8 | 8 |
9 delimiterChar = '%'; | 9 delimiterChar = '%'; |
10 | 10 |
11 def getFileType(s): | |
12 filePrefix = utils.removeExtension(s) | |
13 i = filePrefix.rfind('-') | |
14 if i>0: | |
15 return filePrefix[i+1:] | |
16 else: | |
17 return '' | |
18 | |
11 def getLines(f): | 19 def getLines(f): |
12 '''Gets a complete entry (all the lines) in between delimiterChar.''' | 20 '''Gets a complete entry (all the lines) in between delimiterChar.''' |
13 dataStrings = [] | 21 dataStrings = [] |
14 s = utils.readline(f) | 22 s = utils.readline(f) |
15 while (len(s) > 0) and (not s.startswith(delimiterChar)): | 23 while (len(s) > 0) and (not s.startswith(delimiterChar)): |
16 dataStrings += [s.strip()] | 24 dataStrings += [s.strip()] |
17 s = utils.readline(f) | 25 s = utils.readline(f) |
18 return dataStrings | 26 return dataStrings |
27 | |
28 def saveTrajectorieUserTypes(inFilename, outFilename, objects): | |
29 '''The program saves the objects, | |
30 by just copying the corresponding trajectory and velocity data | |
31 from the inFilename, and saving the characteristics in objects (first line) | |
32 into outFilename''' | |
33 infile = utils.openCheck(inFilename) | |
34 outfile = utils.openCheck(outFilename) | |
35 | |
36 if (not infile) | (not outfile): | |
37 return | |
38 | |
39 lines = getLines(file) | |
40 objNum = 0 | |
41 while lines != []: | |
42 i = 0 | |
43 while (i<len(objects)) and (objects[i].num != objNum): | |
44 i+=1 | |
45 if i<len(objects): | |
46 l = lines[0].split(' ') | |
47 outfile.write() | |
48 # next object | |
49 objNum += 1 | |
19 | 50 |
20 def loadTrajectories(filename, nObjects = -1): | 51 def loadTrajectories(filename, nObjects = -1): |
21 '''Loads trajectories''' | 52 '''Loads trajectories''' |
22 | 53 |
23 file = utils.openCheck(filename) | 54 file = utils.openCheck(filename) |
24 if (not file): | 55 if (not file): |
25 return [] | 56 return [] |
26 | 57 |
27 objects = [] | 58 objects = [] |
28 objNum = 0 | 59 objNum = 0 |
29 isObjectFile = filename.endswith('objects.txt'); | 60 objectType = getFileType(filename) |
30 lines = getLines(file) | 61 lines = getLines(file) |
31 while (lines != []) and ((nObjects<0) or (objNum<nObjects)): | 62 while (lines != []) and ((nObjects<0) or (objNum<nObjects)): |
32 l = lines[0].split(' ') | 63 l = lines[0].split(' ') |
33 parsedLine = [int(n) for n in l[:4]] | 64 parsedLine = [int(n) for n in l[:4]] |
34 obj = MovingObject(num = objNum, timeInterval = TimeInterval(parsedLine[1],parsedLine[2])) | 65 obj = MovingObject(num = objNum, timeInterval = TimeInterval(parsedLine[1],parsedLine[2])) |
35 #add = True | 66 #add = True |
36 if len(lines) >= 3: | 67 if len(lines) >= 3: |
37 obj.positions = Trajectory.load(lines[1], lines[2]) | 68 obj.positions = Trajectory.load(lines[1], lines[2]) |
38 if len(lines) >= 5: | 69 if len(lines) >= 5: |
39 obj.velocities = Trajectory.load(lines[3], lines[4]) | 70 obj.velocities = Trajectory.load(lines[3], lines[4]) |
40 if (isObjectFile): | 71 if objectType == 'objects': |
41 obj.userType = parsedLine[3] | 72 obj.userType = parsedLine[3] |
42 obj.nObjects = float(l[4]) | 73 obj.nObjects = float(l[4]) |
43 obj.featureNumbers = [int(n) for n in l[5:]] | 74 obj.featureNumbers = [int(n) for n in l[5:]] |
44 | 75 |
45 # load contour data if available | 76 # load contour data if available |
46 if len(lines) >= 6: | 77 if len(lines) >= 6: |
47 obj.contourType = utils.line2Floats(lines[6]) | 78 obj.contourType = utils.line2Floats(lines[6]) |
48 obj.contourOrigins = Trajectory.load(lines[7], lines[8]) | 79 obj.contourOrigins = Trajectory.load(lines[7], lines[8]) |
49 obj.contourSizes = Trajectory.load(lines[9], lines[10]) | 80 obj.contourSizes = Trajectory.load(lines[9], lines[10]) |
81 elif objectType == 'prototypes': | |
82 obj.userType = parsedLine[3] | |
83 obj.nMatchings = int(l[4]) | |
50 | 84 |
51 if len(lines) != 2: | 85 if len(lines) != 2: |
52 objects.append(obj) | 86 objects.append(obj) |
53 objNum+=1 | 87 objNum+=1 |
54 else: | 88 else: |