【发布时间】:2017-07-03 02:17:05
【问题描述】:
当我使用 setBitmap 给 Frame.builder() 一个位图时,一切都按预期工作。人脸被检测到,微笑概率也有效。但是,出于某种原因,带有 NV21 格式 YUV 图像的 ByteBuffer 的 .setImageData 不起作用。它不会抛出任何错误,但没有使用 YuvImage 数据检测到人脸。
i420Frame 来自这里:https://media.twiliocdn.com/sdk/android/conversations/releases/0.8.1/docs/com/twilio/conversations/I420Frame.html
(我假设它与 Webrtc 的 i420Frame 非常相似,如果不完全相同的话)
这里是主要代码:
@Override
public void renderFrame(final I420Frame i420Frame) {
YuvImage yuvImage = i420ToYuvImage(i420Frame.yuvPlanes, i420Frame.yuvStrides, i420Frame.width, i420Frame.height);
// Set image data (YUV N21 format) -- NOT working. The commented bitmap line works.
Frame frame = new Frame.Builder().setImageData(ByteBuffer.wrap(yuvImage.getYuvData()), yuvImage.getWidth(), yuvImage.getHeight(), yuvImage.getYuvFormat()).build();
//Frame frame = new Frame.Builder().setBitmap(yuvImage).build();
// Detect faces
SparseArray<Face> faces = detector.detect(frame);
if (!detector.isOperational()) {
Log.e(TAG, "Detector is not operational!");
}
if (faces.size() > 0) {
Log.i("yuv", "Smiling %: " + faces.valueAt(0).getIsSmilingProbability());
}
i420Frame.release();
Log.i("yuv", "Faces detected: " + faces.size());
}
这里是用于 i420ToYuvImage 的函数(取自 Twilio 的快速入门指南)。 i420 到 YuvImage 的代码不是我编写的,但我相信它工作正常,因为我将输出的 yuvimage 转换为 jpeg,再将其转换为位图,并且谷歌移动视觉库能够检测位图中的人脸.但是进行所有这些转换会产生大量开销。所以我尝试使用 YuvImage 直接输入到上面的移动视觉库中。
private YuvImage i420ToYuvImage(ByteBuffer[] yuvPlanes, int[] yuvStrides, int width, int height) {
if (yuvStrides[0] != width) {
return fastI420ToYuvImage(yuvPlanes, yuvStrides, width, height);
}
if (yuvStrides[1] != width / 2) {
return fastI420ToYuvImage(yuvPlanes, yuvStrides, width, height);
}
if (yuvStrides[2] != width / 2) {
return fastI420ToYuvImage(yuvPlanes, yuvStrides, width, height);
}
byte[] bytes = new byte[yuvStrides[0] * height +
yuvStrides[1] * height / 2 +
yuvStrides[2] * height / 2];
ByteBuffer tmp = ByteBuffer.wrap(bytes, 0, width * height);
copyPlane(yuvPlanes[0], tmp);
byte[] tmpBytes = new byte[width / 2 * height / 2];
tmp = ByteBuffer.wrap(tmpBytes, 0, width / 2 * height / 2);
copyPlane(yuvPlanes[2], tmp);
for (int row = 0 ; row < height / 2 ; row++) {
for (int col = 0 ; col < width / 2 ; col++) {
bytes[width * height + row * width + col * 2]
= tmpBytes[row * width / 2 + col];
}
}
copyPlane(yuvPlanes[1], tmp);
for (int row = 0 ; row < height / 2 ; row++) {
for (int col = 0 ; col < width / 2 ; col++) {
bytes[width * height + row * width + col * 2 + 1] =
tmpBytes[row * width / 2 + col];
}
}
return new YuvImage(bytes, NV21, width, height, null);
}
private YuvImage fastI420ToYuvImage(ByteBuffer[] yuvPlanes,
int[] yuvStrides,
int width,
int height) {
byte[] bytes = new byte[width * height * 3 / 2];
int i = 0;
for (int row = 0 ; row < height ; row++) {
for (int col = 0 ; col < width ; col++) {
bytes[i++] = yuvPlanes[0].get(col + row * yuvStrides[0]);
}
}
for (int row = 0 ; row < height / 2 ; row++) {
for (int col = 0 ; col < width / 2; col++) {
bytes[i++] = yuvPlanes[2].get(col + row * yuvStrides[2]);
bytes[i++] = yuvPlanes[1].get(col + row * yuvStrides[1]);
}
}
return new YuvImage(bytes, NV21, width, height, null);
}
private void copyPlane(ByteBuffer src, ByteBuffer dst) {
src.position(0).limit(src.capacity());
dst.put(src);
dst.position(0).limit(dst.capacity());
}
【问题讨论】: