<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Samuel Böhme on Medium]]></title>
        <description><![CDATA[Stories by Samuel Böhme on Medium]]></description>
        <link>https://medium.com/@sa_m_u?source=rss-afd147720676------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6I1_W-LuRSBF6g2CAEBjLA.jpeg</url>
            <title>Stories by Samuel Böhme on Medium</title>
            <link>https://medium.com/@sa_m_u?source=rss-afd147720676------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 18:46:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sa_m_u/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Android Tensorflow Image recognition]]></title>
            <link>https://medium.com/@sa_m_u/develop-your-own-android-image-recognition-app-with-tensorflow-in-20-minutes-ed8c773b95f5?source=rss-afd147720676------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed8c773b95f5</guid>
            <category><![CDATA[image-recognition]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[beginners-guide]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Samuel Böhme]]></dc:creator>
            <pubDate>Mon, 18 Dec 2017 12:58:35 GMT</pubDate>
            <atom:updated>2018-01-04T13:45:59.799Z</atom:updated>
            <content:encoded><![CDATA[<h3>Develop your own Android app for image recognition with tensorflow in 20 minutes</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jmQfx5o5G0bPa3OnRrMfUQ.png" /></figure><h3><strong>1. Introduction</strong></h3><h4>1.1 About this tutorial</h4><p>I have first heard of tensorflow in 2017, when I started to get a deeper look in Machine learning. Since this year I am really fascinated about the possibilities. So I thought “let’s do an app to analyze pictures with my own model”.</p><p>When I started to develop apps with android and tensorflow I felt a little bit lost. Most of the tutorials referenced to the standard android tensorflow example app from google with very limited documentation.</p><p>So after I did my first app with tensorflow on android I thought it might be a good idea to post my experiences as a documentation on how to do it.</p><p>My aim is to help others to find an easy access into developing with tensorflow on Android. This tutorial is mainly for beginners in android and tensorflow.</p><p>I’ll quickly introduce tensorflow and guide you through the process of creating your first app. You will be able to pick a photo from your gallery and let your model analyze it and see the result instantly.</p><p>For my personal use I created a model to identify and organize my study notes. I’m using this model for our demo app, but you can easily create you’re own one. It’s actually quite fun. Check out this <a href="https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#0">google codelab</a> tutorial to create your own model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*5DZky5MKyhR38RFDsExkaA.jpeg" /><figcaption>Screenshot from our example app</figcaption></figure><p>This is how our app will look like. You can press the button “ select Photo”. Your gallery will open and you can choose a photo. The app will analyze the picture and print out “studynotes” or “nostudynotes”. As I mentioned its a basic model to learn how it works.</p><h4>1.2 What is tensorflow?</h4><p>I assume most of you already know the basics but I’ll quickly repeat it for those who don’t. Tensorflow is a library from Google to provide deep learning methods. For example: linear models, regression, image recognition, audio recognition and a lot more. Check <a href="https://medium.freecodecamp.org/want-to-know-how-deep-learning-works-heres-a-quick-guide-for-everyone-1aedeca88076">this </a>article to get more insights in deep learning.</p><p>Recently tensorflow got really popular and is constantly updated. The Libary is available in Java, python, and has 2 mobile variants: tensorflow mobile and lite. tensorflow lite (tf lite) is an evolution of tf mobile but does not support all the functions yet. tf mobile is the better documented version. This tutorial is based on tf mobile. Once tf lite supports all the functions I will update this tutorial. Till then tf mobile is the better choice.</p><h3>2. Lets start with your app</h3><p>Now I will guide you through the developing steps for your app. I assume you have already downloaded android studio and know the basics of Java.</p><p>I have prepared a <a href="https://github.com/samuel13b/AndroidTensorflowTutorial">Github repository </a>for the demo app. You can either follow this tutorial step by step or just clone the Github repository. Use this code however you like. Keep in mind sharing is awesome. I would be very happy to see your latest android tensorflow projects on Github.</p><h4>2.1 Requirements for Tensorflow</h4><p>Here is a list of the necessary components to do machine learning in android.</p><ul><li>Dependency: tensorflow preview (you can also use the latest version but the preview works just fine)</li><li><a href="https://github.com/samuel13b/AndroidTensorflowTutorial/blob/master/app/src/main/java/com/samuel/smartgallery/smartgallery/ClassifierActivity.java">Initialization of Tensorflow (method in ClassifierActivity)</a></li><li><a href="https://github.com/samuel13b/AndroidTensorflowTutorial/blob/master/app/src/main/java/com/samuel/smartgallery/smartgallery/TensorFlowImageClassifier.java">TensorFlowImageClassifier</a></li><li><a href="https://github.com/samuel13b/AndroidTensorflowTutorial/blob/master/app/src/main/java/com/samuel/smartgallery/smartgallery/Classifier.java">Classifier Interface</a></li></ul><p>You can easily copy the Classifier Interface and TensorflowImageClassifer. To view these file, follow this link. I provide you with clearer instructions later in this tutorial.</p><p>So lets get started.</p><h4>2.3 First basic steps</h4><ol><li>Create a new android project. Choose Empty Activity and name it: “ClassifierActivity”</li><li>In your build.gradle(Modue:app) add compile ‘org.tensorflow:tensorflow-android:1.2.0-preview’ in dependencies.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dfk68LdD0NZiD-84cRjJIw.png" /></figure><p>3. Create an Asset Folder in the app Folder. (right click on app &gt; new &gt; folder &gt; Asset Folder). Copy the 2 files “retrained_labels.txt” and “rounded_graph.pb”. You can find these files <a href="https://github.com/samuel13b/AndroidTensorflowTutorial/tree/master/app/src/main/assets">here</a>.</p><p>4. Copy <a href="https://github.com/samuel13b/AndroidTensorflowTutorial/blob/master/app/src/main/java/com/samuel/smartgallery/smartgallery/TensorFlowImageClassifier.java">TensorFlowImageClassifier</a> and the interface <a href="https://github.com/samuel13b/AndroidTensorflowTutorial/blob/master/app/src/main/java/com/samuel/smartgallery/smartgallery/Classifier.java">Classifier</a> in the same folder where your ClassifierActivity is.</p><h3>3. Necessary code</h3><h4>3.1. Initialization of Tensorflow</h4><p>In the ClassifierActitity class inside the “on create method”, you need to call the method “initTensorFlowAndLoadModel”. Tensorflow gets initialized at the start of your app. To make this happen you need to copy the code below inside the ClassifierActivity class. I assume you understand this code, if not leave a comment.</p><pre><strong>private void </strong>initTensorFlowAndLoadModel() {<br>    <strong>executor</strong>.execute(<strong>new </strong>Runnable() {<br>        @Override<br>        <strong>public void </strong>run() {<br>            <strong>try </strong>{<br>                <strong>classifier </strong>= TensorFlowImageClassifier.<em>create</em>(<br>                        getAssets(),<br>                        <strong><em>MODEL_FILE</em></strong>,<br>                        <strong><em>LABEL_FILE</em></strong>,<br>                        <strong><em>INPUT_SIZE</em></strong>,<br>                        <strong><em>IMAGE_MEAN</em></strong>,<br>                        <strong><em>IMAGE_STD</em></strong>,<br>                        <strong><em>INPUT_NAME</em></strong>,<br>                        <strong><em>OUTPUT_NAME</em></strong>);<br>            } <strong>catch </strong>(<strong>final </strong>Exception e) {<br>                <strong>throw new </strong>RuntimeException(<strong>&quot;Error initializing TensorFlow!&quot;</strong>, e);<br>            }<br>        }<br>    });<br>}</pre><p>This Methods runs as a thread so you should put “public Executor executor = Executors.<em>newSingleThreadExecutor</em>()” at the top of this class. The variables are explained in the <a href="https://www.tensorflow.org/tutorials/image_recognition">tensorflow image recognition tutorial</a>. For our model these are the right parameters.</p><pre><strong>public static final int <em>INPUT_SIZE </em></strong>= 224;<br><strong>public static final int <em>IMAGE_MEAN </em></strong>= 128;<br><strong>public static final float <em>IMAGE_STD </em></strong>= 128.0f;<br><strong>public static final </strong>String <strong><em>INPUT_NAME </em></strong>= <strong>&quot;input&quot;</strong>;<br><strong>public static final </strong>String <strong><em>OUTPUT_NAME </em></strong>= <strong>&quot;final_result&quot;</strong>;<br><strong>public static final </strong>String <strong><em>MODEL_FILE </em></strong>= <strong>&quot;file:///android_asset/rounded_graph.pb&quot;</strong>;<br><strong>public static final </strong>String <strong><em>LABEL_FILE </em></strong>= <strong>&quot;file:///android_asset/retrained_labels.txt&quot;</strong>;<br><br><strong>public </strong>Classifier <strong>classifier</strong>;<br><strong>public </strong>Executor <strong>executor </strong>= Executors.<em>newSingleThreadExecutor</em>();</pre><h4>3.2 Add tensorflow image Classifierer and the Classifier Interface</h4><p>I’m not going to give you a deeper look in whats going on in these classes. The code is from google and easy to understand. There are many good tutorials with clear explanation. So for now we will just copy the two classes. You need to copy those two files next to the ClassiferActivity. Here are the links:</p><ul><li><a href="https://github.com/samuel13b/AndroidTensorflowTutorial/blob/master/app/src/main/java/com/samuel/smartgallery/smartgallery/TensorFlowImageClassifier.java">TensorFlowImageClassifier</a></li><li><a href="https://github.com/samuel13b/AndroidTensorflowTutorial/blob/master/app/src/main/java/com/samuel/smartgallery/smartgallery/Classifier.java">Classifier</a></li></ul><p>Same as always, if you need help, leave a comment and I’m happy to help you out.</p><h4>3.3 Analyze a picture</h4><p>This method calls the method recognizeImage to analyze the picture. The results are stored in a list.</p><pre><strong>public </strong>List&lt;Classifier.Recognition&gt; analyse(Bitmap bitmap)<br>{<br>    bitmap = Bitmap.<em>createScaledBitmap</em>(bitmap, <strong><em>INPUT_SIZE</em></strong>,              <strong><em>INPUT_SIZE</em></strong>, <strong>false</strong>);<br>    <strong>final </strong>List&lt;Classifier.Recognition&gt; results = <strong>classifier</strong>.recognizeImage(bitmap);<br>    <strong>return </strong>results;<br>}</pre><h4>3.4 Pick a photo from the gallery app to analyze it</h4><p>This is a very simple intent provided from android to select a photo out of your Gallery</p><pre><strong>public void </strong>selectPhoto(View v)<br>{<br>    Intent photoPickerIntent = <strong>new </strong>Intent(Intent.<strong><em>ACTION_PICK</em></strong>);<br>    photoPickerIntent.setType(<strong>&quot;image/*&quot;</strong>);<br>    startActivityForResult(photoPickerIntent, <strong>PICK_IMAGE_REQUEST</strong>);<br>}<br>@Override<br><strong>protected void </strong>onActivityResult(<strong>int </strong>reqCode, <strong>int </strong>resultCode, Intent data) {<br>    <strong>super</strong>.onActivityResult(reqCode, resultCode, data);<br>    <strong>if </strong>(resultCode == <strong><em>RESULT_OK</em></strong>) {<br>        <strong>try<br>        </strong>{<br>            <strong>final </strong>Uri imageUri = data.getData();<br>            <strong>final </strong>InputStream imageStream = getContentResolver().openInputStream(imageUri);<br>            <strong>final </strong>Bitmap selectedImage = BitmapFactory.<em>decodeStream</em>(imageStream);<br>            List&lt;Classifier.Recognition&gt; results = analyse(selectedImage);<br>            <strong>TV1</strong>.setText(results.get(0).toString());<br>            setPicture(selectedImage);<br><br>        } <strong>catch </strong>(FileNotFoundException e) {<br>            e.printStackTrace();<br>        }<br>    }<br>}<br><em>//set Picture in the ImageView (iV1)<br></em><strong>public void </strong>setPicture(Bitmap bp)<br>{<br>    Bitmap scaledBp =  Bitmap.<em>createScaledBitmap</em>(bp, <strong>iV1</strong>.getWidth(), <strong>iV1</strong>.getHeight(), <strong>false</strong>);<br>    <strong>iV1</strong>.setImageBitmap(scaledBp);<br>}</pre><p>Now we have all code parts implemented. To interact with the code we need to build an UI.</p><h4>3.5 Add the UI</h4><p>Basically we only need a button to perform the event, an image view and text view to show the result. Feel free to use my example <a href="https://github.com/samuel13b/AndroidTensorflowTutorial/blob/master/app/src/main/res/layout/activity_classifier.xml">“classifier_activity.xml”</a> layout or be creative and design an awesome UI for YOUR APP.</p><h4>3.6 Test your app &amp; summary</h4><p>Congratulations you have done it. Just plug in your phone and run the app. Awesome, right ?</p><h3>4. Train your own model</h3><p>Google provides a very helpful tutorial which explains how to train your own model. The tutorial is easy to understand. It takes about an hour to learn the skills to train your own model.<a href="https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#0"> link to google codelab</a></p><h3>5. Further interesting articles</h3><p>I would like to share some really helpful articles and websites with you:</p><ul><li><a href="https://codelabs.developers.google.com/">Google codelab</a>(select tensorflow, you can learn how to train a model, how to use a model for android, and get used to tf lite)</li><li><a href="https://blog.mindorks.com/android-tensorflow-machine-learning-example-ff0e9b2654cc">This article </a>helped me a lot to develop my first android tensorflow app.</li><li>Check out the official t<a href="https://www.tensorflow.org/mobile/">ensorflow mobile page</a> and <a href="https://www.tensorflow.org/tutorials/image_recognition">tensorflow image recognition tutorial</a></li></ul><h3>6. About myself</h3><p>My name is Samuel, I’m studying international Business Technology. In my free time I love to code and study all kind of new innovation topics. The last couple of months I did some studies in machine learning on my own. I’m looking forward to hearing from you in the comments.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed8c773b95f5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>