【发布时间】:2018-07-12 06:31:19
【问题描述】:
我使用 Tensorflow 构建了一个图像分类器,我使用 Android Tensorflow 库在 Android 上运行该分类器。我的问题是,在 Android 上对图像进行分类时,预测的类完全关闭。但是当使用 Python 对图像进行分类时,使用相同的模型预测的类是正确的。
以下方法是我如何将位图转换为 RGB 像素值数组。(我取自 sample-tensorflow-imageclassifier 和 here)。
public static float[] getPixels(Bitmap bitmap) {
final int IMAGE_SIZE = 168;
int[] intValues = new int[IMAGE_SIZE * IMAGE_SIZE];
float[] floatValues = new float[IMAGE_SIZE * IMAGE_SIZE * 3];
if (bitmap.getWidth() != IMAGE_SIZE || bitmap.getHeight() != IMAGE_SIZE) {
// rescale the bitmap if needed
bitmap = ThumbnailUtils.extractThumbnail(bitmap, IMAGE_SIZE, IMAGE_SIZE);
}
bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for (int i = 0; i < intValues.length; ++i) {
final int val = intValues[i];
// bitwise shifting - without our image is shaped [1, 168, 168, 1] but we need [1, 168, 168, 3]
floatValues[i * 3] = Color.red(val);
floatValues[i * 3 + 1] = Color.green(val);
floatValues[i * 3 + 2] = Color.blue(val);
}
return floatValues;
}
我尝试将从 getPixels(bitmap:Bitmap) 接收到的浮点像素数组转换回位图,并注意到颜色有所不同,所以我猜这是问题所在?有没有办法在不丢失颜色信息的情况下转换像素?
附件是原图和我应用上述功能后转换回来的图像。
Image converted with above method
任何帮助将不胜感激。
【问题讨论】:
标签: android tensorflow