Android things feature 6 complete chestnuts: using TensorFlow to parse images

Source: Internet
Author: User

Google Development Technology expert (GDE) Wangyucheng (York Wang)

So much has been said, as if there is not a whole concept, how do we write a complete code?

Deep learning is now very hot. So we're in Android things. Use the camera to snap pictures. Let TensorFlow to identify the image. Finally, we use the speaker to tell us the result.

Isn't it really cool? Say the main function is said so long a string, how long is the base code?

Project structure

Let's start with the beginning of Android Studio's ring.

After you start Android Studio, be sure to upgrade the version number of SDK Tools to 24 and above. Then upgrade the SDK to Android 7.0 and above. Let Android Studio finish its own update of the related components. To import a project, the structure of the project is as follows:

The Imageclassifier in the code is used to interact with TensorFlow, as well as the camera head. Image processing related to handler.

Let's take a look at the external reference library:

Contains the relevant libraries for Android things and TensorFlow. Of course, the version number of the Android API is 24. Gradle's dependence and filer in Manifest are consistent with the previous set-up of the development environment.


The referenced TensorFlow library is an AAR-packaged Tensorflow-android-inference-alpha-debug.aar. That means. We do not need the NDK environment to compile the entire project.

The main focus is to pay attention to the dependencies. Contains libraries for TensorFlow and Android thing:

And then applied for the camera-related permissions.

To add, Android things is not supported for dynamic permissions.

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvd2zpbmc=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70 /gravity/southeast "alt=" here to write a picture descriptive narrative "title=" ">

Hardware connection

The next step is how the hardware is connected.

Hardware inventory such as the following:
Android Things compatible Development Board, for example Raspberry Pi 3
Android things compatible camera, for example Raspberry Pi 3 camera Module
Components:
1 button, see bread plate
2 resistors, this piece must be explained: Because the picture is connected to the 5V voltage, generally GPIO and led pressure capacity is 3V. Some GPIO are compatible with 5V. So the middle of the need to series 100~200 ohms resistance. Of course, for insurance, 3.3V voltage is recommended.
1 x LED Lights
1 Sheets of bread
DuPont Line several
Optional: Speakers or headphones
Optional: HDMI output

The hardware is finished. We need to understand the operation process at this time.

Operation Flow

In accordance with the previous commentary, use Andorid Studio, connect ADB, configure the Development Board Wi-Fi. The application is then loaded onto the Development Board.

The operating procedures are as follows:
Restart the device and execute the program until the LED lights start flashing;
Aim the camera at the cat. Oh, dog. or some furniture;
Press the switch to start shooting pictures;
In the Raspberry Pi 3. generally within 1s. Able to complete the picture capture. Treated by TensorFlow. Then the voice is released through TTS.

The LED lights are off during the execution.
The Logcat will print out the results at last. If there is a display device connection, the picture and the results will be displayed;
Let's say you have a speaker or a headset. Will broadcast the results in a voice.

Because the structure of the code is particularly simple, pay attention to a few pieces of the key operation can be. Presumably graphics, camera operation in the Android programming, everyone will. So I don't do the commentary.

Code flow

Mainly to see the initial operation of the LED:

It is necessary to say that Imageclassifieractivity.java is the portal of the application's only Activity.

There is already a definition in Manifest. It initializes the LEDs, Camera, Tensorfflow and other components. Among them, the Button we use is BCM32 this pin, the LED used is the BCM6 pin. The associated initialization has been completed in this Activity.

This part of the code is the code that captures the key press. When the button is pressed, the camera starts capturing the data.


After turning the camera data into a Bitmap file, we call TensorFlow to process the image.


This function calls the TensorFlow for processing. Finally, the results are output to logcat.

Assume that the TTS engine is called in the code. Then turn the results into speech and read them out. It seems that the most important thing is the recognizeimage () interface of the Tensorflowclassifie class. Let's keep looking down.


This is the last step. Call TensorFlow for image recognition:
Convert the RGB image into data that TensorFlow can recognize.
Copy the data into the TensorFlow.
Identify the image. Give the result.

The process of calling TensorFlow is quite fun. also very convenient. So, why is it that TensorFlow can identify what the picture is all of a sudden? TensorFlow's official website gives the following answers:
Www.tensorflow.org/tutorials/image_recognition

It is necessary to note that TensorFlow's image recognition classification can be submitted to the server for identification and offline data recognition. Be able to put around 200M of identification data locally, and then after the submission of recognition. Now it is possible to distinguish between 1000 categories of images, which 1000 categories? The project code already contains the OH.

It is particularly easy to use TensorFlow to deal with IoT data. Not only is TensorFlow, Firebase can also use the Android things. This feature, the powerful nothing to say.

The project mentioned today comes from a project that Google maintains on GitHub, and the address of the project is
Github.com/androidthings/sample-tensorflow-imageclassifier

Of course, there are a lot of Android things code on GITHUB that can be tested.

Do you want to write an application? In fact, the project can be played in a slightly modified program. With an infrared sensor, for example, once a creature is nearby, it is immediately photographed and identified.

Open your brain hole.

Postscript

This article is the last article of the topic.

After writing the whole project, I found that Android things brought too much convenience to the developers, how to burn the files? How do I use the SDK? How do you even use other Google services to do IoT-related data processing? There are too many ready-made options for us to choose from. It's so convenient to use Android things for IOT application development!

If you have any idea of what is involved with Android things, you are welcome to leave a message below and we will transfer the good advice to the Android things product department.

Perhaps someday, your advice is part of andorid things.

Android things feature 6 complete chestnuts: using TensorFlow to parse images

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.