create account

Tensorflow: Image Recognition App in Kivy by faad

View this thread on: hive.blogpeakd.comecency.com
· @faad · (edited)
$0.35
Tensorflow: Image Recognition App in Kivy
#### Repository
https://github.com/tensorflow/tensorflow

#### What Will I Learn?
Here, today we come up with a new tutorial, in this tutorial you will learn Image  Recognition  using Tensorflow and Kivy. Real time image recognition, open the camera to capture the image and the App automatically detect the objects from the image. 

- Tensorflow Image Recognition
- Tensorflow with Kivy

#### Requirements

- Programming Fundamentals 
- Python Essentials
- Kivy Essentials

#### Difficulty

- Intermediate

#### Tutorial Contents

- Introduction
- Setting up the Environment
- Develop Image Recognition

## Introduction

In this tutorial we are going to develop Image Recognition App, the will allow you to detect the objects from the image. For this we'll use Tensorflow in order to detect the objects in the image, we'll talk about it in just a moment. We'll also use Kivy (a python library) to the App's GUI, if you know about our Kivy tutorial series then you know about the Kivy, but if not then please check our Kivy tutorial series. 

## TensorFlow

TensorFlow is an open source library and develop by researchers and engineers from the Google Brain team within Google’s AI organization for high performance numerical computation, and supports machine learning and deep learning. For more info visit [TensorFlow](https://www.tensorflow.org/).

Google trained Inception-v3 for [ImageNet](http://image-net.org/) large visual recognition in which models try to classify the image into 1000 classes. For complete description please visit TensorFlow [ImageRecognition](https://www.tensorflow.org/tutorials/image_recognition). 

## Setting up the Environment

Before we move to develop our Image Recognition App, first we have to set a working environment that will allow us to develop our app. For this first of all [install the TensorFlow](https://www.tensorflow.org/install/) and then [install the Kivy](https://kivy.org/docs/installation) library on your machine, visit their sites in order to install. You also have to install some other python packages to run it properly on the machine, these packages are [argparse](https://pypi.org/project/argparse/) and [numpy](https://pypi.org/project/numpy/). 

## Develop Image Recognition App

In order to develop an Image Recognition App, first of all create a file in your text editor (we are using [PyCharm](https://www.jetbrains.com/pycharm/download/)) and save it with a name ```main.py ```. 

First of all we'll develop our App in Kivy, for this first import the Kivy packages, write the following code in you file.

 ```

import kivy
kivy.require('1.9.0')

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label

```
These are the packages we'll use in order to develop our app. 

Next up we'll develop the App's GUI, for this write the following code. 


```
Builder.load_string('''
<CaputeImage>:
    orientation: 'vertical'
    Button:
        text: 'Capture'
        size_hint_y: None
        height: '48dp'
''')

class CaputeImage(BoxLayout):
  pass

class ImageRecognition(App):

    def build(self):
        return CaputeImage()

if __name__ == "__main__":
    imagerecognition = ImageRecognition()
    imagerecognition.run()


```

Output: 

![ImageRecognition-1.JPG](https://cdn.steemitimages.com/DQmaKnkQAN5MpFhMiba1zCms18F2dX9u42dQi69ScnZH7yK/ImageRecognition-1.JPG)

Now run the camera in the app in order to capture the images, for this write this code in the ```Builder.load_string ``` method. 

```

    Camera:
        id: camera
        resolution: (720, 480)
        play: True

```
Place this code above to the capture Button code, by default we set the camera ```play:True ```, this means whenever you launch the App camera will automatically starts. 

Output: 

![ImageRecognition-2.gif](https://cdn.steemitimages.com/DQmNX5ENFbZVNLmLn9ziAM1KhryytauNSxjLqFfwrH2w4f3/ImageRecognition-2.gif)

You can see our camera is working fine, Now we'll capture the image using camera. For this develop a method ```def capture(self): ``` within ``` CameraClick ```  class and write the following code in order to capture the image. 

``` 

    def capture(self):
        '''
        Function to capture the images and give them the names
        according to their captured time and date.
        '''

        camera = self.ids['camera']
        camera.export_to_png("test_images/IMG.png") 
        print("Captured")
        self.ids['camera'].play = False

``` 

Also call the method whenever you click the capture button, set the this in the button properties,  ``` on_press: root.capture() ``` 

``` camera.export_to_png("image/IMG.png") ```  allow us to export the image into image folder and save it with name ```  IMG.png ```  . So, also make a folder with the name ```  image ```   in the working directory in order to save the images. 

Now run the app and click over the capture button it will capture the image and export it to ``` image ``` folder.

Next up we'll move to our real work, detecting the objects from the image. For this first copy this code from [github](https://raw.githubusercontent.com/tensorflow/models/master/tutorials/image/imagenet/classify_image.py). Create a new file with name ```  imgrecog.py ```  and paste copied code in this file and save it.  And import this file in the ```  main.py ```  file for this write the code. ```   import imagrecog as ir ```  

Now made some changes to this code, first of all cut the main method and past it in the ```  main.py ```   file within the ```  CaputeImage ``` class. and remove the _ from the parameter body and write the self. and change the code as

 ``` 
    def main(self):
        ir.maybe_download_and_extract()
        image = (ir.FLAGS.image_file if ir.FLAGS.image_file else
                 ir.os.path.join('image/', 'IMG.png'))
        ir.run_inference_on_image(image) 

``` 

Now remove the ``` if __name__ == '__main__': ```  from ``` imagerecog.py ```  and remove indentation of the code within the if statement.  

And lastly remove this line of cod ``` tf.app.run(main=main, argv=[sys.argv[0]] + unparsed) ``` from ``` imagerecog.py ```  file. 

Now move toward ```  main(self)```   method in the ```  main.py```   file. And make a label to show the returning text from the ```  imagerecog.py ```   file. For this add the following code in the method.

```   
self.add_widget(Label(text=string, text_size=(600, None), line_height=1.5))

```   

Now move toward ```  capture(self) ```   method and call the main(self) method after capturing the image. Your code in the ```  capture(self) ```  method will be 

```
    def capture(self):
        '''
        Function to capture the images and give them the names
        according to their captured time and date.
        '''

        camera = self.ids['camera']
        camera.export_to_png("test_images/IMG.png")
        print("Captured")
        self.ids['camera'].play = False
        self.clear_widgets()
        self.main()
  
```  

At the last point open the imagerecog.py file and jump toward ``` def run_inference_on_image(image):  ```  method and replace the for loop code with the following code in the method. 

```  
    string = ""
    top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1]
    for node_id in top_k:
      human_string = node_lookup.id_to_string(node_id)
      score = predictions[node_id]
      string += '%s (score = %.5f)' % (human_string, score)
    return string

```  
Here in my file it is the line no# 162 - 168

Now run the App, you will see whatever you capture in the image the App will detect it from the image. 

Output:

![ImageRecognition-3.gif](https://cdn.steemitimages.com/DQmettuJLWEF1sKVuACq8RKsDx79tdZXdku7CN1GQERiFni/ImageRecognition-3.gif)


You can see in the above image we capture the cellphone image and it detects all the possible results in the image. 


#### Proof of Work Done

https://github.com/faad1/tensorflow-image-recognition
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
👎  , , ,
properties (23)
authorfaad
permlinktensorflow-image-recognition-app-in-kivy
categoryutopian-io
json_metadata{"tags":["utopian-io","tutorials","tensorflow","kivy"],"image":["https://cdn.steemitimages.com/DQmaKnkQAN5MpFhMiba1zCms18F2dX9u42dQi69ScnZH7yK/ImageRecognition-1.JPG","https://cdn.steemitimages.com/DQmNX5ENFbZVNLmLn9ziAM1KhryytauNSxjLqFfwrH2w4f3/ImageRecognition-2.gif","https://cdn.steemitimages.com/DQmettuJLWEF1sKVuACq8RKsDx79tdZXdku7CN1GQERiFni/ImageRecognition-3.gif"],"links":["https://github.com/tensorflow/tensorflow","https://www.tensorflow.org/","http://image-net.org/","https://www.tensorflow.org/tutorials/image_recognition","https://www.tensorflow.org/install/","https://kivy.org/docs/installation","https://pypi.org/project/argparse/","https://pypi.org/project/numpy/","https://www.jetbrains.com/pycharm/download/","https://raw.githubusercontent.com/tensorflow/models/master/tutorials/image/imagenet/classify_image.py","https://github.com/faad1/tensorflow-image-recognition"],"app":"steemit/0.1","format":"markdown"}
created2018-06-12 07:06:33
last_update2018-06-12 14:10:00
depth0
children5
last_payout2018-06-19 07:06:33
cashout_time1969-12-31 23:59:59
total_payout_value0.312 HBD
curator_payout_value0.037 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,985
author_reputation5,712,498,691,861
root_title"Tensorflow: Image Recognition App in Kivy"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id60,408,150
net_rshares134,661,470,600
author_curate_reward""
vote details (39)
@abusereports ·
@faad you were flagged by a worthless gang of trolls, so, I gave you an upvote to counteract it!  Enjoy!!
properties (22)
authorabusereports
permlinkabusereports-re-faadtensorflow-image-recognition-app-in-kivy
categoryutopian-io
json_metadata""
created2018-07-16 18:52:45
last_update2018-07-16 18:52:45
depth1
children0
last_payout2018-07-23 18:52:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length105
author_reputation199,407,425,243,286
root_title"Tensorflow: Image Recognition App in Kivy"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,903,239
net_rshares0
@mcfarhat ·
Thank you for your contribution.
- You have missed properly explaining in your tutorial what you are doing to the reader. Instructing readers to just copy and paste code, and add indentation does not really qualify to instruct the user or inform him how and why to do things as they are.
- You have also used an incorrect github repo for tensor, [this here](https://github.com/tensorflow/tensorflow) would be a better suited one 
- You also referenced code that you have not used anywhere in your tutorial (about the on.press capture code). Please focus on proper explanations everywhere.

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
properties (22)
authormcfarhat
permlinkre-faad-tensorflow-image-recognition-app-in-kivy-20180612t122941999z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://github.com/tensorflow/tensorflow","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-06-12 12:29:51
last_update2018-06-12 12:29:51
depth1
children1
last_payout2018-06-19 12:29:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length758
author_reputation150,651,671,367,256
root_title"Tensorflow: Image Recognition App in Kivy"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id60,439,586
net_rshares0
@faad ·
Sorry for Mistake.
I have updated Github Repo.
properties (22)
authorfaad
permlinkre-mcfarhat-re-faad-tensorflow-image-recognition-app-in-kivy-20180612t141058111z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-06-12 14:11:00
last_update2018-06-12 14:11:00
depth2
children0
last_payout2018-06-19 14:11:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length46
author_reputation5,712,498,691,861
root_title"Tensorflow: Image Recognition App in Kivy"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id60,451,329
net_rshares0
@steemitboard ·
Congratulations @faad! You received a personal award!

<table><tr><td>https://steemitimages.com/70x70/http://steemitboard.com/@faad/birthday1.png</td><td>1 Year on Steemit</td></tr></table>

<sub>_[Click here to view your Board of Honor](https://steemitboard.com/@faad)_</sub>


> Support [SteemitBoard's project](https://steemit.com/@steemitboard)! **[Vote for its witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1)** and **get one more award**!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-faad-20181222t023656000z
categoryutopian-io
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2018-12-22 02:36:57
last_update2018-12-22 02:36:57
depth1
children0
last_payout2018-12-29 02:36:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length490
author_reputation38,975,615,169,260
root_title"Tensorflow: Image Recognition App in Kivy"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id77,201,859
net_rshares0
@steemitboard ·
Congratulations @faad! You received a personal award!

<table><tr><td>https://steemitimages.com/70x70/http://steemitboard.com/@faad/birthday2.png</td><td>Happy Birthday! - You are on the Steem blockchain for 2 years!</td></tr></table>

<sub>_You can view [your badges on your Steem Board](https://steemitboard.com/@faad) and compare to others on the [Steem Ranking](https://steemitboard.com/ranking/index.php?name=faad)_</sub>


###### [Vote for @Steemitboard as a witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1) to get one more award and increased upvotes!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-faad-20191222t032918000z
categoryutopian-io
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2019-12-22 03:29:18
last_update2019-12-22 03:29:18
depth1
children0
last_payout2019-12-29 03:29:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length604
author_reputation38,975,615,169,260
root_title"Tensorflow: Image Recognition App in Kivy"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id93,625,852
net_rshares0