示例#1
0
    def findObject(frameNum, x, y,):
        import cv2
        from numpy import array
        from numpy.core import dot
        from numpy.lib.function_base import append
        from numpy.linalg.linalg import inv
        from numpy import loadtxt

        homographyFilename = "laurier-homography.txt"
        homography = inv(loadtxt(homographyFilename))
        databaseFilename = "laurier.sqlite"
        trajectoryType = "object"
        cap = cv2.VideoCapture('laurier.avi')
        width = cap.get(3)
        height = cap.get(4)
        cap.release()

        objects = storage.loadTrajectoriesFromSqlite(databaseFilename, trajectoryType)
        features = storage.loadTrajectoriesFromSqlite(databaseFilename, "feature")
        px = 0.2
        py = 0.2
        pixelThreshold = 800
        for obj in objects:
            if obj.existsAtInstant(frameNum):
                obj.setFeatures(features)
                if obj.hasFeatures():
                    u = []
                    v = []
                    for f in obj.getFeatures():
                        if f.existsAtInstant(frameNum):
                            projectedPosition = f.getPositionAtInstant(frameNum).project(homography)
                            u.append(projectedPosition.x)
                            v.append(projectedPosition.y)
                    xmin = min(u)
                    xmax = max(u)
                    ymin = min(v)
                    ymax = max(v)
                    xMm = px * (xmax - xmin)
                    yMm = py * (ymax - ymin)
                    a = max(ymax - ymin + (2 * yMm), xmax - (xmin + 2 * xMm))
                    yCropMin = int(max(0, .5 * (ymin + ymax - a)))
                    yCropMax = int(min(height - 1, .5 * (ymin + ymax + a)))
                    xCropMin = int(max(0, .5 * (xmin + xmax - a)))
                    xCropMax = int(min(width - 1, .5 * (xmin + xmax + a)))
                    if yCropMax != yCropMin and xCropMax != xCropMin and (yCropMax - yCropMin) * (xCropMax - xCropMin) > pixelThreshold:
                        if x > xCropMin and x < xCropMax and y > yCropMin and y < yCropMax:
                            print obj.getNum()
示例#2
0
    def test_ex1(self):
        import storage
        objects = storage.loadTrajectoriesFromSqlite('../samples/laurier.sqlite', 'object')

        speed = objects[0].getVelocityAtInstant(10).norm2()
        timeInterval = objects[0].getTimeInterval()
        speeds = [objects[0].getVelocityAtInstant(t).norm2() for t in range(timeInterval.first, timeInterval.last)]
        speeds = [v.norm2() for v in objects[0].getVelocities()]

        from matplotlib.pyplot import plot, close, axis
        plot(range(timeInterval.first, timeInterval.last+1), speeds)

        close('all')
        objects[0].plot()
        axis('equal')

        features = storage.loadTrajectoriesFromSqlite('../samples/laurier.sqlite', 'feature')
        objects[0].setFeatures(features)

        for f in objects[0].features:
            f.plot()
        axis('equal')
示例#3
0
文件: TrOPed.py 项目: Drushkey/TrOPed
def extract_trajectories(sqlite_filename):
	import storage

	objects = storage.loadTrajectoriesFromSqlite(sqlite_filename,'object')
	object_positions = []
	a = 0
	for o in objects:
		instant = o.timeInterval[0]
		for p in o.positions:
			object_positions.append([a,instant,p.x,p.y])
			instant += 1
		a += 1

	return object_positions
示例#4
0
def extract_trajectories(sqlite_filename):
    import storage

    objects = storage.loadTrajectoriesFromSqlite(sqlite_filename, 'object')
    object_positions = []
    a = 0
    for o in objects:
        instant = o.timeInterval[0]
        for p in o.positions:
            object_positions.append([a, instant, p.x, p.y])
            instant += 1
        a += 1

    return object_positions
