diff --git a/dicoogle/src/main/java/pt/ua/dicoogle/server/web/servlets/mlprovider/MakePredictionServlet.java b/dicoogle/src/main/java/pt/ua/dicoogle/server/web/servlets/mlprovider/MakePredictionServlet.java index 177e1d3da..7f5c3c48a 100644 --- a/dicoogle/src/main/java/pt/ua/dicoogle/server/web/servlets/mlprovider/MakePredictionServlet.java +++ b/dicoogle/src/main/java/pt/ua/dicoogle/server/web/servlets/mlprovider/MakePredictionServlet.java @@ -27,9 +27,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; -import java.io.PrintWriter; +import java.io.*; +import java.nio.file.Files; import java.util.List; import java.util.UUID; import java.util.concurrent.ExecutionException; @@ -194,7 +193,14 @@ private Task sendRequest(String provider, String modelID, DimLevel return; } - if(prediction.getDicomSEG() != null){ + if((prediction.getDicomSEG() != null) && !prediction.hasResults()){ + response.setContentType("application/dicom"); + ServletOutputStream out = response.getOutputStream(); + try (InputStream fi = Files.newInputStream(prediction.getDicomSEG())) { + IOUtils.copy(fi, out); + out.flush(); + } + } else if((prediction.getDicomSEG() != null) && prediction.hasResults()){ // We have a file to send, got to build a multi part response String boundary = UUID.randomUUID().toString(); response.setContentType("multipart/form-data; boundary=" + boundary); @@ -213,14 +219,19 @@ private Task sendRequest(String provider, String modelID, DimLevel out.println(); out.print("Content-Disposition: form-data; name=\"dicomseg\"; filename=\"dicomseg.dcm\""); out.println(); - out.print("Content-Type: application/octet-stream"); + out.print("Content-Type: application/dicom"); out.println(); out.println(); - byte[] targetArray = new byte[prediction.getDicomSEG().available()]; - prediction.getDicomSEG().read(targetArray); - out.write(targetArray); + try (InputStream fi = Files.newInputStream(prediction.getDicomSEG())) { + IOUtils.copy(fi, out); + out.flush(); + } + + out.println(); + out.print("--" + boundary + "--"); out.flush(); out.close(); + } else { response.setContentType("application/json"); PrintWriter out = response.getWriter(); diff --git a/sdk/src/main/java/pt/ua/dicoogle/sdk/mlprovider/MLInference.java b/sdk/src/main/java/pt/ua/dicoogle/sdk/mlprovider/MLInference.java index 5af1b5483..d00508b43 100644 --- a/sdk/src/main/java/pt/ua/dicoogle/sdk/mlprovider/MLInference.java +++ b/sdk/src/main/java/pt/ua/dicoogle/sdk/mlprovider/MLInference.java @@ -4,6 +4,7 @@ import org.dcm4che2.io.DicomInputStream; import pt.ua.dicoogle.sdk.datastructs.dim.BulkAnnotation; +import java.nio.file.Path; import java.util.HashMap; import java.util.List; @@ -23,7 +24,7 @@ public class MLInference { private String resourcesFolder; @JsonIgnore - private DicomInputStream dicomSEG; + private Path dicomSEG; public HashMap getMetrics() { return metrics; @@ -49,11 +50,11 @@ public void setResourcesFolder(String resourcesFolder) { this.resourcesFolder = resourcesFolder; } - public DicomInputStream getDicomSEG() { + public Path getDicomSEG() { return dicomSEG; } - public void setDicomSEG(DicomInputStream dicomSEG) { + public void setDicomSEG(Path dicomSEG) { this.dicomSEG = dicomSEG; } @@ -64,4 +65,8 @@ public String getVersion() { public void setVersion(String version) { this.version = version; } + + public boolean hasResults(){ + return metrics != null || annotations != null; + } }