-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrop.py
124 lines (105 loc) · 4.15 KB
/
crop.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import argparse
import os
import os.path as osp
import numpy as np
import ray
import yaml
from PIL import Image
from fundus_circle_cropping import fundus_cropping, utils
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--cfg", type=str, help="Path to config file.")
if __name__ == "__main__":
failures = []
# Load config from yaml file.
args = parser.parse_args()
yaml_path = os.path.join(args.cfg)
with open(yaml_path) as f:
cfg = yaml.safe_load(f)
# Create folders in case they are not already existing.
utils.create_folder(osp.join(cfg["root_folder"], cfg["image_folder"]))
if not cfg["minimal_save"]:
utils.create_folder(osp.join(cfg["root_folder"], cfg["mask_folder"]))
# Data folder and ids.
data_folder = osp.join(cfg["root_folder"], cfg["data_folder"])
with open(cfg["ids_file"], "r") as f:
list_files = f.read().split("\n")
if cfg["minimal_save"]:
id_to_ratios_mask = {}
if cfg["parallel_processing"]:
# MSA's additions for parallel processing
num_workers = os.cpu_count() - cfg["num_workers_reverse"]
print(f"Number of workers : {num_workers}")
ray.init(num_cpus=num_workers)
assert ray.is_initialized()
remote_fundus_cropping = ray.remote(fundus_cropping.fundus_image)
# Start tasks in parallel.
futures = [
remote_fundus_cropping.remote(
x=np.array(
Image.open(
osp.join(
data_folder, f"{x_id}.{cfg['file_extension_data_in']}"
),
),
),
x_id=x_id,
image_folder=osp.join(cfg["root_folder"], cfg["image_folder"]),
file_extension=cfg["file_extension_data_out"],
mask_folder=osp.join(cfg["root_folder"], cfg["mask_folder"]),
resize_shape=cfg["resize_shape"],
resize_canny_edge=cfg["resize_canny_edge"],
sigma_scale=cfg["sigma_scale"],
circle_fit_steps=cfg["circle_fit_steps"],
λ=cfg["λ"],
remove_rectangles=cfg["remove_rectangles"],
minimal_save=cfg["minimal_save"],
)
for x_id in list_files
]
results = ray.get(futures)
for result_idx, result in enumerate(results):
if result[1]: # failure
failures.append(list_files[result_idx])
if cfg["minimal_save"]:
id_to_ratios_mask[list_files[result_idx]] = result[0]
ray.shutdown()
assert not ray.is_initialized()
else:
for id in list_files:
print(f"Crop and save image mask of {id}.\n")
# Load image.
x = np.array(
Image.open(
osp.join(data_folder, f"{id}.{cfg['file_extension_data_in']}")
)
)
ratios, failure = fundus_cropping.fundus_image(
x=x,
x_id=id,
image_folder=osp.join(cfg["root_folder"], cfg["image_folder"]),
file_extension=cfg["file_extension_data_out"],
mask_folder=osp.join(cfg["root_folder"], cfg["mask_folder"]),
resize_shape=cfg["resize_shape"],
resize_canny_edge=cfg["resize_canny_edge"],
sigma_scale=cfg["sigma_scale"],
circle_fit_steps=cfg["circle_fit_steps"],
λ=cfg["λ"],
remove_rectangles=cfg["remove_rectangles"],
minimal_save=cfg["minimal_save"],
)
if failure:
failures.append(id)
if cfg["minimal_save"]:
id_to_ratios_mask[id] = ratios
# Save file names of failure images.
with open(
osp.join(cfg["root_folder"], cfg["image_folder"], "failures.lst"),
"w",
) as f:
f.write("\n".join(failures))
# Save mask ratios.
if cfg["minimal_save"]:
with open(
osp.join(cfg["root_folder"], f"{cfg['mask_ratios']}.yml"), "w"
) as outfile:
yaml.dump(id_to_ratios_mask, outfile)