Page MenuHomec4science

datareader_preprocessed.py
No OneTemporary

File Metadata

Created
Sun, Apr 28, 07:15

datareader_preprocessed.py

import pickle
import os
import pickle
import argparse
import time
import torch
from torch.autograd import Variable
import numpy as np
import project.data_utils as du
# all_files = os.listdir("test/") # imagine you're one directory above test dir
# print(all_files) #
'''
Reads preprocessed data. For each pedestrian, the coordinates are normalized and the coordinates of pedestrians in same frames are expressed
relatively to them
'''
#import utils
def DataPaths():
'''
returns the path of all videos for each scene
DataPaths[0] = biwi
DataPaths[1] = crowds
DataPaths[2] = mot
DataPaths[3] = stanford
'''
#biwi datasets Project/preprocessed_data/biwi/biwi_hotel.txt_5.txt
biwi_list = os.listdir('preprocessed_data/biwi')
#print(os.listdir('Project/preprocessed_data/biwi'))
#print(biwi_list, 'biwi list')
if 'Icon\r' in biwi_list: #remove the icon file
biwi_list.remove('Icon\r')
biwi_files = np.array(biwi_list) #
biwi_paths = np.array(['preprocessed_data/biwi/' + x for x in biwi_files])
#print(biwi_files,'biwi')
#crowds
crowds_list = os.listdir('preprocessed_data/crowds')
if 'Icon\r' in crowds_list: #remove the icon file
crowds_list.remove('Icon\r')
crowds_files = np.array(crowds_list) #
crowds_paths = np.array(['preprocessed_data/crowds/' + x for x in crowds_files])
#mot
mot_list = os.listdir('preprocessed_data/mot')
if 'Icon\r' in mot_list: #remove the icon file
mot_list.remove('Icon\r')
mot_files = np.array(mot_list) #
mot_paths = np.array(['preprocessed_data/mot/' + x for x in mot_files])
#stanford
stanford_list = os.listdir('preprocessed_data/stanford')
if 'Icon\r' in stanford_list: #remove the icon file
stanford_list.remove('Icon\r')
stanford_files = np.array(stanford_list) #
stanford_paths = np.array(['preprocessed_data/stanford/' + x for x in stanford_files])
path_dataset=[biwi_paths, crowds_paths, mot_paths, stanford_paths]
return path_dataset
class Pedestrian():
'''
Class of pedestrians, instanciated from data text files
Attributes:
frames: frames where the pedestrian appears
neighbors : other pedestrians in the frames
coords : coordinates of the pedestrian in each frame
neigh_coords: coordinates of the neighbors in each frame
'''
def __init__(self, pedID, frames, coords, neighbors, velocity):
self.pedID = pedID
self.frames = frames
self.coords = coords
self.neighbors = neighbors
self.velocity = velocity
def getPedID(self):
return self.pedID
def getFrames(self):
return self.frames
def getCoords(self):
return self.coords
def getNeighbors(self):
return self.neighbors
def getVelocity(self):
return self.velocity
def DataLoader(path_dataset):
'''
Returns a list of "Pedestrian" objects for each dataset passed as arg
'''
datalist = []
numpeds = len(path_dataset)
train_perc = 0.75
test_perc = 0.25
numtrain = np.ceil(numpeds*train_perc) #number of pedestrians for training
numtest = numpeds - numtrain #number of pedestrians for test set
for directory in range(0,len(path_dataset)):
data = np.genfromtxt(path_dataset[directory], dtype = 'float')
data = torch.from_numpy(data).view(-1,4)
pedID = data[0,1]
ped_data= data[(data[:,1]==pedID).nonzero(),:].view(-1,4)
frames = ped_data[:,0]
coords = ped_data[:,2:4]
#print(coords.size())
#print(path_dataset[directory])
if ped_data.size()[0] == data.size()[0]:#check if there are other peds in frame
neighbors = None
else:
neighbors = data[(data[:,1]!=pedID).nonzero(),:].view(-1,4)
#print(neighbors.size())
velocity = du.computeVelocity(coords)
current_ped = Pedestrian(pedID, frames, coords, neighbors, velocity)
datalist.append(current_ped)
#print('Number of frames: %f' %len(frameList))
traindata = datalist[:np.int_(numtrain)]
testdata = datalist[np.int_(numtrain):]
print('Number of pedestrians: %f' %numpeds)
print('Number of pedestrians in training set: %f' %numtrain)
print('Number of pedestrians in test set: %f' %numtest)
return traindata, testdata

Event Timeline