comparison python/moving.py @ 851:07fb949ff98f

added display of object id
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 13 Sep 2016 15:30:51 -0400
parents aa98e773ac91
children a8ca72dc1564
comparison
equal deleted inserted replaced
850:c724a51d4f5f 851:07fb949ff98f
4 import utils, cvutils 4 import utils, cvutils
5 from base import VideoFilenameAddable 5 from base import VideoFilenameAddable
6 6
7 from math import sqrt, atan2, cos, sin 7 from math import sqrt, atan2, cos, sin
8 from numpy import median, array, zeros, hypot, NaN, std, floor, float32 8 from numpy import median, array, zeros, hypot, NaN, std, floor, float32
9 from matplotlib.pyplot import plot 9 from matplotlib.pyplot import plot, text
10 from scipy.stats import scoreatpercentile 10 from scipy.stats import scoreatpercentile
11 from scipy.spatial.distance import cdist 11 from scipy.spatial.distance import cdist
12 12
13 try: 13 try:
14 from shapely.geometry import Polygon, Point as shapelyPoint 14 from shapely.geometry import Polygon, Point as shapelyPoint
730 def duplicateLastPosition(self): 730 def duplicateLastPosition(self):
731 self.positions[0].append(self.positions[0][-1]) 731 self.positions[0].append(self.positions[0][-1])
732 self.positions[1].append(self.positions[1][-1]) 732 self.positions[1].append(self.positions[1][-1])
733 733
734 @staticmethod 734 @staticmethod
735 def _plot(positions, options = '', withOrigin = False, lastCoordinate = None, timeStep = 1, **kwargs): 735 def _plot(positions, options = '', withOrigin = False, lastCoordinate = None, timeStep = 1, objNum = None, **kwargs):
736 if lastCoordinate is None: 736 if lastCoordinate is None:
737 plot(positions[0][::timeStep], positions[1][::timeStep], options, **kwargs) 737 plot(positions[0][::timeStep], positions[1][::timeStep], options, **kwargs)
738 elif 0 <= lastCoordinate <= len(positions[0]): 738 elif 0 <= lastCoordinate <= len(positions[0]):
739 plot(positions[0][:lastCoordinate:timeStep], positions[1][:lastCoordinate:timeStep], options, **kwargs) 739 plot(positions[0][:lastCoordinate:timeStep], positions[1][:lastCoordinate:timeStep], options, **kwargs)
740 if withOrigin: 740 if withOrigin:
741 plot([positions[0][0]], [positions[1][0]], 'ro', **kwargs) 741 plot([positions[0][0]], [positions[1][0]], 'ro', **kwargs)
742 742 if objNum is not None:
743 text(positions[0][0], positions[1][0], '{}'.format(objNum))
744
743 def project(self, homography): 745 def project(self, homography):
744 return Trajectory(cvutils.projectTrajectory(homography, self.positions).tolist()) 746 return Trajectory(cvutils.projectTrajectory(homography, self.positions).tolist())
745 747
746 def plot(self, options = '', withOrigin = False, timeStep = 1, **kwargs): 748 def plot(self, options = '', withOrigin = False, timeStep = 1, objNum = None, **kwargs):
747 Trajectory._plot(self.positions, options, withOrigin, None, timeStep, **kwargs) 749 Trajectory._plot(self.positions, options, withOrigin, None, timeStep, objNum, **kwargs)
748 750
749 def plotAt(self, lastCoordinate, options = '', withOrigin = False, timeStep = 1, **kwargs): 751 def plotAt(self, lastCoordinate, options = '', withOrigin = False, timeStep = 1, objNum = None, **kwargs):
750 Trajectory._plot(self.positions, options, withOrigin, lastCoordinate, timeStep, **kwargs) 752 Trajectory._plot(self.positions, options, withOrigin, lastCoordinate, timeStep, objNum, **kwargs)
751 753
752 def plotOnWorldImage(self, nPixelsPerUnitDistance, options = '', withOrigin = False, timeStep = 1, **kwargs): 754 def plotOnWorldImage(self, nPixelsPerUnitDistance, options = '', withOrigin = False, timeStep = 1, objNum = None, **kwargs):
753 imgPositions = [[x*nPixelsPerUnitDistance for x in self.positions[0]], 755 imgPositions = [[x*nPixelsPerUnitDistance for x in self.positions[0]],
754 [x*nPixelsPerUnitDistance for x in self.positions[1]]] 756 [x*nPixelsPerUnitDistance for x in self.positions[1]]]
755 Trajectory._plot(imgPositions, options, withOrigin, None, timeStep, **kwargs) 757 Trajectory._plot(imgPositions, options, withOrigin, None, timeStep, objNum, **kwargs)
756 758
757 def getXCoordinates(self): 759 def getXCoordinates(self):
758 return self.positions[0] 760 return self.positions[0]
759 761
760 def getYCoordinates(self): 762 def getYCoordinates(self):
1299 return self.positions.getXCoordinates() 1301 return self.positions.getXCoordinates()
1300 1302
1301 def getYCoordinates(self): 1303 def getYCoordinates(self):
1302 return self.positions.getYCoordinates() 1304 return self.positions.getYCoordinates()
1303 1305
1304 def plot(self, options = '', withOrigin = False, timeStep = 1, withFeatures = False, **kwargs): 1306 def plot(self, options = '', withOrigin = False, timeStep = 1, withFeatures = False, withIds = False, **kwargs):
1307 if withIds:
1308 objNum = self.getNum()
1309 else:
1310 objNum = None
1305 if withFeatures and self.hasFeatures(): 1311 if withFeatures and self.hasFeatures():
1306 for f in self.getFeatures(): 1312 for f in self.getFeatures():
1307 f.positions.plot('r', True, timeStep, **kwargs) 1313 f.positions.plot('r', True, timeStep, **kwargs)
1308 self.positions.plot('bx-', True, timeStep, **kwargs) 1314 self.positions.plot('bx-', True, timeStep, objNum, **kwargs)
1309 else: 1315 else:
1310 self.positions.plot(options, withOrigin, timeStep, **kwargs) 1316 self.positions.plot(options, withOrigin, timeStep, objNum, **kwargs)
1311 1317
1312 def plotOnWorldImage(self, nPixelsPerUnitDistance, options = '', withOrigin = False, timeStep = 1, **kwargs): 1318 def plotOnWorldImage(self, nPixelsPerUnitDistance, options = '', withOrigin = False, timeStep = 1, withIds = False, **kwargs):
1313 self.positions.plotOnWorldImage(nPixelsPerUnitDistance, options, withOrigin, timeStep, **kwargs) 1319 if withIds:
1320 self.positions.plotOnWorldImage(nPixelsPerUnitDistance, options, withOrigin, timeStep, self.getNum(), **kwargs)
1321 else:
1322 self.positions.plotOnWorldImage(nPixelsPerUnitDistance, options, withOrigin, timeStep, None, **kwargs)
1314 1323
1315 def play(self, videoFilename, homography = None, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1.): 1324 def play(self, videoFilename, homography = None, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1.):
1316 cvutils.displayTrajectories(videoFilename, [self], homography = homography, firstFrameNum = self.getFirstInstant(), lastFrameNumArg = self.getLastInstant(), undistort = undistort, intrinsicCameraMatrix = intrinsicCameraMatrix, distortionCoefficients = distortionCoefficients, undistortedImageMultiplication = undistortedImageMultiplication) 1325 cvutils.displayTrajectories(videoFilename, [self], homography = homography, firstFrameNum = self.getFirstInstant(), lastFrameNumArg = self.getLastInstant(), undistort = undistort, intrinsicCameraMatrix = intrinsicCameraMatrix, distortionCoefficients = distortionCoefficients, undistortedImageMultiplication = undistortedImageMultiplication)
1317 1326
1318 def speedDiagnostics(self, framerate = 1., display = False): 1327 def speedDiagnostics(self, framerate = 1., display = False):