SungemSDK-Android API

The current version is 0.2.0,support for multiple models

ConnectStatus Parameter

  • HS_OK : The function call worked as expected.
  • HS_BUSY : The device is busy, retry later.
  • HS_ERROR : An unexpected error was encountered during the function call.
  • HS_NO_CONTEXT: No context.
  • HS_NO_FILE : No files found.
  • HS_DEVICE_NOT_FOUND : No device found.
  • HS_DEVICE_OPEN_FAILED : Failed to open device.
  • HS_UNSUPPORTED_GRAPH_FILE :The graph file is corrupt. Try to recompile the graph file with the version of the toolkit that corresponds to the API version.
  • HS_NO_OPEN : Error status caused by communication operation before calling openDevice().

HsBaseThread

HsBaseThread is a thread for HornedSungem interacion, developers can extend this class for function expansion.

Constructor

  • context
  • usbDevice:Used to connect the Usb device to the current class.
  • zoom : the parameter decided to get the resolution of the image of the hornedsungem’s own camera image, true is 640*360 image, false is 1920*1080 image

Public Member Functions

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
  1. allocateGraph(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().

    • Parameters:
      • filename : A string that is the path to the graph file.
    • Return : If it is abnormal, it returns an error code. If it is normal, it returns the id corresponding to the model. (<0 is abnormal)
      • HS_ERROR(-2): Failed to load.
      • HS_UNSUPPORTED_GRAPH_FILE(-10)
      • HS_NO_FILE(-12)
    • Example:
      int id = allocateGraph(Environment.getExternalStorageDirectory().getAbsolutePath() + "/hs/" + "graph_face_SSD");
    
  2. allocateGraphByAssets(filename):

    This function is used to create an instance of a Graph that represents a neural network,which under the assets package

    • Example:
      int id = allocateGraphByAssets("graph_face_SSD");
    
  3. getImage(std,mean,id):

    This function is to get the graph image

    • Parameters :
      • std,mean :The values ​​of the parameters vary depending on the use of different convolutional neural networks. For details,please click ModelList
      • id : Used to identify the specified model.
    • 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);
    
  4. deviceGetImage():

    This function is to get the device image

    • Return : The return value is similar to the getImage() function. So don’t elaborate
  5. setZoom(zoom):

    This function set the device camera parameter

    • zoom : boolean type
  6. loadTensor(inputTensor,length,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.

    According to the developer’s needs, use the parameters to select the corresponding function

    • Parameters :

      • inputTensor : float[] type or byte[] type
      • length : Array size
      • id : Used to identify the specified model.
    • 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);
    }
    
  7. getResult(id):

    This function retrieves the result of an inference that was initiated via loadTensor() or getImage() on the specified graph.

    • Parameters:
      • id : Used to identify the specified model.
    • Example :
      float[] result = mHsApi.getResult(0);
    
    • Return : Depending on the neural network, the return array length and resolution are different.

      • MobilenetSSD, the parsed code is as follows :
      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;
            }
        }
      }
      
      • Mnist : Returns 10 float numbers, corresponding to 0-9 credibility.
      • SketchGraph : Returns 345 float numbers,corresponding to the credibility of 345 objects,For details,please click Sketch recognisor
  8. openDevice():

    Open the device.

    • No parameter.
    • Return: Returns the status after the command is executed.
      • ConnectStatus.HS_DEVICE_NOT_FOUND, no HornedSungem equipment found, please make sure the USB cable is connected properly.
      • ConnectStatus.HS_NO_CONTEXT, there is no incoming context when initializing the thread and it does not work properly
      • ConnectStatus.HS_DEVICE_OPEN_FAILED, failed to open the device, please make sure the permissions are turned on.
      • ConnectStatus.HS_OK, normal.
  9. closeDevice():

    Turn off the device.


HsDevice

This class defines several abstract callback functions for various behavior processing, which is the basic device of developers using horned sungem.

Callback

Return Function Overview
void openSucceed(UsbDevice usbDevice) Successfully connected the HornedSungem device
void openFailed() Connection failed or permission not granted
void disConnected() Disconnect device
  1. openSucceed(usbDevice):

    Successfully connected the HornedSungem device

    • Parameters:
      • usbDevice
    • Example:
    @Override
    public void openSucceed(UsbDevice usbDevice) {
        mTvTip.setVisibility(View.GONE);
        mHsThread = new FaceDetectionThread(FaceDetectorActivity.this, usbDevice, mHandler);
        mHsThread.start();
    }
    
  2. openFailed():

    Connection failed or permission not granted

    • No parameters
    • Example:
    @Override
    public void openFailed() {
        mTvTip.setText("Please re-plug the HornedSungem and open the permissions.");
      }
    
  3. disConnected():

    Disconnect device

    • No parameters
    • Example:
    @Override
    public void disConnected() {
        Toast.makeText(this, "Disconnected", Toast.LENGTH_SHORT).show();
    }