Last active
September 12, 2017 16:14
-
-
Save tawnkramer/5691e82fa870a675601f4d9d2803495e to your computer and use it in GitHub Desktop.
A FIFO of N previous images into a single N channel image, after converting each to grayscale.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ImgFIFO: | |
""" | |
Stack N previous images into a single N channel image, after converting each to grayscale. | |
The most recent image is the last channel, and pushes previous images towards the front. | |
""" | |
def __init__(self, num_channels=3): | |
self.img_arr = None | |
self.num_channels = num_channels | |
def run(self, img_arr): | |
width, height, _ = img_arr.shape | |
gray = cv2.cvtColor(img_arr, cv2.COLOR_RGB2GRAY) | |
if self.img_arr is None: | |
self.img_arr = np.zeros([width, height, self.num_channels], dtype=np.dtype('B')) | |
for ch in range(self.num_channels - 1): | |
self.img_arr[...,ch] = self.img_arr[...,ch+1] | |
self.img_arr[...,self.num_channels - 1:] = np.reshape(gray, (width, height, 1)) | |
return self.img_arr | |
class TubImageStacker(dk.parts.Tub): | |
''' | |
A Tub to try out training against images that are saved out in the normal format, not the one created above. | |
If you drive with the above image fifo part, then you don't need to do any extra work on the training. Just make | |
sure your inference pass also sees the same fifo image. | |
''' | |
def stack3Images(self, img_a, img_b, img_c): | |
''' | |
convert 3 rgb images into grayscale and put them into the 3 channels of | |
a single output image | |
''' | |
width, height, _ = img_a.shape | |
gray_a = cv2.cvtColor(img_a, cv2.COLOR_RGB2GRAY) | |
gray_b = cv2.cvtColor(img_b, cv2.COLOR_RGB2GRAY) | |
gray_c = cv2.cvtColor(img_c, cv2.COLOR_RGB2GRAY) | |
img_arr = np.zeros([width, height, 3], dtype=np.dtype('B')) | |
img_arr[...,0] = np.reshape(gray_a, (width, height)) | |
img_arr[...,1] = np.reshape(gray_b, (width, height)) | |
img_arr[...,2] = np.reshape(gray_c, (width, height)) | |
return img_arr | |
def get_record(self, ix): | |
''' | |
get the current record and two previous. | |
stack the 3 images into a single image. | |
''' | |
data = super(TubImageStacker, self).get_record(ix) | |
if ix > 1: | |
data_ch1 = super(TubImageStacker, self).get_record(ix - 1) | |
data_ch0 = super(TubImageStacker, self).get_record(ix - 2) | |
json_data = self.get_json_record(ix) | |
for key, val in json_data.items(): | |
typ = self.get_input_type(key) | |
#load objects that were saved as separate files | |
if typ == 'image': | |
val = self.stack3Images(data_ch0[key], data_ch0[key], data[key]) | |
data[key] = val | |
elif typ == 'image_array': | |
img = self.stack3Images(data_ch0[key], data_ch0[key], data[key]) | |
val = np.array(img) | |
return data | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment