Skip to content

Commit

Permalink
Changed type of DICOM SEG. Improved make prediction servlet
Browse files Browse the repository at this point in the history
  • Loading branch information
Rui-Jesus committed Jul 10, 2023
1 parent b2fb511 commit ea37d05
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -194,7 +193,14 @@ private Task<MLInference> 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);
Expand All @@ -213,14 +219,19 @@ private Task<MLInference> 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();
Expand Down
11 changes: 8 additions & 3 deletions sdk/src/main/java/pt/ua/dicoogle/sdk/mlprovider/MLInference.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,7 +24,7 @@ public class MLInference {
private String resourcesFolder;

@JsonIgnore
private DicomInputStream dicomSEG;
private Path dicomSEG;

public HashMap<String, String> getMetrics() {
return metrics;
Expand All @@ -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;
}

Expand All @@ -64,4 +65,8 @@ public String getVersion() {
public void setVersion(String version) {
this.version = version;
}

public boolean hasResults(){
return metrics != null || annotations != null;
}
}

0 comments on commit ea37d05

Please sign in to comment.