Skip to content

Commit

Permalink
update in real time
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Dec 2, 2024
1 parent ffaa227 commit e84e154
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
14 changes: 13 additions & 1 deletion src/main/java/ai/nets/samj/gui/MainGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,21 @@ public void updateProgress(int n) {

@Override
public void drawRoi(List<Mask> masks) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(() -> consumer.addPolygonsFromGUI(masks));

}

@Override
public void deletePointPrompt(List<int[]> promptList) {
SwingUtilities.invokeLater(() -> promptList.forEach(proi -> consumer.deletePointRoi(proi)));
}

@Override
public void deleteRectPrompt(List<int[]> promptList) {
SwingUtilities.invokeLater(() -> promptList.stream()
.map(rect -> new Rectangle(rect[0], rect[1], rect[2] - rect[0], rect[3] - rect[1]))
.forEach(roi -> consumer.deleteRectRoi(roi)));
}

};
}
Expand Down
35 changes: 25 additions & 10 deletions src/main/java/ai/nets/samj/models/AbstractSamJ.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public interface BatchCallback {
void updateProgress(int n);

void drawRoi(List<Mask> masks);

void deletePointPrompt(List<int[]> promptList);

void deleteRectPrompt(List<int[]> promptList);

}

/** Essentially, a syntactic-shortcut for a String consumer */
Expand Down Expand Up @@ -377,6 +382,7 @@ else if (cropSize.length == 3 && cropSize[2] != 3)

}

@SuppressWarnings("unchecked")
private List<Mask> processAndRetrieveContours(HashMap<String, Object> inputs, BatchCallback callback)
throws IOException, RuntimeException, InterruptedException {
Map<String, Object> results = null;
Expand All @@ -390,8 +396,12 @@ private List<Mask> processAndRetrieveContours(HashMap<String, Object> inputs, Ba
break;
else if (task.message.equals(UPDATE_ID_CONTOUR)) {
callback.updateProgress(nRoisProcessed ++);
List<Mask> polys = defineMask((List<List<Number>>)task.outputs.get("temp_x"),
(List<List<Number>>)task.outputs.get("temp_y"), (List<List<Number>>)task.outputs.get("temp_mask"));
callback.drawRoi(polys);
} else if (task.message.equals(UPDATE_ID_N_CONTOURS)) {
callback.setTotalNumberOfRois(Integer.parseInt((String) task.outputs.get("n")));

}
break;
default:
Expand Down Expand Up @@ -423,18 +433,23 @@ else if (task.outputs.get("rle") == null)
}
throw e;
}

final List<List<Number>> contours_x_container = (List<List<Number>>)results.get("contours_x");
final Iterator<List<Number>> contours_x = contours_x_container.iterator();
final Iterator<List<Number>> contours_y = ((List<List<Number>>)results.get("contours_y")).iterator();
final Iterator<List<Number>> rles = ((List<List<Number>>)results.get("rle")).iterator();
final List<Mask> masks = new ArrayList<Mask>(contours_x_container.size());
while (contours_x.hasNext()) {
int[] xArr = contours_x.next().stream().mapToInt(Number::intValue).toArray();
int[] yArr = contours_y.next().stream().mapToInt(Number::intValue).toArray();
long[] rle = rles.next().stream().mapToLong(Number::longValue).toArray();
List<Mask> polys = defineMask((List<List<Number>>)results.get("contours_x"),
(List<List<Number>>)results.get("contours_y"), (List<List<Number>>)results.get("rle"));
return polys;
}

private List<Mask> defineMask(List<List<Number>> contoursX, List<List<Number>> contoursY, List<List<Number>> rles) {
final Iterator<List<Number>> contoursXIt = contoursX.iterator();
final Iterator<List<Number>> contoursYIt = contoursY.iterator();
final Iterator<List<Number>> rleIt = rles.iterator();
final List<Mask> masks = new ArrayList<Mask>(contoursX.size());
while (contoursXIt.hasNext()) {
int[] xArr = contoursXIt.next().stream().mapToInt(Number::intValue).toArray();
int[] yArr = contoursYIt.next().stream().mapToInt(Number::intValue).toArray();
long[] rle = rleIt.next().stream().mapToLong(Number::longValue).toArray();
masks.add(Mask.build(new Polygon(xArr, yArr, xArr.length), rle));
}
recalculatePolys(masks, encodeCoords);
return masks;
}

Expand Down

0 comments on commit e84e154

Please sign in to comment.