-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_data.py
83 lines (71 loc) · 2.63 KB
/
load_data.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# import torch
# import pandas as pd
# from preprocess import get_files, get_dirs
# import numpy as np
# # def load(img_path, label_path):
# # label = pd.read_csv(label_path)
# # types = ['Freeform', 'Northwind']
# # paths, labels = [], []
# # for t in types:
# # dirs = get_dirs(img_path + t)
# # for d in dirs:
# # no = d[-5:]
# # l = label[label['file'] == no]['label'].to_numpy()[0]
# # files = get_files(d)
# # for file in files:
# # paths.append(d + '/' + file)
# # labels.append(l)
# # return paths, torch.from_numpy(np.array(labels)).view((len(labels), 1))
# def load(img_path, label_path):
# label = pd.read_csv(label_path)
# types = ['Freeform', 'Northwind']
# paths, labels = [], []
# for t in types:
# dirs = get_dirs(img_path + t)
# for d in dirs:
# files = get_files(d)
# for file in files:
# no = file
# l = label[label['file'] == no]['label'].to_numpy()[0]
# paths.append(d + '/' + file)
# labels.append(l)
# return paths, torch.from_numpy(np.array(labels)).view((len(labels), 1))
# if __name__ == "__main__":
# img_path = "your_img_path"
# label_path = "G:\keep\label.csv"
# paths, labels = load(img_path, label_path)
# print(paths)
# print(labels)
import torch
import pandas as pd
from preprocess import get_files, get_dirs
import numpy as np
def load(img_path, label_path):
label = pd.read_csv(label_path)
types = ['Freeform', 'Northwind']
paths, labels = [], []
for t in types:
print(f"Processing type: {t}")
dirs = get_dirs(img_path + '/' + t)
for d in dirs:
print(f"Processing directory: {d}")
files = get_files(d)
for file in files:
print(f"Processing file: {file}")
no = file
try:
l = label[label['file'] == no]['label'].to_numpy()[0]
print(f"Found label: {l} for file {file}")
paths.append(d + '/' + file)
labels.append(l)
except IndexError as e:
print(f"Label for file {file} not found.")
return paths, torch.from_numpy(np.array(labels)).view((len(labels), 1))
if __name__ == "__main__":
img_path = "G:\\keep\\test" # Update this path with the correct one.
label_path = "G:\\keep\\label.csv" # Ensure this path is correct and accessible.
paths, labels = load(img_path, label_path)
print("Final paths:")
print(paths)
print("Final labels:")
print(labels)