Skip to content

Commit

Permalink
1.Move EasyEdge Ui code to fastdeploy;
Browse files Browse the repository at this point in the history
2.Add photo recognition, real-time recognition, album selection recognition, detail page display and other functions.
  • Loading branch information
WinterGeng committed Nov 3, 2022
1 parent 9437dec commit 3fd54d6
Show file tree
Hide file tree
Showing 31 changed files with 2,247 additions and 181 deletions.
4 changes: 2 additions & 2 deletions java/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.baidu.paddle.fastdeploy.app.examples.ocr.MainActivity">
<activity android:name=".detection.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="com.baidu.paddle.fastdeploy.app.examples.ocr.SettingsActivity"
android:name=".detection.SettingsActivity"
android:label="Settings">
</activity>
</application>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,40 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.paddle.fastdeploy.RuntimeOption;
import com.baidu.paddle.fastdeploy.app.examples.R;
import com.baidu.paddle.fastdeploy.app.ui.CameraSurfaceView;
import com.baidu.paddle.fastdeploy.app.ui.Utils;
import com.baidu.paddle.fastdeploy.app.ui.view.ResultListView;
import com.baidu.paddle.fastdeploy.app.ui.view.Utils;
import com.baidu.paddle.fastdeploy.app.ui.view.adapter.DetectResultAdapter;
import com.baidu.paddle.fastdeploy.app.ui.view.model.BaseResultModel;
import com.baidu.paddle.fastdeploy.vision.DetectionResult;
import com.baidu.paddle.fastdeploy.vision.detection.PicoDet;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements View.OnClickListener, CameraSurfaceView.OnTextureChangedListener {
private static final String TAG = MainActivity.class.getSimpleName();
Expand All @@ -45,6 +55,25 @@ public class MainActivity extends Activity implements View.OnClickListener, Came
ImageView realtimeToggleButton;
boolean isRealtimeStatusRunning = false;
ImageView backInPreview;
private ImageView albumSelectButton;
private View mCameraPageView;
private ViewGroup mResultPageView;
private ImageView resultImage;
private ImageView backInResult;
private SeekBar confidenceSeekbar;
private TextView seekbarText;
private float resultNum = 1.0f;
private ResultListView detectResultView;
private Bitmap shutterBitmap;
private Bitmap originShutterBitmap;
private Bitmap picBitmap;
private Bitmap originPicBitmap;
public static final int BTN_SHUTTER = 0;
public static final int ALBUM_SELECT = 1;
private static int TYPE = BTN_SHUTTER;

private static final int REQUEST_PERMISSION_CODE_STORAGE = 101;
private static final int INTENT_CODE_PICK_IMAGE = 100;

String savedImagePath = "result.jpg";
int lastFrameIndex = 0;
Expand Down Expand Up @@ -83,12 +112,13 @@ public void onClick(View v) {
svPreview.switchCamera();
break;
case R.id.btn_shutter:
@SuppressLint("SimpleDateFormat")
SimpleDateFormat date = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
synchronized (this) {
savedImagePath = Utils.getDCIMDirectory() + File.separator + date.format(new Date()).toString() + ".png";
}
Toast.makeText(MainActivity.this, "Save snapshot to " + savedImagePath, Toast.LENGTH_SHORT).show();
TYPE = BTN_SHUTTER;
svPreview.onPause();
mCameraPageView.setVisibility(View.GONE);
mResultPageView.setVisibility(View.VISIBLE);
seekbarText.setText(resultNum + "");
confidenceSeekbar.setProgress((int) (resultNum * 100));
resultImage.setImageBitmap(shutterBitmap);
break;
case R.id.btn_settings:
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
Expand All @@ -99,9 +129,64 @@ public void onClick(View v) {
case R.id.back_in_preview:
finish();
break;
case R.id.albumSelect:
TYPE = ALBUM_SELECT;
// 判断是否已经赋予权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// 如果应用之前请求过此权限但用户拒绝了请求,此方法将返回 true。
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION_CODE_STORAGE);
} else {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, INTENT_CODE_PICK_IMAGE);
}
break;
case R.id.back_in_result:
mResultPageView.setVisibility(View.GONE);
mCameraPageView.setVisibility(View.VISIBLE);
svPreview.onResume();
break;

}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == INTENT_CODE_PICK_IMAGE) {
if (resultCode == Activity.RESULT_OK) {
mCameraPageView.setVisibility(View.GONE);
mResultPageView.setVisibility(View.VISIBLE);
seekbarText.setText(resultNum + "");
confidenceSeekbar.setProgress((int) (resultNum * 100));
Uri uri = data.getData();
String path = getRealPathFromURI(uri);
picBitmap = decodeBitmap(path, 720, 1280);
originPicBitmap = picBitmap.copy(Bitmap.Config.ARGB_8888, true);
resultImage.setImageBitmap(picBitmap);
}
}
}

