Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java代码将图像转换为数组?_Java_Python_Android_Arrays_Image - Fatal编程技术网

如何使用Java代码将图像转换为数组?

如何使用Java代码将图像转换为数组?,java,python,android,arrays,image,Java,Python,Android,Arrays,Image,我试图使用Tensorflow库在Android上建立一个MLKit检测项目,我使用Tensorflow lite在输出上得到了错误的值(与冻结模型推断不同的值) 我怀疑问题出在输入(图像)上,所以我想比较两张相同的图像 为了完成这项任务,我在python上使用了PIL image和numpy库来获取字节数组,并将可绘制图像转换为位图,从位图转换为字节数组 我不知道函数np.asarray是否应给出与以下两个函数相同的值: Java代码: private float[]bitmapToInput

我试图使用Tensorflow库在Android上建立一个MLKit检测项目,我使用Tensorflow lite在输出上得到了错误的值(与冻结模型推断不同的值)

我怀疑问题出在输入(图像)上,所以我想比较两张相同的图像

为了完成这项任务,我在python上使用了PIL image和numpy库来获取字节数组,并将可绘制图像转换为位图,从位图转换为字节数组

我不知道函数
np.asarray
是否应给出与以下两个函数相同的值:

Java代码:

private float[]bitmapToInputArray(){
//[启动mlkit_位图_输入]
位图位图=getYourInputImage();
int batchNum=0;
浮动[]输入=新浮动[1][112][112][3];
对于(int x=0;x<112;x++){
对于(int y=0;y<112;y++){
int pixel=bitmap.getPixel(x,y);
//将通道值规格化为[-1.0,1.0]。此要求因时间而异
//例如,一些模型可能需要标准化值
//改为范围[0.0,1.0]。
输入[batchNum][x][y][0]=(颜色.红色(像素))/255.0f;
输入[batchNum][x][y][1]=(颜色.绿色(像素))/255.0f;
输入[batchNum][x][y][2]=(颜色.蓝色(像素))/255.0f;
Log.i(“输入”、“输入”+输入[batchNum][x][y][0]);
Log.i(“输入”、“输入”+输入[batchNum][x][y][1]);
}
}
//[结束mlkit_位图_输入]
返回输入;
}
公共字节[]convertBitmapToByteArray(位图){
ByteArrayOutputStream=null;
试一试{
stream=newbytearrayoutputstream();
压缩(bitmap.CompressFormat.JPEG,100,流);
返回stream.toByteArray();
}最后{
if(流!=null){
试一试{
stream.close();
}捕获(IOE异常){
Log.e(ThemedSpinnerAdapter.Helper.class.getSimpleName(),“ByteArrayOutputStream未关闭”);
}
}
}
}
私有位图getYourInputImage(){
//这种方法只是作秀而已
BitmapDrawable drawable=(BitmapDrawable)image2.getDrawable();
位图Bitmap=drawable.getBitmap();
位图bitmapp=Bitmap.createScaledBitmap(位图,112112,true);
位图bitmap2=bitmapp.copy(Bitmap.Config.ARGB_8888,true);
返回位图2;
}
byte[]bytes=convertBitmapToByteArray(bitmap1);
Log.i(“字节”,“数组+字符串(字节));
浮动[]inp=新浮动[1][112][112][3];
inp=bitmapToInputArray();
Log.i(“字节2”,“”+Arrays.toString(inp[0]);
Python脚本:


img=Image.open(“irisdata-300VW_数据集_2015_12_14-017-000880.jpg”)
img.load()
img=img.resize((112112),PIL.Image.ANTIALIAS)
打印(str(图像到字节数组(img)))
#标准化为[0,1]
data=np.asarray(img,dtype=“float32”)
打印(数据)
  • java代码的输出:
2019-08-20 19:01:13.589 1513-1513/com.example.irisdetection I/字节:[-1, -40, -1, -32, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, -1, -37, 0, 67, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -37, 0, 67, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -64, 0, 17, 8, 0, 112, 0, 112, 3, 1, 34, 0, 2, 17, 1, 3, 17, 1, -1, -60, 0, 29, 0, 0, 2, 2, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 8, 6, 9, 4, 5, 10, 3, 2, 1, -1, -60, 0, 69, 16, 0, 1, 3, 3, 1, 5, 5, 4, 7, 5, 6, 5, 5, 0, 0, 0, 3, 1, 2, 4, 5, 6, 17, 33, 0, 7, 18, 19, 49, 8, 34, 65, 81, 97, 20, 35, 113, -127, 9, 21, 50, 51, -111, -95, -79, 36, 66, -63, -47, -16, 22, 23, 67, 82, 83, -31, 37, 52, 98, 114, -15, 99, 115, -126, -125, -94, -1, -60, 0, 28, 1, 0, 2, 2, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 4, 5, 2, 3, 8, 0, 1, -1, -60, 0, 55, 17, 0, 0, 4, 3, 6, 3, 8, 1, 3, 4, 3, 1, 0, 0, 0, 0, 1, 2, 3, 4, 5, 17, 33, 0, 6, 49, 65, 81, 97, 18, 19, 113, 7, 20, 35, -127, -111, -95, -79, -16, -47, 34, -63, -31, 21, 36, 67, -15, 8, 22, 50, 51, -1, -38, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0, -28, -105, 115, 116, 26, 45, 98, -97, 117, 74, -71, -31, -57, -88, 40, -73, 93, 88, -83, 83, 89, 39, 41, -20, -43, 40, -7, 28, 115, 12, -100, -52, -116, -88, 78, 53, -45, 84, -49, -53, 102, 114, 124, 34, -52, -20, -3, -72, 91, -115, 6, -58, -120, -76, -5, -102, -126, 71, 36, -107, 43, -97, -11, 61, 93, -17, 78, 96, 63, 115, -35, -52, 8, -3, -30, -89, -40, -26, 117, -40, 29, -70, 107, 106, 21, 112, 86, -36, 7, 73, 36, 120, -9, 30, -19, -18, -48, -56, 86, -79, -88, -31, -106, -105, 36, -53, -53, 103, 30, -92, 121, 24, -120, 76, 47, -7, -15, -99, 54, 103, 44, -88, -107, 42, -57, 98, 107, 90, 83, -98, 105, 20, -21, 43, 126, 53, -117, 106, 58, -71, -116, 84, -114, 58, -11, 22, 53, 77, -84, 119, 47, -34, -14, -52, 72, 102, 127, -68, -57, -61, 77, -78, 126, -35, 18, -79, 112, 9, 38, 41, 40, 50, 3, 104, 63, -7, -64, 66, 97, -100, -66, -115, -115, 59, 43, -119, 21, 43, -50, -111, 79, 62, 82, -55, -72, 64, 70, -76, -92, -6, 5, 119, -54, -42, 15, -40, -126, -36, -81, 86, 109, 8, 55, 69, -62, 114, 31, -39, -30, 125, 65, 65, 9, 120, -47, -111, -24, -48, 36, -55, 56, -58, 49, -12, 103, 52, -14, 77, -24, -68, 12, -41, 24, -38, -49, 45, -6, 122, -80, -104, -106, -88, 32, -76, 92, 72, -67, 51, -13, 95, 92, -2, 105, -90, 112, -85, -1, 0, 101, -21, 110, -105, 72, -35, 117, -93, 21, -22, -48, 56, 84, 56, 50, 9, -62, -33, -74, 105, 35, -25, -99, 126, 42, 71, -89, -126, -86, -2, 91, 77, 119, -59, -67, 56, 86, 101, 29, -50, -116, -41, 28, -83, 78, 1, 2, 59, 85, 78, 103, -82, -120, -120, -119, -16, 84, -8, -81, -82, -43, -119, 2, 73, -74, -32, 34, 124, -95, -91, 103, -120, -56