Search in sources :

Example 1 with FrameValues

use of androidx.media.filterfw.FrameValues in project platform_frameworks_base by android.

the class FaceSquareFilterTest method testFaceSquareFilter.

public void testFaceSquareFilter() throws Exception {
    final int INPUT_WIDTH = 1536;
    final int INPUT_HEIGHT = 2048;
    FrameImage2D image = createFrame(FrameType.image2D(FrameType.ELEMENT_RGBA8888, FrameType.READ_CPU), new int[] { INPUT_WIDTH, INPUT_HEIGHT }).asFrameImage2D();
    FrameValues facesFrame = createFrame(FrameType.array(Camera.Face.class), new int[] { 1, 1 }).asFrameValues();
    Bitmap bitmap = BitmapFactory.decodeStream(assetMgr.open("XZZ019.jpg"));
    image.setBitmap(bitmap);
    injectInputFrame("image", image);
    Face face = new Face();
    Rect faceRect = new Rect();
    // These are the values for image 141 with 1 face
    faceRect.set(-533, -453, 369, 224);
    face.rect = faceRect;
    Face[] faces = new Face[1];
    faces[0] = face;
    facesFrame.setValue(faces);
    injectInputFrame("faces", facesFrame);
    process();
    // ensure the output image has the rectangle in the right place
    FrameImage2D outputImage = getOutputFrame("image").asFrameImage2D();
    int[] pixels = new int[bitmap.getByteCount()];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    final int FACE_X_RANGE = 2000;
    final int WIDTH_OFFSET = 1000;
    final int HEIGHT_OFFSET = 1000;
    int top = (faceRect.top + HEIGHT_OFFSET) * bitmap.getHeight() / FACE_X_RANGE;
    int bottom = (faceRect.bottom + HEIGHT_OFFSET) * bitmap.getHeight() / FACE_X_RANGE;
    int left = (faceRect.left + WIDTH_OFFSET) * bitmap.getWidth() / FACE_X_RANGE;
    int right = (faceRect.right + WIDTH_OFFSET) * bitmap.getWidth() / FACE_X_RANGE;
    if (top < 0) {
        top = 0;
    } else if (top > bitmap.getHeight()) {
        top = bitmap.getHeight();
    }
    if (left < 0) {
        left = 0;
    } else if (left > bitmap.getWidth()) {
        left = bitmap.getWidth();
    }
    if (bottom > bitmap.getHeight()) {
        bottom = bitmap.getHeight();
    } else if (bottom < 0) {
        bottom = 0;
    }
    if (right > bitmap.getWidth()) {
        right = bitmap.getWidth();
    } else if (right < 0) {
        right = 0;
    }
    for (int j = 0; j < (bottom - top); j++) {
        // Left edge
        if (left > 0 && top > 0) {
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + left) + ImageConstants.RED_OFFSET] = (byte) ImageConstants.MAX_BYTE;
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + left) + ImageConstants.GREEN_OFFSET] = (byte) ImageConstants.MAX_BYTE;
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + left) + ImageConstants.BLUE_OFFSET] = (byte) ImageConstants.MAX_BYTE;
        }
        // Right edge
        if (right > 0 && top > 0) {
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + right) + ImageConstants.RED_OFFSET] = (byte) ImageConstants.MAX_BYTE;
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + right) + ImageConstants.GREEN_OFFSET] = (byte) ImageConstants.MAX_BYTE;
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + right) + ImageConstants.BLUE_OFFSET] = (byte) ImageConstants.MAX_BYTE;
        }
    }
    for (int k = 0; k < (right - left); k++) {
        // Top edge
        if (top < bitmap.getHeight()) {
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * top + left + k) + ImageConstants.RED_OFFSET] = (byte) ImageConstants.MAX_BYTE;
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * top + left + k) + ImageConstants.GREEN_OFFSET] = (byte) ImageConstants.MAX_BYTE;
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * top + left + k) + ImageConstants.BLUE_OFFSET] = (byte) ImageConstants.MAX_BYTE;
        }
        // Bottom edge
        if (bottom < bitmap.getHeight()) {
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * bottom + left + k) + ImageConstants.RED_OFFSET] = (byte) ImageConstants.MAX_BYTE;
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * bottom + left + k) + ImageConstants.GREEN_OFFSET] = (byte) ImageConstants.MAX_BYTE;
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * bottom + left + k) + ImageConstants.BLUE_OFFSET] = (byte) ImageConstants.MAX_BYTE;
        }
    }
    Bitmap outputBitmap = outputImage.toBitmap();
    int[] outputPixels = new int[outputBitmap.getByteCount()];
    outputBitmap.getPixels(outputPixels, 0, outputBitmap.getWidth(), 0, 0, outputBitmap.getWidth(), outputBitmap.getHeight());
    int equalCount = 0;
    for (int i = 0; i < outputBitmap.getByteCount(); i++) {
        if (pixels[i] == outputPixels[i])
            equalCount++;
    }
    if (equalCount + (0.05f * outputBitmap.getByteCount()) < outputBitmap.getByteCount()) {
        // Assertion will fail if condition is true
        assertEquals(equalCount, outputBitmap.getByteCount());
    }
}
Also used : Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) FrameValues(androidx.media.filterfw.FrameValues) FrameImage2D(androidx.media.filterfw.FrameImage2D) Camera(android.hardware.Camera) Face(android.hardware.Camera.Face)