private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = null;
try {
cursor = getContentResolver().query(contentURI, null, null, null, null);
} catch (Throwable e) {
e.printStackTrace();
}
if (cursor == null) {
result = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
}

private void toggleRealtimeStyle() {
if (isRealtimeStatusRunning) {
isRealtimeStatusRunning = false;
Expand All @@ -125,8 +210,10 @@ public boolean onTextureChanged(Bitmap ARGB8888ImageBitmap) {
public boolean onTextureChanged(Bitmap ARGB8888ImageBitmap) {
String savedImagePath = "";
synchronized (this) {
savedImagePath = MainActivity.this.savedImagePath;
savedImagePath = Utils.getDCIMDirectory() + File.separator + "result.png";
}
shutterBitmap = ARGB8888ImageBitmap.copy(Bitmap.Config.ARGB_8888,true);
originShutterBitmap = ARGB8888ImageBitmap.copy(Bitmap.Config.ARGB_8888,true);
boolean modified = false;
DetectionResult result = predictor.predict(
ARGB8888ImageBitmap, savedImagePath, SettingsActivity.scoreThreshold);
Expand All @@ -151,6 +238,38 @@ public void run() {
return modified;
}

/**
* @param path 路径
* @param displayWidth 需要显示的宽度
* @param displayHeight 需要显示的高度
* @return Bitmap
*/
public static Bitmap decodeBitmap(String path, int displayWidth, int displayHeight) {
BitmapFactory.Options op = new BitmapFactory.Options();
op.inJustDecodeBounds = true;
// op.inJustDecodeBounds = true;表示我们只读取Bitmap的宽高等信息,不读取像素。
Bitmap bmp = BitmapFactory.decodeFile(path, op); // 获取尺寸信息
// op.outWidth表示的是图像真实的宽度
// op.inSamplySize 表示的是缩小的比例
// op.inSamplySize = 4,表示缩小1/4的宽和高,1/16的像素,android认为设置为2是最快的。
// 获取比例大小
int wRatio = (int) Math.ceil(op.outWidth / (float) displayWidth);
int hRatio = (int) Math.ceil(op.outHeight / (float) displayHeight);
// 如果超出指定大小,则缩小相应的比例
if (wRatio > 1 && hRatio > 1) {
if (wRatio > hRatio) {
// 如果太宽,我们就缩小宽度到需要的大小,注意,高度就会变得更加的小。
op.inSampleSize = wRatio;
} else {
op.inSampleSize = hRatio;
}
}
op.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(path, op);
// 从原Bitmap创建一个给定宽高的Bitmap
return Bitmap.createScaledBitmap(bmp, displayWidth, displayHeight, true);
}

@Override
protected void onResume() {
super.onResume();
Expand Down Expand Up @@ -191,6 +310,63 @@ public void initView() {
realtimeToggleButton.setOnClickListener(this);
backInPreview = findViewById(R.id.back_in_preview);
backInPreview.setOnClickListener(this);
albumSelectButton = findViewById(R.id.albumSelect);
albumSelectButton.setOnClickListener(this);
mCameraPageView = findViewById(R.id.camera_page);
mResultPageView = findViewById(R.id.result_page);
resultImage = findViewById(R.id.result_image);
backInResult = findViewById(R.id.back_in_result);
backInResult.setOnClickListener(this);
confidenceSeekbar = findViewById(R.id.confidence_seekbar);
seekbarText = findViewById(R.id.seekbar_text);
detectResultView = findViewById(R.id.result_list_view);

List<BaseResultModel> results = new ArrayList<>();
results.add(new BaseResultModel(1, "cup", 0.4f));
results.add(new BaseResultModel(2, "pen", 0.6f));
results.add(new BaseResultModel(3, "tang", 1.0f));
final DetectResultAdapter adapter = new DetectResultAdapter(this, R.layout.result_detect_item, results);
detectResultView.setAdapter(adapter);
detectResultView.invalidate();

confidenceSeekbar.setMax(100);
confidenceSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float resultConfidence = seekBar.getProgress() / 100f;
BigDecimal bd = new BigDecimal(resultConfidence);
resultNum = bd.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
seekbarText.setText(resultNum + "");
confidenceSeekbar.setProgress((int) (resultNum * 100));
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (TYPE == ALBUM_SELECT) {
SystemClock.sleep(500);
predictor.predict(picBitmap, savedImagePath, resultNum);
resultImage.setImageBitmap(picBitmap);
picBitmap = originPicBitmap.copy(Bitmap.Config.ARGB_8888, true);
resultNum = 1.0f;
} else {
SystemClock.sleep(500);
predictor.predict(shutterBitmap, savedImagePath, resultNum);
resultImage.setImageBitmap(shutterBitmap);
shutterBitmap = originShutterBitmap.copy(Bitmap.Config.ARGB_8888, true);
resultNum = 1.0f;
}
}
});
}
});
}

