Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mobile Vision API new detector frame get Bitmap Image

Tags:

android

dlib

I know similar Question has been asked Before:- Mobile Vision API - concatenate new detector object to continue frame processing

I am receiving the frame but when I call frame.getBitmap() it returns a null object. I want to use this bitmap in https://github.com/tzutalin/dlib-android-app (Android dlib Library) FaceDet function.

like image 602
Honney Goyal Avatar asked Apr 27 '17 03:04

Honney Goyal


1 Answers

According to Mobile Vision API documentation, Frame object has method getBitmap() but it's clearly stated that

getBitmap()
Returns the bitmap which was specified in creating this frame, or null if no bitmap was used to create this frame.

If you really want to get Bitmap object, you will have to create it yourself. One options is by getGrayscaleImageData() method on Frame object.
If there are some bytes in returned ByteBuffer, you can convert it to Bitmap.

Firstly you must create YuvImage using byte array from your getGrayscaleImageData() result. It's a mandatory step because byte array have image in YUV/YCbCr color space, encoded in NV21 format. So first line will look like this:

YuvImage yuvImage = new YuvImage(frame.getGrayscaleImageData().array(), ImageFormat.NV21, width, height, null);

width and height can be extracted from frame by getMedatada().getHeight() / getMedatada().getWidth() methods.

Then you can use ByteArrayOutputStream to quickly compress your YuvImage object.

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, byteArrayOutputStream);

From there you can convert it to byte array again to finally use it in BitmapFactory.

byte[] jpegArray = byteArrayOutputStream.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.length);

I know that it's alot of code comparing to simple getBitmap() method usage, but it will do the job if you really need bitmap in that kind of situation.

like image 100
palucdev Avatar answered Oct 21 '22 09:10

palucdev