[Tensorflow] TensorFlow Lite Tutorial
一个自定义demo
一、学习资料
Ref: Tensorflow lite 系列视频集,学习TensorFlow Lite的部署实践。[有点过时]
Official sample: TensorFlow Lite example apps
Object Detection: android/iso --> https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection
二、代码分析

tflite定义了分类器的接口和具体的一种实现。
@Override public List<Recognition> recognizeImage(final Bitmap bitmap)
DetectorActivity.java继承了CameraActivity.java,在其中调用 recognizeImage()。
@Override protected void processImage() {
++timestamp; final long currTimestamp = timestamp; trackingOverlay.postInvalidate(); // No mutex needed as this method is not reentrant. if (computingDetection) { readyForNextImage(); return; } computingDetection = true; LOGGER.i("Preparing image " + currTimestamp + " for detection in bg thread."); rgbFrameBitmap.setPixels(getRgbBytes(), 0, previewWidth, 0, 0, previewWidth, previewHeight); readyForNextImage(); final Canvas canvas = new Canvas(croppedBitmap); canvas.drawBitmap(rgbFrameBitmap, frameToCropTransform, null); // For examining the actual TF input. if (SAVE_PREVIEW_BITMAP) { ImageUtils.saveBitmap(croppedBitmap); } runInBackground( new Runnable() { @Override public void run() { LOGGER.i("Running detection on image " + currTimestamp); final long startTime = SystemClock.uptimeMillis(); /** * predict! */ final List<Classifier.Recognition> results = detector.recognizeImage(croppedBitmap); lastProcessingTimeMs = SystemClock.uptimeMillis() - startTime;
//-------------------------------------------------- cropCopyBitmap = Bitmap.createBitmap(croppedBitmap); final Canvas canvas = new Canvas(cropCopyBitmap); final Paint paint = new Paint(); paint.setColor(Color.RED); paint.setStyle(Style.STROKE); paint.setStrokeWidth(2.0f);
//-------------------------------------------------- float minimumConfidence = MINIMUM_CONFIDENCE_TF_OD_API; switch (MODE) { case TF_OD_API: minimumConfidence = MINIMUM_CONFIDENCE_TF_OD_API; break; } final List<Classifier.Recognition> mappedRecognitions = new LinkedList<Classifier.Recognition>(); for (final Classifier.Recognition result : results) { final RectF location = result.getLocation(); if (location != null && result.getConfidence() >= minimumConfidence) {
canvas.drawRect(location, paint); cropToFrameTransform.mapRect(location); result.setLocation(location); mappedRecognitions.add(result); } }
////////////////////////////////////////////////////////
// implement, call jni opencv
////////////////////////////////////////////////////////
tracker.trackResults(mappedRecognitions, currTimestamp); trackingOverlay.postInvalidate(); computingDetection = false; runOnUiThread( new Runnable() { @Override public void run() { showFrameInfo(previewWidth + "x" + previewHeight); showCropInfo(cropCopyBitmap.getWidth() + "x" + cropCopyBitmap.getHeight()); showInference(lastProcessingTimeMs + "ms"); } }); } }); }
模型转换
一、识别模型
在assets文件夹中。

/* implement */

浙公网安备 33010602011771号