Mercurial > hg > nsaunier > traffic-intelligence
comparison python/utils.py @ 513:dbf4b83afbb9
pulled in and merged the new functionalities to deal with camera distortion (eg GoPro cameras)
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 04 Jun 2014 10:57:09 -0400 |
| parents | ad518f0c3218 |
| children | 0c86c73f3c09 |
comparison
equal
deleted
inserted
replaced
| 505:35c99776e593 | 513:dbf4b83afbb9 |
|---|---|
| 4 #from numpy import * | 4 #from numpy import * |
| 5 #from pylab import * | 5 #from pylab import * |
| 6 from datetime import time, datetime | 6 from datetime import time, datetime |
| 7 | 7 |
| 8 __metaclass__ = type | 8 __metaclass__ = type |
| 9 | |
| 10 commentChar = '#' | |
| 11 | |
| 12 delimiterChar = '%'; | |
| 13 | 9 |
| 14 datetimeFormat = "%Y-%m-%d %H:%M:%S" | 10 datetimeFormat = "%Y-%m-%d %H:%M:%S" |
| 15 | 11 |
| 16 ######################### | 12 ######################### |
| 17 # Enumerations | 13 # Enumerations |
| 465 | 461 |
| 466 ######################### | 462 ######################### |
| 467 # file I/O section | 463 # file I/O section |
| 468 ######################### | 464 ######################### |
| 469 | 465 |
| 470 def openCheck(filename, option = 'r', quit = False): | |
| 471 '''Open file filename in read mode by default | |
| 472 and checks it is open''' | |
| 473 try: | |
| 474 return open(filename, option) | |
| 475 except IOError: | |
| 476 print 'File %s could not be opened.' % filename | |
| 477 if quit: | |
| 478 from sys import exit | |
| 479 exit() | |
| 480 return None | |
| 481 | |
| 482 def readline(f, commentCharacter = commentChar): | |
| 483 '''Modified readline function to skip comments.''' | |
| 484 s = f.readline() | |
| 485 while (len(s) > 0) and s.startswith(commentCharacter): | |
| 486 s = f.readline() | |
| 487 return s.strip() | |
| 488 | |
| 489 def getLines(f, delimiterCharacter = delimiterChar): | |
| 490 '''Gets a complete entry (all the lines) in between delimiterChar.''' | |
| 491 dataStrings = [] | |
| 492 s = readline(f) | |
| 493 while (len(s) > 0) and (not s.startswith(delimiterCharacter)): | |
| 494 dataStrings += [s.strip()] | |
| 495 s = readline(f) | |
| 496 return dataStrings | |
| 497 | |
| 498 class FakeSecHead(object): | |
| 499 '''Add fake section header [asection] | |
| 500 | |
| 501 from http://stackoverflow.com/questions/2819696/parsing-properties-file-in-python/2819788#2819788 | |
| 502 use read_file in Python 3.2+ | |
| 503 ''' | |
| 504 def __init__(self, fp): | |
| 505 self.fp = fp | |
| 506 self.sechead = '[main]\n' | |
| 507 | |
| 508 def readline(self): | |
| 509 if self.sechead: | |
| 510 try: return self.sechead | |
| 511 finally: self.sechead = None | |
| 512 else: return self.fp.readline() | |
| 513 | |
| 514 def removeExtension(filename, delimiter = '.'): | 466 def removeExtension(filename, delimiter = '.'): |
| 515 '''Returns the filename minus the extension (all characters after last .)''' | 467 '''Returns the filename minus the extension (all characters after last .)''' |
| 516 i = filename.rfind(delimiter) | 468 i = filename.rfind(delimiter) |
| 517 if i>0: | 469 if i>0: |
| 518 return filename[:i] | 470 return filename[:i] |
| 592 | 544 |
| 593 return optionValues | 545 return optionValues |
| 594 | 546 |
| 595 | 547 |
| 596 ######################### | 548 ######################### |
| 597 # Utils to read .ini type text files for configuration, meta data... | |
| 598 ######################### | |
| 599 | |
| 600 class TrackingParameters: | |
| 601 '''Class for tracking and safety parameters | |
| 602 | |
| 603 Note: framerate is already taken into account''' | |
| 604 def loadConfigFile(self, filename): | |
| 605 from ConfigParser import ConfigParser | |
| 606 from numpy import loadtxt | |
| 607 from os import path | |
| 608 | |
| 609 config = ConfigParser() | |
| 610 config.readfp(FakeSecHead(openCheck(filename))) | |
| 611 self.sectionHeader = config.sections()[0] | |
| 612 self.videoFilename = config.get(self.sectionHeader, 'video-filename') | |
| 613 self.databaseFilename = config.get(self.sectionHeader, 'database-filename') | |
| 614 self.homographyFilename = config.get(self.sectionHeader, 'homography-filename') | |
| 615 if (path.exists(self.homographyFilename)): | |
| 616 self.homography = loadtxt(self.homographyFilename) | |
| 617 else: | |
| 618 self.homography = None | |
| 619 self.firstFrameNum = config.getint(self.sectionHeader, 'frame1') | |
| 620 self.videoFrameRate = config.getfloat(self.sectionHeader, 'video-fps') | |
| 621 | |
| 622 self.maxPredictedSpeed = config.getfloat(self.sectionHeader, 'max-predicted-speed')/3.6/self.videoFrameRate | |
| 623 self.predictionTimeHorizon = config.getfloat(self.sectionHeader, 'prediction-time-horizon')*self.videoFrameRate | |
| 624 self.collisionDistance = config.getfloat(self.sectionHeader, 'collision-distance') | |
| 625 self.crossingZones = config.getboolean(self.sectionHeader, 'crossing-zones') | |
| 626 self.predictionMethod = config.get(self.sectionHeader, 'prediction-method') | |
| 627 self.nPredictedTrajectories = config.getint(self.sectionHeader, 'npredicted-trajectories') | |
| 628 self.maxNormalAcceleration = config.getfloat(self.sectionHeader, 'max-normal-acceleration')/self.videoFrameRate**2 | |
| 629 self.maxNormalSteering = config.getfloat(self.sectionHeader, 'max-normal-steering')/self.videoFrameRate | |
| 630 self.minExtremeAcceleration = config.getfloat(self.sectionHeader, 'min-extreme-acceleration')/self.videoFrameRate**2 | |
| 631 self.maxExtremeAcceleration = config.getfloat(self.sectionHeader, 'max-extreme-acceleration')/self.videoFrameRate**2 | |
| 632 self.maxExtremeSteering = config.getfloat(self.sectionHeader, 'max-extreme-steering')/self.videoFrameRate | |
| 633 self.useFeaturesForPrediction = config.getboolean(self.sectionHeader, 'use-features-prediction') | |
| 634 | |
| 635 class SceneParameters: | |
| 636 def __init__(self, config, sectionName): | |
| 637 from ConfigParser import NoOptionError | |
| 638 from ast import literal_eval | |
| 639 try: | |
| 640 self.sitename = config.get(sectionName, 'sitename') | |
| 641 self.databaseFilename = config.get(sectionName, 'data-filename') | |
| 642 self.homographyFilename = config.get(sectionName, 'homography-filename') | |
| 643 self.calibrationFilename = config.get(sectionName, 'calibration-filename') | |
| 644 self.videoFilename = config.get(sectionName, 'video-filename') | |
| 645 self.frameRate = config.getfloat(sectionName, 'framerate') | |
| 646 self.date = datetime.strptime(config.get(sectionName, 'date'), datetimeFormat) # 2011-06-22 11:00:39 | |
| 647 self.translation = literal_eval(config.get(sectionName, 'translation')) # = [0.0, 0.0] | |
| 648 self.rotation = config.getfloat(sectionName, 'rotation') | |
| 649 self.duration = config.getint(sectionName, 'duration') | |
| 650 except NoOptionError as e: | |
| 651 print(e) | |
| 652 print('Not a section for scene meta-data') | |
| 653 | |
| 654 @staticmethod | |
| 655 def loadConfigFile(filename): | |
| 656 from ConfigParser import ConfigParser | |
| 657 config = ConfigParser() | |
| 658 config.readfp(openCheck(filename)) | |
| 659 configDict = dict() | |
| 660 for sectionName in config.sections(): | |
| 661 configDict[sectionName] = SceneParameters(config, sectionName) | |
| 662 return configDict | |
| 663 | |
| 664 ######################### | |
| 665 # running tests | 549 # running tests |
| 666 ######################### | 550 ######################### |
| 667 | 551 |
| 668 if __name__ == "__main__": | 552 if __name__ == "__main__": |
| 669 import doctest | 553 import doctest |
