import os
import cv2 as cv2
import numpy as np
import torch
from torch.utils.data import Dataset
from torchsig.image_datasets.transforms.denoising import normalize_image
[docs]
def load_image_rgb(filepath):
f = cv2.imread(filepath)
img = cv2.cvtColor(f, cv2.COLOR_BGR2RGB)
return img
[docs]
def load_image_grey(filepath):
f = cv2.imread(filepath)
img = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
return img
'''
Get Coordinates, Height, Width of drawn bounding boxes
Inputs:
img: image array in rbg format
Output:
boxes: list of image arrays in rbg format, corresponding to the contents of each bounding box
'''
'''
Isolate SOI
soi_image, assumed BGR colorspace
'''
[docs]
def isolate_soi(soi_image, filter_strength=0):
test_hsv = cv2.cvtColor(soi_image, cv2.COLOR_BGR2HSV)
lower = np.array([0, 0, 0])
upper = np.array([360, 255, int(255/2)]) # hand tuned, HARD CODED # TODO hard coded considered harmful :(
upper = np.array([360, 255, int(255/2) - filter_strength]) # HARD CODED # TODO hard coded considered harmful :(
mask = cv2.inRange(test_hsv, lower, upper)
# plt.imshow(mask)
img_contours, _ = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
blank = np.ones(soi_image.shape, np.uint8) * 255
d = cv2.drawContours(blank, img_contours, -1, (0, 0, 0), -1)
final_image = np.bitwise_or(d, soi_image)
return final_image
'''
Process SOI images
Inputs:
filepaths: list of filepaths to SOI images
Outputs:
boxes: list of extracted SOIs, list of SignalBoxImage
'''
"""
A Dataset class for loading marked signals of interest (SOIs) from images in a folder
Inputs:
filepath: a string file path to a folder containing images in which all signals of interest have been marked wit ha colored bounding box
transforms: either a single function or list of functions from images to images to be applied to each SOI; used for adding noise and impairments to data; defaults to None
read_black_hot: whether or not to read loaded images as black-hot; this will invert the value of loaded SOIs
"""
"""
A Dataset class for loading image files from a directory
Inputs:
filepath: a string file path to a folder containing .png images to load
transforms: either a single function or list of functions from images to images to be applied to each loaded image; used for adding noise and impairments to data; defaults to None
read_black_hot: whether or not to read loaded images as black-hot; this will invert the value of loaded SOIs
"""
[docs]
class ImageDirectoryDataset(Dataset):
[docs]
def __init__(self, filepath: str, transforms = None, read_black_hot = False):
self.filepath = filepath
self.transforms = transforms
image_paths = []
for f in os.listdir(filepath):
if f.endswith(".png"):
image_paths.append(os.path.join(filepath, f))
if read_black_hot:
self.images = [normalize_image(-load_image_rgb(path).mean(axis=-1)).unsqueeze(0) for path in image_paths]
else:
self.images = [normalize_image(load_image_rgb(path).mean(axis=-1)).unsqueeze(0) for path in image_paths]
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
image = torch.Tensor(self.images[idx])
if self.transforms:
if type(self.transforms) == list:
for transform in self.transforms:
image = transform(image)
else:
image = self.transforms(image)
return image
[docs]
def next(self):
return self[np.random.randint(len(self))]
"""
As ImageDirectoryDataset, but with lazy evaluation, so files are not loaded in advance
"""
[docs]
class LazyImageDirectoryDataset(Dataset):
[docs]
def __init__(self, filepath: str, transforms = None, read_black_hot = False):
self.filepath = filepath
self.transforms = transforms
self.image_paths = [os.path.join(filepath, f) for f in os.listdir(filepath) if f.endswith(".png")]
self.read_black_hot = read_black_hot
#if read_black_hot:
# self.images = [normalize_image(-load_image_rgb(path).mean(axis=-1)).unsqueeze(0) for path in image_paths]
#else:
# self.images = [normalize_image(load_image_rgb(path).mean(axis=-1)).unsqueeze(0) for path in image_paths]
def __len__(self):
return len(self.image_paths)
def __getitem__(self, idx):
image = []
path = self.image_paths[idx]
if self.read_black_hot:
image = normalize_image(-load_image_rgb(path).mean(axis=-1)).unsqueeze(0)
else:
image = normalize_image(load_image_rgb(path).mean(axis=-1)).unsqueeze(0)
image = torch.Tensor(image)
if self.transforms:
if type(self.transforms) == list:
for transform in self.transforms:
image = transform(image)
else:
image = self.transforms(image)
return image
[docs]
def next(self):
return self[np.random.randint(len(self))]