@SuppressLint("ApplySharedPref")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import com.baidu.paddle.fastdeploy.app.examples.R;
import com.baidu.paddle.fastdeploy.app.ui.AppCompatPreferenceActivity;
import com.baidu.paddle.fastdeploy.app.ui.Utils;
import com.baidu.paddle.fastdeploy.app.ui.view.Utils;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -46,8 +46,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity implements
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// addPreferencesFromResource(R.xml.settings);
addPreferencesFromResource(R.xml.detection_settings);
addPreferencesFromResource(R.xml.settings);
ActionBar supportActionBar = getSupportActionBar();
if (supportActionBar != null) {
supportActionBar.setDisplayHomeAsUpEnabled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.baidu.paddle.fastdeploy.RuntimeOption;
import com.baidu.paddle.fastdeploy.app.examples.R;
import com.baidu.paddle.fastdeploy.app.ui.CameraSurfaceView;
import com.baidu.paddle.fastdeploy.app.ui.Utils;
import com.baidu.paddle.fastdeploy.app.ui.view.Utils;
import com.baidu.paddle.fastdeploy.vision.OCRResult;
import com.baidu.paddle.fastdeploy.pipeline.PPOCRv2;
import com.baidu.paddle.fastdeploy.vision.ocr.Classifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import com.baidu.paddle.fastdeploy.app.examples.R;
import com.baidu.paddle.fastdeploy.app.ui.AppCompatPreferenceActivity;
import com.baidu.paddle.fastdeploy.app.ui.Utils;
import com.baidu.paddle.fastdeploy.app.ui.view.Utils;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import android.util.AttributeSet;
import android.util.Log;

import com.baidu.paddle.fastdeploy.app.ui.view.Utils;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.baidu.paddle.fastdeploy.app.ui.camera;

import android.graphics.Bitmap;

/**
* Created by ruanshimin on 2017/11/30.
*/

public class CameraListener {
public interface CommonListener {
void onSurfaceReady();

void onSwitchCamera();
}

public interface TakePictureListener {
void onTakenPicture(Bitmap bitmap);
}
}
Loading

0 comments on commit 3fd54d6

Please sign in to comment.