Mercurial Hosting > traffic-intelligence
comparison trafficintelligence/storage.py @ 1041:fc7c0f38e8a6
added nObjects to MovingObject, with loading/saving
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 04 Jul 2018 16:06:23 -0400 |
parents | 20799ac9524e |
children | 75a6ad604cc5 1748c02f9ac3 |
comparison
equal
deleted
inserted
replaced
1040:20799ac9524e | 1041:fc7c0f38e8a6 |
---|---|
227 elif obj is not None: | 227 elif obj is not None: |
228 print('Object {} is missing {} positions'.format(obj.getNum(), int(obj.length())-obj.positions.length())) | 228 print('Object {} is missing {} positions'.format(obj.getNum(), int(obj.length())-obj.positions.length())) |
229 | 229 |
230 return objects | 230 return objects |
231 | 231 |
232 def loadUserTypesFromTable(cursor, objectNumbers): | 232 def loadObjectAttributesFromTable(cursor, objectNumbers, loadNObjects = False): |
233 objectCriteria = getObjectCriteria(objectNumbers) | 233 objectCriteria = getObjectCriteria(objectNumbers) |
234 queryStatement = 'SELECT object_id, road_user_type FROM objects' | 234 queryStatement = 'SELECT object_id, road_user_type' |
235 if loadNObjects: | |
236 queryStatement += ', n_objects' | |
237 queryStatement += ' FROM objects' | |
235 if objectNumbers is not None: | 238 if objectNumbers is not None: |
236 queryStatement += ' WHERE object_id '+objectCriteria | 239 queryStatement += ' WHERE object_id '+objectCriteria |
237 cursor.execute(queryStatement) | 240 cursor.execute(queryStatement) |
238 userTypes = {} | 241 attributes = {} |
239 for row in cursor: | 242 if loadNObjects: |
240 userTypes[row[0]] = row[1] | 243 for row in cursor: |
241 return userTypes | 244 attributes[row[0]] = row[1:] |
245 else: | |
246 for row in cursor: | |
247 attributes[row[0]] = row[1] | |
248 return attributes | |
242 | 249 |
243 def loadTrajectoriesFromSqlite(filename, trajectoryType, objectNumbers = None, withFeatures = False, timeStep = None, maxNObjectFeatures = 1): | 250 def loadTrajectoriesFromSqlite(filename, trajectoryType, objectNumbers = None, withFeatures = False, timeStep = None, maxNObjectFeatures = 1): |
244 '''Loads the trajectories (in the general sense, | 251 '''Loads the trajectories (in the general sense, |
245 either features, objects (feature groups), longest features per object, or bounding box series) | 252 either features, objects (feature groups), longest features per object, or bounding box series) |
246 | 253 |
288 | 295 |
289 for obj in objects: | 296 for obj in objects: |
290 obj.featureNumbers = featureNumbers[obj.getNum()] | 297 obj.featureNumbers = featureNumbers[obj.getNum()] |
291 | 298 |
292 # load userType | 299 # load userType |
293 userTypes = loadUserTypesFromTable(cursor, objectNumbers) | 300 attributes = loadObjectAttributesFromTable(cursor, objectNumbers, True) |
294 for obj in objects: | 301 for obj in objects: |
295 obj.userType = userTypes[obj.getNum()] | 302 userType, nObjects = attributes[obj.getNum()] |
303 obj.setUserType(userType) | |
304 obj.setNObjects(nObjects) | |
296 | 305 |
297 if withFeatures: | 306 if withFeatures: |
298 nFeatures = 0 | 307 nFeatures = 0 |
299 for obj in objects: | 308 for obj in objects: |
300 nFeatures = max(nFeatures, max(obj.featureNumbers)) | 309 nFeatures = max(nFeatures, max(obj.featureNumbers)) |
450 objects = [] | 459 objects = [] |
451 with sqlite3.connect(filename) as connection: | 460 with sqlite3.connect(filename) as connection: |
452 if objectType == 'bb': | 461 if objectType == 'bb': |
453 topCorners = loadTrajectoriesFromTable(connection, 'bounding_boxes', 'bbtop', objectNumbers, timeStep) | 462 topCorners = loadTrajectoriesFromTable(connection, 'bounding_boxes', 'bbtop', objectNumbers, timeStep) |
454 bottomCorners = loadTrajectoriesFromTable(connection, 'bounding_boxes', 'bbbottom', objectNumbers, timeStep) | 463 bottomCorners = loadTrajectoriesFromTable(connection, 'bounding_boxes', 'bbbottom', objectNumbers, timeStep) |
455 userTypes = loadUserTypesFromTable(connection.cursor(), objectNumbers) # string format is same as object | 464 userTypes = loadObjectAttributesFromTable(connection.cursor(), objectNumbers) # string format is same as object |
456 | 465 |
457 for t, b in zip(topCorners, bottomCorners): | 466 for t, b in zip(topCorners, bottomCorners): |
458 num = t.getNum() | 467 num = t.getNum() |
459 if t.getNum() == b.getNum(): | 468 if t.getNum() == b.getNum(): |
460 annotation = moving.BBMovingObject(num, t.getTimeInterval(), t, b, userTypes[num]) | 469 annotation = moving.BBMovingObject(num, t.getTimeInterval(), t, b, userTypes[num]) |
1031 grouped = data.loc[:,['NO','TIME']].groupby(['NO'], as_index = False) | 1040 grouped = data.loc[:,['NO','TIME']].groupby(['NO'], as_index = False) |
1032 instants = grouped['TIME'].agg({'first': npmin, 'last': npmax}) | 1041 instants = grouped['TIME'].agg({'first': npmin, 'last': npmax}) |
1033 for row_index, row in instants.iterrows(): | 1042 for row_index, row in instants.iterrows(): |
1034 objNum = int(row['NO']) | 1043 objNum = int(row['NO']) |
1035 tmp = data[data['NO'] == objNum] | 1044 tmp = data[data['NO'] == objNum] |
1036 objects[objNum] = moving.MovingObject(num = objNum, timeInterval = moving.TimeInterval(row['first'], row['last'])) | 1045 objects[objNum] = moving.MovingObject(num = objNum, timeInterval = moving.TimeInterval(row['first'], row['last']), nObjects = 1) |
1037 # positions should be rounded to nDecimals decimals only | 1046 # positions should be rounded to nDecimals decimals only |
1038 objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory(S = npround(tmp['POS'].tolist(), nDecimals), Y = npround(tmp['POSLAT'].tolist(), nDecimals), lanes = tmp['LANE'].tolist()) | 1047 objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory(S = npround(tmp['POS'].tolist(), nDecimals), Y = npround(tmp['POSLAT'].tolist(), nDecimals), lanes = tmp['LANE'].tolist()) |
1039 if objectNumbers is not None and objectNumbers > 0 and len(objects) >= objectNumbers: | 1048 if objectNumbers is not None and objectNumbers > 0 and len(objects) >= objectNumbers: |
1040 return list(objects.values()) | 1049 return list(objects.values()) |
1041 else: | 1050 else: |
1050 y = float(data[5]) | 1059 y = float(data[5]) |
1051 lane = data[2]+'_'+data[3] | 1060 lane = data[2]+'_'+data[3] |
1052 if objNum not in objects: | 1061 if objNum not in objects: |
1053 if warmUpLastInstant is None or instant >= warmUpLastInstant: | 1062 if warmUpLastInstant is None or instant >= warmUpLastInstant: |
1054 if objectNumbers is None or len(objects) < objectNumbers: | 1063 if objectNumbers is None or len(objects) < objectNumbers: |
1055 objects[objNum] = moving.MovingObject(num = objNum, timeInterval = moving.TimeInterval(instant, instant)) | 1064 objects[objNum] = moving.MovingObject(num = objNum, timeInterval = moving.TimeInterval(instant, instant), nObjects = 1) |
1056 objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory() | 1065 objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory() |
1057 if (warmUpLastInstant is None or instant >= warmUpLastInstant) and objNum in objects: | 1066 if (warmUpLastInstant is None or instant >= warmUpLastInstant) and objNum in objects: |
1058 objects[objNum].timeInterval.last = instant | 1067 objects[objNum].timeInterval.last = instant |
1059 objects[objNum].curvilinearPositions.addPositionSYL(s, y, lane) | 1068 objects[objNum].curvilinearPositions.addPositionSYL(s, y, lane) |
1060 line = utils.readline(inputfile, '*$') | 1069 line = utils.readline(inputfile, '*$') |
1074 y = row[5] | 1083 y = row[5] |
1075 lane = '{}_{}'.format(row[2], row[3]) | 1084 lane = '{}_{}'.format(row[2], row[3]) |
1076 if objNum not in objects: | 1085 if objNum not in objects: |
1077 if warmUpLastInstant is None or instant >= warmUpLastInstant: | 1086 if warmUpLastInstant is None or instant >= warmUpLastInstant: |
1078 if objectNumbers is None or len(objects) < objectNumbers: | 1087 if objectNumbers is None or len(objects) < objectNumbers: |
1079 objects[objNum] = moving.MovingObject(num = objNum, timeInterval = moving.TimeInterval(instant, instant)) | 1088 objects[objNum] = moving.MovingObject(num = objNum, timeInterval = moving.TimeInterval(instant, instant), nObjects = 1) |
1080 objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory() | 1089 objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory() |
1081 if (warmUpLastInstant is None or instant >= warmUpLastInstant) and objNum in objects: | 1090 if (warmUpLastInstant is None or instant >= warmUpLastInstant) and objNum in objects: |
1082 objects[objNum].timeInterval.last = instant | 1091 objects[objNum].timeInterval.last = instant |
1083 objects[objNum].curvilinearPositions.addPositionSYL(s, y, lane) | 1092 objects[objNum].curvilinearPositions.addPositionSYL(s, y, lane) |
1084 except sqlite3.OperationalError as error: | 1093 except sqlite3.OperationalError as error: |
1179 lastFrameNum = firstFrameNum+int(numbers[2])-1 | 1188 lastFrameNum = firstFrameNum+int(numbers[2])-1 |
1180 #time = moving.TimeInterval(firstFrameNum, firstFrameNum+int(numbers[2])-1) | 1189 #time = moving.TimeInterval(firstFrameNum, firstFrameNum+int(numbers[2])-1) |
1181 obj = moving.MovingObject(num = int(numbers[0]), | 1190 obj = moving.MovingObject(num = int(numbers[0]), |
1182 timeInterval = moving.TimeInterval(firstFrameNum, lastFrameNum), | 1191 timeInterval = moving.TimeInterval(firstFrameNum, lastFrameNum), |
1183 positions = moving.Trajectory([[float(numbers[6])],[float(numbers[7])]]), | 1192 positions = moving.Trajectory([[float(numbers[6])],[float(numbers[7])]]), |
1184 userType = int(numbers[10])) | 1193 userType = int(numbers[10]), nObjects = 1) |
1185 obj.userType = int(numbers[10]) | 1194 obj.userType = int(numbers[10]) |
1186 obj.laneNums = [int(numbers[13])] | 1195 obj.laneNums = [int(numbers[13])] |
1187 obj.precedingVehicles = [int(numbers[14])] # lead vehicle (before) | 1196 obj.precedingVehicles = [int(numbers[14])] # lead vehicle (before) |
1188 obj.followingVehicles = [int(numbers[15])] # following vehicle (after) | 1197 obj.followingVehicles = [int(numbers[15])] # following vehicle (after) |
1189 obj.spaceHeadways = [float(numbers[16])] # feet | 1198 obj.spaceHeadways = [float(numbers[16])] # feet |