# HG changeset patch # User Nicolas Saunier # Date 1393214214 18000 # Node ID cb9683f9efe772a3f728c49c284db923a22d5e1a # Parent 55b424d98b688943595947f78c248d5bad1f12b1# Parent af2222c0c9c0f6e1846cf8a0e7a13b0ed699764e Merged in szangenehpour/trafficintelligence (pull request #5) TTC Sample diff -r af2222c0c9c0 -r cb9683f9efe7 CMakeLists.txt --- a/CMakeLists.txt Mon Feb 03 15:41:57 2014 -0500 +++ b/CMakeLists.txt Sun Feb 23 22:56:54 2014 -0500 @@ -14,15 +14,19 @@ CMAKE_CXX_FLAGS "-g -Wall" ) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin) + add_executable(feature-based-tracking c/cvutils.cpp c/feature-based-tracking.cpp c/Motion.cpp c/Parameters.cpp c/utils.cpp + c/InputFrameListModule.cpp + c/InputVideoFileModule.cpp ) -find_package(Boost REQUIRED program_options) +find_package(Boost REQUIRED program_options filesystem system) find_library(TrajectoryManagement_LIBRARY TrajectoryManagementAndAnalysis) find_path(TrajectoryManagement_INCLUDE_DIR src/Trajectory.h) diff -r af2222c0c9c0 -r cb9683f9efe7 python/events.py --- a/python/events.py Mon Feb 03 15:41:57 2014 -0500 +++ b/python/events.py Sun Feb 23 22:56:54 2014 -0500 @@ -67,6 +67,7 @@ self.roadUser2 = roadUser2 self.categoryNum = categoryNum self.indicators = {} + self.interactionInterval = None def getRoadUserNumbers(self): return self.roadUserNumbers @@ -85,6 +86,7 @@ velocityAngles = {} distances = {}#[0]*int(self.timeInterval.length()) speedDifferentials = {} + interactionInstants = [] for instant in self.timeInterval: deltap = self.roadUser1.getPositionAtInstant(instant)-self.roadUser2.getPositionAtInstant(instant) v1 = self.roadUser1.getVelocityAtInstant(instant) @@ -94,18 +96,20 @@ collisionCourseDotProducts[instant] = moving.Point.dot(deltap, deltav) distances[instant] = deltap.norm2() speedDifferentials[instant] = deltav.norm2() - #if collisionCourseDotProducts[instant] > 0: + if collisionCourseDotProducts[instant] > 0: + interactionInstants.append(instant) collisionCourseAngles[instant] = arccos(collisionCourseDotProducts[instant]/(distances[instant]*speedDifferentials[instant])) - # todo shorten the time intervals based on the interaction definition + if len(interactionInstants) >= 2: + self.interactionInterval = moving.TimeInterval(interactionInstants[0], interactionInstants[-1]) + else: + self.interactionInterval = moving.TimeInterval() self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[0], collisionCourseDotProducts)) self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[1], collisionCourseAngles)) self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[2], distances)) self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[4], velocityAngles)) self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[5], speedDifferentials)) - # todo test for interaction instants and interval, compute indicators - # if we have features, compute other indicators if self.roadUser1.features != None and self.roadUser2.features != None: minDistance={} @@ -141,8 +145,8 @@ self.videoFilename= videoFilename def addInteractionType(self,interactionType): - ''' interaction types: conflict or collision if they are known''' - self.interactionType= interactionType + ''' interaction types: conflict or collision if they are known''' + self.interactionType= interactionType def createInteractions(objects): '''Create all interactions of two co-existing road users @@ -163,7 +167,8 @@ Returns the prototype indices (in the interaction list) and the label of each indicator (interaction) if an indicator profile (time series) is different enough ( 0 and tmin < 0: + nbre_jours_gel_degel += 1 + for l in range(len(seuils_T)): + if tmax - tmin >=seuils_T[l]: + deltas_T[l] += 1 + if not np.isnan(data['tmoy'][i]): + tmoys.append(data['tmoy'][i]) + if tmax != None: + if tmax < 0: + compteur += 1 + elif tmax >= 0 and compteur >= nbre_jours_gel_consecutifs: + nbre_jours_gel_consecutifs = compteur + compteur = 0 + else: + compteur = 0 + nbre_jours_gel_consecutifs = max(nbre_jours_gel_consecutifs,compteur) + if len(tmoys) > 0 and float(len(tmoys))/nDays >= minProportionMeasures: + if tmoys != []: + ecart_type_T = np.std(tmoys) + else: + ecart_type = None + if neige_tot < snowThreshold: + neigeEC_sup_seuil = 0 + else: + neigeEC_sup_seuil = 1 + return (nbre_jours_T_negatif,nbre_jours_gel_degel, deltas_T, nbre_jours_gel_consecutifs, pluie_tot, neige_tot, neigeEC_sup_seuil, ecart_type_T) + else: + return [None]*2+[[None]*len(seuils_T)]+[None]*5 + +def mtqWeatherIndicators(data, startDate, endDate,tmax,tmin,tmoy): + from matplotlib.mlab import find + nbre_jours_T_negatif,nbre_jours_gel_degel,ecart_type_T = 0,0,0 + compteur,nbre_jours_gel_consecutifs=0,0 + tmoys = [] + seuils_T = [20,15,10,5] + deltas_T = [0,0,0,0] + startIndex = find(data['date'] == startDate) + nDays = (endDate - startDate).days+1 + for i in range(startIndex, startIndex+nDays): + if tmax[i] < 0: + nbre_jours_T_negatif += 1 + if tmax[i] > 0 and tmin[i] < 0: + nbre_jours_gel_degel += 1 + for l in range(len(seuils_T)): + if tmax[i] - tmin[i] >=seuils_T[l]: + deltas_T[l] += 1 + tmoys.append(tmoy[i]) + if tmax[i] < 0: + compteur += 1 + elif tmax[i] >= 0 and compteur >= nbre_jours_gel_consecutifs: + nbre_jours_gel_consecutifs = compteur + compteur = 0 + else: + compteur = 0 + nbre_jours_gel_consecutifs = max(nbre_jours_gel_consecutifs,compteur) + if tmoys != []: + ecart_type_T = np.std(tmoys) else: - tmax = None - if data['tmin'][i] != '' and data['tmin'][i] != None: - tmin = float(data['tmin'][i].replace(',','.')) - if data['pluie_tot'][i] != '' and data['pluie_tot'][i] != None: - pluie_tot += float(data['pluie_tot'][i].replace(',','.')) - if data['neige_tot'][i] != '' and data['neige_tot'][i] != None: - neige_tot += float(data['neige_tot'][i].replace(',','.')) - if tmax != None: - if tmax < 0: - nbre_jours_T_negatif += 1 - if tmax != None and data['tmin'][i] != '' and data['tmin'][i] != None: - if tmax > 0 and tmin < 0: - nbre_jours_gel_degel += 1 - for l in range(len(seuils_T)): - if tmax - tmin >=seuils_T[l]: - deltas_T[l] += 1 - if data['tmoy'][i] != '' and data['tmoy'][i] != None: - tmoys.append(float(data['tmoy'][i].replace(',','.'))) - if tmax != None: - if tmax < 0: - compteur += 1 - elif tmax >= 0 and compteur >= nbre_jours_gel_consecutifs: - nbre_jours_gel_consecutifs = compteur - compteur = 0 - else: - compteur = 0 - nbre_jours_gel_consecutifs = max(nbre_jours_gel_consecutifs,compteur) - ecart_type_T = np.std(tmoys) - if neige_tot < snowThreshold: - neigeEC_sup_seuil = 0 - else: - neigeEC_sup_seuil = 1 + ecart_type = None + + return (nbre_jours_T_negatif,nbre_jours_gel_degel, deltas_T, nbre_jours_gel_consecutifs, ecart_type_T) + +class RTSS: + '''class for data related to a RTSS: + - agregating pavement marking measurements + - RTSS characteristics from FMR: pavement type, age, AADT, truck AADT + - winter maintenance level from V155 + + If divided highway, the RTSS ends with G or D and are distinct: there is no ambiguity + - retroreflectivity types: there are CB, RJ and RB + If undivided, ending with C + - durability is fine: ETAT_MARQG_RG ETAT_MARQG_CL ETAT_MARQG_RD (+SG/SD, but recent) + - retroreflectivity: CJ is center line, RB and SB are left/right if DEBUT-FIN>0 or <0 + ''' + + def __init__(self, _id, name, data): + self.id = _id + self.name = name + self.data = data + +class MarkingTest: + '''class for a test site for a given product + + including the series of measurements over the years''' + + def __init__(self, _id, paintingDate, paintingType, color, data): + self.id = _id + self.paintingDate = paintingDate + self.paintingType = paintingType + self.color = color + self.data = data + self.nMeasures = len(data) + + def getSite(self): + return int(self.id[:2]) + + def getTestAttributes(self): + return [self.paintingType, self.color, self.paintingDate.year] - return (nbre_jours_T_negatif,nbre_jours_gel_degel, deltas_T, nbre_jours_gel_consecutifs, pluie_tot, neige_tot, neigeEC_sup_seuil, ecart_type_T) + def plot(self, measure, options = 'o', dayRatio = 1., **kwargs): + from matplotlib.pyplot import plot + plot(self.data['jours']/float(dayRatio), + self.data[measure], options, **kwargs) + + def getMarkingMeasures(self, dataLabel): + nonZeroIndices = ~np.isnan(self.data[dataLabel]) + return self.data[nonZeroIndices]['jours'], self.data[nonZeroIndices][dataLabel] + + def plotMarkingMeasures(self, measure, options = 'o', dayRatio = 1., **kwargs): + for i in range(1,7): + self.plot('{}_{}'.format(measure, i), options, dayRatio, **kwargs) + + def computeMarkingMeasureVariations(self, dataLabel, lanePositions, weatherData, snowThreshold, minProportionMeasures = 0.): + '''Computes for each successive measurement + lanePositions = None + measure variation, initial measure, time duration, weather indicators + + TODO if measurements per lane, add a variable for lane position (position1 to 6) + lanePositions = list of integers (range(1,7)) + measure variation, initial measure, time duration, lane position1, weather indicators + measure variation, initial measure, time duration, lane position2, weather indicators + ...''' + variationData = [] + if lanePositions == None: + nonZeroIndices = ~np.isnan(self.data[dataLabel]) + days = self.data[nonZeroIndices]['jours'] + dates = self.data[nonZeroIndices]['date_mesure'] + measures = self.data[nonZeroIndices][dataLabel] + for i in range(1, len(dates)): + nDaysTNegative, nDaysThawFreeze, deltaTemp, nConsecutiveFrozenDays, totalRain, totalSnow, snowAboveThreshold, stdevTemp = ecWeatherIndicators(weatherData, dates[i-1], dates[i], snowThreshold, minProportionMeasures) + if dates[i-1].year+1 == dates[i].year: + winter = 1 + if days[i-1]<365: + firstWinter = 1 + else: + winter = 0 + firstWinter = 0 + variationData.append([measures[i-1]-measures[i], measures[i-1], days[i]-days[i-1], days[i-1], winter, firstWinter, nDaysTNegative, nDaysThawFreeze] + deltaTemp + [nConsecutiveFrozenDays, totalRain, totalSnow, snowAboveThreshold, stdevTemp]) + return variationData diff -r af2222c0c9c0 -r cb9683f9efe7 python/prediction.py --- a/python/prediction.py Mon Feb 03 15:41:57 2014 -0500 +++ b/python/prediction.py Sun Feb 23 22:56:54 2014 -0500 @@ -215,20 +215,19 @@ maxSpeed = self.maxSpeed)] class NormalAdaptationPredictionParameters(PredictionParameters): - def __init__(self, maxSpeed, nPredictedTrajectories, maxAcceleration, maxSteering, useFeatures = False): + def __init__(self, maxSpeed, nPredictedTrajectories, accelerationDistribution, steeringDistribution, useFeatures = False): + '''An example of acceleration and steering distributions is + lambda: random.triangular(-self.maxAcceleration, self.maxAcceleration, 0.) + ''' if useFeatures: name = 'point set normal adaptation' else: name = 'normal adaptation' PredictionParameters.__init__(self, name, maxSpeed) self.nPredictedTrajectories = nPredictedTrajectories - self.maxAcceleration = maxAcceleration - self.maxSteering = maxSteering self.useFeatures = useFeatures - self.accelerationDistribution = lambda: random.triangular(-self.maxAcceleration, - self.maxAcceleration, 0.) - self.steeringDistribution = lambda: random.triangular(-self.maxSteering, - self.maxSteering, 0.) + self.accelerationDistribution = accelerationDistribution + self.steeringDistribution = steeringDistribution def __str__(self): return PredictionParameters.__str__(self)+' {0} {1} {2}'.format(self.nPredictedTrajectories, @@ -271,21 +270,19 @@ return predictedTrajectories class EvasiveActionPredictionParameters(PredictionParameters): - def __init__(self, maxSpeed, nPredictedTrajectories, minAcceleration, maxAcceleration, maxSteering, useFeatures = False): + def __init__(self, maxSpeed, nPredictedTrajectories, accelerationDistribution, steeringDistribution, useFeatures = False): + '''Suggested acceleration distribution may not be symmetric, eg + lambda: random.triangular(self.minAcceleration, self.maxAcceleration, 0.)''' + if useFeatures: name = 'point set evasive action' else: name = 'evasive action' PredictionParameters.__init__(self, name, maxSpeed) self.nPredictedTrajectories = nPredictedTrajectories - self.minAcceleration = minAcceleration - self.maxAcceleration = maxAcceleration - self.maxSteering = maxSteering self.useFeatures = useFeatures - self.accelerationDistribution = lambda: random.triangular(self.minAcceleration, - self.maxAcceleration, 0.) - self.steeringDistribution = lambda: random.triangular(-self.maxSteering, - self.maxSteering, 0.) + self.accelerationDistribution = accelerationDistribution + self.steeringDistribution = steeringDistribution def __str__(self): return PredictionParameters.__str__(self)+' {0} {1} {2} {3}'.format(self.nPredictedTrajectories, self.minAcceleration, self.maxAcceleration, self.maxSteering) diff -r af2222c0c9c0 -r cb9683f9efe7 python/tests/moving.txt --- a/python/tests/moving.txt Mon Feb 03 15:41:57 2014 -0500 +++ b/python/tests/moving.txt Sun Feb 23 22:56:54 2014 -0500 @@ -42,6 +42,8 @@ >>> Point(3,4)-Point(1,7) (2.000000,-3.000000) +>>> -Point(1,2) +(-1.000000,-2.000000) >>> Point(3,2).norm2Squared() 13 diff -r af2222c0c9c0 -r cb9683f9efe7 python/tests/utils.txt --- a/python/tests/utils.txt Mon Feb 03 15:41:57 2014 -0500 +++ b/python/tests/utils.txt Sun Feb 23 22:56:54 2014 -0500 @@ -44,6 +44,15 @@ >>> stepPlot([3, 5, 7, 8], 1, 10, 0) ([1, 3, 3, 5, 5, 7, 7, 8, 8, 10], [0, 0, 1, 1, 2, 2, 3, 3, 4, 4]) +>>> mostCommon(['a','b','c','b']) +'b' +>>> mostCommon(['a','b','c','b', 'c']) +'b' +>>> mostCommon(range(10)+[1]) +1 +>>> mostCommon([range(2), range(4), range(2)]) +[0, 1] + >>> lcss = LCSS(lambda x,y: abs(x-y) <= 0.1) >>> lcss.compute(range(5), range(5)) 5 diff -r af2222c0c9c0 -r cb9683f9efe7 python/utils.py --- a/python/utils.py Mon Feb 03 15:41:57 2014 -0500 +++ b/python/utils.py Sun Feb 23 22:56:54 2014 -0500 @@ -269,6 +269,33 @@ return coef ######################### +# iterable section +######################### + +def mostCommon(L): + '''Returns the most frequent element in a iterable + + taken from http://stackoverflow.com/questions/1518522/python-most-common-element-in-a-list''' + from itertools import groupby + from operator import itemgetter + # get an iterable of (item, iterable) pairs + SL = sorted((x, i) for i, x in enumerate(L)) + # print 'SL:', SL + groups = groupby(SL, key=itemgetter(0)) + # auxiliary function to get "quality" for an item + def _auxfun(g): + item, iterable = g + count = 0 + min_index = len(L) + for _, where in iterable: + count += 1 + min_index = min(min_index, where) + # print 'item %r, count %r, minind %r' % (item, count, min_index) + return count, -min_index + # pick the highest-count/earliest item + return max(groups, key=_auxfun)[0] + +######################### # sequence section ######################### diff -r af2222c0c9c0 -r cb9683f9efe7 scripts/compute-homography.py --- a/scripts/compute-homography.py Mon Feb 03 15:41:57 2014 -0500 +++ b/scripts/compute-homography.py Sun Feb 23 22:56:54 2014 -0500 @@ -85,11 +85,11 @@ print('Click on {0} points in the video frame'.format(nPoints)) plt.figure() plt.imshow(videoImg) - videoPts = np.array(plt.ginput(nPoints)) + videoPts = np.array(plt.ginput(nPoints, timeout=3000)) print('Click on {0} points in the world image'.format(nPoints)) plt.figure() plt.imshow(worldImg) - worldPts = unitsPerPixel*np.array(plt.ginput(nPoints)) + worldPts = unitsPerPixel*np.array(plt.ginput(nPoints, timeout=3000)) plt.close('all') homography, mask = cv2.findHomography(videoPts, worldPts) # save the points in file diff -r af2222c0c9c0 -r cb9683f9efe7 scripts/setup-tracking.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/setup-tracking.sh Sun Feb 23 22:56:54 2014 -0500 @@ -0,0 +1,39 @@ +version="$(wget -q -O - http://sourceforge.net/projects/opencvlibrary/files/opencv-unix | egrep -m1 -o '\"[0-9](\.[0-9])+' | cut -c2-)" +echo "Removing any pre-installed ffmpeg and x264" +sudo apt-get -qq remove ffmpeg x264 libx264-dev +echo "Installing Dependencies" +sudo apt-get -qq install libopencv-dev build-essential checkinstall cmake pkg-config yasm libtiff4-dev libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils ffmpeg +sudo apt-get -qq install libavfilter-dev libboost-dev libboost-program-options-dev libboost-graph-dev python-setuptools python-dev libcppunit-dev sqlite3 libsqlite3-dev cmake-qt-gui libboost-all-dev +sudo easy_install -U mercurial +echo "Installing OpenCV" $version +cd +mkdir OpenCV +cd OpenCV +echo "Downloading OpenCV" $version +wget -O OpenCV-$version.tar.gz http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/$version/opencv-"$version".tar.gz/download +echo "Installing OpenCV" $version +tar -xvf OpenCV-$version.tar.gz +cd opencv-$version +mkdir release +cd release +cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. +make +sudo make install +echo "OpenCV" $version "ready to be used" + +echo "Installing Traffic Intelligence..." +cd +mkdir Research +cd Research +mkdir Code +cd Code +hg clone https://Nicolas@bitbucket.org/trajectories/trajectorymanagementandanalysis +hg clone https://Nicolas@bitbucket.org/Nicolas/trafficintelligence +cd trajectorymanagementandanalysis/trunk/src/TrajectoryManagementAndAnalysis/ +cmake . +make TrajectoryManagementAndAnalysis +cd +cd trafficintelligence/c/ +make feature-based-tracking +cd +