Update Android Demo

master
tunmx 2 years ago
parent e679fe8727
commit 649588fb0c

@ -1,8 +1,6 @@
package com.hyperai.hyperlpr_sdk_demo; package com.hyperai.hyperlpr_sdk_demo;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Paint; import android.graphics.Paint;
import android.hardware.Camera; import android.hardware.Camera;
import android.os.Handler; import android.os.Handler;
@ -20,11 +18,10 @@ import android.view.WindowManager;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.hyperai.hyperlpr3.HyperLPR3; import com.hyperai.hyperlpr3.HyperLPR3;
import com.hyperai.hyperlpr3.bean.Parameter; import com.hyperai.hyperlpr3.bean.HyperLPRParameter;
import com.hyperai.hyperlpr3.bean.Plate; import com.hyperai.hyperlpr3.bean.Plate;
/** /**
@ -45,8 +42,6 @@ public class CameraPreviews extends SurfaceView implements SurfaceHolder.Callbac
private Context mContext; private Context mContext;
HyperLPR3 hyperLPR3;
public CameraPreviews(Context context) { public CameraPreviews(Context context) {
super(context); super(context);
mContext = context; mContext = context;
@ -57,8 +52,9 @@ public class CameraPreviews extends SurfaceView implements SurfaceHolder.Callbac
mPaint.setStyle(Paint.Style.STROKE); mPaint.setStyle(Paint.Style.STROKE);
// mPaint.setColor(ContextCompat.getColor(context, R.color.colorAccent)); // mPaint.setColor(ContextCompat.getColor(context, R.color.colorAccent));
Parameter parameter = new Parameter(); HyperLPRParameter parameter = new HyperLPRParameter();
hyperLPR3 = new HyperLPR3(mContext, parameter); // hyperLPR3 = new HyperLPR3();
// hyperLPR3.init(mContext, parameter);
} }
public Camera getCameraInstance(){ public Camera getCameraInstance(){
if (mCamera == null){ if (mCamera == null){
@ -135,16 +131,12 @@ public class CameraPreviews extends SurfaceView implements SurfaceHolder.Callbac
synchronized (lock){ synchronized (lock){
// 处理data // 处理data
Camera.Size previewSize = camera.getParameters().getPreviewSize(); Camera.Size previewSize = camera.getParameters().getPreviewSize();
Plate[] plates = HyperLPR3.getInstance().plateRecognition(data, previewSize.height, previewSize.width, HyperLPR3.CAMERA_ROTATION_270, HyperLPR3.STREAM_YUV_NV21);
// String save_path = mContext.getExternalFilesDir(null).getAbsolutePath() + "/" + "a.jpg";
// core.testBuffer(save_path, data, previewSize.height, previewSize.width, 3);
Plate[] plates = hyperLPR3.plateRecognition(data, previewSize.height, previewSize.width, HyperLPR3.CAMERA_ROTATION_270, HyperLPR3.STREAM_YUV_NV21);
for (Plate plate : plates) { for (Plate plate : plates) {
Log.i(TAG, "" + plate.toString()); Log.i(TAG, "" + plate.toString());
} }
if(!isStopReg && plates.length > 0) { if(!isStopReg && plates.length > 0) {
// isStopReg = true; // isStopReg = true;
sendPlate(plates); sendPlate(plates);
} }

@ -19,7 +19,7 @@ import android.widget.TextView;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.hyperai.hyperlpr3.HyperLPR3; import com.hyperai.hyperlpr3.HyperLPR3;
import com.hyperai.hyperlpr3.bean.Parameter; import com.hyperai.hyperlpr3.bean.HyperLPRParameter;
import com.hyperai.hyperlpr3.bean.Plate; import com.hyperai.hyperlpr3.bean.Plate;
import com.yuyh.library.imgsel.ISNav; import com.yuyh.library.imgsel.ISNav;
import com.yuyh.library.imgsel.common.ImageLoader; import com.yuyh.library.imgsel.common.ImageLoader;
@ -42,8 +42,6 @@ public class MainActivity extends AppCompatActivity {
private TextView mResult; private TextView mResult;
HyperLPR3 hyperLPR3;
private static final int REQUEST_EXTERNAL_STORAGE = 1; private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static final String[] PERMISSIONS_STORAGE = { private static final String[] PERMISSIONS_STORAGE = {
@ -81,8 +79,13 @@ public class MainActivity extends AppCompatActivity {
verifyStoragePermissions(this); verifyStoragePermissions(this);
Parameter parameter = new Parameter(); // 车牌识别算法配置参数
hyperLPR3 = new HyperLPR3(mCtx, parameter); HyperLPRParameter parameter = new HyperLPRParameter()
.setDetLevel(HyperLPR3.DETECT_LEVEL_LOW)
.setMaxNum(1)
.setRecConfidenceThreshold(0.85f);
// 初始化(仅执行一次生效)
HyperLPR3.getInstance().init(this, parameter);
ISNav.getInstance().init(new ImageLoader() { ISNav.getInstance().init(new ImageLoader() {
@Override @Override
@ -158,7 +161,7 @@ public class MainActivity extends AppCompatActivity {
if (bitmap != null) { if (bitmap != null) {
imageView.setImageBitmap(bitmap); imageView.setImageBitmap(bitmap);
Plate[] plates = hyperLPR3.plateRecognition(bitmap, HyperLPR3.CAMERA_ROTATION_0, HyperLPR3.STREAM_BGRA); Plate[] plates = HyperLPR3.getInstance().plateRecognition(bitmap, HyperLPR3.CAMERA_ROTATION_0, HyperLPR3.STREAM_BGRA);
for (Plate plate: plates) { for (Plate plate: plates) {
String type = "未知车牌"; String type = "未知车牌";
if (plate.getType() != HyperLPR3.PLATE_TYPE_UNKNOWN) { if (plate.getType() != HyperLPR3.PLATE_TYPE_UNKNOWN) {

@ -5,7 +5,7 @@ import android.graphics.Bitmap;
import android.util.Log; import android.util.Log;
import com.hyperai.hyperlpr3.api.APIDefine; import com.hyperai.hyperlpr3.api.APIDefine;
import com.hyperai.hyperlpr3.bean.Parameter; import com.hyperai.hyperlpr3.bean.HyperLPRParameter;
import com.hyperai.hyperlpr3.bean.Plate; import com.hyperai.hyperlpr3.bean.Plate;
import com.hyperai.hyperlpr3.core.HyperLPRCore; import com.hyperai.hyperlpr3.core.HyperLPRCore;
import com.hyperai.hyperlpr3.settings.TypeDefine; import com.hyperai.hyperlpr3.settings.TypeDefine;
@ -14,28 +14,26 @@ import com.hyperai.hyperlpr3.utils.SDKUtils;
public class HyperLPR3 extends TypeDefine implements APIDefine { public class HyperLPR3 extends TypeDefine implements APIDefine {
private final String TAG = "HyperLPR3"; private final String TAG = "HyperLPR3-SDK";
private final HyperLPRCore mCore; private final HyperLPRCore mCore;
private final Context mContext; private boolean isInitSuccess;
public HyperLPR3(Context mContext, Parameter parameter) { private HyperLPR3() {
this.mContext = mContext;
String mResourceFolderPath = mContext.getExternalFilesDir(null).getAbsolutePath() + "/";
SDKUtils.copyFilesFromAssets(mContext, SDKConfig.packDirName, mResourceFolderPath);
Log.i(TAG, "resource: " + mResourceFolderPath);
if (parameter.getModelPath() == null || "".equals(parameter.getModelPath())) {
parameter.setModelPath(mResourceFolderPath);
}
mCore = new HyperLPRCore(); mCore = new HyperLPRCore();
mCore.createRecognizerContext(parameter); isInitSuccess = false;
}
private static class LazyHolder {
private static final HyperLPR3 INSTANCE = new HyperLPR3();
} }
public Context getContext() { public static final HyperLPR3 getInstance() {
return mContext; return LazyHolder.INSTANCE;
} }
public void release() { public void release() {
mCore.release(); mCore.release();
} }
@ -46,6 +44,26 @@ public class HyperLPR3 extends TypeDefine implements APIDefine {
release(); release();
} }
/**
* Initialize the license plate recognition algorithm SDK
*
* @param context context
* @param parameter Initialization parameter
*/
@Override
public void init(Context context, HyperLPRParameter parameter) {
if (!isInitSuccess) {
String mResourceFolderPath = context.getExternalFilesDir(null).getAbsolutePath() + "/";
SDKUtils.copyFilesFromAssets(context, SDKConfig.packDirName, mResourceFolderPath);
Log.i(TAG, "resource: " + mResourceFolderPath);
if (parameter.getModelPath() == null || "".equals(parameter.getModelPath())) {
parameter.setModelPath(mResourceFolderPath);
}
mCore.createRecognizerContext(parameter);
isInitSuccess = true;
}
}
/** /**
* License plate recognition interface. * License plate recognition interface.
* *
@ -58,6 +76,10 @@ public class HyperLPR3 extends TypeDefine implements APIDefine {
*/ */
@Override @Override
public Plate[] plateRecognition(byte[] buf, int height, int width, int rotation, int format) { public Plate[] plateRecognition(byte[] buf, int height, int width, int rotation, int format) {
if (!isInitSuccess) {
Log.e(TAG, "HyperLPR3 is uninitialized.");
return new Plate[0];
}
return mCore.plateRecognitionFromBuffer(buf, height, width, rotation, format); return mCore.plateRecognitionFromBuffer(buf, height, width, rotation, format);
} }
@ -71,6 +93,10 @@ public class HyperLPR3 extends TypeDefine implements APIDefine {
*/ */
@Override @Override
public Plate[] plateRecognition(Bitmap image, int rotation, int format) { public Plate[] plateRecognition(Bitmap image, int rotation, int format) {
if (!isInitSuccess) {
Log.e(TAG, "HyperLPR3 is uninitialized.");
return new Plate[0];
}
int mWidth = image.getWidth(); int mWidth = image.getWidth();
int mHeight = image.getHeight(); int mHeight = image.getHeight();
int[] data = new int[image.getWidth() * image.getHeight()]; int[] data = new int[image.getWidth() * image.getHeight()];

@ -1,13 +1,24 @@
package com.hyperai.hyperlpr3.api; package com.hyperai.hyperlpr3.api;
import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import com.hyperai.hyperlpr3.bean.HyperLPRParameter;
import com.hyperai.hyperlpr3.bean.Plate; import com.hyperai.hyperlpr3.bean.Plate;
public interface APIDefine { public interface APIDefine {
/**
* Initialize the license plate recognition algorithm SDK
*
* @param context context
* @param parameter Initialization parameter
* */
void init(Context context, HyperLPRParameter parameter);
/** /**
* License plate recognition interface. * License plate recognition interface.
*
* @param buf Image data buffer. * @param buf Image data buffer.
* @param height Height of the image * @param height Height of the image
* @param width Width of the image * @param width Width of the image
@ -19,6 +30,7 @@ public interface APIDefine {
/** /**
* License plate recognition interface. * License plate recognition interface.
*
* @param image Bitmap image * @param image Bitmap image
* @param rotation Original data buffer rotation Angle * @param rotation Original data buffer rotation Angle
* @param format Buffer data coded format * @param format Buffer data coded format

@ -2,7 +2,7 @@ package com.hyperai.hyperlpr3.bean;
import com.hyperai.hyperlpr3.settings.TypeDefine; import com.hyperai.hyperlpr3.settings.TypeDefine;
public class Parameter { public class HyperLPRParameter {
private String modelPath; private String modelPath;
@ -14,76 +14,86 @@ public class Parameter {
private float nmsThreshold = 0.6f; private float nmsThreshold = 0.6f;
private float recConfidenceThreshold = 0.75f; private float recConfidenceThreshold = 0.85f;
private int detLevel = TypeDefine.DETECT_LEVEL_LOW; private int detLevel = TypeDefine.DETECT_LEVEL_LOW;
private int maxNum = 3; private int maxNum = 3;
public Parameter() { public HyperLPRParameter() {
} }
public int getMaxNum() { public int getMaxNum() {
return maxNum; return maxNum;
} }
public void setMaxNum(int maxNum) { public HyperLPRParameter setMaxNum(int maxNum) {
this.maxNum = maxNum; this.maxNum = maxNum;
return this;
} }
public String getModelPath() { public String getModelPath() {
return modelPath; return modelPath;
} }
public void setModelPath(String modelPath) { public HyperLPRParameter setModelPath(String modelPath) {
this.modelPath = modelPath; this.modelPath = modelPath;
return this;
} }
public int getThreads() { public int getThreads() {
return threads; return threads;
} }
public void setThreads(int threads) { public HyperLPRParameter setThreads(int threads) {
this.threads = threads; this.threads = threads;
return this;
} }
public boolean isUseHalf() { public boolean isUseHalf() {
return useHalf; return useHalf;
} }
public void setUseHalf(boolean useHalf) { public HyperLPRParameter setUseHalf(boolean useHalf) {
this.useHalf = useHalf; this.useHalf = useHalf;
return this;
} }
public float getBoxConfThreshold() { public float getBoxConfThreshold() {
return boxConfThreshold; return boxConfThreshold;
} }
public void setBoxConfThreshold(float boxConfThreshold) { public HyperLPRParameter setBoxConfThreshold(float boxConfThreshold) {
this.boxConfThreshold = boxConfThreshold; this.boxConfThreshold = boxConfThreshold;
return this;
} }
public float getNmsThreshold() { public float getNmsThreshold() {
return nmsThreshold; return nmsThreshold;
} }
public void setNmsThreshold(float nmsThreshold) { public HyperLPRParameter setNmsThreshold(float nmsThreshold) {
this.nmsThreshold = nmsThreshold; this.nmsThreshold = nmsThreshold;
return this;
} }
public float getRecConfidenceThreshold() { public float getRecConfidenceThreshold() {
return recConfidenceThreshold; return recConfidenceThreshold;
} }
public void setRecConfidenceThreshold(float recConfidenceThreshold) { public HyperLPRParameter setRecConfidenceThreshold(float recConfidenceThreshold) {
this.recConfidenceThreshold = recConfidenceThreshold; this.recConfidenceThreshold = recConfidenceThreshold;
return this;
} }
public int getDetLevel() { public int getDetLevel() {
return detLevel; return detLevel;
} }
public void setDetLevel(int detLevel) { public HyperLPRParameter setDetLevel(int detLevel) {
this.detLevel = detLevel; this.detLevel = detLevel;
return this;
} }
} }

@ -2,7 +2,7 @@ package com.hyperai.hyperlpr3.core;
import android.util.Log; import android.util.Log;
import com.hyperai.hyperlpr3.bean.Parameter; import com.hyperai.hyperlpr3.bean.HyperLPRParameter;
import com.hyperai.hyperlpr3.bean.Plate; import com.hyperai.hyperlpr3.bean.Plate;
public class HyperLPRCore { public class HyperLPRCore {
@ -17,11 +17,7 @@ public class HyperLPRCore {
private boolean isRunning_; private boolean isRunning_;
// public void testBuffer(String savePath, byte[] buf, int height, int width, int rotation) { public void createRecognizerContext(HyperLPRParameter parameter) {
// TestBuffer(savePath, buf, height, width, rotation);
// }
public void createRecognizerContext(Parameter parameter) {
ctxHandle_ = CreateRecognizerContext(parameter); ctxHandle_ = CreateRecognizerContext(parameter);
Log.i(TAG, "HANDLE: " + ctxHandle_); Log.i(TAG, "HANDLE: " + ctxHandle_);
isRunning_ = true; isRunning_ = true;
@ -49,7 +45,7 @@ public class HyperLPRCore {
// native void TestBuffer(String savePath, byte[] buf, int height, int width, int rotation); // native void TestBuffer(String savePath, byte[] buf, int height, int width, int rotation);
native long CreateRecognizerContext(Parameter parameterObj); native long CreateRecognizerContext(HyperLPRParameter parameterObj);
native int ReleaseRecognizerContext(long handle); native int ReleaseRecognizerContext(long handle);

@ -0,0 +1,6 @@
#pragma once
#define HYPERLPR_VER_MAJOR 3
#define HYPERLPR_VER_MINOR 0
#define HYPERLPR_VER_PATCH 1
Loading…
Cancel
Save