Skip to content

Instantly share code, notes, and snippets.

@louismullie
Created April 5, 2018 23:44
Show Gist options
  • Save louismullie/42595ca78f45a18a8df3849e551d4bcb to your computer and use it in GitHub Desktop.
Save louismullie/42595ca78f45a18a8df3849e551d4bcb to your computer and use it in GitHub Desktop.
from flask import Flask, request, send_file
import numpy as np, os, json
from scipy.misc import imsave
from coreslicer import read_dcm
app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = os.path.join(APP_ROOT, '../tmp/coreslicer')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def import_slice_file(request, index, upload_folder):
slices = json.loads(request.form['slices'])
slice_name = slices[index]['filename']
slices_data = request.files
slice_data = slices_data[slice_name]
slice_filename = os.path.join(upload_folder, slice_data.filename)
slice_data.save(slice_filename)
slice_data.stream.seek(0)
return slice_filename
def export_slice_file(mask, upload_folder):
image_filename = os.path.join(upload_folder, 'result.png')
imsave(image_filename, mask)
return image_filename
@app.route('/endpoint', methods = ['POST'])
def segmentation_function():
slice_filename = import_slice_file(request, 0, app.config['UPLOAD_FOLDER'])
dcm_image, grayscale, pixel_spacing = read_dcm(slice_filename)
mask = np.zeros((dcm_image.shape[0], dcm_image.shape[1], 4))
mask[dcm_image > 0] = (1, 1, 1, 1)
return send_file(
export_slice_file(mask, app.config['UPLOAD_FOLDER']),
attachment_filename='result.png', mimetype='image/png')
if __name__ == '__main__':
port = 8000
app.run(host='0.0.0.0', port=port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment