Android Studio4.1实现实时预测

导入tflite模型

代码

AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/goodsview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:textColor="#F8044B"
app:layout_constraintBottom_toBottomOf="@+id/surfaceView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;
import com.example.usetflite.ml.efficientnetLite4Fp322;
import org.json.JSONException;
import org.json.JSONObject;
import org.tensorflow.lite.support.image.TensorImage;
import org.tensorflow.lite.support.label.Category;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class MainActivity extends AppCompatActivity {
private SurfaceView surfaceview;
private SurfaceHolder surfaceholder;
private TextView goodsview;
private Camera camera;
private String GoodTitle = null;

private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.INTERNET,
Manifest.permission.CAMERA
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();//隐藏标题栏

applypermission();
initView();

camera = Camera.open();

surfaceholder = surfaceview.getHolder();//获取对接数据流的接口
surfaceholder.addCallback(surfaceholdercallback); //添加回调函数


//设置surfaceholder不维护缓冲
surfaceholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
camera.addCallbackBuffer(new byte[4718592]);
camera.getParameters().setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
camera.setPreviewCallbackWithBuffer(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] bytes, Camera camera) {
Camera.Size size = camera.getParameters().getPreviewSize();
camera.addCallbackBuffer(bytes);


YuvImage img = new YuvImage(bytes, ImageFormat.NV21,size.width,size.height,null);
if (img != null){
try {
efficientnetLite4Fp322 model = efficientnetLite4Fp322.newInstance(MainActivity.this);

//生成图片bitmap
ByteArrayOutputStream stream = new ByteArrayOutputStream();
img.compressToJpeg(new Rect(0, 0, size.width, size.height), 80, stream);
Bitmap bmp = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());

// Creates inputs for reference.
TensorImage image = TensorImage.fromBitmap(bmp);

// Runs model inference and gets result.
efficientnetLite4Fp322.Outputs outputs = model.process(image);
List<Category> probability = outputs.getProbabilityAsCategoryList();


int index = 0;
for(int i = 1;i < probability.size();i++){
if(probability.get(i).getScore() > probability.get(index).getScore()){
index = i;
}
}
GoodTitle = probability.get(index).getLabel();
goodsview.setText(GoodTitle+"("+Translate(GoodTitle)+")"+"-->"+probability.get(index).getScore()+"%");




// Releases model resources if no longer used.
model.close();
} catch (IOException e) {
// TODO Handle the exception
}

}


}

});



}

public void initView(){
surfaceview = (SurfaceView)findViewById(R.id.surfaceView);
goodsview = (TextView)findViewById(R.id.goodsview);
}



SurfaceHolder.Callback surfaceholdercallback = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {

try {
camera.setPreviewDisplay(surfaceholder);//设置用于显示浏览的surfaceview
android.hardware.Camera.Parameters parameters = camera.getParameters();//获取相机参数
parameters.setPictureFormat(PixelFormat.JPEG);//指定图片为jpg格式
parameters.set("jpeg-quality",80);//设置图片质量
camera.setParameters(parameters);//重新设置相机参数
camera.startPreview();//开始浏览
camera.autoFocus(null);//设置自动对焦
camera.setDisplayOrientation(90);
}catch (Exception e){
e.printStackTrace();
}
}

@Override
public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

}

@Override
public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {

}
};


public String HttpGet(final String url) {//url为网站地址
final StringBuilder sb = new StringBuilder();
FutureTask<String> task = new FutureTask<String>(new Callable<String>() {
@Override
public String call() throws Exception {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL requestUrl = new URL(url);
connection = (HttpURLConnection) requestUrl.openConnection();//建立连接
connection.setRequestMethod("GET");//get请求
connection.setConnectTimeout(8000);//连接超时设置
connection.setReadTimeout(8000);//读取超时
if (connection.getResponseCode() == 200) {
InputStream in = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));//编码utf-8
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
if (connection != null) {
connection.disconnect();
}
}
return sb.toString();
}
});
new Thread(task).start();
String s = null;
try {
s = task.get();
} catch (Exception e) {
e.printStackTrace();
}
return s;
}



//有道接口实现翻译
public String Translate(String translatetxt) {
String result = null;
String html = HttpGet("http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&i="+translatetxt+"&from=AUTO&to=AUTO&smartresult=dict&client=fanyideskweb&salt=16090402595894&sign=8065527b064c3bafc0699115a37aa5ac&lts=1609040259589&bv=02edb5d6c6ac4286cd4393133e5aab14&doctype=json&version=2.1&keyfrom=fanyi.web&action=FY_BY_REALTlME");
try {
JSONObject json = new JSONObject(html);
result = json.getJSONArray("translateResult").getJSONArray(0).getJSONObject(0).getString("tgt");

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}




//定义判断权限申请的函数,在onCreat中调用就行
public void applypermission(){
if(Build.VERSION.SDK_INT>=23){
boolean needapply=false;
for(int i=0;i<PERMISSIONS_STORAGE.length;i++){
int chechpermission= ContextCompat.checkSelfPermission(getApplicationContext(),
PERMISSIONS_STORAGE[i]);
if(chechpermission!= PackageManager.PERMISSION_GRANTED){
needapply=true;
}
}
if(needapply){
ActivityCompat.requestPermissions(this,PERMISSIONS_STORAGE,1);
}
}
}
}

ps: get请求有些机型用不了。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