{"id":91,"date":"2024-10-09T09:26:57","date_gmt":"2024-10-09T07:26:57","guid":{"rendered":"https:\/\/lukasza.hopto.org\/blog\/wordpress\/?p=91"},"modified":"2024-10-09T09:26:57","modified_gmt":"2024-10-09T07:26:57","slug":"ml-kit-on-android-bringing-ai-to-your-fingertips","status":"publish","type":"post","link":"https:\/\/lukasza.hopto.org\/index.php\/2024\/10\/09\/ml-kit-on-android-bringing-ai-to-your-fingertips\/","title":{"rendered":"ML Kit on Android: Bringing AI to Your Fingertips"},"content":{"rendered":"\n<p>So, you\u2019ve got an Android app idea, and you want to sprinkle in some AI-powered magic\u2014whether it\u2019s image recognition, text detection, or even real-time translation. But here\u2019s the catch: you\u2019re not exactly looking to become a machine learning expert overnight, right? Enter <strong>ML Kit<\/strong>, Google\u2019s easy-to-use, AI-powered toolkit that takes care of the heavy lifting, letting you plug advanced machine learning features right into your app with minimal fuss.<\/p>\n\n\n\n<p>With <strong>ML Kit<\/strong>, you can tap into powerful AI models directly on your device or in the cloud. Whether it\u2019s scanning barcodes, understanding text, or even recognizing faces, ML Kit has you covered, and the best part? You can get up and running faster than it takes to watch a couple of tutorials on YouTube.<\/p>\n\n\n\n<p>Let\u2019s dive into what makes ML Kit so great for Android developers, and how you can get started building AI-infused apps without needing a PhD in data science.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What is ML Kit?<\/strong><\/h3>\n\n\n\n<p><strong>ML Kit<\/strong> is Google\u2019s machine learning toolkit for Android and iOS that brings powerful, pre-built machine learning models right to your mobile app. It handles tasks like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Image Labeling<\/strong>: Identify objects in photos.<\/li>\n\n\n\n<li><strong>Text Recognition<\/strong>: Extract text from images (useful for scanning documents or reading street signs).<\/li>\n\n\n\n<li><strong>Face Detection<\/strong>: Recognize faces in photos or videos, complete with landmarks like eyes, mouth, and even whether someone\u2019s smiling.<\/li>\n\n\n\n<li><strong>Barcode Scanning<\/strong>: Scan and process various barcode formats quickly and easily.<\/li>\n\n\n\n<li><strong>Language Translation<\/strong>: Translate text into different languages in real time.<\/li>\n\n\n\n<li><strong>Object Detection and Tracking<\/strong>: Detect and track multiple objects in a video feed.<\/li>\n<\/ul>\n\n\n\n<p>What makes ML Kit shine is its simplicity\u2014there\u2019s no need to train your own models (although you can, if you want). Google provides a suite of ready-to-go models that handle most common use cases, and you just need to integrate them into your app.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Why ML Kit?<\/strong><\/h3>\n\n\n\n<p>If you\u2019re thinking, \u201cWhy not just roll my own machine learning models?\u201d, here\u2019s why ML Kit might be a better choice for most developers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Pre-Trained Models<\/strong>: Google\u2019s models are top-notch and cover a wide range of use cases. These models have been trained on massive datasets, so they\u2019re optimized for high accuracy.<\/li>\n\n\n\n<li><strong>On-Device or Cloud-Based<\/strong>: You can run ML Kit models locally on your device or in the cloud, depending on your needs (on-device for speed and privacy, cloud for heavy-duty processing).<\/li>\n\n\n\n<li><strong>Ease of Use<\/strong>: No need to learn TensorFlow, PyTorch, or any other machine learning framework. ML Kit integrates smoothly with Android Studio, and you\u2019ll be up and running in minutes.<\/li>\n\n\n\n<li><strong>Battery-Friendly<\/strong>: Since you\u2019re working with models that are optimized for mobile, you don\u2019t have to worry about your app draining the user&#8217;s battery.<\/li>\n\n\n\n<li><strong>Offline Support<\/strong>: Some models can run offline, which is great for apps that need to work without internet access (think of field workers or travelers).<\/li>\n<\/ul>\n\n\n\n<p>Now that we\u2019ve got a good grasp of why ML Kit rocks, let\u2019s jump into how to get it up and running in your Android app.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Getting Started with ML Kit in Android<\/strong><\/h3>\n\n\n\n<p>To begin using ML Kit, you&#8217;ll need to set up your Android project to integrate with <strong>Firebase<\/strong>, as ML Kit is part of Firebase\u2019s suite of tools. Here\u2019s a quick rundown of how to get started.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 1: Set Up Firebase in Your Android Project<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create a Firebase Project<\/strong>: Head over to the <a>Firebase Console<\/a> and create a new project. Add your Android app by following the steps provided in the console.<\/li>\n\n\n\n<li><strong>Add Firebase SDK to Your App<\/strong>: In your <code>build.gradle<\/code> files, add the following dependencies:In the <strong>project-level<\/strong> <code>build.gradle<\/code>:gradleCopy code<code>classpath 'com.google.gms:google-services:4.3.10' <\/code>In the <strong>app-level<\/strong> <code>build.gradle<\/code>:gradleCopy code<code>implementation platform('com.google.firebase:firebase-bom:31.0.2') implementation 'com.google.firebase:firebase-ml-vision:24.0.3' implementation 'com.google.firebase:firebase-ml-natural-language:22.0.0' <\/code>And don\u2019t forget to apply the Google services plugin:gradleCopy code<code>apply plugin: 'com.google.gms.google-services'<\/code><\/li>\n\n\n\n<li><strong>Sync your project<\/strong> and make sure everything is set up.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Implementing Your First ML Kit Feature<\/strong><\/h3>\n\n\n\n<p>Let\u2019s say you want to add <strong>Text Recognition<\/strong> (OCR) to your app, allowing users to take a picture of text, and have it automatically extracted. Here\u2019s how you can do it using ML Kit.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Text Recognition Example<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Set Up a Camera Intent<\/strong>: First, you\u2019ll need to allow the user to capture an image using the camera or choose one from the gallery. Once you have the image, pass it to ML Kit\u2019s text recognition model.<\/li>\n\n\n\n<li><strong>Process the Image<\/strong>:Use the <code>TextRecognition<\/code> API provided by ML Kit to analyze the image and extract text:javaCopy code<code>FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap); FirebaseVisionTextRecognizer recognizer = FirebaseVision.getInstance().getOnDeviceTextRecognizer(); recognizer.processImage(image) .addOnSuccessListener(new OnSuccessListener&lt;FirebaseVisionText&gt;() { @Override public void onSuccess(FirebaseVisionText result) { \/\/ Text extracted successfully String recognizedText = result.getText(); Log.d(\"MLKit\", \"Text: \" + recognizedText); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.e(\"MLKit\", \"Error: \" + e.getMessage()); } }); <\/code>That\u2019s it! ML Kit will handle the heavy lifting of extracting text from the image, and you\u2019ll get a nice, clean string with all the detected words.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Expanding with Other ML Kit Features<\/strong><\/h3>\n\n\n\n<p>ML Kit isn\u2019t just about recognizing text. You can extend your app with a whole range of AI-powered features. Here are some cool ideas:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Image Labeling<\/strong><\/h4>\n\n\n\n<p>If you want your app to identify objects in images (like &#8220;dog,&#8221; &#8220;cat,&#8221; &#8220;car&#8221;), you can use <strong>Image Labeling<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">javaCopy code<code>FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);\nFirebaseVisionImageLabeler labeler = FirebaseVision.getInstance().getOnDeviceImageLabeler();\n\nlabeler.processImage(image)\n    .addOnSuccessListener(new OnSuccessListener&lt;List&lt;FirebaseVisionImageLabel&gt;&gt;() {\n        @Override\n        public void onSuccess(List&lt;FirebaseVisionImageLabel&gt; labels) {\n            for (FirebaseVisionImageLabel label : labels) {\n                String text = label.getText();\n                float confidence = label.getConfidence();\n                Log.d(\"MLKit\", \"Label: \" + text + \", Confidence: \" + confidence);\n            }\n        }\n    })\n    .addOnFailureListener(new OnFailureListener() {\n        @Override\n        public void onFailure(@NonNull Exception e) {\n            Log.e(\"MLKit\", \"Error: \" + e.getMessage());\n        }\n    });\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Face Detection<\/strong><\/h4>\n\n\n\n<p>Want to detect faces and even determine if someone\u2019s smiling or has their eyes open? You can do that too with <strong>Face Detection<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">javaCopy code<code>FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);\nFirebaseVisionFaceDetector detector = FirebaseVision.getInstance().getVisionFaceDetector();\n\ndetector.detectInImage(image)\n    .addOnSuccessListener(new OnSuccessListener&lt;List&lt;FirebaseVisionFace&gt;&gt;() {\n        @Override\n        public void onSuccess(List&lt;FirebaseVisionFace&gt; faces) {\n            for (FirebaseVisionFace face : faces) {\n                if (face.getSmilingProbability() != FirebaseVisionFace.UNCOMPUTED_PROBABILITY) {\n                    float smileProb = face.getSmilingProbability();\n                    Log.d(\"MLKit\", \"Smiling probability: \" + smileProb);\n                }\n            }\n        }\n    })\n    .addOnFailureListener(new OnFailureListener() {\n        @Override\n        public void onFailure(@NonNull Exception e) {\n            Log.e(\"MLKit\", \"Error: \" + e.getMessage());\n        }\n    });\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 4: Going Further with Custom Models<\/strong><\/h3>\n\n\n\n<p>While ML Kit offers plenty of built-in models, there might be times when you need something more specific. Luckily, ML Kit also supports <strong>custom models<\/strong>. If you\u2019ve got a TensorFlow Lite model you\u2019ve trained yourself, you can integrate it into ML Kit easily.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Upload the Model to Firebase<\/strong>: In the Firebase Console, upload your custom TensorFlow Lite model under the \u201cML\u201d section.<\/li>\n\n\n\n<li><strong>Integrate It in Your App<\/strong>: Use Firebase\u2019s custom model APIs to load and run your model within your Android app.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Final Thoughts: AI Power at Your Fingertips<\/strong><\/h3>\n\n\n\n<p>ML Kit gives Android developers an incredible tool to add AI features to their apps without breaking a sweat. Whether you\u2019re building a photo-scanning app, a barcode scanner, or a real-time translation tool, ML Kit provides the building blocks you need to infuse your app with advanced AI capabilities.<\/p>\n\n\n\n<p>And the best part? You don\u2019t need to become an AI expert. With just a few lines of code, you can leverage Google\u2019s state-of-the-art models to supercharge your app\u2019s functionality.<\/p>\n\n\n\n<p>So, grab that Android device,<\/p>\n\n\n\n<script type=\"text\/javascript\" src=\"https:\/\/cdnjs.buymeacoffee.com\/1.0.0\/button.prod.min.js\" data-name=\"bmc-button\" data-slug=\"lukasza\" data-color=\"#BD5FFF\" data-emoji=\"\"  data-font=\"Cookie\" data-text=\"Buy me a coffee\" data-outline-color=\"#000000\" data-font-color=\"#ffffff\" data-coffee-color=\"#FFDD00\" ><\/script>\n","protected":false},"excerpt":{"rendered":"<p>So, you\u2019ve got an Android app idea, and you want to sprinkle in some AI-powered magic\u2014whether it\u2019s image recognition, text detection, or even real-time translation.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-bez-kategorii"],"_links":{"self":[{"href":"https:\/\/lukasza.hopto.org\/index.php\/wp-json\/wp\/v2\/posts\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lukasza.hopto.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lukasza.hopto.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lukasza.hopto.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lukasza.hopto.org\/index.php\/wp-json\/wp\/v2\/comments?post=91"}],"version-history":[{"count":0,"href":"https:\/\/lukasza.hopto.org\/index.php\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"wp:attachment":[{"href":"https:\/\/lukasza.hopto.org\/index.php\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lukasza.hopto.org\/index.php\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lukasza.hopto.org\/index.php\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}