HsBaseThread继承Thread的一个子线程,负责与角蜂鸟通信,开发者可根据自己需求继承此类去作功能扩展。
返回值 | 函数名 | 描述 |
---|---|---|
int | allocateGraph(String filename) | 分配卷积神经网络模型到角蜂鸟,传入文件路径 |
int | allocateGraphByAssets(String filename) | 分配卷积神经网络模型到角蜂鸟,传入文件在assets下路径 |
byte[] | getImage(float std,float mean,int id) | 获取某graph对应的原始图像 |
byte[] | deviceGetImage() | 获取设备图像 |
void | setZoom(boolean zoom) | 设置摄像头分辨率 |
int | loadTensor(float[] data,int length,int id) | 角蜂鸟加载预处理后的图像数据 |
int | loadTensor(byte[] data,int length,int id) | 角蜂鸟加载经过预处理后的数据 |
byte[] | getResult(int id) | 获取返回结果 |
int | openDevice() | 打开设备 |
void | closeDevice() | 关闭设备 |
int id=allocateGraph(Environment.getExternalStorageDirectory().getAbsolutePath() + "/hs/" + "graph_face_SSD");
if(id<0)//异常状态
else //graphId
int id = allocateGraphByAssets("graph_face_SSD");
返回 return:返回 byte[] ,表示该图像的原始数据
如果参数zoom为true: byte[]大小为640*360*3,得到bgr的图像
如果参数zoom为false: byte[]大小为1920*1080*3 rgb图像顺序排列,需要作对应转化才能显示正确,转化代码如下:
byte[] bytes = mHsApi.getImage(STD, MEAN,0);
int FRAME_W = 1920;
int FRAME_H = 1080;
byte[] bytes_rgb = new byte[FRAME_W * FRAME_H * 3];
for (int i = 0; i < FRAME_H * FRAME_W; i++) {
bytes_rgb[i * 3 + 2] = bytes[i];//r
bytes_rgb[i * 3 + 1] = bytes[FRAME_W * FRAME_H + i];//g
bytes_rgb[i * 3] = bytes[FRAME_W * FRAME_H * 2 + i];//b
}
opencv_core.IplImage bgrImage = opencv_core.IplImage.create(FRAME_W, FRAME_H, opencv_core.IPL_DEPTH_8U, 3);
bgrImage.getByteBuffer().put(bytes_rgb);
该方法重载,可根据开发者使用参数不同选择对应函数
参数 Parameters:
返回 return:返回该函数执行后状态
Example:此处采用hello2018的示例代码
for (int i = 1; i < 5; i++) {
int[] ints = new int[28 * 28];
try {
InputStream inputStream = mActivity.getAssets().open("hello/" + i + ".jpg");
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
bitmap.getPixels(ints, 0, 28, 0, 0, 28, 28);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
float[] float_tensor = new float[28 * 28];
for (int j = 0; j < 28 * 28; j++) {
float_tensor[j] = Color.red(ints[j]) * 0.007843f - 1;
}
int status_load = mHsApi.loadTensor(float_tensor, float_tensor.length,0);
}
float[] result = mHsApi.getResult(0);
返回 return:返回float[] 根据使用神经网络不同,返回数组长度和解析也不尽相同
//结果处理
int num = (int) floats[0];//第一个数为检测到的个数
if (num > 0) {
for (int i = 0; i < num; i++) {
int type = (int) (floats[7 * (i + 1) + 1]);//类别
int x1 = (int) (floats[7 * (i + 1) + 3] * FRAME_W);
int y1 = (int) (floats[7 * (i + 1) + 4] * FRAME_H);
int x2 = (int) (floats[7 * (i + 1) + 5] * FRAME_W);
int y2 = (int) (floats[7 * (i + 1) + 6] * FRAME_H);
int wight = x2 - x1;
int height = y2 - y1;
//如果有值不满足条件,这组数据干掉
int percentage = (int) (floats[7 * (i + 1) + 2] * 100);//置信度
if (percentage <= MIN_SCORE_PERCENT) {
continue;
}
if (wight >= FRAME_W * 0.8 || height >= FRAME_H * 0.8) {
continue;
}
if (x1 < 0 || x2 < 0 || y1 < 0 || y2 < 0 || wight < 0 || height < 0) {
continue;
}
}
}
该类为初始化角蜂鸟设备,定义几个抽象回调函数用于各种行为处理
返回值 | 函数名 | 描述 |
---|---|---|
void | openSucceed(UsbDevice usbDevice) | 搜索到角蜂鸟设备,并连接成功 |
void | openFailed() | 角蜂鸟连接失败或未授予权限 |
void | disConnected() | 断开角蜂鸟连接 |
@Override
public void openSucceed(UsbDevice usbDevice) {
mTvTip.setVisibility(View.GONE);
mHsThread = new FaceDetectionThread(FaceDetectorActivity.this, usbDevice, mHandler);
mHsThread.start();
}
@Override
public void openFailed() {
mTvTip.setText("请重新插拔角蜂鸟允许权限");
}
@Override
public void disConnected() {
Toast.makeText(this, "断开连接", Toast.LENGTH_SHORT).show();
if (mHsThread != null) {
mHsThread.close();
}
}