Android QR Code Detection with TensorFlow Lite

Steps to Train QR Code Detection Model with TensorFlow Lite Model Maker

  1. Install TensorFlow Lite Model Maker:
pip install tflite-model-maker
git clone
cd tensorflow-lite
python -x -i ../images -r 0.1 -o ./
from tflite_model_maker.config import QuantizationConfig
from tflite_model_maker.config import ExportFormat
from tflite_model_maker import model_spec
from tflite_model_maker import object_detector

import tensorflow as tf
assert tf.__version__.startswith('2')

from absl import logging

spec = model_spec.get('efficientdet_lite0')
train_data = object_detector.DataLoader.from_pascal_voc(images_dir="train", annotations_dir="train", label_map={1: "QR_CODE"} )
validation_data = object_detector.DataLoader.from_pascal_voc(images_dir="test", annotations_dir="test", label_map={1: "QR_CODE"} )
model = object_detector.create(train_data, model_spec=spec, batch_size=8, train_whole_model=True, validation_data=validation_data)
  1. Drag the model.tflite file to assets folder.
  2. Change TF_OD_API_MODEL_FILE to model.tflite in
private static final String TF_OD_API_MODEL_FILE = "model.tflite";

QR Code Scanner with TensorFlow Lite

  1. Copy model.tflite and labelmap.txt to assets folder.
  2. Add the TensorFlow Lite library to build.gradle:
// Import the Task Vision Library dependency (NNAPI is included)
implementation 'org.tensorflow:tensorflow-lite-task-vision:0.3.0'
private TFLiteObjectDetectionAPIModel(Context context, String modelFilename) throws IOException {
modelBuffer = FileUtil.loadMappedFile(context, modelFilename);
optionsBuilder = ObjectDetectorOptions.builder();
objectDetector = ObjectDetector.createFromBufferAndOptions(modelBuffer,;
// Configuration values for the prepackaged QR Code model.
private static final int TF_OD_API_INPUT_SIZE = 416;
private static final boolean TF_OD_API_IS_QUANTIZED = true;
private static final String TF_OD_API_MODEL_FILE = "model.tflite";
private static final String TF_OD_API_LABELS_FILE = "labelmap.txt";
// Minimum detection confidence to track a detection.
private static final float MINIMUM_CONFIDENCE_TF_OD_API = 0.5f;
private Detector detector;
int cropSize = TF_OD_API_INPUT_SIZE;

private void initTFDetector() {
try {
detector =
} catch (final IOException e) {
Toast toast =
getApplicationContext(), "Detector could not be initialized", Toast.LENGTH_SHORT);;
imageProxy -> {
Bitmap bitmap = ImageUtils.getBitmap(imageProxy);
final List<Detector.Recognition> tfResults = detector.recognizeImage(bitmap);
if (tfResults.size() > 0) {
for (final Detector.Recognition tfResult : tfResults) {
final RectF location = tfResult.getLocation();
if (location != null && tfResult.getConfidence() >= MINIMUM_CONFIDENCE_TF_OD_API) {
overlay.add(new BarcodeGraphic(overlay, location, null, isPortrait));
TextResult[] results = null;
try {
PublicRuntimeSettings settings = reader.getRuntimeSettings();
settings.barcodeFormatIds = EnumBarcodeFormat.BF_QR_CODE;
settings.expectedBarcodesCount = tfResults.size();
} catch (BarcodeReaderException e) {

Source Code




Manager of Dynamsoft Open Source Projects | Tech Lover

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Configuring Single Sign-On for Dropbox Business with WSO2 Identity Server

Minimal APIs in Net6

Why I go to hackathons and why you should consider going

Top 10 Benefits of Continuous Integration & Continuous Delivery

Comprehensive Guide on Successful NetSuite ERP Integration

taking Smoky Quartz 925 Sterling Silver Brown Ring wholesales US 6789

Ultimate Gaming Performance Guide (Maximum FPS Optimizations)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Xiao Ling

Xiao Ling

Manager of Dynamsoft Open Source Projects | Tech Lover

More from Medium

Classifying categorical data in Weka via the API

Micro QR Code Scanning and Generating Tools

Adding Android Studio to your Applications Launcher and Favorites Bar in Ubuntu

Find the Bokeh Mode images using Huawei Camera Engine in Android (Kotlin)