/** * Creates a new frame based on the original frame, with additional width on the right to * increase the size to avoid the bug in the underlying face detector. */ private Frame padFrameRight(Frame originalFrame, int newWidth) { Frame.Metadata metadata = originalFrame.getMetadata(); int width = metadata.getWidth(); int height = metadata.getHeight(); Log.i(TAG, "Padded image from: " + width + "x" + height + " to " + newWidth + "x" + height); ByteBuffer origBuffer = originalFrame.getGrayscaleImageData(); int origOffset = origBuffer.arrayOffset(); byte[] origBytes = origBuffer.array(); // This can be changed to just .allocate in the future, when Frame supports non-direct // byte buffers. ByteBuffer paddedBuffer = ByteBuffer.allocateDirect(newWidth * height); int paddedOffset = paddedBuffer.arrayOffset(); byte[] paddedBytes = paddedBuffer.array(); Arrays.fill(paddedBytes, (byte) 0); for (int y = 0; y < height; ++y) { int origStride = origOffset + y * width; int paddedStride = paddedOffset + y * newWidth; System.arraycopy(origBytes, origStride, paddedBytes, paddedStride, width); } return new Frame.Builder() .setImageData(paddedBuffer, newWidth, height, ImageFormat.NV21) .setId(metadata.getId()) .setRotation(metadata.getRotation()) .setTimestampMillis(metadata.getTimestampMillis()) .build(); }
.setRotation(metadata.getRotation()) .setTimestampMillis(metadata.getTimestampMillis()) .build();
Frame frame = new Frame.Builder().setBitmap(bitmap).build(); SparseArray<Face> faces = safeDetector.detect(frame);
.setTimestampMillis(mPendingTimeMillis) .setRotation(mRotation) .build();
.setTimestampMillis(mPendingTimeMillis) .setRotation(mRotation) .build();
.setTimestampMillis(mPendingTimeMillis) .setRotation(getDetectorOrientation(mSensorOrientation)) .build();
.setTimestampMillis(mPendingTimeMillis) .setRotation(mRotation) .build();
.setTimestampMillis(mPendingTimeMillis) .setRotation(mRotation) .build();
.setTimestampMillis(mPendingTimeMillis) .setRotation(mRotation) .build();
.setTimestampMillis(mPendingTimeMillis) .setRotation(mRotation) .build();
Frame frame = new Frame.Builder().setBitmap(bitmap).build(); SparseArray<TextBlock> origTextBlocks = textRecognizer.detect(frame); List<TextBlock> textBlocks = new ArrayList<>();
/** * Calculates a point (focus point) in the bitmap, around which cropping needs to be performed. * * @param bitmap Bitmap in which faces are to be detected. * @param centerOfAllFaces To store the center point. */ private void detectFace(Bitmap bitmap, PointF centerOfAllFaces) { FaceDetector faceDetector = PicassoFaceDetector.getFaceDetector(); if (!faceDetector.isOperational()) { centerOfAllFaces.set(bitmap.getWidth() / 2, bitmap.getHeight() / 2); // center crop return; } Frame frame = new Frame.Builder().setBitmap(bitmap).build(); SparseArray<Face> faces = faceDetector.detect(frame); final int totalFaces = faces.size(); if (totalFaces > 0) { float sumX = 0f; float sumY = 0f; for (int i = 0; i < totalFaces; i++) { PointF faceCenter = new PointF(); getFaceCenter(faces.get(faces.keyAt(i)), faceCenter); sumX = sumX + faceCenter.x; sumY = sumY + faceCenter.y; } centerOfAllFaces.set(sumX / totalFaces, sumY / totalFaces); return; } centerOfAllFaces.set(bitmap.getWidth() / 2, bitmap.getHeight() / 2); // center crop }
/** * Calculates a point (focus point) in the bitmap, around which cropping needs to be performed. * * @param bitmap Bitmap in which faces are to be detected. * @param centerOfAllFaces To store the center point. */ private void detectFace(Bitmap bitmap, PointF centerOfAllFaces) { FaceDetector faceDetector = GlideFaceDetector.getFaceDetector(); if (!faceDetector.isOperational()) { centerOfAllFaces.set(bitmap.getWidth() / 2, bitmap.getHeight() / 2); // center crop return; } Frame frame = new Frame.Builder().setBitmap(bitmap).build(); SparseArray<Face> faces = faceDetector.detect(frame); final int totalFaces = faces.size(); if (totalFaces > 0) { float sumX = 0f; float sumY = 0f; for (int i = 0; i < totalFaces; i++) { PointF faceCenter = new PointF(); getFaceCenter(faces.get(faces.keyAt(i)), faceCenter); sumX = sumX + faceCenter.x; sumY = sumY + faceCenter.y; } centerOfAllFaces.set(sumX / totalFaces, sumY / totalFaces); return; } centerOfAllFaces.set(bitmap.getWidth() / 2, bitmap.getHeight() / 2); // center crop }
public void setBitmap( Bitmap bitmap ) { mBitmap = bitmap; FaceDetector detector = new FaceDetector.Builder( getContext() ) .setTrackingEnabled(true) .setLandmarkType(FaceDetector.ALL_LANDMARKS) .setMode(FaceDetector.ACCURATE_MODE) .build(); if (!detector.isOperational()) { //Handle contingency } else { Frame frame = new Frame.Builder().setBitmap(bitmap).build(); mFaces = detector.detect(frame); detector.release(); } logFaceData(); invalidate(); }
.setTimestampMillis(mPendingTimeMillis) .setRotation(mRotation) .build();
.setTimestampMillis(mPendingTimeMillis) .setRotation(rotation) .build();
.setTimestampMillis(mPendingTimeMillis) .setRotation(mRotation) .build();