【发布时间】:2021-07-28 15:39:12
【问题描述】:
这是我的代码:
imageAnalysis.setAnalyzer(ThreadManager.getInstance().cameraExecutor, new ImageAnalysis.Analyzer() {
@SuppressLint("UnsafeOptInUsageError")
@Override
public void analyze(@NonNull ImageProxy image) {
int rotationDegrees = image.getImageInfo().getRotationDegrees();
// insert your code here.
Log.d(TAG, "starting");
Bitmap bmp = Bitmap.createBitmap(270,480, Bitmap.Config.ARGB_8888);
conv.yuvToRgb(image.getImage(), bmp);
ByteBuffer buffer = ByteBuffer.allocate(3*270*480*4);
int pixelVals[] = new int[480*270*3];
bmp.getPixels(pixelVals, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());
int itr = 0;
for(int i=0;i<480;++i)
{
for(int j=0;j<270;++j)
{
int x = pixelVals[itr++];
float red = ((x >> 16) & 0xFF) / 256.f;
float green = ((x >> 8) & 0xFF) / 256.f;
float blue = (x & 0xFF) / 256.f;
buffer.putFloat(red);
buffer.putFloat(green);
buffer.putFloat(blue);
}
}
float[][] output = new float[1][1];
tflite.run(buffer, output);
float out = output[0][0];
ContextCompat.getMainExecutor(MainActivity.this).execute(new Runnable() {
@Override
public void run() {
scoretv.setText(out+"");
Log.d(TAG, "updating");
}
});
image.close();
}
});
问题是tflite.run() 总是返回相同的输出,不管输入是什么。我已经在我的电脑上使用 python 脚本测试了我的.tflite 模型,它可以正常工作。
我不确定buffer 是否在调用run() 时保存正确转换的图像,但使用调试器我验证它至少看起来像正确的图像数据,当然不应该每次都给出相同的结果。 (缓冲区数据改变,所以输出也应该改变)
此外,当检测到对象时,此模型应输出更接近 1.0 的浮点数,而在没有检测到对象时应输出更接近 0.0 的浮点数。如果图像在转换过程中损坏,模型应该输出低值(通过我的电脑上的测试确认)。
模型总是输出 0.7001277
编辑: 这是经过编辑的版本,原始问题现已过时。抱歉,我可能应该再等一会儿再问问题。
【问题讨论】:
-
为什么要用RGB_565?我问这是否是故意的,否则您可以轻松使用 TensorFlow Lite 支持库
-
不,事实上我不知道我应该使用什么位图选项,但这是唯一一个具有“RGB”的选项,而不是 ARGB 的 RGBA @Farmaker
标签: java android keras android-bitmap tensorflow-lite