This article describes how to use the Mnist convolutional neural network to achieve digital recognition through the Sungem-SDK in the Android Studio development environment. The current example uses the camera that comes with the HornedSungem. If you need an external image source, please refer to SungemSDK-AndroidExamples.
The lib contains the jar and .so required by horned-sungemSDK and javacv.
Node: Chinese characters cannot be included when adding the libs folder path, otherwise compilation will fail.
Open the AndroidManifest.xml file located under app > src > main and Add the required device permissions to the file.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.hardware.usb.host"/>
<uses-permission android:name="android.hardware.usb.accessory"/>
<uses-feature android:name="android.hardware.usb.host"/>
Here’s a simple hello2018 example to explain how to use the HornedSungem.
Init HsDevice, used for HornedSungem communication and display results, as follows:
mHsDevice = new HsDevice(this) {
@Override
public void openSucceed(UsbDevice usbDevice) {
Toast.makeText(MainActivity.this, "connection succeeded.", Toast.LENGTH_SHORT).show();
mUsbDevice = usbDevice;
mCbDevice.setChecked(true);
}
@Override
public void openFailed() {
mCbDevice.setChecked(false);
Toast.makeText(MainActivity.this, "Please re-plug and allow permission", Toast.LENGTH_SHORT).show();
}
@Override
public void disConnected() {
mCbDevice.setChecked(false);
Toast.makeText(MainActivity.this, "Disconnected", Toast.LENGTH_SHORT).show();
}
};
Creating the Hello2018Thread class which extends HsThread, the core code is as follows:
@Override
public void run() {
super.run();
int status = openDevice();
if (status != ConnectStatus.HS_OK) {
return;
}
int id = allocateGraphByAssets("graph_mnist");
if (id<0) {
return;
}
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 = loadTensor(float_tensor, float_tensor.length, id);
if (status_load == ConnectStatus.HS_OK) {
float[] result = getResult(id);
if (result != null) {
int max = getMaxPossible(result);
mHandler.sendEmptyMessage(max);
}
}
}
}
public int getMaxPossible(float[] arr) {
int max = 0;
float max_f = 0;
for (int i = 0; i < arr.length; i++) {
float temp = arr[i];
if (temp > max_f) {
max = i;
max_f = temp;
}
}
return max;
}
Node: If you do not open the device, the program will perform the openFailed() callback. If you want to use it again, please re-plug the device.
If you want to know more details, please click to view the example tutorial, or download the demo project.