3 from pathlib
import Path
8 from zipfile
import ZipFile
17 if not zip_file.endswith(
".zip"):
21 Path(
"data").mkdir(parents=
True)
22 except FileExistsError
as e:
24 "'data' directory exist in the working directory already. Can't process images."
35 except FileExistsError
as e:
37 "Given zip file already exist. Pick different name, or move the zip file."
40 os.remove(
"partition.pickle")
41 os.remove(
"labels.pickle")
44 print(
"Created %s file" % (self.
zip_file))
46 zipObj.write(
"partition.pickle")
47 print(
"Zipped 'partition.pickle' file.")
48 zipObj.write(
"labels.pickle")
49 print(
"Zipped 'labels.pickle' file.")
51 images = os.listdir(
"data")
54 zipObj.write(os.path.join(
"data", img))
55 print(
"Zipped 'data' directory.")
58 print(
"Zip archive ready, removing temp files.")
60 os.remove(
"partition.pickle")
61 os.remove(
"labels.pickle")
64 labels = {
"linear": {},
"angular": {}}
65 partition = {
"validation": [],
"train": []}
68 print(
"processing %s/labels.txt" % (path))
69 file = open(os.path.join(path,
"labels.txt"),
"r")
70 lines = file.readlines()
74 labels[
"linear"][photo] = label_tuple[0]
75 labels[
"angular"][photo] = label_tuple[1]
78 partition[
"validation"].append(photo)
80 partition[
"train"].append(photo)
82 shutil.copy(os.path.join(path, photo), os.path.join(
"data", photo))
85 with open(
"labels.pickle",
"wb")
as handle:
86 pickle.dump(labels, handle, pickle.DEFAULT_PROTOCOL)
88 with open(
"partition.pickle",
"wb")
as handle:
89 pickle.dump(partition, handle, protocol=pickle.DEFAULT_PROTOCOL)
92 divide = string.split(
",")
93 first = float(divide[0][1:])
94 second = float(divide[1][:-1])
95 return (first, second)
98 divide = line.split(
":")
104 if __name__ ==
"__main__":
105 parser = argparse.ArgumentParser(description=
"Prepare dataset for neural network")
113 help=
"paths to directiories with training data",
123 help=
"paths to directiories with validation data",
132 help=
"name of the zip archive with dataset",
134 default=
"my_dataset.zip",
137 args = parser.parse_args(sys.argv[1:])