use of androidx.media.filterfw.FrameImage2D in project platform_frameworks_base by android.
the class ContrastRatioFilter method onProcess.
@Override
protected void onProcess() {
FrameImage2D inputImage = getConnectedInputPort("image").pullFrame().asFrameImage2D();
float contrastRatio;
ByteBuffer inputBuffer = inputImage.lockBytes(Frame.MODE_READ);
contrastRatio = contrastOperator(inputImage.getWidth(), inputImage.getHeight(), inputBuffer);
inputImage.unlock();
if (mLogVerbose)
Log.v(TAG, "contrastRatio: " + contrastRatio);
OutputPort contrastToGoodnessPort = getConnectedOutputPort("contrastRatingToGoodness");
FrameValue contrastOutFrame2 = contrastToGoodnessPort.fetchAvailableFrame(null).asFrameValue();
contrastOutFrame2.setValue(contrastRatio);
contrastToGoodnessPort.pushFrame(contrastOutFrame2);
}
use of androidx.media.filterfw.FrameImage2D 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);
}
use of androidx.media.filterfw.FrameImage2D in project platform_frameworks_base by android.
the class ImageGoodnessFilter method onProcess.
/**
* @see androidx.media.filterfw.Filter#onProcess()
*/
@Override
protected void onProcess() {
FrameValue sharpnessFrameValue = getConnectedInputPort("sharpness").pullFrame().asFrameValue();
float sharpness = ((Float) sharpnessFrameValue.getValue()).floatValue();
FrameValue overExposureFrameValue = getConnectedInputPort("overExposure").pullFrame().asFrameValue();
float overExposure = ((Float) overExposureFrameValue.getValue()).floatValue();
FrameValue underExposureFrameValue = getConnectedInputPort("underExposure").pullFrame().asFrameValue();
float underExposure = ((Float) underExposureFrameValue.getValue()).floatValue();
FrameValue colorfulnessFrameValue = getConnectedInputPort("colorfulness").pullFrame().asFrameValue();
float colorfulness = ((Float) colorfulnessFrameValue.getValue()).floatValue();
FrameValue contrastRatingFrameValue = getConnectedInputPort("contrastRating").pullFrame().asFrameValue();
float contrastRating = ((Float) contrastRatingFrameValue.getValue()).floatValue();
FrameValue brightnessFrameValue = getConnectedInputPort("brightness").pullFrame().asFrameValue();
float brightness = ((Float) brightnessFrameValue.getValue()).floatValue();
FrameValue motionValuesFrameValue = getConnectedInputPort("motionValues").pullFrame().asFrameValue();
float[] motionValues = (float[]) motionValuesFrameValue.getValue();
float vectorAccel = (float) Math.sqrt(Math.pow(motionValues[0], 2) + Math.pow(motionValues[1], 2) + Math.pow(motionValues[2], 2));
String outStr;
FrameValue capturingFrameValue = getConnectedInputPort("capturing").pullFrame().asFrameValue();
boolean capturing = (Boolean) capturingFrameValue.getValue();
FrameImage2D inputImage = getConnectedInputPort("image").pullFrame().asFrameImage2D();
// TODO: get rid of magic numbers
float score = 0.0f;
score = computePictureScore(vectorAccel, sharpness, underExposure, overExposure, contrastRating, colorfulness, brightness);
if (scoreMean == 0)
scoreMean = score;
else
scoreMean = scoreMean * (1 - DECAY) + score * DECAY;
if (motionMean == 0)
motionMean = vectorAccel;
else
motionMean = motionMean * (1 - DECAY) + vectorAccel * DECAY;
float classifierScore = classifierComputeScore(vectorAccel, sharpness, underExposure, colorfulness, contrastRating, score);
// Log.v(TAG, "ClassifierScore:: " + classifierScore);
final float GREAT_SCORE = 3.5f;
final float GOOD_SCORE = 2.5f;
final float OK_SCORE = 1.5f;
final float BAD_SCORE = 0.5f;
if (score >= GREAT_SCORE) {
outStr = GREAT;
} else if (score >= GOOD_SCORE) {
outStr = GOOD;
} else if (score >= OK_SCORE) {
outStr = OK;
} else if (score >= BAD_SCORE) {
outStr = BAD;
} else {
outStr = AWFUL;
}
if (capturing) {
if (outStr.equals(GREAT)) {
// take a picture
Bitmap bitmap = inputImage.toBitmap();
new AsyncOperation().execute(bitmap);
final float RESET_FEATURES = 0.01f;
sharpnessMean = RESET_FEATURES;
underExposureMean = RESET_FEATURES;
overExposureMean = RESET_FEATURES;
contrastMean = RESET_FEATURES;
colorfulnessMean = RESET_FEATURES;
brightnessMean = RESET_FEATURES;
}
}
OutputPort outPort = getConnectedOutputPort("goodOrBadPic");
FrameValue stringFrame = outPort.fetchAvailableFrame(null).asFrameValue();
stringFrame.setValue(outStr);
outPort.pushFrame(stringFrame);
OutputPort scoreOutPort = getConnectedOutputPort("score");
FrameValue scoreFrame = scoreOutPort.fetchAvailableFrame(null).asFrameValue();
scoreFrame.setValue(score);
scoreOutPort.pushFrame(scoreFrame);
}
use of androidx.media.filterfw.FrameImage2D in project platform_frameworks_base by android.
the class ToGrayValuesFilter method onProcess.
@Override
protected void onProcess() {
OutputPort outPort = getConnectedOutputPort("image");
FrameImage2D inputImage = getConnectedInputPort("image").pullFrame().asFrameImage2D();
int[] dim = inputImage.getDimensions();
FrameBuffer2D outputFrame;
ByteBuffer grayBuffer;
if (isOpenGLSupported()) {
// crop out the portion of inputImage that will be used to generate outputFrame.
int modular = dim[0] % 4;
int[] outDim = new int[] { dim[0] - modular, dim[1] };
outputFrame = outPort.fetchAvailableFrame(outDim).asFrameBuffer2D();
grayBuffer = outputFrame.lockBytes(Frame.MODE_WRITE);
int[] targetDims = new int[] { outDim[0] / 4, outDim[1] };
FrameImage2D targetFrame = Frame.create(mImageInType, targetDims).asFrameImage2D();
mShader.setSourceQuad(Quad.fromRect(0f, 0f, ((float) outDim[0]) / dim[0], 1f));
mShader.setUniformValue("pix_stride", 1f / outDim[0]);
mShader.process(inputImage, targetFrame);
RenderTarget grayTarget = targetFrame.lockRenderTarget();
grayTarget.readPixelData(grayBuffer, targetDims[0], targetDims[1]);
targetFrame.unlock();
targetFrame.release();
} else {
outputFrame = outPort.fetchAvailableFrame(dim).asFrameBuffer2D();
grayBuffer = outputFrame.lockBytes(Frame.MODE_WRITE);
ByteBuffer inputBuffer = inputImage.lockBytes(Frame.MODE_READ);
if (!toGrayValues(inputBuffer, grayBuffer)) {
throw new RuntimeException("Native implementation encountered an error during processing!");
}
inputImage.unlock();
}
outputFrame.unlock();
outPort.pushFrame(outputFrame);
}
use of androidx.media.filterfw.FrameImage2D in project platform_frameworks_base by android.
the class CropFilter method onProcess.
@Override
protected void onProcess() {
OutputPort outPort = getConnectedOutputPort("image");
// Pull input frame
FrameImage2D inputImage = getConnectedInputPort("image").pullFrame().asFrameImage2D();
int[] inDims = inputImage.getDimensions();
int[] croppedDims = { (int) Math.ceil(mCropRect.xEdge().length() * inDims[0]), (int) Math.ceil(mCropRect.yEdge().length() * inDims[1]) };
int[] outDims = { getOutputWidth(croppedDims[0], croppedDims[1]), getOutputHeight(croppedDims[0], croppedDims[1]) };
FrameImage2D outputImage = outPort.fetchAvailableFrame(outDims).asFrameImage2D();
if (isOpenGLSupported()) {
FrameImage2D sourceFrame;
Quad sourceQuad = null;
boolean scaleDown = (outDims[0] < croppedDims[0]) || (outDims[1] < croppedDims[1]);
if (scaleDown && mUseMipmaps) {
mPow2Frame = TransformUtils.makeMipMappedFrame(mPow2Frame, croppedDims);
int[] extDims = mPow2Frame.getDimensions();
float targetWidth = croppedDims[0] / (float) extDims[0];
float targetHeight = croppedDims[1] / (float) extDims[1];
Quad targetQuad = Quad.fromRect(0f, 0f, targetWidth, targetHeight);
mShader.setSourceQuad(mCropRect);
mShader.setTargetQuad(targetQuad);
mShader.process(inputImage, mPow2Frame);
TransformUtils.generateMipMaps(mPow2Frame);
sourceFrame = mPow2Frame;
sourceQuad = targetQuad;
} else {
sourceFrame = inputImage;
sourceQuad = mCropRect;
}
mShader.setSourceQuad(sourceQuad);
mShader.setTargetRect(0f, 0f, 1f, 1f);
mShader.process(sourceFrame, outputImage);
} else {
// Convert quads to canvas coordinate space
Quad sourceQuad = mCropRect.scale2(inDims[0], inDims[1]);
Quad targetQuad = Quad.fromRect(0f, 0f, inDims[0], inDims[1]);
// Calculate transform for crop
Matrix transform = Quad.getTransform(sourceQuad, targetQuad);
transform.postScale(outDims[0] / (float) inDims[0], outDims[1] / (float) inDims[1]);
// Create target canvas
Bitmap.Config config = Bitmap.Config.ARGB_8888;
Bitmap cropped = Bitmap.createBitmap(outDims[0], outDims[1], config);
Canvas canvas = new Canvas(cropped);
// Draw source bitmap into target canvas
Paint paint = new Paint();
paint.setFilterBitmap(true);
Bitmap sourceBitmap = inputImage.toBitmap();
canvas.drawBitmap(sourceBitmap, transform, paint);
// Assign bitmap to output frame
outputImage.setBitmap(cropped);
}
outPort.pushFrame(outputImage);
}
Aggregations