Skip to content

Commit

Permalink
add rle encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Oct 25, 2024
1 parent 0673f1a commit 38abb46
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
9 changes: 6 additions & 3 deletions src/main/java/ai/nets/samj/models/EfficientSamJ.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ protected void processMasksWithSam(SharedMemoryArray shmArr, boolean returnAll)
+ " cont_y += cont_y_val" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = cont_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = cont_y" + System.lineSeparator();
+ "task.outputs['contours_y'] = cont_y" + System.lineSeparator()
+ "task.outputs['rle'] = rle_masks" + System.lineSeparator();
code += "mask = 0" + System.lineSeparator();
code += "shm_mask.close()" + System.lineSeparator();
code += "shm_mask.unlink()" + System.lineSeparator();
Expand Down Expand Up @@ -319,7 +320,8 @@ protected void processPointsWithSAM(int nPoints, int nNegPoints, boolean returnA
+ "contours_x,contours_y = get_polygons_from_binary_mask(mask, only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = contours_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator();
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator()
+ "task.outputs['rle'] = rle_masks" + System.lineSeparator();
this.script = code;
}

Expand Down Expand Up @@ -350,7 +352,8 @@ protected void processBoxWithSAM(boolean returnAll) {
+ "contours_x,contours_y = get_polygons_from_binary_mask(mask, only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = contours_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator();
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator()
+ "task.outputs['rle'] = rle_masks" + System.lineSeparator();
this.script = code;
}

Expand Down
9 changes: 6 additions & 3 deletions src/main/java/ai/nets/samj/models/EfficientViTSamJ.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ protected void processMasksWithSam(SharedMemoryArray shmArr, boolean returnAll)
+ " contours_y += contours_y_val" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = contours_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator();
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator()
+ "task.outputs['rle'] = rle_masks" + System.lineSeparator();
code += "mask = 0" + System.lineSeparator();
code += "shm_mask.close()" + System.lineSeparator();
code += "shm_mask.unlink()" + System.lineSeparator();
Expand Down Expand Up @@ -405,7 +406,8 @@ protected void processPointsWithSAM(int nPoints, int nNegPoints, boolean returnA
+ "contours_x,contours_y = get_polygons_from_binary_mask(mask[0], only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = contours_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator();
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator()
+ "task.outputs['rle'] = rle_masks" + System.lineSeparator();
this.script = code;
}

Expand All @@ -426,7 +428,8 @@ protected void processBoxWithSAM(boolean returnAll) {
+ "contours_x,contours_y = get_polygons_from_binary_mask(mask[0], only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = contours_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator();
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator()
+ "task.outputs['rle'] = rle_masks" + System.lineSeparator();
this.script = code;
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/ai/nets/samj/models/PythonMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ public class PythonMethods {
* Run-Length Encoding (RLE) algorithm
*/
protected static String RLE_METHOD = ""
+ "def encode_rle(mask: Union[np.ndarray, List[List[int]]]) -> List[int]:" + System.lineSeparator()
+ "def encode_rle(mask: np.ndarray) -> List[int]:" + System.lineSeparator()
+ " \"\"\"" + System.lineSeparator()
+ " Encode a binary mask using Run-Length Encoding (RLE)." + System.lineSeparator()
+ " " + System.lineSeparator()
+ " Args:" + System.lineSeparator()
+ " mask: A 2D binary array (numpy array or list of lists) where 1 represents the object" + System.lineSeparator()
+ " mask: A 2D binary array (numpy array) where 1 represents the object" + System.lineSeparator()
+ " and 0 represents the background" + System.lineSeparator()
+ " " + System.lineSeparator()
+ " Returns:" + System.lineSeparator()
Expand Down Expand Up @@ -139,7 +139,8 @@ public class PythonMethods {
+ " if binary[start] == 1:" + System.lineSeparator()
+ " rle.extend([start, length])" + System.lineSeparator()
+ " " + System.lineSeparator()
+ " return rle" + System.lineSeparator();
+ " return rle" + System.lineSeparator()
+ "globals()['encode_rle'] = encode_rle" + System.lineSeparator();


protected static String SAM_EVERYTHING = ""
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/ai/nets/samj/models/Sam2.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ protected void processMasksWithSam(SharedMemoryArray shmArr, boolean returnAll)
+ " contours_y += contours_y_val" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = contours_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator();
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator()
+ "task.outputs['rle'] = rle_masks" + System.lineSeparator();
code += "mask = 0" + System.lineSeparator();
code += "shm_mask.close()" + System.lineSeparator();
code += "shm_mask.unlink()" + System.lineSeparator();
Expand Down Expand Up @@ -386,7 +387,8 @@ protected void processPointsWithSAM(int nPoints, int nNegPoints, boolean returnA
+ "contours_x,contours_y = get_polygons_from_binary_mask(mask[0], only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = contours_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator();
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator()
+ "task.outputs['rle'] = rle_masks" + System.lineSeparator();
this.script = code;
}

Expand All @@ -407,7 +409,8 @@ protected void processBoxWithSAM(boolean returnAll) {
+ "contours_x,contours_y = get_polygons_from_binary_mask(mask[0], only_biggest=" + (!returnAll ? "True" : "False") + ")" + System.lineSeparator()
+ "task.update('all contours traced')" + System.lineSeparator()
+ "task.outputs['contours_x'] = contours_x" + System.lineSeparator()
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator();
+ "task.outputs['contours_y'] = contours_y" + System.lineSeparator()
+ "task.outputs['rle'] = rle_masks" + System.lineSeparator();
this.script = code;
}

Expand Down

0 comments on commit 38abb46

Please sign in to comment.