1 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊
所以我被困在這個(gè)問題上,因?yàn)槲艺陉P(guān)注ML vision docs上的谷歌文檔, 其中圖像在將其提供給分類器之前被轉(zhuǎn)換為浮點(diǎn)數(shù)組,它看起來像這樣:
val bitmap = Bitmap.createScaledBitmap(yourInputImage, 224, 224, true)
val batchNum = 0
val input = Array(1) { Array(224) { Array(224) { FloatArray(3) } } }
for (x in 0..223) {
for (y in 0..223) {
val pixel = bitmap.getPixel(x, y)
// Normalize channel values to [-1.0, 1.0]. This requirement varies by
// model. For example, some models might require values to be normalized
// to the range [0.0, 1.0] instead.
input[batchNum][x][y][0] = (Color.red(pixel) - 127) / 255.0f
input[batchNum][x][y][1] = (Color.green(pixel) - 127) / 255.0f
input[batchNum][x][y][2] = (Color.blue(pixel) - 127) / 255.0f
}
}
然后我一步一步分析,發(fā)現(xiàn)獲取像素的方式是錯(cuò)誤的,和python做這一切的方式完全不同。
然后我從這個(gè)來源找到了這種方法,我用我的方法改變了這個(gè)功能:
private fun convertBitmapToByteBuffer(bitmap: Bitmap): ByteBuffer {
val imgData = ByteBuffer.allocateDirect(4 * INPUT_SIZE * INPUT_SIZE * PIXEL_SIZE)
imgData.order(ByteOrder.nativeOrder())
val intValues = IntArray(INPUT_SIZE * INPUT_SIZE)
imgData.rewind()
bitmap.getPixels(intValues, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height)
// Convert the image to floating point.
var pixel = 0
for (i in 0 until INPUT_SIZE) {
for (j in 0 until INPUT_SIZE) {
val `val` = intValues[pixel++]
imgData.putFloat(((`val`.shr(16) and 0xFF) - IMAGE_MEAN)/IMAGE_STD)
imgData.putFloat(((`val`.shr(8) and 0xFF)- IMAGE_MEAN)/ IMAGE_STD)
imgData.putFloat(((`val` and 0xFF) - IMAGE_MEAN)/IMAGE_STD)
}
}
return imgData;
}
添加回答
舉報(bào)