示例#5
0
                                                                           params.nPredictedTrajectories, 
                                                                           accelerationDistribution,
                                                                           steeringDistribution,
                                                                           params.useFeaturesForPrediction)
elif predictionMethod == 'ps':
    predictionParameters = prediction.PointSetPredictionParameters(params.maxPredictedSpeed)
# no else required, since parameters is required as argument

# evasiveActionPredictionParameters = prediction.EvasiveActionPredictionParameters(params.maxPredictedSpeed, 
#                                                                                  params.nPredictedTrajectories, 
#                                                                                  params.minExtremeAcceleration,
#                                                                                  params.maxExtremeAcceleration,
#                                                                                  params.maxExtremeSteering,
#                                                                                  params.useFeaturesForPrediction)

objects = storage.loadTrajectoriesFromSqlite(params.databaseFilename,'object')
if params.useFeaturesForPrediction:
    features = storage.loadTrajectoriesFromSqlite(params.databaseFilename,'feature') # needed if normal adaptation
    for obj in objects:
        obj.setFeatures(features)

interactions = events.createInteractions(objects)
for inter in interactions:
    inter.computeIndicators()
    inter.computeCrossingsCollisions(predictionParameters, params.collisionDistance, params.predictionTimeHorizon, params.crossingZones, nProcesses = args.nProcesses)

storage.saveIndicators(params.databaseFilename, interactions)

if args.displayCollisionPoints:
    plt.figure()
    allCollisionPoints = []
示例#6
0
文件: main.py 项目: Transience/buffer
__author__ = 'Akhil'

import cv2
import storage
from numpy.linalg.linalg import inv
from numpy import loadtxt
import cvutils

databaseFilename = "laurier.sqlite"
objectNumber = [0]
trajectoryType = "feature"

objects = storage.loadTrajectoriesFromSqlite(databaseFilename, trajectoryType)
obj = objects[0]
print obj.existsAtInstant(5)
homographyFilename = "laurier-homography.txt"
homography = inv(loadtxt(homographyFilename))
fNo = 10

p = [150, 80]

def projectArray(homography, points):
    from numpy.core import dot
    from numpy.lib.function_base import append

    if points.shape[0] != 2:
        raise Exception('points of dimension {0} {1}'.format(points.shape[0], points.shape[1]))

    if (homography is not None) and homography.size>0:
        augmentedPoints = append(points,[[1]*points.shape[1]], 0)
        prod = dot(homography, augmentedPoints)
