提交 3ff9cc93 编写于 作者: W WenmuZhou

update ui

上级 7a32e25c
...@@ -10,7 +10,9 @@ import android.content.pm.PackageManager; ...@@ -10,7 +10,9 @@ import android.content.pm.PackageManager;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.media.ExifInterface; import android.media.ExifInterface;
import android.content.res.AssetManager;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
...@@ -24,6 +26,7 @@ import android.util.Log; ...@@ -24,6 +26,7 @@ import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
...@@ -61,6 +64,7 @@ public class MainActivity extends AppCompatActivity { ...@@ -61,6 +64,7 @@ public class MainActivity extends AppCompatActivity {
// UI components of object detection // UI components of object detection
protected TextView tvInputSetting; protected TextView tvInputSetting;
protected TextView tvStatus;
protected ImageView ivInputImage; protected ImageView ivInputImage;
protected TextView tvOutputResult; protected TextView tvOutputResult;
protected TextView tvInferenceTime; protected TextView tvInferenceTime;
...@@ -77,6 +81,7 @@ public class MainActivity extends AppCompatActivity { ...@@ -77,6 +81,7 @@ public class MainActivity extends AppCompatActivity {
protected float[] inputStd = new float[]{}; protected float[] inputStd = new float[]{};
protected float scoreThreshold = 0.1f; protected float scoreThreshold = 0.1f;
private String currentPhotoPath; private String currentPhotoPath;
private AssetManager assetManager =null;
protected Predictor predictor = new Predictor(); protected Predictor predictor = new Predictor();
...@@ -89,7 +94,16 @@ public class MainActivity extends AppCompatActivity { ...@@ -89,7 +94,16 @@ public class MainActivity extends AppCompatActivity {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear(); editor.clear();
editor.commit(); editor.apply();
// Setup the UI components
tvInputSetting = findViewById(R.id.tv_input_setting);
tvStatus = findViewById(R.id.tv_model_img_status);
ivInputImage = findViewById(R.id.iv_input_image);
tvInferenceTime = findViewById(R.id.tv_inference_time);
tvOutputResult = findViewById(R.id.tv_output_result);
tvInputSetting.setMovementMethod(ScrollingMovementMethod.getInstance());
tvOutputResult.setMovementMethod(ScrollingMovementMethod.getInstance());
// Prepare the worker thread for mode loading and inference // Prepare the worker thread for mode loading and inference
receiver = new Handler() { receiver = new Handler() {
...@@ -97,20 +111,28 @@ public class MainActivity extends AppCompatActivity { ...@@ -97,20 +111,28 @@ public class MainActivity extends AppCompatActivity {
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
switch (msg.what) { switch (msg.what) {
case RESPONSE_LOAD_MODEL_SUCCESSED: case RESPONSE_LOAD_MODEL_SUCCESSED:
pbLoadModel.dismiss(); if(pbLoadModel!=null && pbLoadModel.isShowing()){
pbLoadModel.dismiss();
}
onLoadModelSuccessed(); onLoadModelSuccessed();
break; break;
case RESPONSE_LOAD_MODEL_FAILED: case RESPONSE_LOAD_MODEL_FAILED:
pbLoadModel.dismiss(); if(pbLoadModel!=null && pbLoadModel.isShowing()){
pbLoadModel.dismiss();
}
Toast.makeText(MainActivity.this, "Load model failed!", Toast.LENGTH_SHORT).show(); Toast.makeText(MainActivity.this, "Load model failed!", Toast.LENGTH_SHORT).show();
onLoadModelFailed(); onLoadModelFailed();
break; break;
case RESPONSE_RUN_MODEL_SUCCESSED: case RESPONSE_RUN_MODEL_SUCCESSED:
pbRunModel.dismiss(); if(pbRunModel!=null && pbRunModel.isShowing()){
pbRunModel.dismiss();
}
onRunModelSuccessed(); onRunModelSuccessed();
break; break;
case RESPONSE_RUN_MODEL_FAILED: case RESPONSE_RUN_MODEL_FAILED:
pbRunModel.dismiss(); if(pbRunModel!=null && pbRunModel.isShowing()){
pbRunModel.dismiss();
}
Toast.makeText(MainActivity.this, "Run model failed!", Toast.LENGTH_SHORT).show(); Toast.makeText(MainActivity.this, "Run model failed!", Toast.LENGTH_SHORT).show();
onRunModelFailed(); onRunModelFailed();
break; break;
...@@ -146,14 +168,6 @@ public class MainActivity extends AppCompatActivity { ...@@ -146,14 +168,6 @@ public class MainActivity extends AppCompatActivity {
} }
} }
}; };
// Setup the UI components
tvInputSetting = findViewById(R.id.tv_input_setting);
ivInputImage = findViewById(R.id.iv_input_image);
tvInferenceTime = findViewById(R.id.tv_inference_time);
tvOutputResult = findViewById(R.id.tv_output_result);
tvInputSetting.setMovementMethod(ScrollingMovementMethod.getInstance());
tvOutputResult.setMovementMethod(ScrollingMovementMethod.getInstance());
} }
@Override @Override
...@@ -224,17 +238,18 @@ public class MainActivity extends AppCompatActivity { ...@@ -224,17 +238,18 @@ public class MainActivity extends AppCompatActivity {
" Thread Num: " + Integer.toString(cpuThreadNum) + "\n" + "CPU Power Mode: " + cpuPowerMode); " Thread Num: " + Integer.toString(cpuThreadNum) + "\n" + "CPU Power Mode: " + cpuPowerMode);
tvInputSetting.scrollTo(0, 0); tvInputSetting.scrollTo(0, 0);
// Reload model if configure has been changed // Reload model if configure has been changed
loadModel(); // loadModel();
set_img();
} }
} }
public void loadModel() { public void loadModel() {
pbLoadModel = ProgressDialog.show(this, "", "Loading model...", false, false); pbLoadModel = ProgressDialog.show(this, "", "loading model...", false, false);
sender.sendEmptyMessage(REQUEST_LOAD_MODEL); sender.sendEmptyMessage(REQUEST_LOAD_MODEL);
} }
public void runModel() { public void runModel() {
pbRunModel = ProgressDialog.show(this, "", "Running model...", false, false); pbRunModel = ProgressDialog.show(this, "", "running model...", false, false);
sender.sendEmptyMessage(REQUEST_RUN_MODEL); sender.sendEmptyMessage(REQUEST_RUN_MODEL);
} }
...@@ -252,36 +267,15 @@ public class MainActivity extends AppCompatActivity { ...@@ -252,36 +267,15 @@ public class MainActivity extends AppCompatActivity {
public void onLoadModelSuccessed() { public void onLoadModelSuccessed() {
// Load test image from path and run model // Load test image from path and run model
try { tvStatus.setText("STATUS: load model successed");
if (imagePath.isEmpty()) {
return;
}
Bitmap image = null;
// Read test image file from custom path if the first character of mode path is '/', otherwise read test
// image file from assets
if (!imagePath.substring(0, 1).equals("/")) {
InputStream imageStream = getAssets().open(imagePath);
image = BitmapFactory.decodeStream(imageStream);
} else {
if (!new File(imagePath).exists()) {
return;
}
image = BitmapFactory.decodeFile(imagePath);
}
if (image != null && predictor.isLoaded()) {
predictor.setInputImage(image);
runModel();
}
} catch (IOException e) {
Toast.makeText(MainActivity.this, "Load image failed!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
} }
public void onLoadModelFailed() { public void onLoadModelFailed() {
tvStatus.setText("STATUS: load model failed");
} }
public void onRunModelSuccessed() { public void onRunModelSuccessed() {
tvStatus.setText("STATUS: run model successed");
// Obtain results and update UI // Obtain results and update UI
tvInferenceTime.setText("Inference time: " + predictor.inferenceTime() + " ms"); tvInferenceTime.setText("Inference time: " + predictor.inferenceTime() + " ms");
Bitmap outputImage = predictor.outputImage(); Bitmap outputImage = predictor.outputImage();
...@@ -293,6 +287,7 @@ public class MainActivity extends AppCompatActivity { ...@@ -293,6 +287,7 @@ public class MainActivity extends AppCompatActivity {
} }
public void onRunModelFailed() { public void onRunModelFailed() {
tvStatus.setText("STATUS: run model failed");
} }
public void onImageChanged(Bitmap image) { public void onImageChanged(Bitmap image) {
...@@ -303,6 +298,19 @@ public class MainActivity extends AppCompatActivity { ...@@ -303,6 +298,19 @@ public class MainActivity extends AppCompatActivity {
} }
} }
public void set_img() {
// Load test image from path and run model
try {
assetManager= getAssets();
InputStream in=assetManager.open(imagePath);
Bitmap bmp=BitmapFactory.decodeStream(in);
ivInputImage.setImageBitmap(bmp);
} catch (IOException e) {
Toast.makeText(MainActivity.this, "Load image failed!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
public void onSettingsClicked() { public void onSettingsClicked() {
startActivity(new Intent(MainActivity.this, SettingsActivity.class)); startActivity(new Intent(MainActivity.this, SettingsActivity.class));
} }
...@@ -316,8 +324,6 @@ public class MainActivity extends AppCompatActivity { ...@@ -316,8 +324,6 @@ public class MainActivity extends AppCompatActivity {
public boolean onPrepareOptionsMenu(Menu menu) { public boolean onPrepareOptionsMenu(Menu menu) {
boolean isLoaded = predictor.isLoaded(); boolean isLoaded = predictor.isLoaded();
menu.findItem(R.id.open_gallery).setEnabled(isLoaded);
menu.findItem(R.id.take_photo).setEnabled(isLoaded);
return super.onPrepareOptionsMenu(menu); return super.onPrepareOptionsMenu(menu);
} }
...@@ -327,16 +333,6 @@ public class MainActivity extends AppCompatActivity { ...@@ -327,16 +333,6 @@ public class MainActivity extends AppCompatActivity {
case android.R.id.home: case android.R.id.home:
finish(); finish();
break; break;
case R.id.open_gallery:
if (requestAllPermissions()) {
openGallery();
}
break;
case R.id.take_photo:
if (requestAllPermissions()) {
takePhoto();
}
break;
case R.id.settings: case R.id.settings:
if (requestAllPermissions()) { if (requestAllPermissions()) {
// Make sure we have SDCard r&w permissions to load model from SDCard // Make sure we have SDCard r&w permissions to load model from SDCard
...@@ -434,6 +430,9 @@ public class MainActivity extends AppCompatActivity { ...@@ -434,6 +430,9 @@ public class MainActivity extends AppCompatActivity {
Cursor cursor = managedQuery(uri, proj, null, null, null); Cursor cursor = managedQuery(uri, proj, null, null, null);
cursor.moveToFirst(); cursor.moveToFirst();
onImageChanged(image); onImageChanged(image);
if (image != null) {
ivInputImage.setImageBitmap(image);
}
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
...@@ -452,6 +451,9 @@ public class MainActivity extends AppCompatActivity { ...@@ -452,6 +451,9 @@ public class MainActivity extends AppCompatActivity {
Bitmap image = BitmapFactory.decodeFile(currentPhotoPath); Bitmap image = BitmapFactory.decodeFile(currentPhotoPath);
image = Utils.rotateBitmap(image, orientation); image = Utils.rotateBitmap(image, orientation);
onImageChanged(image); onImageChanged(image);
if (image != null) {
ivInputImage.setImageBitmap(image);
}
} else { } else {
Log.e(TAG, "currentPhotoPath is null"); Log.e(TAG, "currentPhotoPath is null");
} }
...@@ -462,6 +464,36 @@ public class MainActivity extends AppCompatActivity { ...@@ -462,6 +464,36 @@ public class MainActivity extends AppCompatActivity {
} }
} }
public void btn_load_model_click(View view) {
tvStatus.setText("STATUS: load model ......");
loadModel();
}
public void btn_run_model_click(View view) {
Bitmap image =((BitmapDrawable)ivInputImage.getDrawable()).getBitmap();
if(image == null) {
tvStatus.setText("STATUS: image is not exists");
}
else if (!predictor.isLoaded()){
tvStatus.setText("STATUS: model is not loaded");
}else{
tvStatus.setText("STATUS: run model ...... ");
predictor.setInputImage(image);
runModel();
}
}
public void btn_choice_img_click(View view) {
if (requestAllPermissions()) {
openGallery();
}
}
public void btn_take_photo_click(View view) {
if (requestAllPermissions()) {
takePhoto();
}
}
@Override @Override
protected void onDestroy() { protected void onDestroy() {
if (predictor != null) { if (predictor != null) {
......
...@@ -16,6 +16,43 @@ ...@@ -16,6 +16,43 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:layout_alignParentTop="true"
android:orientation="vertical"> android:orientation="vertical">
<LinearLayout
android:id="@+id/btn_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_load_model"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="btn_load_model_click"
android:text="加载模型" />
<Button
android:id="@+id/btn_run_model"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="btn_run_model_click"
android:text="运行模型" />
<Button
android:id="@+id/btn_take_photo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="btn_take_photo_click"
android:text="拍照识别" />
<Button
android:id="@+id/btn_choice_img"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="btn_choice_img_click"
android:text="选取图片" />
</LinearLayout>
<TextView <TextView
android:id="@+id/tv_input_setting" android:id="@+id/tv_input_setting"
android:layout_width="wrap_content" android:layout_width="wrap_content"
...@@ -29,6 +66,19 @@ ...@@ -29,6 +66,19 @@
android:singleLine="false" android:singleLine="false"
android:maxLines="6" android:maxLines="6"
android:text=""/> android:text=""/>
<TextView
android:id="@+id/tv_model_img_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="-5dp"
android:layout_marginBottom="5dp"
android:lineSpacingExtra="4dp"
android:singleLine="false"
android:maxLines="6"
android:text="STATUS: ok"/>
</LinearLayout> </LinearLayout>
...@@ -95,5 +145,4 @@ ...@@ -95,5 +145,4 @@
</RelativeLayout> </RelativeLayout>
</RelativeLayout> </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册