Mercurial Hosting > traffic-intelligence
comparison python/storage.py @ 778:bd684e57c431 dev
integrated code from Laurent Gauthier
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 05 Feb 2016 17:54:38 -0500 |
parents | ef6dd60be2e1 |
children | 1b22d81ef5ff |
comparison
equal
deleted
inserted
replaced
777:ef6dd60be2e1 | 778:bd684e57c431 |
---|---|
253 objects = [] | 253 objects = [] |
254 | 254 |
255 connection.close() | 255 connection.close() |
256 return objects | 256 return objects |
257 | 257 |
258 def loadCurvilinearTrajectoriesFromSqlite(filename, objects, objectNumbers = None): | |
259 '''load alignement curvilinear positions (s_coordinate, y_coordinate, lane) | |
260 from an object database to an existing MovingObject list''' | |
261 connection = sqlite3.connect(inputFilename) | |
262 cursor = connection.cursor() | |
263 | |
264 try: | |
265 cursor.execute('SELECT * from curvilinear_positions order by trajectory_id, frame_number') | |
266 except sqlite3.OperationalError as error: | |
267 printDBError(error) | |
268 return [] | |
269 | |
270 objNum = None | |
271 for row in cursor: | |
272 if objNum != row[0]: | |
273 objNum = row[0] | |
274 objects[objNum].curvilinearPositions = moving.CurvilinearPositions() | |
275 else: | |
276 objects[objNum].curvilinearPositions.addPositionsSYL(row[2],row[3],row[4]) | |
277 | |
278 return objects | |
279 | |
280 def saveTrajectoriesToSqlite(outputFilename, objects, trajectoryType, withFeatures = False): | |
281 '''Writes features, ie the trajectories positions (and velocities if exist) | |
282 with their instants to a specified sqlite file | |
283 | |
284 TODO: Not implemented for other trajectoryType than features | |
285 For objects, with features will control whether the features | |
286 corresponding to the object are also saved''' | |
287 | |
288 connection = sqlite3.connect(outputFilename) | |
289 try: | |
290 cursor = connection.cursor() | |
291 | |
292 if trajectoryType in ['feature', 'curvilinear']: | |
293 cursor.execute("CREATE TABLE IF NOT EXISTS positions (trajectory_id INTEGER, frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))") | |
294 cursor.execute("CREATE TABLE IF NOT EXISTS velocities (trajectory_id INTEGER, frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))") | |
295 if trajectoryType == 'curvilinear': | |
296 cursor.execute("CREATE TABLE IF NOT EXISTS curvilinear_positions (trajectory_id INTEGER, frame_number INTEGER, s_coordinate REAL, y_coordinate REAL, lane TEXT, PRIMARY KEY(trajectory_id, frame_number))") | |
297 | |
298 positionQuery = "insert into positions (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)" | |
299 velocityQuery = "insert into velocities (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)" | |
300 curvilinearQuery = "insert into curvilinear_positions (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)" | |
301 for obj in objects: | |
302 num = obj.getNum() | |
303 frame_number = obj.getFirstInstant() | |
304 for position in obj.getPositions(): | |
305 cursor.execute(positionQuery, (num, frame_number, position.x, position.y)) | |
306 frame_number += 1 | |
307 # velocities | |
308 velocities = obj.getVelocities() | |
309 if velocities is not None: | |
310 frame_number = obj.getFirstInstant() | |
311 for i in xrange(velocities.length()-1): | |
312 v = velocities[i] | |
313 cursor.execute(velocityQuery, (num, frame_number, v.x, v.y)) | |
314 frame_number += 1 | |
315 # curvilinear trajectories | |
316 if trajectoryType == 'curvilinear' and hasattr(obj, 'curvilinearPositions') is not None: | |
317 frame_number = obj.getFirstInstant() | |
318 for position in obj.getCurvilinearPositions(): | |
319 cursor.execute(curvilinearQuery, (num, frame_number, position[0], position[1], position[2])) | |
320 frame_number += 1 | |
321 connection.commit() | |
322 #elif trajectoryType == 'object': | |
323 else: | |
324 print('Unknown trajectory type {}'.format(trajectoryType)) | |
325 except sqlite3.OperationalError as error: | |
326 printDBError(error) | |
327 connection.close() | |
328 | |
258 def savePrototypesToSqlite(filename, prototypes, trajectoryType = 'feature'): | 329 def savePrototypesToSqlite(filename, prototypes, trajectoryType = 'feature'): |
259 'Work in progress, do not use' | 330 'Work in progress, do not use' |
260 connection = sqlite3.connect(filename) | 331 connection = sqlite3.connect(filename) |
261 cursor = connection.cursor() | 332 cursor = connection.cursor() |
262 try: | 333 try: |
425 except sqlite3.OperationalError as error: | 496 except sqlite3.OperationalError as error: |
426 printDBError(error) | 497 printDBError(error) |
427 return boundingBoxes | 498 return boundingBoxes |
428 connection.close() | 499 connection.close() |
429 return boundingBoxes | 500 return boundingBoxes |
430 | |
431 def saveTrajectoriesToSqlite(outputFilename, objects, trajectoryType, withFeatures = False): | |
432 '''Writes features, ie the trajectories positions (and velocities if exist) | |
433 with their instants to a specified sqlite file | |
434 | |
435 TODO: Not implemented for other trajectoryType than features | |
436 For objects, with features will control whether the features | |
437 corresponding to the object are also saved''' | |
438 | |
439 connection = sqlite3.connect(outputFilename) | |
440 try: | |
441 cursor = connection.cursor() | |
442 | |
443 if trajectoryType == 'feature': | |
444 cursor.execute("CREATE TABLE IF NOT EXISTS positions (trajectory_id INTEGER, frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))") | |
445 cursor.execute("CREATE TABLE IF NOT EXISTS velocities (trajectory_id INTEGER, frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))") | |
446 | |
447 positionQuery = "insert into positions (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)" | |
448 velocityQuery = "insert into velocities (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)" | |
449 for obj in objects: | |
450 num = obj.getNum() | |
451 frame_number = obj.getFirstInstant() | |
452 for position in obj.getPositions(): | |
453 cursor.execute(positionQuery, (num, frame_number, position.x, position.y)) | |
454 frame_number += 1 | |
455 if obj.getVelocities() is not None: | |
456 frame_number = obj.getFirstInstant() | |
457 velocities = obj.getVelocities() | |
458 for i in xrange(velocities.length()-1): | |
459 v = velocities[i] | |
460 cursor.execute(velocityQuery, (num, frame_number, v.x, v.y)) | |
461 frame_number += 1 | |
462 connection.commit() | |
463 #elif trajectoryType == 'feature': | |
464 else: | |
465 print('Unknown trajectory type {}'.format(trajectoryType)) | |
466 except sqlite3.OperationalError as error: | |
467 printDBError(error) | |
468 connection.close() | |
469 | 501 |
470 ######################### | 502 ######################### |
471 # saving and loading for scene interpretation (Mohamed Gomaa Mohamed's PhD) | 503 # saving and loading for scene interpretation (Mohamed Gomaa Mohamed's PhD) |
472 ######################### | 504 ######################### |
473 | 505 |