def create_trajectory_video(video_path,
                            db_filename,
                            homography_path,
                            output_path,
                            first_frame=0,
                            last_frame=None,
                            video_type='object',
                            objects_to_label=None,
                            bounding_boxes=False):
    '''
    Creates a video of tracked trajectories.

    video_path: a path to the video on which to overlay
    db_filename: path to the database of tracked objects and features
    homography_path: the path to the homography.txt of the project
    output_path: The path of the video to be created. Please be sure that the output video format works on your system! (mp4 doesn't work on Windows AFAIK)
    first_frame: frame to start at
    last_frame: frame to end at
    video_type: either 'object' or 'feature'
    bounding_boxes: boolean indicating whether to show bounding boxes for objects
    '''
    capture = cv2.VideoCapture(video_path)
    width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
    height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))

    capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, first_frame)
    frame_num = first_frame

    framerate = get_framerate(video_path)

    print('Loading objects, please wait...')
    objects = loadTrajectoriesFromSqlite(db_filename,
                                         video_type,
                                         objectNumbers=objects_to_label,
                                         withFeatures=bounding_boxes)
    homography = inv(loadtxt(homography_path))

    ret = True
    objectToDeleteIds = []

    out = get_video_writer(output_path, framerate, width, height)

    while ret:
        if last_frame is not None and frame_num > last_frame:
            break

        ret, img = capture.read()
        if ret:
            if frame_num % 100 == 0:
                print('frame {0}'.format(frame_num))

            if len(objectToDeleteIds) > 0:
                objects = [
                    o for o in objects if o.getNum() not in objectToDeleteIds
                ]
                objectToDeleteIds = []

            # plot objects
            for obj in objects:
                if obj.existsAtInstant(frame_num):
                    # Only draw for objects that should be labeled, if passed in
                    if objects_to_label is not None and obj.getNum(
                    ) not in objects_to_label:
                        continue

                    if obj.getLastInstant() == frame_num:
                        objectToDeleteIds.append(obj.getNum())

                    # Project the positions with homography
                    if not hasattr(obj, 'projectedPositions'):
                        obj.projectedPositions = obj.positions.project(
                            homography)

                    # Plot it's trajectory until now
                    cvPlot(img, obj.projectedPositions, colors[obj.getNum()],
                           frame_num - obj.getFirstInstant())

                    # Plot the object's bounds if it has features
                    if obj.hasFeatures():
                        imgcrop, yCropMin, yCropMax, xCropMin, xCropMax = imageBox(
                            img, obj, frame_num, homography, width, height)
                        cv2.rectangle(img, (xCropMin, yCropMin),
                                      (xCropMax, yCropMax), green, 1)

                    # Put object id and type if it's an object video
                    # If it's a feature video, there's too many numbers, let's ignore it.
                    if video_type == 'object':
                        objDescription = '{} '.format(obj.num)
                        if userTypeNames[obj.userType] != 'unknown':
                            objDescription += userTypeNames[
                                obj.userType][0].upper()
                        cv2.putText(
                            img,
                            objDescription,
                            obj.projectedPositions[
                                frame_num -
                                obj.getFirstInstant()].asint().astuple(),
                            cv2.FONT_HERSHEY_PLAIN,
                            3,
                            colors[obj.getNum()],
                            thickness=4)

            # Write image
            out.write(img)

            frame_num += 1

    capture.release()
    out.release()
    cv2.destroyAllWindows()
示例#8
0
__author__ = 'Akhil'

import cv2
import storage
import sqlite3
import cvutils
import itertools
import shutil
from numpy.linalg.linalg import inv
from numpy import loadtxt

homographyFilename = "laurier-homography.txt"
homography = inv(loadtxt(homographyFilename))
databaseFilename = "laurier.sqlite"
newFilename = "corrected.sqlite"
cObjects = storage.loadTrajectoriesFromSqlite(newFilename, "object")
objects = storage.loadTrajectoriesFromSqlite(databaseFilename, "object")
features = storage.loadTrajectoriesFromSqlite(databaseFilename, "feature")

drawing = False   # true if mouse is pressed
cArray = []   # stores new trajectory positions (temporary)
fNo = 0   # stores the frame number
objTag = None   # stores selected object's id
edit = False   # turns on edit mode if true
trace = []   # holds the trace coordinates

def findObject(frameNum, x=None, y=None):
    global objects, features, objTag
    box = []
    for obj in objects:
        if obj.existsAtInstant(frameNum):
    undistortedImageMultiplication = params.undistortedImageMultiplication
    undistort = params.undistort
    firstFrameNum = params.firstFrameNum
else:
    homography = None
    undistort = False
    intrinsicCameraMatrix = None
    distortionCoefficients = []
    undistortedImageMultiplication = None
    firstFrameNum = 0

if not args.configFilename and args.videoFilename is not None:
    videoFilename = args.videoFilename
if not args.configFilename and args.databaseFilename is not None:
    databaseFilename = args.databaseFilename
if not args.configFilename and args.homographyFilename is not None:
    homography = inv(loadtxt(args.homographyFilename))            
if not args.configFilename and args.intrinsicCameraMatrixFilename is not None:
    intrinsicCameraMatrix = loadtxt(args.intrinsicCameraMatrixFilename)
if not args.configFilename and args.distortionCoefficients is not None:
    distortionCoefficients = args.distortionCoefficients
