Custom Image Classification

Hi,

I have trained a CNN model to recognize 3 classes of objects. The sketch is as follow (from the example sketch):

/*

 Example guide:
 TBD

 NN Model Selection
 Select Neural Network(NN) task and models using .modelSelect(nntask, objdetmodel, facedetmodel, facerecogmodel, audclassmodel, imgclassmodel).
 Replace with NA_MODEL if they are not necessary for your selected NN Task.

 NN task
 =======
 OBJECT_DETECTION/ FACE_DETECTION/ FACE_RECOGNITION/ AUDIO CLASSIFICATION/ IMAGE CLASSIFICATION

 Models
 =======
 YOLOv3 model         DEFAULT_YOLOV3TINY   / CUSTOMIZED_YOLOV3TINY
 YOLOv4 model         DEFAULT_YOLOV4TINY   / CUSTOMIZED_YOLOV4TINY
 YOLOv7 model         DEFAULT_YOLOV7TINY   / CUSTOMIZED_YOLOV7TINY
 SCRFD model          DEFAULT_SCRFD        / CUSTOMIZED_SCRFD
 MobileFaceNet model  DEFAULT_MOBILEFACENET/ CUSTOMIZED_MOBILEFACENET
 YAMNET model         DEFAULT_YAMNET       / CUSTOMIZED_YAMNET
 CNN model            DEFAULT_IMGCLASS     / CUSTOMIZED_IMGCLASS
 No model             NA_MODEL
 */

#include "WiFi.h"
#include "StreamIO.h"
#include "VideoStream.h"
#include "RTSP.h"
#include "NNImageClassification.h"
#include "VideoStreamOverlay.h"
#include "ClassificationClassList.h"

// Color of images used to train the cnn model (RGB or Grayscale)
#define IMAGERGB 1

#define CHANNEL   0
#define CHANNELNN 3

// Lower resolution for NN processing
// Modify according to the model's input shape size
#define NNWIDTH  224
#define NNHEIGHT 224

VideoSetting config(VIDEO_FHD, 30, VIDEO_H264, 0);
VideoSetting configNN(NNWIDTH, NNHEIGHT, 10, VIDEO_RGB, 0);
NNImageClassification imgclass;
RTSP rtsp;
StreamIO videoStreamer(1, 1);
StreamIO videoStreamerNN(1, 1);

char ssid[] = "Network_SSID";    // your network SSID (name)
char pass[] = "Password";        // your network password
int status = WL_IDLE_STATUS;

IPAddress ip;
int rtsp_portnum;

void setup()
{
    Serial.begin(115200);

    // attempt to connect to Wifi network:
    while (status != WL_CONNECTED) {
        Serial.print("Attempting to connect to WPA SSID: ");
        Serial.println(ssid);
        status = WiFi.begin(ssid, pass);

        // wait 2 seconds for connection:
        delay(2000);
    }
    ip = WiFi.localIP();

    // Configure camera video channels with video format information
    // Adjust the bitrate based on your WiFi network quality
    config.setBitrate(2 * 1024 * 1024);    // Recommend to use 2Mbps for RTSP streaming to prevent network congestion
    Camera.configVideoChannel(CHANNEL, config);
    Camera.configVideoChannel(CHANNELNN, configNN);
    Camera.videoInit();

    // Configure RTSP with corresponding video format information
    rtsp.configVideo(config);
    rtsp.begin();
    rtsp_portnum = rtsp.getPort();

    // Configure object detection with corresponding video format information
    // Select Neural Network(NN) task and models
    imgclass.configVideo(configNN);
    imgclass.configInputImageColor(IMAGERGB);
    imgclass.setResultCallback(ICPostProcess);
    imgclass.modelSelect(IMAGE_CLASSIFICATION, NA_MODEL, NA_MODEL, NA_MODEL, NA_MODEL, CUSTOMIZED_IMGCLASS);
    imgclass.begin();

    // Configure StreamIO object to stream data from video channel to RTSP
    videoStreamer.registerInput(Camera.getStream(CHANNEL));
    videoStreamer.registerOutput(rtsp);
    if (videoStreamer.begin() != 0) {
        Serial.println("StreamIO link start failed");
    }

    // Start data stream from video channel
    Camera.channelBegin(CHANNEL);

    // Configure StreamIO object to stream data from RGB video channel to object detection
    videoStreamerNN.registerInput(Camera.getStream(CHANNELNN));
    videoStreamerNN.setStackSize();
    videoStreamerNN.setTaskPriority();
    videoStreamerNN.registerOutput(imgclass);
    if (videoStreamerNN.begin() != 0) {
        Serial.println("StreamIO link start failed");
    }

    // Start data stream from video channel
    Camera.channelBegin(CHANNELNN);
}

void loop()
{
    // Do nothing
}

// User callback function
void ICPostProcess(void)
{
    int class_id = imgclass.classID();
    if (imgclassItemList[class_id].filter) {    // header file
        float prob = imgclass.score();
        printf("class %d, score: %f, name: %s\r\n", class_id, prob, imgclassItemList[class_id].imgclassName);
    }
}

I have already put my .nb file (converted from .H5 format) in the sketch folder as shown in the above image.

When I compile the sketch, I got the following error:

Model (.nb file) missing or customized model name mismatch. Please include your customized model in the sketch folder, or rename your model if it is already in sketch folder.
exit status 1

Compilation error: exit status 1

I don’t know what else to do. Please help me understand what I did wrong with this project. Thank you.

Hi @Zul77, I have tried it out, and it works for me. Ensure that the name of your nb file does not contain any spacing. May I know which SDK version are you currently using?

Thanks for your reply. I am using Arduino IDE 2.3.2 version. I have attached the .nb file that I converted from the .h5 file, Arduino sketch, and the .h file. This is an object classification project.
AmebaImageClassification.zip (879.1 KB).

I have create a folder in this directory C:\Users\USER\Documents\Arduino\AmebaImageClassification. I have put all the 3 files under the AmebaImageClassification folder.

Hi @Zul77,

You cannot create a folder in the Arduino folder, as we are considered a third-party board on Arduino IDE. Hence, you need to install our SDK via Arduino IDE by navigating to Preferences and adding https://github.com/Ameba-AIoT/ameba-arduino-pro2/raw/main/Arduino_package/package_realtek_amebapro2_index.json under Additional Boards Manager URLs.

You may refer to this page, which will teach you how to install sdk and get started with the board, Ameba ARDUINO with AMB82-mini (RTL8735B) — Ameba Arduino AIoT Documentation v1.1 documentation.

After installing the sdk, you can find the SDK in ..\AppData\Local\Arduino15\packages\realtek\hardware\AmebaPro2 folder.

You need to use the example from our SDK folder.

For customized model, place your model in ..\Arduino15\packages\realtek\hardware\AmebaPro2\4.0.9-build20250528\libraries\NeuralNetwork\examples\RTSPImageClassification Folder with name “img_class_cnn.nb”.