Tensorflow Android demo 源码分析

1.TensorFlowMultiBoxDetector中部分代码如下

Bitmap转成矩阵数据

@Override
public List<Recognition> recognizeImage(final Bitmap bitmap) {
  // Log this method so that it can be analyzed with systrace.
  Trace.beginSection("recognizeImage");
  Trace.beginSection("preprocessBitmap");
  // Preprocess the image data from 0-255 int to normalized float based
  // on the provided parameters.
  bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
  for (int i = 0; i < intValues.length; ++i) {
    floatValues[i * 3 + 0] = (((intValues[i] >> 16) & 0xFF) - imageMean) / imageStd;
    floatValues[i * 3 + 1] = (((intValues[i] >> 8) & 0xFF) - imageMean) / imageStd;
    floatValues[i * 3 + 2] = ((intValues[i] & 0xFF) - imageMean) / imageStd;
  }
  Trace.endSection(); // preprocessBitmap
  // Copy the input data into TensorFlow.
  Trace.beginSection("feed");
  inferenceInterface.feed(inputName, floatValues, 1, inputSize, inputSize, 3);

getPixels()函数把一张图片,从指定的偏移位置(offset),指定的位置(x,y)截取指定的宽高(width,height ),把所得图像的每个像素颜色转为int值,存入pixels。

2.实现自己的Android模型

1.定义TensorFlowInferenceInterface 接口,加载xx.pb模型

2.inferenceInterface.feed(INPUT_NODE, inputs, WIDTH, HEIGHT);加载数据

3.inferenceInterface.run(outputNames);运行

4.inferenceInterface.fetch(OUTPUT_NODE, outputs);返回数据

关键代码如下:

private TensorFlowInferenceInterface inferenceInterface;

    ministmodel(AssetManager assetManager) {
        //接口定义
        inferenceInterface = new TensorFlowInferenceInterface(assetManager,MODEL_FILE);
    }
    public float[] getAddResult(float[]inputs) {

        inferenceInterface.feed(INPUT_NODE, inputs, WIDTH, HEIGHT);
        String[] outputNames = new String[] {OUTPUT_NODE};
        inferenceInterface.run(outputNames);
        inferenceInterface.fetch(OUTPUT_NODE, outputs);
        return outputs;

    }



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AICVer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值