DEV Community

Cover image for Real-time sign language detection android application using TensorFlow lite
Deepak Raj
Deepak Raj

Posted on • Updated on • Originally published at

Real-time sign language detection android application using TensorFlow lite

In this article, we are going to convert the TensorFlow model to tflite model and will use it in a real-time Sign language detection app. we will not cover the training part in this article btw I used the TensorFlow object detection API for that. first, we will save the inference model from the checkpoint which we created while training our model. this article is helpful for those who are starting the TensorFlow lite. I hope it will be helpful. Application link at the end of the article.

Alt Text

  1. First we will freeze the inference graph using TensorFlow od API.

freezing infernce_graph
Freezing is the process to identify and save all of the required things(graph, weights, etc) in a single file that you can easily use.

python models/research/object_detection/ \
 --input_type image_tensor \
 --pipeline_config_path /output/exported_models/training/001/pipeline.config \
 --trained_checkpoint_dir output/exported_models/training/001/ \
 --output_directory output/exported_models/inference_model
Enter fullscreen mode Exit fullscreen mode

2.Then we will convert the model to the tflite inference graph.

python models/research/object_detection/ \
 --pipeline_config_path output/exported_models/inference_model/inference_modelsaved_model/pipeline.config \
 --trained_checkpoint_dir output/exported_models/inference_model/saved_model/checkpint \
 --output_directory output/exported_models/tflite_infernce
Enter fullscreen mode Exit fullscreen mode

3.Then we will post quantize the graph and save the tflite model.

# save this file as
def representative_dataset():
    for _ in range(100):
      data = np.random.rand(1, 320, 320, 3)
      yield [data.astype(np.float32)]

import numpy as np
import tensorflow as tf

saved_model_dir = "output/exported_models/tflite_infernce/saved_model"

converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.allow_custom_ops = True
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset
converter.inference_input_type = tf.uint8  # or tf.uint8
converter.inference_output_type = tf.uint8  # or tf.uint8
tflite_quant_model = converter.convert()

with, 'wb') as f:
Enter fullscreen mode Exit fullscreen mode

4.Write metadata into the tflite model to use with the android.

''' Writing MetaData to TfLite Model 
save it as

from tflite_support.metadata_writers import object_detector
from tflite_support.metadata_writers import writer_utils
from tflite_support import metadata

ObjectDetectorWriter = object_detector.MetadataWriter
_MODEL_PATH = <tf_lite_model_path>
_LABEL_FILE = <label_path>
_SAVE_TO_PATH = <path_to_tflite_path/tflite_with_metadata.tflite>

writer = ObjectDetectorWriter.create_for_inference(
    writer_utils.load_file(_MODEL_PATH), [127.5], [127.5], [_LABEL_FILE])
writer_utils.save_file(writer.populate(), _SAVE_TO_PATH)

# Verify the populated metadata and associated files.
displayer = metadata.MetadataDisplayer.with_model_file(_SAVE_TO_PATH)
print("Metadata populated:")
print("Associated file(s) populated:")
Enter fullscreen mode Exit fullscreen mode

5.Clone the Tensorflow-examples repository from the TensorFlow GitHub account.

Download the Android studio and SDK file to use the android app for detection.

git clone
Enter fullscreen mode Exit fullscreen mode

copy your tflite_with_metadata.tflite file and rename it as 'detect.tfliteand save it in theapp/src/main/assets/detect.tflite`

Change the TF_OD_API_INPUT_SIZE to the model in the app\src\main\java\org\tensorflow\lite\examples\detection\ to 320.

Create the virtual device or connect your phone and run the object detection application successfully.

If this Article helpful then Please hit a like and subscribe to the channel to encourage us to make more videos and articles.

In the next article, I will share how to train the TensorFlow ssdMobilenet for object detection.

GitHub Link with all data including android app

Download the APK for testing from Google Drive

Thanks to David Lee and Roboflow for the Dataset.

You can read awesome articles like this on Codeperfectplus

More Articles by Author

Join for Weekly Updates.

Top comments (0)