From c712a665806e1157ed419493d88731e65a038512 Mon Sep 17 00:00:00 2001 From: Christopher Rhodes <christopher.rhodes@embl.de> Date: Fri, 1 Sep 2023 16:43:07 +0200 Subject: [PATCH] Still working to decode model_id and run inference in ImageJ Jython environment --- imagej/infer_ilastik_by_api.py | 50 ++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 imagej/infer_ilastik_by_api.py diff --git a/imagej/infer_ilastik_by_api.py b/imagej/infer_ilastik_by_api.py new file mode 100644 index 00000000..f974f8a5 --- /dev/null +++ b/imagej/infer_ilastik_by_api.py @@ -0,0 +1,50 @@ +import httplib +import urllib2 +import json +import urllib + +import os +import sys +print(sys.version) + +from ij import IJ + +host = '127.0.0.1' +port = 8001 +uri = 'http://{}:{}/'.format(host, port) + +abspath = IJ.getImage().getProp('Location') +input_filename = os.path.split(abspath)[-1] + +outpath = 'C:\\Users\\rhodes\\projects\\proj0015-model-server\\resources\\testdata' + +global connection +connection = httplib.HTTPConnection('127.0.0.1', 8001) +def hit_endpoint(method, endpoint, params=None): + if not method in ['GET', 'PUT']: + raise Exception('Can only handle GET and PUT requests') + if params: + url = endpoint + '?' + urllib.urlencode(params) + else: + url = endpoint + connection.request(method, url) + resp = connection.getresponse() + print(method + ' ' + url + ', status ' + str(resp.status) + ':\n' + str(json.loads(resp.read()))) + return resp + +hit_endpoint('GET', '/') +hit_endpoint('GET', '/models') +hit_endpoint('PUT', '/bounce_back', {'par1': 'ghij'}) +hit_endpoint('PUT', '/models/ilastik/pixel_classification/load/', {'project_file': 'demo_px.ilp'}) +resp = hit_endpoint('GET', '/models') + +print(resp.read()) +#print(json.loads(resp.read())) # trying to extract model_id, but json.loads throws an error + +#infer_params = { +# 'model_id': model_id, +# 'input_filename': input_filename, +# 'channel': 0 +# } +# +#hit_endpoint('PUT', '/infer/from_image_file', infer_params) \ No newline at end of file -- GitLab