if not args.configFilename and args.undistortedImageMultiplication is not None:
    undistortedImageMultiplication = args.undistortedImageMultiplication
if args.firstFrameNum is not None:
    firstFrameNum = args.firstFrameNum


objects = storage.loadTrajectoriesFromSqlite(databaseFilename, args.trajectoryType)
boundingBoxes = storage.loadBoundingBoxTableForDisplay(databaseFilename)
cvutils.displayTrajectories(videoFilename, objects, boundingBoxes, homography, firstFrameNum, args.lastFrameNum, rescale = args.rescale, nFramesStep = args.nFramesStep, saveAllImages = args.saveAllImages, undistort = (undistort or args.undistort), intrinsicCameraMatrix = intrinsicCameraMatrix, distortionCoefficients = distortionCoefficients, undistortedImageMultiplication = undistortedImageMultiplication)
示例#10
0
parser.add_argument('-g', dest = 'groundTruthDatabaseFilename', help = 'name of the Sqlite database containing the ground truth', required = True)
parser.add_argument('-o', dest = 'homographyFilename', help = 'name of the filename for the homography (if tracking was done using the homography)')
parser.add_argument('-m', dest = 'matchingDistance', help = 'matching distance between tracker and ground truth trajectories', required = True, type = float)
parser.add_argument('--mask', dest = 'maskFilename', help = 'filename of the mask file used to define the where objects were tracked')
parser.add_argument('-f', dest = 'firstInstant', help = 'first instant for measurement', required = True, type = int)
parser.add_argument('-l', dest = 'lastInstant', help = 'last instant for measurement', required = True, type = int)
parser.add_argument('--display', dest = 'display', help = 'display the ground truth to object matches (graphically)', action = 'store_true')
parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (for display)')
args = parser.parse_args()

if args.homographyFilename is not None:
    homography = loadtxt(args.homographyFilename)
else:
    homography = None

objects = storage.loadTrajectoriesFromSqlite(args.trackerDatabaseFilename, 'object')

if args.maskFilename is not None:
    maskObjects = []
    from matplotlib.pyplot import imread
    mask = imread(args.maskFilename)
    if len(mask) > 1:
        mask = mask[:,:,0]
    for obj in objects:
        maskObjects += obj.getObjectsInMask(mask, inv(homography), 2) # TODO add option to keep object if at least one feature in mask
    objects = maskObjects    

annotations = storage.loadGroundTruthFromSqlite(args.groundTruthDatabaseFilename)
for a in annotations:
    a.computeCentroidTrajectory(homography)
#  filtering.py database_file_name fps
import numpy as np
import sys
import os
import gen2_tools
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'TI'))
import storage

database_file_name = sys.argv[1] # sqlite file
fps = float(sys.argv[2])
print 'loading database...'
objects = storage.loadTrajectoriesFromSqlite(database_file_name, 'object')
print 'filtering...'

for obj in objects:
	obj_speed = obj.getSpeeds()
	obj_speed.sort()
	obj_max_speed = obj_speed[int(.95 * len(obj))] * fps * 3.6 # in kmph
	obj_positions = obj.getPositions()
	first_last_position_distance = ((obj_positions[0].x - obj_positions[-1].x) ** 2 + (obj_positions[0].y - obj_positions[-1].y) ** 2) ** .5
	if first_last_position_distance < 2: # remove any object that moved less than 2m
		obj.userType = gen2_tools.userType2Num('unknown')
	elif obj.userType == gen2_tools.userType2Num('car'):
		if obj_max_speed < 5:
			obj.userType = gen2_tools.userType2Num('unknown')
	elif obj_max_speed < 2:
	 	obj.userType = gen2_tools.userType2Num('unknown')

