Item recognition of AndroidThings

1. Add artificial intelligence framework

1.

TensorFlow is an open source library for machine learning and deep neural networks, and TensorFlow Lite is TensorFlow 's lightweight solution for mobile and embedded devices. So first we put the library dependencies into the build.gradle file and resync the Gradle project.


Call TensorFlowLite in the main program , open ImageClassifierActivity.java and add some code to the program.

First add the action of initializing TensorFlow Interpreter from the model file in assets , and use the loadModelFile() method to assist in reading the file; use the readLabels() method to read the list of labels and associate the neural network results with the identified ones. name or category;

2.

••

After completing the above operations, the doRecognize () method needs to be executed, and this method is called when the user needs to recognize the photo. It takes the image as a parameter and runs the callback method of onPhotoRecognitionReady and contains a list of recognition items describing what the input image recognizes and the confidence level of each result;

2. Add a camera

We need to add a part of code to realize the camera to take pictures of the external environment. After this operation, you will be able to point the camera at an object, and the photos obtained by taking pictures of the object will be recognized by the operation of the Raspberry Pi in the background. The accuracy of photographing objects is the same.

Permission to add cameras

Just like compiling an Android program, when we need to call the interface to the camera, we need to set user permissions in AndroidManifest.xml . But unlike the Android program, when you add the camera permission, when you call the camera application later, the user will no longer be asked whether to enable the camera permission in things .

update code

In the main program, we need to add code to realize the connection of the camera and the function of taking pictures. Open ImageClassifierActivity and add in the program:

1. Add the relevant variables of the camera;

2. Update the initCamera() method so that it can initialize the ImagePreprocessor and CameraHandler objects ;

3. OnImageAvailableListener is called when the camera's image is ready . This listener will call the ImagePreprocessor.preprocessImage() method and pass the processed bitmap to onPhoteReady() , which will re-pass the bitmap back to the image recognition method described in the previous section.

4. Pass the new listener to CameraHandler.initializeCamera() .

5. Finally, call closeCamera() and loadPhoto() to close the camera and trigger CameraHandler .

6. After completing the above operations, you can install the program into our development board to verify the program we designed. When you press the button, the camera will recognize the captured object and prompt the recognition on the screen. Object name.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324848803&siteId=291194637