-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeed_analysis.py
More file actions
64 lines (52 loc) · 2.22 KB
/
speed_analysis.py
File metadata and controls
64 lines (52 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
from PIL import Image
import numpy as np
from utils import loadTracking, create_zoomed_image
from metrics import compute_and_store_metrics
def main():
"""
Run the speed analysis to reproduce the corresponding figures and metrics.
Expects the speed data to be located under `data/speed/Scan_1` ... `Scan_7`.
"""
# Define the main directory where the "Scan_X" folders are located
main_dir = "data/speed"
# Initialize an empty list to store the images
images = []
position = []
# Loop through the folders
for i in range(1, 9):
folder_name = f"Scan_{i}"
# load tracking file
tracking_file = os.path.join(main_dir, folder_name, "800_tracking.ts")
tracking = loadTracking(tracking_file)
if len(position) == 0:
# select the x, y, z position in the middle of the tracking
position = tracking["tracking"][len(tracking["tracking"]) // 2][:3, 3]
# store the index of the position
index = len(tracking["tracking"]) // 2
else:
# iterate through the tracking and select the x, y, z position closest to the previous position
temp_position = tracking["tracking"][0][:3, 3]
for idx, track in enumerate(tracking["tracking"]):
if np.linalg.norm(track[:3, 3] - position) < np.linalg.norm(
temp_position - position
):
temp_position = track[:3, 3]
# store the index of the position
index = idx
image_path = os.path.join(main_dir, folder_name, "800")
# Get the first image in the "800" folder
image_files = sorted(os.listdir(image_path))
closest_image_idx = image_files[index]
closest_image = Image.open(
os.path.join(image_path, closest_image_idx)
).convert("RGB")
# Append the image to the list
images.append(closest_image)
# convert PIL images to numpy arrays
images = [np.array(image) for image in images]
# normalize between 0 and 1
images = np.float32([(image[:, :, 0] / 255) for image in images])
compute_and_store_metrics(images, main_dir, "speed")
if __name__ == "__main__":
main()