'org.pytorch:pytorch_android:1.7.0'的
TensorImageUtils.imageYUV420CenterCropToFloat32Tensor()
是真的天坑,进来的图像RGB是扭曲的(目前),希望 PyTorch Mobile 以后会改。 。
用 PyTorch Mobile 最后要生成图片之前必须要用
TensorImageUtils.bitmapToFloat32Tensor()
至于读进来yuv格式图片的同学们要自己想办法yuv转bitmap辽
出来 FloatArray 用下面的函数转 Bitmap 就好了(改自phillies ),输出张量格式NCHW:
private fun floatArrayToBitmap(floatArray: FloatArray, width: Int, height: Int) : Bitmap {
// Create empty bitmap in RGBA format
val bmp: Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val pixels = IntArray(width * height * 4)
// mapping smallest value to 0 and largest value to 255
val maxValue = floatArray.max() ?: 1.0f
val minValue = floatArray.min() ?: -1.0f
val delta = maxValue-minValue
// Define if float min..max will be mapped to 0..255 or 255..0
val conversion = { v: Float -> ((v-minValue)/delta*255.0f).roundToInt()}
// copy each value from float array to RGB channels and set alpha channel
for (i in 0 until width * height) {
val r = conversion(floatArray[i])
val g = conversion(floatArray[i+width*height])
val b = conversion(floatArray[i+2*width*height])
pixels[i] = rgb(r, g, b)
}
bmp.setPixels(pixels, 0, width, 0, 0, width, height)
return bmp
}