-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathutils.py
71 lines (61 loc) · 1.9 KB
/
utils.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
import os
from PIL import Image
from os import listdir
from os.path import isfile, join
import numpy as np
import os
from glob import glob
np.random.seed(123)
import warnings
import numpy as np
from sklearn.utils import shuffle
import cv2
def create_dir(path):
""" Create a directory. """
try:
if not os.path.exists(path):
os.makedirs(path)
except OSError:
print(f"Error: creating directory with name {path}")
def read_data(x, y):
""" Read the image and mask from the given path. """
image = cv2.imread(x, cv2.IMREAD_COLOR)
mask = cv2.imread(y, cv2.IMREAD_COLOR)
return image, mask
def read_params():
""" Reading the parameters from the JSON file."""
with open("params.json", "r") as f:
data = f.read()
params = json.loads(data)
return params
def load_data(path):
""" Loading the data from the given path. """
images_path = os.path.join(path, "image/*")
masks_path = os.path.join(path, "mask/*")
images = glob(images_path)
masks = glob(masks_path)
return images, masks
def shuffling(x, y):
x, y = shuffle(x, y, random_state=42)
return x, y
def get_image(image_path, image_size_wight, image_size_height,gray=False):
# load image
img = Image.open(image_path)
if img.mode != 'RGB':
img = img.convert('RGB')
if gray==True:
img = img.convert('L')
# center crop
img_center_crop = img
# resize
img_resized = img_center_crop.resize((image_size_height, image_size_wight), Image.ANTIALIAS)
edge = cv2.Canny(np.asarray(np.uint8(img_resized)),10,1000)
flag = False
# convert to numpy and normalize
img_array = np.asarray(img_resized).astype(np.float32)/255.0
edge = np.asarray(edge).astype(np.float32)/255.0
#print(img_array)
if gray==True:
img_array=(img_array >=0.5).astype(int)
img.close()
return img_array,edge