Skip to content

Instantly share code, notes, and snippets.

@tdavchev
Last active September 2, 2018 08:28
Show Gist options
  • Save tdavchev/e0a506a61f859f8df14a6e7670c7b12a to your computer and use it in GitHub Desktop.
Save tdavchev/e0a506a61f859f8df14a6e7670c7b12a to your computer and use it in GitHub Desktop.
import pickle
import numpy as np
def social_frame_preprocess():
'''
Preprocess the frames from the datasets.
Convert values to pixel locations from millimeters
obtain and store all frames data the actually used frames (as some are skipped),
the ids of all pedestrians that are present at each of those frames and the sufficient statistics.
'''
def collect_stats():
''' Collect the means and standard deviation of all (x, y) positions of the agents '''
x_pos = []
y_pos = []
for agent_id in range(1, len(agents)):
trajectory = [[] for _ in range(3)]
traj = agents[agent_id]
for step in traj:
x_pos.append(step[1])
y_pos.append(step[2])
x_pos = np.asarray(x_pos)
y_pos = np.asarray(y_pos)
# takes the average over all points through all agents
return [[np.mean(x_pos), np.std(x_pos)], [np.mean(y_pos), np.std(y_pos)]]
allPedsInFrame = []
Hfile = os.path.join(directory, "H.txt")
obsfile = os.path.join(directory, "obsmat.txt")
# Parse homography matrix.
H = np.loadtxt(Hfile)
Hinv = np.linalg.inv(H)
# Parse pedestrian annotations.
frames, pedsInFrame, agents = parse_annotations(Hinv, obsfile)
allPedsInFrame.append(agents)
framesContent = np.zeros((len(frames), 40, 3))
# collect mean and std
statistics = collect_stats()
# collect the id, normalised x and normalised y of each agent's position
for ind, agent_list in enumerate(pedsInFrame):
pedsWithPos = []
for agent_id in agent_list:
traj = agents[agent_id]
for step in traj:
if step[0] == ind:
pedsWithPos.append([
agent_id,
(step[1] - statistics[0][0]) / statistics[0][1],
(step[2] - statistics[1][0]) / statistics[1][1]
])
framesContent[ind, 0:len(agent_list), :] = np.array(pedsWithPos)
return framesContent, frames, allPedsInFrame, statistics
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment