Arduino串口读取目标检测结果

适用产品

拥有串口的摄像头:

  • SingTown AI Vision Module SC(yes)

  • SingTown AI Vision Module EC(no)

目的

使用Arduino,通过串口获取 SingTown AI Vision Module 的目标检测识别结果。

前提条件

模型已上传到摄像头

连接电路

  • SingTown AI Vision Module RX 不连接

  • SingTown AI Vision Module TX 连接 Arduino D10

  • SingTown AI Vision Module GND 连接 Arduino GND

  • SingTown AI Vision Module VIN 连接 Arduino 5V (可选)

运行Arduino的代码

代码下载:

代码介绍:

#define OBJECT_MAX 8    // Max Found 8 Objects
#define OBJECT_SIZE 10  // Every Object is 10 bytes

struct DetectObj {
  int score;  // max 255
  int idx;
  int x1;  // box left coordinate, max 640
  int y1;  // box top coordinate, max 480
  int x2;  // box right coordinate, max 640
  int y2;  // box bottom coordinate, max 480
};

struct DetectObj objs[OBJECT_MAX];

全局变量 objs ,用于存放读取的结果。

int check_crc(unsigned char* payload) {
  unsigned char num = payload[0];
  unsigned char crc = 0;
  int i = 0, j = 0;
  for (i = 0; i < num * OBJECT_SIZE + 2; i++) {
    crc ^= payload[i];
    for (j = 0; j < 8; j++) {
      if (crc & 1)
        crc ^= 0x91;
      crc >>= 1;
    }
  }
  return crc;
}

check_crc 为校验函数,用于校验数据帧是否正确。

int read_singtownaicam_objs() {
  int i;
  unsigned char num;
  unsigned char payload[OBJECT_MAX * OBJECT_SIZE + 2];
  unsigned char* obj_ptr;
  int byte;
  while (1) {
    if (readByte() != 0xeb) continue;
    if (readByte() != 0x90) continue;

    num = readByte();
    if (num < 0 || num > OBJECT_MAX)
      continue;
    payload[0] = num;

    for (i = 1; i < num * OBJECT_SIZE + 2; i++) {
      byte = readByte();
      if (byte == -1) break;
      payload[i] = byte;
    }

    if (check_crc(payload) != 0) continue;

    for (i = 0; i < num; i++) {
      obj_ptr = payload + i * OBJECT_SIZE + 1;
      objs[i].score = obj_ptr[0];
      objs[i].idx = obj_ptr[1];
      objs[i].x1 = obj_ptr[2] | (obj_ptr[3] << 8);
      objs[i].y1 = obj_ptr[4] | (obj_ptr[5] << 8);
      objs[i].x2 = obj_ptr[6] | (obj_ptr[7] << 8);
      objs[i].y2 = obj_ptr[8] | (obj_ptr[9] << 8);
    }
    return num;
  }
}

read_singtownaicam_objs 是读取数据函数,结果存放在 objs 全局变量中。