run method

List<AiliaDetectorObject> run(
  1. Uint8List img,
  2. int width,
  3. int height,
  4. {double threshold = 0.4,
  5. double iou = 0.45,
  6. int format = ailia_dart.AILIA_IMAGE_FORMAT_RGB}
)

Implementation

List<AiliaDetectorObject> run(Uint8List img, int width, int height,
    {double threshold = 0.4,
    double iou = 0.45,
    int format = ailia_dart.AILIA_IMAGE_FORMAT_RGB}) {
  List pixel = img.buffer.asUint8List().toList();

  if (!available) {
    throw Exception("instance not available");
  }

  int channels = 4;
  if (format == ailia_dart.AILIA_IMAGE_FORMAT_RGB ||
      format == ailia_dart.AILIA_IMAGE_FORMAT_BGR) {
    channels = 3;
  }
  if (pixel.length != width * height * channels) {
    throw Exception("invalid image format");
  }

  Pointer<Uint8> inputData = malloc<Uint8>(pixel.length);
  for (int j = 0; j < pixel.length; j++) {
    inputData[j] = pixel[j];
  }

  int status = model!.ailia.ailiaDetectorCompute(
      ppDetector!.value,
      inputData.cast<Void>(),
      width * channels,
      width,
      height,
      format,
      threshold,
      iou);
  if (status != ailia_dart.AILIA_STATUS_SUCCESS) {
    throw Exception("ailiaDetectorCompute failed $status");
  }

  malloc.free(inputData);
  final Pointer<Uint32> count = malloc<Uint32>();
  count.value = 0;
  status = model!.ailia.ailiaDetectorGetObjectCount(ppDetector!.value, count);
  if (status != ailia_dart.AILIA_STATUS_SUCCESS) {
    throw Exception("ailiaDetectorGetObjectCount failed $status");
  }

  List<AiliaDetectorObject> objList =
      List<AiliaDetectorObject>.empty(growable: true);

  for (int idx = 0; idx < count.value; idx++) {
    Pointer<ailia_dart.AILIADetectorObject> pObj =
        malloc<ailia_dart.AILIADetectorObject>();
    status = model!.ailia.ailiaDetectorGetObject(
      ppDetector!.value,
      pObj,
      idx,
      ailia_dart.AILIA_DETECTOR_OBJECT_VERSION,
    );
    if (status != ailia_dart.AILIA_STATUS_SUCCESS) {
      throw Exception("ailiaDetectorGetObject failed $status");
    }

    AiliaDetectorObject obj = AiliaDetectorObject();
    obj.x = pObj.ref.x;
    obj.y = pObj.ref.y;
    obj.w = pObj.ref.w;
    obj.h = pObj.ref.h;
    obj.category = pObj.ref.category;
    obj.prob = pObj.ref.prob;

    malloc.free(pObj);
    objList.add(obj);
  }
  malloc.free(count);

  return objList;
}