Example 2 with FrameValues

use of androidx.media.filterfw.FrameValues in project platform_frameworks_base by android.

the class FaceSquareFilter method onProcess.

/**
     * @see androidx.media.filterfw.Filter#onProcess()
     */
@Override
protected void onProcess() {
    // Get inputs
    FrameImage2D imageFrame = getConnectedInputPort("image").pullFrame().asFrameImage2D();
    FrameValues facesFrame = getConnectedInputPort("faces").pullFrame().asFrameValues();
    Face[] faces = (Face[]) facesFrame.getValues();
    int[] dims = imageFrame.getDimensions();
    ByteBuffer buffer = imageFrame.lockBytes(Frame.MODE_WRITE);
    byte[] pixels = buffer.array();
    // For every face in faces, draw a white rect around the
    // face following the rect member of the Face
    drawBoxes(pixels, faces, dims);
    imageFrame.unlock();
    OutputPort outPort = getConnectedOutputPort("image");
    outPort.pushFrame(imageFrame);
}
Also used : OutputPort(androidx.media.filterfw.OutputPort) FrameValues(androidx.media.filterfw.FrameValues) FrameImage2D(androidx.media.filterfw.FrameImage2D) Face(android.hardware.Camera.Face) ByteBuffer(java.nio.ByteBuffer)

Example 3 with FrameValues

use of androidx.media.filterfw.FrameValues in project android_frameworks_base by DirtyUnicorns.

the class MotionSensorWTime method onProcess.

@Override
protected void onProcess() {
    OutputPort outPort = getConnectedOutputPort("values");
    FrameValues outFrame = outPort.fetchAvailableFrame(null).asFrameValues();
    synchronized (mValues) {
        if (mCounter < 3 && mCounter >= 0) {
            mTemp[0][mCounter] = mValues[0];
            mTemp[1][mCounter] = mValues[1];
            mTemp[2][mCounter] = mValues[2];
        }
        mCounter = (mCounter + 1) % 3;
        mAvgValues[0] = (mTemp[0][0] + mTemp[0][1] + mTemp[0][2]) / 3;
        mAvgValues[1] = (mTemp[1][0] + mTemp[1][1] + mTemp[1][2]) / 3;
        mAvgValues[2] = (mTemp[2][0] + mTemp[2][1] + mTemp[2][2]) / 3;
        outFrame.setValues(mAvgValues);
    }
    outFrame.setTimestamp(System.currentTimeMillis() * 1000000L);
    outPort.pushFrame(outFrame);
    OutputPort timeOutPort = getConnectedOutputPort("timestamp");
    if (timeOutPort != null) {
        long timestamp = System.nanoTime();
        Log.v("MotionSensor", "Timestamp is: " + timestamp);
        FrameValue timeStampFrame = timeOutPort.fetchAvailableFrame(null).asFrameValue();
        timeStampFrame.setValue(timestamp);
        timeOutPort.pushFrame(timeStampFrame);
    }
}
Also used : OutputPort(androidx.media.filterfw.OutputPort) FrameValues(androidx.media.filterfw.FrameValues) FrameValue(androidx.media.filterfw.FrameValue)

Example 4 with FrameValues

use of androidx.media.filterfw.FrameValues in project android_frameworks_base by DirtyUnicorns.

the class MotionSensor method onProcess.

@Override
protected void onProcess() {
    OutputPort outPort = getConnectedOutputPort("values");
    FrameValues outFrame = outPort.fetchAvailableFrame(null).asFrameValues();
    synchronized (mValues) {
        outFrame.setValues(mValues);
    }
    outFrame.setTimestamp(System.currentTimeMillis() * 1000000L);
    outPort.pushFrame(outFrame);
}
Also used : OutputPort(androidx.media.filterfw.OutputPort) FrameValues(androidx.media.filterfw.FrameValues)

Example 5 with FrameValues

use of androidx.media.filterfw.FrameValues in project android_frameworks_base by ResurrectionRemix.

the class FaceSquareFilterTest method testFaceSquareFilter.

