{"id":5202,"date":"2023-03-04T21:46:11","date_gmt":"2023-03-04T21:46:11","guid":{"rendered":"https:\/\/techzizou.com\/?p=5202"},"modified":"2023-03-04T22:05:33","modified_gmt":"2023-03-04T22:05:33","slug":"build-android-app-for-custom-object-detection","status":"publish","type":"post","link":"https:\/\/techzizou.com\/build-android-app-for-custom-object-detection\/","title":{"rendered":"Build Android app for custom object detection"},"content":{"rendered":"\n
Training a Deep Learning model for custom object detection using TensorFlow Object Detection API<\/strong> in Google Colab and converting it to a TFLite model for deploying on mobile devices like Android, iOS, Raspberry Pi, IoT devices using the sample TFLite object detection app<\/a> from TensorFlow\u2019s GitHub.<\/p>\n\n\n\n This tutorial is for TensorFlow 2.x<\/p>\n\n\n\n THIS IS FOR THE LATEST VERSION TENSORFLOW APP. USE THE FOLLOWING VERSION REFERENCE APP USED IN THIS TUTORIAL:<\/p>\n\n\n\n https:\/\/github.com\/tensorflow\/examples\/tree\/master\/lite\/examples\/object_detection\/android<\/a><\/p>\n\n\n\n In this article, I will be training an object detection model for a custom object and converting it to a TFlite model so it can be deployed on Android, iOS, IoT devices. Follow the 21 steps mentioned below. (The first 16 steps are the same as my previous article on training an ML model using TF 2<\/a>. Since TFLite has support for only SSD models at the moment, we will be using an SSD model here)<\/p>\n\n\n\n ( But first \u2705Subscribe to my YouTube channel \ud83d\udc49\ud83c\udffb https:\/\/bit.ly\/3Ap3sdi<\/a> \ud83d\ude01\ud83d\ude1c) <\/p>\n\n\n\n –Open my Colab notebook<\/a> on your browser.<\/p>\n\n\n\n –Click on File<\/strong> in the menu bar and click on Save a copy in drive<\/strong>. This will open a copy of my Colab notebook on your browser which you can now use.<\/p>\n\n\n\n –Next, once you have opened the copy of my notebook and are connected to the Google Colab VM, click on Runtime<\/strong> in the menu bar and click on Change runtime type<\/strong>. Select GPU<\/strong> and click on save.<\/p>\n\n\n\n Create a folder named customTF2<\/em><\/strong> in your google drive.<\/p>\n\n\n\n Create another folder named training<\/em><\/strong> inside the customTF2<\/em><\/strong> folder ( training<\/em><\/strong> folder is where the checkpoints will be saved during training ).<\/p>\n\n\n\n Create another folder named data<\/em><\/strong> inside the customTF2<\/em><\/strong> folder.<\/p>\n\n\n\n Create a folder named images<\/em><\/strong> for your custom dataset images and create another folder named annotations<\/em><\/strong> for its corresponding PASCAL_VOC format labeled XML files.<\/p>\n\n\n\n Next, create their zip files and upload them to the customTF2<\/em><\/strong> folder in your drive.<\/p>\n\n\n\n Make<\/strong> sure all the image files have their extension as “.jpg” only.<\/strong> <\/p>\n\n\n\n Other formats like <.png><\/strong> , <.jpeg><\/strong> or even <.JPG<\/strong>> will give errors since the generate_tfrecord <\/em><\/strong>and xml_to_csv<\/em><\/strong> scripts here have only <.jpg><\/strong> in them. If you have other format images, then make changes in the scripts accordingly.<\/p>\n\n\n\n <\/p>\n<\/blockquote>\n\n\n\n For Datasets, you can check out my Dataset Sources at the bottom of this article in the credits section. <\/p>\n\n\n\n Input image example (Image1.jpg<\/strong>)<\/p>\n\n\n <\/p>\n\n\n\n <\/p>\n\n\n\n <\/p>\n\n\n\n <\/p>\n\n\n\n <\/p>\n\n\n\n <\/p>\n\n\n\n <\/p>\n\n\n\n <\/p>\n\n\n\n You can use any software for labeling like the labelImg<\/strong><\/a> tool.<\/p>\n\n\n <\/p>\n\n\n\n <\/p>\n\n\n\n <\/p>\n\n\n\n I use an open-source labeling tool called OpenLabeling<\/strong> with a very simple UI.<\/p>\n\n\n <\/p>\n\n\n\n <\/p>\n\n\n\n <\/p>\n\n\n\n <\/p>\n\n\n\n <\/p>\n\n\n\n <\/p>\n\n\n\n <\/p>\n\n\n\n <\/p>\n\n\n\n Click on the link below to know more about the labeling process and other software for it:<\/p>\n\n\n\n NOTE: <\/strong>Garbage In = Garbage Out. Choosing and labeling images is the most important part. Try to find good-quality images. The quality of the data goes a long way towards determining the quality of the result.<\/p>\n\n\n\n The output PASCAL_VOC labeled XML file looks like as shown below:<\/p>\n\n\n You can find the generate_tfrecord.py file here<\/a><\/p>\n\n\n\nIMPORTANT:<\/h3>\n\n\n\n
\n\n\n\nRoadmap<\/h4>\n\n\n\n
\n
\n\n\n\nObjective:<\/span><\/strong> Build android app for custom object detection<\/h3>\n\n\n\n
\n
\n\n\n\nHOW TO BEGIN?<\/h3>\n\n\n\n
<\/figure>\n\n\n\n
\n\n\n\n<\/figure>\n\n\n\n
LET\u2019S BEGIN !!<\/h2>\n\n\n\n
1) Import Libraries<\/h3>\n\n\n\n
import os
import glob
import xml.etree.ElementTree as ET
import pandas as pd
import tensorflow as tf<\/code><\/pre>\n\n\n\n
\n\n\n\n2) Create customTF2, training, and data folders in your google drive<\/h3>\n\n\n\n
\n\n\n\n3) Create and upload your image files and its their corresponding labeled XML files.<\/h3>\n\n\n\n
\n
Collecting Images Dataset and labeling them to get their PASCAL_VOC XML annotations.<\/h4>\n\n\n\n
Labeling your Dataset<\/h4>\n\n\n\n
\n\n\n\n
\n\n\n\n
\n\n\n\n\n
\n\n\n\n4) Upload the generate_tfrecord.py file to the customTF2 folder in your drive.<\/h3>\n\n\n\n