Skip to content
Snippets Groups Projects
ilastik_map_objects.py 2.16 KiB
Newer Older
from os.path import basename, dirname

	"""
	Execute a sequence of client requests that load ilastik pixel and object classifiers, then infer on an image file
	:param request_func: (func) function that implements HTTP client, dependent on which environment request are called from
	:param in_abspath: (str) absolute path to image file to infer
	:param params:
		pixel_classifier_path: (str) absolute path to ilastik project file that defines a pixel classifier
		object_classifier_path: (str) absolute path to ilastik project file that defines an object classifier
		channel (optional): (int) channel of the input image to process, use all channels if not specified
	:return: (str) absolute path where a new object map is written
	"""

	where = dirname(in_abspath)
	in_file = basename(in_abspath)
	
	px_ilp = params['pixel_classifier_path']
	ob_ilp = params['object_classifier_path']
	mip = params.get('mip', False)
	# configure input and output paths
	resp = request_func(
		'PUT',
		{
			'path': where,
		}
	)
	assert resp['status'] == 200, 'Error setting up image directory'
	resp = request_func(
		'PUT',
		'/paths/watch_output',
		{
			'path': where,
		}
	)
	assert resp['status'] == 200, 'Error setting up image directory'
	
	# load pixel classifier
	resp = request_func(
		'PUT',
			'project_file': px_ilp,
			'duplicate': False,
		},
	)
	assert resp['status'], 'Error loading classifier: ' + px_ilp
	id_px_mod = resp['content']['model_id']
	
	# load object classifier
	resp = request_func(
		'PUT', '/ilastik/pxmap_to_obj/load/',
			'project_file': ob_ilp,
			'duplicate': False,
		},
	)
	assert resp['status'] == 200, 'Error loading object classifier: ' + {ob_ilp}
	id_ob_mod = resp['content']['model_id']
	
	# run inference
	resp = request_func(
		'PUT',
		'/ilastik/pixel_then_object_classification/infer',
		{
			'px_model_id': id_px_mod,
			'ob_model_id': id_ob_mod,
			'input_filename': in_file,
			'channel': channel,
	return resp['content']['object_map_filepath']