class SegmentationProgress(object):
	def __init__(self):
		self.logger = None
	
	def progress(self, current, total):
		
		# Create the progress logger once we know the total number of steps
		if self.logger is None:
			self.logger = ProgressLogger(total)
		
		# Display progress output to the user
		self.logger.progress(current, 'Performing inference...', sameLine = True)
# Progress output
numImages = len(originalImages)
print(
    'Generating validation report for the segmentation network ({} images)...'.
    format(numImages))

# Keep track of processing progress and timing
timer = TimeLogger(numImages, 'image')
progress = ProgressLogger(numImages)

# Process each input file
tableRows = []
for filenum, infile in enumerate(originalImages):

    # Progress output
    progress.progress(filenum,
                      'Processing validation image "{}"...'.format(infile))

    # Split the ground-truth binary mask from the other image channels
    raster = cv2.imread(infile, cv2.IMREAD_UNCHANGED)
    channels, groundTruth = maskutils.splitAlphaMask(raster)

    # Perform inference on the image data
    probabilities = SegmentationNetwork.infer(model, metadata, channels)
    prediction = np.argmax(probabilities, axis=2).astype(np.uint8)

    # Denoise the predicted mask and retrieve the exact bounding box of each individual particle
    denoised = SegmentationNetwork.denoise(prediction)
    sliced = cv2.cvtColor(denoised * 255, cv2.COLOR_GRAY2RGB)
    particleDetails = []
    borderColour = (0, 0, 255)
    borderThickness = math.floor(channels.shape[0] * 0.01)
# Progress output
print(
    'Preprocessing raw data for the segmentation network ({} files)...'.format(
        len(inputFiles)))

# Keep track of processing progress and timing
numFiles = len(inputFiles)
timer = TimeLogger(numFiles, 'file')
progress = ProgressLogger(numFiles)

# Process each input file
for filenum, infile in enumerate(inputFiles):

    # Progress output
    progress.progress(filenum,
                      'Preprocessing input file "{}"...'.format(infile))

    # Create a temporary directory to hold our intermediate files
    with tempfile.TemporaryDirectory() as tempDir:

        # Create the filenames for our intermediate files and output file
        channelsFile = os.path.join(tempDir, 'channels.tif')
        maskFile = os.path.join(tempDir, 'mask.tif')
        outfile = os.path.join(
            outputDir,
            infile.replace(inputDir + '/', '').replace('/', '-').replace(
                ' ', '-').replace('.tif', '.png'))

        # Use ImageMagick to extract the layers containing the RGB channel data and the binary mask
        subprocess.call([
            'magick', 'convert', '-quiet',
示例#4
0
# Retrieve the list of input files
inputDir = Configuration.path('segmentation_data_preprocessed')
outputDir = Configuration.path('segmentation_data_sliced')
inputFiles = natsorted(glob.glob(os.path.join(inputDir, '*.png')))

# Progress output
print('Slicing preprocessed data for the segmentation network ({} files)...'.
      format(len(inputFiles)))

# Keep track of processing progress and timing
numFiles = len(inputFiles)
timer = TimeLogger(numFiles, 'file')
progress = ProgressLogger(numFiles)

# Process each input file
for filenum, infile in enumerate(inputFiles):

    # Progress output
    progress.progress(filenum, 'Slicing input file "{}"...'.format(infile))

    # Slice the file
    SegmentationNetwork.sliceToDir(infile,
                                   outputDir,
                                   includeMask=True,
                                   warnOnOverwrite=True)

# Progress output
timer.end()
Logger.success('slicing complete ({}).'.format(timer.report()))
#!/usr/bin/env python3
from common import Configuration, Logger, TimeLogger, ProgressLogger, SegmentationNetwork
from natsort import natsorted
import glob, os

# Retrieve the list of input files
inputDir = Configuration.path('segmentation_data_sliced')
outputDir = Configuration.path('segmentation_data_windowed')
inputFiles = natsorted(glob.glob(os.path.join(inputDir, '*.png')))

# Progress output
print('Windowing sliced data for the segmentation network ({} files)...'.format(len(inputFiles)))

# Keep track of processing progress and timing
numFiles = len(inputFiles)
timer = TimeLogger(numFiles, 'file')
progress = ProgressLogger(numFiles)

# Process each input file
for filenum, infile in enumerate(inputFiles):
	
	# Progress output
	progress.progress(filenum, 'Windowing input file "{}"...'.format(infile))
	
	# Slice the file
	SegmentationNetwork.windowToDir(infile, outputDir, warnOnOverwrite=True)

# Progress output
timer.end()
Logger.success('windowing complete ({}).'.format(timer.report()))