HsBaseThread is a thread for HornedSungem interacion, developers can extend this class for function expansion.
Return | Function name | Overview |
---|---|---|
int | allocateGraph(String filename) | This function is used to create an instance of a Graph that represents a neural network, which can be used to infer results via methods loadTensor() and getResult(). |
int | allocateGraphByAssets(String filename) | This function is used to create an instance of a Graph that represents a neural network,which under the assets package |
byte[] | getImage(float std,float mean,int id) | This function is to get the graph image |
byte[] | deviceGetImage() | This function is to get the device image |
void | setZoom(boolean zoom) | This function set the device camera parameter |
int | loadTensor(float[] data,int length,int id) | This function initiates an inference on the specified graph via the associated Horned Sungem device. After calling this function, use the getResult() function to retrieve the inference result. |
int | loadTensor(byte[] data,int length,int id) | This function initiates an inference on the specified graph via the associated Horned Sungem device. After calling this function, use the getResult() function to retrieve the inference result. |
byte[] | getResult(int id) | This function retrieves the result of an inference that was initiated via loadTensor() or getImage() on the specified graph. |
int | openDevice() | Open the device |
void | closeDevice() | Turn off the device |
int id = allocateGraph(Environment.getExternalStorageDirectory().getAbsolutePath() + "/hs/" + "graph_face_SSD");
int id = allocateGraphByAssets("graph_face_SSD");
Return: The return value represents the raw data of the image
If zoom parameter is set to true, the return value size is 640*360*3
If zoom parameter is set to false, the return value size is 1920*1080*3, The rgb images are arranged in order and need to be converted to display correctly. The conversion code is as follows:
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);
According to the developer’s needs, use the parameters to select the corresponding function
Parameters :
Return : Returns the state after the function is executed
Example : The code is taken at 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 : Depending on the neural network, the return array length and resolution are different.
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;
}
}
}
This class defines several abstract callback functions for various behavior processing, which is the basic device of developers using horned sungem.
Return | Function | Overview |
---|---|---|
void | openSucceed(UsbDevice usbDevice) | Successfully connected the HornedSungem device |
void | openFailed() | Connection failed or permission not granted |
void | disConnected() | Disconnect device |
@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("Please re-plug the HornedSungem and open the permissions.");
}
@Override
public void disConnected() {
Toast.makeText(this, "Disconnected", Toast.LENGTH_SHORT).show();
}