Mercurial Hosting > traffic-intelligence
comparison trafficintelligence/storage.py @ 1054:d13f9bfbf3ff
Retry
author | Wendlasida |
---|---|
date | Fri, 06 Jul 2018 18:42:58 -0400 |
parents | 60cc87e824c4 c9c03c97ed9f |
children | 9e4e80fc5943 67144f26609e |
comparison
equal
deleted
inserted
replaced
1053:60cc87e824c4 | 1054:d13f9bfbf3ff |
---|---|
5 from pathlib import Path | 5 from pathlib import Path |
6 import shutil | 6 import shutil |
7 from copy import copy | 7 from copy import copy |
8 import sqlite3, logging | 8 import sqlite3, logging |
9 | 9 |
10 from numpy import log, min as npmin, max as npmax, round as npround, array, sum as npsum, loadtxt, floor as npfloor, ceil as npceil, linalg | 10 from numpy import log, min as npmin, max as npmax, round as npround, array, sum as npsum, loadtxt, floor as npfloor, ceil as npceil, linalg, int32, int64 |
11 from pandas import read_csv, merge | 11 from pandas import read_csv, merge |
12 | 12 |
13 from trafficintelligence import utils, moving, events, indicators | 13 from trafficintelligence import utils, moving, events, indicators |
14 from trafficintelligence.base import VideoFilenameAddable | 14 from trafficintelligence.base import VideoFilenameAddable |
15 | 15 |
19 'truck':3} | 19 'truck':3} |
20 | 20 |
21 tableNames = {'feature':'positions', | 21 tableNames = {'feature':'positions', |
22 'object': 'objects', | 22 'object': 'objects', |
23 'objectfeatures': 'positions'} | 23 'objectfeatures': 'positions'} |
24 | |
25 sqlite3.register_adapter(int64, lambda val: int(val)) | |
26 sqlite3.register_adapter(int32, lambda val: int(val)) | |
24 | 27 |
25 ######################### | 28 ######################### |
26 # Sqlite | 29 # Sqlite |
27 ######################### | 30 ######################### |
28 | 31 |
245 else: | 248 else: |
246 for row in cursor: | 249 for row in cursor: |
247 attributes[row[0]] = row[1] | 250 attributes[row[0]] = row[1] |
248 return attributes | 251 return attributes |
249 | 252 |
250 def loadTrajectoriesFromSqlite(filename, trajectoryType, objectNumbers = None, withFeatures = False, timeStep = None, maxNObjectFeatures = 1): | 253 def loadTrajectoriesFromSqlite(filename, trajectoryType, objectNumbers = None, withFeatures = False, timeStep = None, nLongestFeaturesPerObject = None): |
251 '''Loads the trajectories (in the general sense, | 254 '''Loads the trajectories (in the general sense, |
252 either features, objects (feature groups), longest features per object, or bounding box series) | 255 either features, objects (feature groups), longest features per object, or bounding box series) |
256 types are only feature or object | |
257 if object, features can be loaded with withFeatures or nLongestObjectFeatures used to select the n longest features | |
253 | 258 |
254 The number loaded is either the first objectNumbers objects, | 259 The number loaded is either the first objectNumbers objects, |
255 or the indices in objectNumbers from the database''' | 260 or the indices in objectNumbers from the database''' |
256 objects = [] | 261 objects = [] |
257 with sqlite3.connect(filename) as connection: | 262 with sqlite3.connect(filename) as connection: |
258 if trajectoryType == 'objectfeature': | 263 objects = loadTrajectoriesFromTable(connection, 'positions', trajectoryType, objectNumbers, timeStep) |
259 objectFeatureNumbers = loadObjectFeatureFrameNumbers(filename, objectNumbers) | 264 objectVelocities = loadTrajectoriesFromTable(connection, 'velocities', trajectoryType, objectNumbers, timeStep) |
260 featureNumbers = [] | |
261 for numbers in objectFeatureNumbers.values(): | |
262 featureNumbers += numbers[:min(len(numbers), maxNObjectFeatures)] | |
263 objects = loadTrajectoriesFromTable(connection, 'positions', 'feature', featureNumbers, timeStep) | |
264 objectVelocities = loadTrajectoriesFromTable(connection, 'velocities', 'feature', featureNumbers, timeStep) | |
265 else: | |
266 objects = loadTrajectoriesFromTable(connection, 'positions', trajectoryType, objectNumbers, timeStep) | |
267 objectVelocities = loadTrajectoriesFromTable(connection, 'velocities', trajectoryType, objectNumbers, timeStep) | |
268 | 265 |
269 if len(objectVelocities) > 0: | 266 if len(objectVelocities) > 0: |
270 for o,v in zip(objects, objectVelocities): | 267 for o,v in zip(objects, objectVelocities): |
271 if o.getNum() == v.getNum(): | 268 if o.getNum() == v.getNum(): |
272 o.velocities = v.positions | 269 o.velocities = v.positions |
281 queryStatement = 'SELECT trajectory_id, object_id FROM objects_features' | 278 queryStatement = 'SELECT trajectory_id, object_id FROM objects_features' |
282 if objectNumbers is not None: | 279 if objectNumbers is not None: |
283 queryStatement += ' WHERE object_id '+getObjectCriteria(objectNumbers) | 280 queryStatement += ' WHERE object_id '+getObjectCriteria(objectNumbers) |
284 queryStatement += ' ORDER BY object_id' # order is important to group all features per object | 281 queryStatement += ' ORDER BY object_id' # order is important to group all features per object |
285 logging.debug(queryStatement) | 282 logging.debug(queryStatement) |
286 cursor.execute(queryStatement) | 283 cursor.execute(queryStatement) |
287 | 284 |
288 featureNumbers = {} | 285 featureNumbers = {} |
289 for row in cursor: | 286 for row in cursor: |
290 objId = row[1] | 287 objId = row[1] |
291 if objId not in featureNumbers: | 288 if objId not in featureNumbers: |
301 for obj in objects: | 298 for obj in objects: |
302 userType, nObjects = attributes[obj.getNum()] | 299 userType, nObjects = attributes[obj.getNum()] |
303 obj.setUserType(userType) | 300 obj.setUserType(userType) |
304 obj.setNObjects(nObjects) | 301 obj.setNObjects(nObjects) |
305 | 302 |
303 # add features | |
306 if withFeatures: | 304 if withFeatures: |
307 nFeatures = 0 | |
308 for obj in objects: | 305 for obj in objects: |
309 nFeatures = max(nFeatures, max(obj.featureNumbers)) | 306 obj.features = loadTrajectoriesFromSqlite(filename, 'feature', obj.featureNumbers, timeStep = timeStep) |
310 features = loadTrajectoriesFromSqlite(filename, 'feature', nFeatures+1, timeStep = timeStep) | 307 elif nLongestFeaturesPerObject is not None: |
311 for obj in objects: | 308 for obj in objects: |
312 obj.setFeatures(features) | 309 queryStatement = 'SELECT trajectory_id, max(frame_number)-min(frame_number) AS length FROM positions WHERE trajectory_id '+getObjectCriteria(obj.featureNumbers)+' GROUP BY trajectory_id ORDER BY length DESC' |
310 logging.debug(queryStatement) | |
311 cursor.execute(queryStatement) | |
312 obj.features = loadTrajectoriesFromSqlite(filename, 'feature', [row[0] for i,row in enumerate(cursor) if i<nLongestFeaturesPerObject], timeStep = timeStep) | |
313 | 313 |
314 except sqlite3.OperationalError as error: | 314 except sqlite3.OperationalError as error: |
315 printDBError(error) | 315 printDBError(error) |
316 return objects | 316 return objects |
317 | 317 |
335 objectFeatureNumbers[objId] = [row[1]] | 335 objectFeatureNumbers[objId] = [row[1]] |
336 return objectFeatureNumbers | 336 return objectFeatureNumbers |
337 except sqlite3.OperationalError as error: | 337 except sqlite3.OperationalError as error: |
338 printDBError(error) | 338 printDBError(error) |
339 return None | 339 return None |
340 | |
341 def loadObjectTrajectoriesFromSqlite(): | |
342 '''Loads object trajectories | |
343 either simply objects or features (defaults to loadTrajectoriesFromSqlite) | |
344 or the longest features for each object ''' | |
345 | |
346 | 340 |
347 def addCurvilinearTrajectoriesFromSqlite(filename, objects): | 341 def addCurvilinearTrajectoriesFromSqlite(filename, objects): |
348 '''Adds curvilinear positions (s_coordinate, y_coordinate, lane) | 342 '''Adds curvilinear positions (s_coordinate, y_coordinate, lane) |
349 from a database to an existing MovingObject dict (indexed by each objects's num)''' | 343 from a database to an existing MovingObject dict (indexed by each objects's num)''' |
350 with sqlite3.connect(filename) as connection: | 344 with sqlite3.connect(filename) as connection: |