Posted in Information Technology

TensorFlow Image Recognition Python API Tutorial

It is the fastest and the simplest way to do image recognition on your laptop or computer without any GPU because it is just an API and your CPU is good enough for this.

I know, I’m a little late with this specific API because it came with the early edition of tensorflow. The API uses a CNN model trained on 1000 classesFor more details refer this tensorflow page.

Before I start showing you guys how to implement this API with any image, here is an example

Input is an Image of Space Rocket/Shuttle whatever you wanna call it.

Fig: Rocketlaunch.jpg

The Output is “space shuttle (score = 89.639%)” on the command line.

Fig: Space shuttle (score 89.639%)

Don’t worry if you have linux or Mac. I’m sure this will work on every system with any CPU assuming you already have TensorFlow 1.4 installed.


The whole process will be done in 4 steps :

1. Download the model from tensorflow repository.

Go to the tensorflow repository link and download the thing on your computer and extract it in root folder and since I’m using Windows I’ll extract it in “C:” drive.

Now name the folder “models”.

2. Command line

Open the Command prompt (as Admin).

Now, we need to run the classify_image.py file which is in “models>tutorials>imagenet>classify_image.py” type the following commands and press Enter.

Fig: Running the classifier file

This will download a 200mb model which will help you in recognising your image.

If everything worked perfectly you will see in your command prompt:

giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca (score = 0.88493)
indri, indris, Indri indri, Indri brevicaudatus (score = 0.00878)
lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens (score = 0.00317)
custard apple (score = 0.00149)
earthstar (score = 0.00127)

Now just to make sure that we understand how to use this properly we will do this twice. Once keeping the image file in the “models>tutorials>imagenet>” directory and second keeping the image in different directory or drive 😏

3. Download the image in the directory

Feel free to use any image from the internet or anywhere else and paste it in the “models>tutorials>imagenet>images.png” directory with the classify_image.py and then we’ll paste it in “D:\images.png” or whatever directory you want to, just don’t forget to keep in mind to type the correct address in the command prompt.The image I used is below.

Fig: images.png

4. Use Command prompt to perform recognition

To perform this you need to just edit the “— image_file” argument like this.

a) For the image in the same directory as the classify_image.py file. After coming in the imagenet directory, open the command prompt and type…

python classify_image.py --image_file images.png
Fig: images.png is the same directory

b) For image in the different directory type by pointing towards the directory where your image is placed.

python classify_image.py --image_file D:/images.png
Fig: images.png in different directory

Result

Now, obviously results for both the Images were same which is given as below.

Fig Result for images.png

As you can see the score is pretty accurate i.e. 98.028% for mobile phone.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s