public void testFaceSquareFilter() throws Exception {
    final int INPUT_WIDTH = 1536;
    final int INPUT_HEIGHT = 2048;
    FrameImage2D image = createFrame(FrameType.image2D(FrameType.ELEMENT_RGBA8888, FrameType.READ_CPU), new int[] { INPUT_WIDTH, INPUT_HEIGHT }).asFrameImage2D();
    FrameValues facesFrame = createFrame(FrameType.array(Camera.Face.class), new int[] { 1, 1 }).asFrameValues();
    Bitmap bitmap = BitmapFactory.decodeStream(assetMgr.open("XZZ019.jpg"));
    image.setBitmap(bitmap);
    injectInputFrame("image", image);
    Face face = new Face();
    Rect faceRect = new Rect();
    // These are the values for image 141 with 1 face
    faceRect.set(-533, -453, 369, 224);
    face.rect = faceRect;
    Face[] faces = new Face[1];
    faces[0] = face;
    facesFrame.setValue(faces);
    injectInputFrame("faces", facesFrame);
    process();
    // ensure the output image has the rectangle in the right place
    FrameImage2D outputImage = getOutputFrame("image").asFrameImage2D();
    int[] pixels = new int[bitmap.getByteCount()];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    final int FACE_X_RANGE = 2000;
    final int WIDTH_OFFSET = 1000;
    final int HEIGHT_OFFSET = 1000;
    int top = (faceRect.top + HEIGHT_OFFSET) * bitmap.getHeight() / FACE_X_RANGE;
    int bottom = (faceRect.bottom + HEIGHT_OFFSET) * bitmap.getHeight() / FACE_X_RANGE;
    int left = (faceRect.left + WIDTH_OFFSET) * bitmap.getWidth() / FACE_X_RANGE;
    int right = (faceRect.right + WIDTH_OFFSET) * bitmap.getWidth() / FACE_X_RANGE;
    if (top < 0) {
        top = 0;
    } else if (top > bitmap.getHeight()) {
        top = bitmap.getHeight();
    }
    if (left < 0) {
        left = 0;
    } else if (left > bitmap.getWidth()) {
        left = bitmap.getWidth();
    }
    if (bottom > bitmap.getHeight()) {
        bottom = bitmap.getHeight();
    } else if (bottom < 0) {
        bottom = 0;
    }
    if (right > bitmap.getWidth()) {
        right = bitmap.getWidth();
    } else if (right < 0) {
        right = 0;
    }
    for (int j = 0; j < (bottom - top); j++) {
        // Left edge
        if (left > 0 && top > 0) {
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + left) + ImageConstants.RED_OFFSET] = (byte) ImageConstants.MAX_BYTE;
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + left) + ImageConstants.GREEN_OFFSET] = (byte) ImageConstants.MAX_BYTE;
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + left) + ImageConstants.BLUE_OFFSET] = (byte) ImageConstants.MAX_BYTE;
        }
        // Right edge
        if (right > 0 && top > 0) {
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + right) + ImageConstants.RED_OFFSET] = (byte) ImageConstants.MAX_BYTE;
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + right) + ImageConstants.GREEN_OFFSET] = (byte) ImageConstants.MAX_BYTE;
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + right) + ImageConstants.BLUE_OFFSET] = (byte) ImageConstants.MAX_BYTE;
        }
    }
    for (int k = 0; k < (right - left); k++) {
        // Top edge
        if (top < bitmap.getHeight()) {
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * top + left + k) + ImageConstants.RED_OFFSET] = (byte) ImageConstants.MAX_BYTE;
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * top + left + k) + ImageConstants.GREEN_OFFSET] = (byte) ImageConstants.MAX_BYTE;
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * top + left + k) + ImageConstants.BLUE_OFFSET] = (byte) ImageConstants.MAX_BYTE;
        }
        // Bottom edge
        if (bottom < bitmap.getHeight()) {
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * bottom + left + k) + ImageConstants.RED_OFFSET] = (byte) ImageConstants.MAX_BYTE;
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * bottom + left + k) + ImageConstants.GREEN_OFFSET] = (byte) ImageConstants.MAX_BYTE;
            pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * bottom + left + k) + ImageConstants.BLUE_OFFSET] = (byte) ImageConstants.MAX_BYTE;
        }
    }
    Bitmap outputBitmap = outputImage.toBitmap();
    int[] outputPixels = new int[outputBitmap.getByteCount()];
    outputBitmap.getPixels(outputPixels, 0, outputBitmap.getWidth(), 0, 0, outputBitmap.getWidth(), outputBitmap.getHeight());
    int equalCount = 0;
    for (int i = 0; i < outputBitmap.getByteCount(); i++) {
        if (pixels[i] == outputPixels[i])
            equalCount++;
    }
    if (equalCount + (0.05f * outputBitmap.getByteCount()) < outputBitmap.getByteCount()) {
        // Assertion will fail if condition is true
        assertEquals(equalCount, outputBitmap.getByteCount());
    }
}
Also used : Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) FrameValues(androidx.media.filterfw.FrameValues) FrameImage2D(androidx.media.filterfw.FrameImage2D) Camera(android.hardware.Camera) Face(android.hardware.Camera.Face)

Aggregations

FrameValues (androidx.media.filterfw.FrameValues)16 OutputPort (androidx.media.filterfw.OutputPort)12 Face (android.hardware.Camera.Face)8 FrameImage2D (androidx.media.filterfw.FrameImage2D)8 Bitmap (android.graphics.Bitmap)4 Rect (android.graphics.Rect)4 Camera (android.hardware.Camera)4 FrameValue (androidx.media.filterfw.FrameValue)4 ByteBuffer (java.nio.ByteBuffer)4