-
Notifications
You must be signed in to change notification settings - Fork 6
/
normalize_labels.py
executable file
·80 lines (65 loc) · 2.24 KB
/
normalize_labels.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Make all images in a folder binary."""
import os
from PIL import Image
import numpy
import scipy.misc
from keras.preprocessing.image import img_to_array
def main(directory):
"""Find all png images in directory and make them binary."""
files = [os.path.join(directory, f)
for f in sorted(os.listdir(directory))
if f.endswith('.png')]
for file_path in files:
img = get_image(file_path, 'L')
img = numpy.squeeze(img_to_array(img))
img = normalize_labels(img)
scipy.misc.imsave(file_path, img)
def get_image(image_path, force_mode=None):
"""
Get a numpy array of an image so that one can access values[y][x].
Parameters
----------
image_path : str
force_mode : {None, 'L', 'RGB', 'RGBA', ...}
Returns
-------
numpy array
"""
image = Image.open(image_path, 'r')
if force_mode is not None:
image = image.convert(mode=force_mode)
width, height = image.size
pixel_values = list(image.getdata())
if image.mode == 'RGB':
channels = 3
elif image.mode == 'RGBA':
image = Image.open(image_path).convert('RGB')
pixel_values = list(image.getdata())
channels = 3
elif image.mode == 'L':
channels = 1
else:
print("Unknown mode: %s" % image.mode)
print("image_path: %s" % image_path)
return None
pixel_values = numpy.array(pixel_values).reshape((height, width, channels))
return pixel_values
def normalize_labels(segmentation):
"""Set all labels which are not 0 to 1."""
return segmentation.astype(bool).astype(int)
def get_parser():
"""Get parser object for script xy.py."""
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
parser = ArgumentParser(description=__doc__,
formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument("-d", "--directory",
dest="directory",
help="directory of images which get binarized",
metavar="DIR",
required=True)
return parser
if __name__ == "__main__":
args = get_parser().parse_args()
main(args.directory)