简体   繁体   中英

unit testing FaceDetector Google mobile vision java.lang.UnsupportedOperationException

i'm setting up unit test for class method that involve FaceDetector from google mobile vision library but i got java.lang.UnsupportedOperationException, but it's work when running on real android device. is it possible to test FaceDetector code? thx

public class HeadDetector
{

    private Bitmap facePicture;

    private Context context;

    private RectF headRectangle=new RectF();

    public HeadDetector(Bitmap facePicture,Context context) {

        this.facePicture = facePicture;
        this.context = context;
    }


    public RectF generateHeadRectangle() {

        FaceDetector faceDetector = new
            FaceDetector.Builder(context).setTrackingEnabled(false)
            .build();

    Frame frame = new Frame.Builder().setBitmap(facePicture).build();
    // when unit test hit below code
    //java.lang.UnsupportedOperationException
    SparseArray<Face> faces = faceDetector.detect(frame);

    if(faces.size()>0){

        Face face = faces.get(0);

        headRectangle = new RectF(0,0,face.getWidth(),face.getHeight());
    }

    return headRectangle;

  }

}

public class HeadDetectorTest  {

    @Test
    public void shouldGenerateHeadRectangle(){

    Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

    HeadDetector headDetector = new HeadDetector( TestHelper.createBitmapTest(context), context);

    RectF rectF = headDetector.generateHeadRectangle();

    assertNotEquals(rectF.width(),0.0f);

    assertNotEquals(rectF.height(),0.0f);

    assertThat(rectF.height(),greaterThan(rectF.width()));

}

}

that UnsupportedOperationException most likely is being caused by attempting to open a hardware camera, which is not available. therefore, you can either annotate the test with @RequiresDevice :

@RequiresDevice
public class HeadDetectorTest  {
    ...
}

Indicates that a specific test should not be run on emulator.

It will be executed only if the test is running on the physical android device.

or connect a webcam to the emulator, in order to provide it with what it demands.

just make the webcam focus on the image of some head, to provide sample data :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM