![](/img/trans.png)
[英]Face Detection and extracting the faces using Bounding Box and creating a new Bitmap
[英]Android MLKit face detection not detecting faces when using Bitmap
我有一個 XR 應用程序,其中顯示屏顯示相機(后)饋送。 因此,捕獲屏幕與捕獲相機饋送幾乎相同......因此,我截取屏幕截圖(位圖),然后嘗試使用 Googles MLKit 檢測其中的面部。
我正在按照官方指南檢測人臉。
為此,我首先啟動我的面部檢測器:
FaceDetector detector;
public MyFaceDetector(){
FaceDetectorOptions realTimeOpts =
new FaceDetectorOptions.Builder()
.setContourMode(FaceDetectorOptions.CONTOUR_MODE_ALL)
.build();
detector = FaceDetection.getClient(realTimeOpts);
}
然后我有一個 function 傳入 bitmap。 我首先將 bitmap 轉換為字節數組。 我這樣做是因為InputImage.fromBitmap
非常慢,而 MLKit 實際上告訴我應該使用字節數組:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
Next I make a mutable copy of the Bitmap (so that I can draw onto it), and set up a Canvas object, along with a color that will be used when drawing on to the Bitmap:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, options);
Canvas canvas = new Canvas(bmp);
Paint p = new Paint();
p.setColor(Color.RED);
全部設置完成后,我使用字節數組創建一個InputImage
(由 FaceDetector 使用):
InputImage image = InputImage.fromByteArray(byteArray, bmp.getWidth(), bmp.getHeight(),0, InputImage.IMAGE_FORMAT_NV21);
注意圖像格式...有一個InputImage.IMAGE_FORMAT_BITMAP
,但使用它會引發 IllegalArgumentException。 無論如何,我接下來嘗試處理 Bitmap,檢測面部,用之前定義的顏色填充每個檢測到的面部,然后將 Bitmap 保存到磁盤:
Task<List<Face>> result = detector.process(image).addOnSuccessListener(
new OnSuccessListener<List<Face>>() {
@Override
public void onSuccess(List<Face> faces) {
Log.e("FACE DETECTION APP", "NUMBER OF FACES: " + faces.size());
Thread processor = new Thread(new Runnable() {
@Override
public void run() {
for (Face face : faces) {
Rect destinationRect = face.getBoundingBox();
canvas.drawRect(destinationRect, p);
canvas.save();
Log.e("FACE DETECTION APP", "WE GOT SOME FACCES!!!");
}
File file = new File(someFilePath);
try {
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
processor.start();
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Task failed with an exception
// ...
}
});
}
雖然此代碼運行(即無異常)並且 bitmap 已正確寫入磁盤,但從未檢測到任何面孔( faces.size() is always 0
)。 我試過旋轉圖像。 我試過改變 Bitmap 的質量。 我已經嘗試使用和不使用線程來處理任何檢測到的面孔。 我已經嘗試了我能想到的一切。
有人有想法么?
機器學習套件輸入圖像。 fromByteArray 僅支持 yv12 和 nv21 格式。 您需要將 bitmap 轉換為其中一種格式,以便 ML 套件管道進行處理。 此外,如果您擁有的原始圖像是 bitmap,您可能只需使用 InputImage.fromBitmap 來構造 InputImage。 它不應該比您當前的方法慢。
我有同樣的問題使用 ImageInput.fromMediaImage(..., ...)
override fun analyze(image: ImageProxy) {
val mediaImage: Image = image.image.takeIf { it != null } ?: run {
image.close()
return
}
val inputImage = InputImage.fromMediaImage(mediaImage, image.imageInfo.rotationDegrees)
// TODO: Your ML Code
}
在此處查看更多詳細信息https://developers.google.com/ml-kit/vision/image-labeling/android
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.