Skip to content

Commit

Permalink
Thread-safe FaceDetectionTranslator.java by using TranslatorContext (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanOltmann authored and frankfliu committed Apr 26, 2024
1 parent b538ed5 commit 0728c38
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public class FaceDetectionTranslator implements Translator<Image, DetectedObject
private double[] variance;
private int[][] scales;
private int[] steps;
private int width;
private int height;

public FaceDetectionTranslator(
double confThresh,
Expand All @@ -61,8 +59,10 @@ public FaceDetectionTranslator(
/** {@inheritDoc} */
@Override
public NDList processInput(TranslatorContext ctx, Image input) {
width = input.getWidth();
height = input.getHeight();

ctx.setAttachment("width", input.getWidth());
ctx.setAttachment("height", input.getHeight());

NDArray array = input.toNDArray(ctx.getNDManager(), Image.Flag.COLOR);
array = array.transpose(2, 0, 1).flip(0); // HWC -> CHW RGB -> BGR
// The network by default takes float32
Expand All @@ -78,6 +78,10 @@ public NDList processInput(TranslatorContext ctx, Image input) {
/** {@inheritDoc} */
@Override
public DetectedObjects processOutput(TranslatorContext ctx, NDList list) {

int width = (int) ctx.getAttachment("width");
int height = (int) ctx.getAttachment("height");

NDManager manager = ctx.getNDManager();
double scaleXY = variance[0];
double scaleWH = variance[1];
Expand Down

0 comments on commit 0728c38

Please sign in to comment.