storage.setRoadUserTypes(database_file_name, objects)
                      'pedestrian': lambda s: norm(params.meanPedestrianSpeed, params.stdPedestrianSpeed).pdf(s), 
                      'bicycle': lambda s: lognorm(params.scaleCyclistSpeed, loc = 0., scale = np.exp(params.locationCyclistSpeed)).pdf(s)} # numpy lognorm shape, loc, scale: shape for numpy is scale (std of the normal) and scale for numpy is location (mean of the normal)

if args.plotSpeedDistribution:
    import matplotlib.pyplot as plt
    plt.figure()
    for k in speedProbabilities:
        plt.plot(np.arange(0.1, args.maxSpeedDistributionPlot, 0.1), [speedProbabilities[k](s/3.6/25) for s in np.arange(0.1, args.maxSpeedDistributionPlot, 0.1)], label = k)
    plt.xlabel('Speed (km/h)')
    plt.ylabel('Probability')
    plt.legend()
    plt.title('Probability Density Function')
    plt.show()
    sys.exit()

objects = storage.loadTrajectoriesFromSqlite(databaseFilename, 'object', args.nObjects, withFeatures = True)
#features = storage.loadTrajectoriesFromSqlite(databaseFilename, 'feature')
intervals = []
for obj in objects:
    #obj.setFeatures(features)
    intervals.append(obj.getTimeInterval())
timeInterval = moving.unionIntervals(intervals)

capture = cv2.VideoCapture(videoFilename)
width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))

pastObjects = []
if params.undistort: # setup undistortion
    [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients)
if capture.isOpened():
parser.add_argument('--metric', dest = 'metric', help = 'metric for the similarity of trajectory points', default = 'cityblock') # default is manhattan distance
parser.add_argument('-s', dest = 'minSimilarity', help = 'minimum similarity to put a trajectory in a cluster', type = float, required = True)
parser.add_argument('-c', dest = 'minClusterSize', help = 'minimum cluster size', type = int, default = None)
parser.add_argument('--display', dest = 'display', help = 'display trajectories', action = 'store_true') # default is manhattan distance

args = parser.parse_args()

# TODO parameters (random init?) and what to learn from: objects, features, longest features from objects
# TODO add possibility to cluter with velocities

trajectoryType = args.trajectoryType
if args.trajectoryType == 'objectfeatures':
    trajectoryType = 'object'

#features = storage.loadTrajectoriesFromSqlite(databaseFilename, args.trajectoryType)
objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, trajectoryType, withFeatures = (args.trajectoryType == 'objectfeatures'), objectNumbers = args.nTrajectories)

if args.trajectoryType == 'objectfeatures':
    features = []
    for o in objects:
        tmp = utils.sortByLength(o.getFeatures(), reverse = True)
        features += tmp[:min(len(tmp), 3)]
    objects = features

trajectories = [o.getPositions().asArray().T for o in objects]

lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon)
nTrajectories = len(trajectories)

similarities = -np.ones((nTrajectories, nTrajectories))
# for i in xrange(nTrajectories):
                    help='display trajectories',
                    action='store_true')  # default is manhattan distance

args = parser.parse_args()

# TODO parameters (random init?) and what to learn from: objects, features, longest features from objects
# TODO add possibility to cluter with velocities

trajectoryType = args.trajectoryType
if args.trajectoryType == 'objectfeatures':
    trajectoryType = 'object'

#features = storage.loadTrajectoriesFromSqlite(databaseFilename, args.trajectoryType)
objects = storage.loadTrajectoriesFromSqlite(
    args.databaseFilename,
    trajectoryType,
    withFeatures=(args.trajectoryType == 'objectfeatures'),
    objectNumbers=args.nTrajectories)

if args.trajectoryType == 'objectfeatures':
    features = []
    for o in objects:
        tmp = utils.sortByLength(o.getFeatures(), reverse=True)
        features += tmp[:min(len(tmp), 3)]
    objects = features

trajectories = [o.getPositions().asArray().T for o in objects]

lcss = utils.LCSS(metric=args.metric, epsilon=args.epsilon)
nTrajectories = len(trajectories)