Tensorflow lite 1----mobile terminal deployment--object detection official course hand-in-hand tutorial

Too many problems occurred during the configuration process. And because I am a novice, I have gone through a lot of detours from the beginning of contact with tflite to the successful layout. Here is mainly to write down the problems I encountered. and detailed procedures. Help novices like me. (Please do not reprint)

This document is suitable for newbies

1. Download the official history:

https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/android

If you can't open it, you may need to go over the wall. Or try this:

Baidu search: tensorflow lite click in, and then click as follows:

2. Environment configuration:

I am using android studio

I also used android studio 3.5.2, without success, I finally used the 4.0 version, the installation tutorial can be similar to Baidu.

3. Import model (key)

3.1 Open android studio-> then click open

3.2 Find the official history folder, which I correspond to:

G:\example\examples-master\examples-master\lite\examples\object_detection\android

 

 Click ok, that's it.

Then you will find that the download is in the lower right corner. If you wait for it to finish, it will succeed. Fortunately:

Then there is no error reported here, so it is lucky. But generally it feels wrong. If it is wrong, just deal with it like this:

It's very strange, when I did it for the first time, I kept reporting an error, but it didn't report an error the second time. But for fear that Xiaobai will not know it, here is a little mention. The general reason for the error is that gradle needs to go over the wall to download. After all, the download speed is too slow, so the error is reported.

Error:Connection timed out: connect

3.2.1 First click:

 Then the corresponding version:

Then enter: Gradle Distributions to download the corresponding version.

3.2.2 After downloading, don't unzip it yet. Go to the folder:

C:\Users\LENOVO\.gradle\wrapper\dists\gradle-6.8.3-bin\7ykxq50lst7lb7wx1nijpicxn

The only difference we may have is: where are the users and the final garbled characters. This gibberish is randomly generated, don't worry about it. into the file, you'll see:

 Delete the following two files:

 Only keep:

Then put the corresponding version you just downloaded and unzip it:

 Then restart Android or click:

 Finally, this problem is solved.

3.3, on the basis of the above, click build->make project:

 3.3.1 Here, it is easy to report such an error:

Connection timed out: connect. If you are behind an HTTP proxy,please configure the proxy settings either in IDE or Gradle.

In fact, error reporting is also very simple. Since this is an official demo, what we just downloaded is the official source code. When building the project, it will download the corresponding tflite and label.txt files we need, but still the same sentence, we need Over the wall, I will still report an error if I over the wall. I have been troubled by this problem for a long time, and finally modify download_model.gradle:

Corresponding to: \app\download_model.gradle

task downloadModelFile(type: Download) {
    src 'https://tfhub.dev/tensorflow/lite-model/ssd_mobilenet_v1/1/metadata/2?lite-format=tflite'
    dest project.ext.ASSET_DIR + '/detect.tflite'
    overwrite false
}

preBuild.dependsOn downloadModelFile
src 'https://tfhub.dev/tensorflow/lite-model/ssd_mobilenet_v1/1/metadata/2?lite-format=tflite'

Change to (reference: Connect to tfhub.dev:443 [tfhub.dev/216.58.200.238] failed problem solving_sinat_29217765's blog-CSDN blog_tfhub.dev ):

  1. src 'https://hub.tensorflow.google.cn/tensorflow/lite-model/ssd_mobilenet_v1/1/metadata/1?lite-format=tflite&hl=zh_cn'

Then run 3.3 and report an error:

Unknown host '不知道这样的主机。 (www.gstaticcnapps.cn)'. You may need to adjust the proxy settings in Gradle.
Learn about configuring HTTP proxies in Gradle

 Eventually changed to:

https://hub.tensorflow.google.cn/tensorflow/lite-model/ssd_mobilenet_v1/1/metadata/1

Then run 3.3, success! You will find that there are more detect.tflite and labelmap.txt files in the assent folder:

Finally, success:

 3.3 Click ran, and then report the error as follows:

 You need to configure a virtual phone, or link your own phone to Android.

To link your own mobile phone, you can refer to: How does Android studio connect to a mobile phone_hasfhaiogheiohf's blog-CSDN blog_android studio connects to a mobile phone

Here is how to link virtual phones:

First of all, know your corresponding version, the demo requires 30 ( after the build is successful here, all .gradle is placed together ):

 then click:

 Here you can choose the virtual phone by yourself. If you don’t know how to choose and are afraid of reporting errors, you can refer to:

 Select, click next,: then find the corresponding version, click to download:

 It is relatively large here, and it takes a long time to download.

 When the download is complete, select:

 Then configure the following conditions:

 Finally click run.

In the end, you may not recognize anything, and then go back and click

 It was found that there was an error: The model is not a valid Flatbuffer buffer

E/tflite: The model is not a valid Flatbuffer buffer
E/TaskJniUtils: Error getting native address of native library: task_vision_jni
    java.lang.IllegalArgumentException: Error occurred when initializing ObjectDetector: The model is not a valid Flatbuffer buffer
        at org.tensorflow.lite.task.vision.detector.ObjectDetector.initJniWithByteBuffer(Native Method)
        at org.tensorflow.lite.task.vision.detector.ObjectDetector.access$100(ObjectDetector.java:88)
        at org.tensorflow.lite.task.vision.detector.ObjectDetector$3.createHandle(ObjectDetector.java:223)
        at org.tensorflow.lite.task.core.TaskJniUtils.createHandleFromLibrary(TaskJniUtils.java:91)
        at org.tensorflow.lite.task.vision.detector.ObjectDetector.createFromBufferAndOptions(ObjectDetector.java:219)
        at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.<init>(TFLiteObjectDetectionAPIModel.java:88)
        at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.create(TFLiteObjectDetectionAPIModel.java:82)
        at org.tensorflow.lite.examples.detection.DetectorActivity.onPreviewSizeChosen(DetectorActivity.java:99)
        at org.tensorflow.lite.examples.detection.CameraActivity.onPreviewFrame(CameraActivity.java:200)
        at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1227)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/tensorflow: CameraActivity: Exception!
    java.lang.IllegalStateException: Error getting native address of native library: task_vision_jni
        at org.tensorflow.lite.task.core.TaskJniUtils.createHandleFromLibrary(TaskJniUtils.java:95)
        at org.tensorflow.lite.task.vision.detector.ObjectDetector.createFromBufferAndOptions(ObjectDetector.java:219)
        at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.<init>(TFLiteObjectDetectionAPIModel.java:88)
        at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.create(TFLiteObjectDetectionAPIModel.java:82)
        at org.tensorflow.lite.examples.detection.DetectorActivity.onPreviewSizeChosen(DetectorActivity.java:99)
        at org.tensorflow.lite.examples.detection.CameraActivity.onPreviewFrame(CameraActivity.java:200)
        at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1227)

The reason for the error here is: there is a problem with the detect.tflite we just downloaded. Here is an official tlite version for download:

链接: https://pan.baidu.com/s/1tP1hFljPSnKpNLOf7SDMsw 提取码: fs2t 复制这段内容后打开百度网盘手机App,操作更方便哦

Then put it under the corresponding asking price, and then click run to run it. no problem! properly

will end up with:

(Move the watch into the frame of the mobile phone, then click ALT, move the mouse, and the field of view will go to) 

4. Summary

This is an implementation of the official process. for reference only. If you have any questions, you can mention them below, and I will reply in time when I see the news.

Guess you like

Origin blog.csdn.net/qq_40214464/article/details/122414407