-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
68 lines (49 loc) · 2.44 KB
/
Copy pathutils.py
File metadata and controls
68 lines (49 loc) · 2.44 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
65
66
67
import os
from sklearn.model_selection import train_test_split
# goes into annotations folder and split the test json file paths and train json file path
def split_by_json_file(
root="/home/public/mkamal/datasets/deep_learning/projdata/uploaded_data",
random_state = 42):
annotations_path = os.path.join(root, "annotations")
json_files = sorted([f for f in os.listdir(annotations_path) if f.endswith(".json")])
train_val_files, test_files = train_test_split(json_files, test_size=0.1, random_state=random_state)
# Save train+val list in current directory
with open("train_val.txt", "w") as f:
for item in train_val_files:
f.write("%s\n" % item)
# Save test list in current directory
with open("test.txt", "w") as f:
for item in test_files:
f.write("%s\n" % item)
print("Sucessfully outputted train-valid and testing files into disk")
return train_val_files, test_files
def read_json_files(json_file_path):
if (not os.path.exists(json_file_path)):
raise FileNotFoundError(f"No JSON file found at {json_file_path}")
with open(json_file_path, "r") as f:
json_files = [line.strip() for line in f.readlines()]
return json_files
# RUN THIS ONCE --- DONE
def scan_and_export_json_files(
annotations_folder_dir = "/home/public/mkamal/datasets/deep_learning/projdata/uploaded_data/annotations",
output_file="video_json_files.txt"):
json_files = []
if not os.path.exists(annotations_folder_dir):
raise FileNotFoundError(f"Directory not found: {annotations_folder_dir}")
if not os.path.isdir(annotations_folder_dir):
raise NotADirectoryError(f"Path is not a directory: {annotations_folder_dir}")
for _, _, files in os.walk(annotations_folder_dir):
for file_name in files:
if not file_name.endswith(".json"):
continue
json_files.append(file_name)
# Write to output file
with open(output_file, 'w') as f:
for json_file in json_files:
f.write(json_file + '\n')
print(f"Found {len(json_files)} JSON files. Written to {output_file}")
return json_files
def split_before_training(json_file, split_ratio, random_state=42):
data = read_json_files(json_file)
train_files, test_files = train_test_split(data, test_size=split_ratio, random_state=random_state)
return train_files, test_files