<commit_before>/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow_lite_support/c/task/vision/image_classifier.h"

#include "tensorflow_lite_support/c/task/processor/classification_result.h"
#include "tensorflow_lite_support/c/task/vision/core/frame_buffer.h"
#include "tensorflow_lite_support/c/test/task/vision/utils/image_utils.h"
#include "tensorflow_lite_support/cc/port/gmock.h"
#include "tensorflow_lite_support/cc/port/gtest.h"
#include "tensorflow_lite_support/cc/test/test_utils.h"

namespace tflite {
namespace task {
namespace vision {
namespace {

using ::tflite::task::JoinPath;

constexpr char kTestDataDirectory[] =
    "tensorflow_lite_support/cc/test/testdata/task/vision/";
// Quantized model.
constexpr char kMobileNetQuantizedWithMetadata[] =
    "mobilenet_v1_0.25_224_quant.tflite";

CImageData LoadImage(const char* image_name) {
  return CDecodeImageFromFile(
      JoinPath("./" /*test src dir*/, kTestDataDirectory, image_name).data());
}

TEST(CImageClassifierFromOptionsTest, FailsWithMissingModelPath) {
  TfLiteImageClassifierOptions options = {0};
  TfLiteImageClassifier* image_classifier =
      TfLiteImageClassifierFromOptions(&options);
  ASSERT_EQ(image_classifier, nullptr);
}

TEST(CImageClassifierFromOptionsTest, SucceedsWithModelPath) {
//   TfLiteImageClassifierOptions* options = TfLiteImageClassifierOptionsCreate();
  const char* model_path = JoinPath("./" /*test src dir*/, kTestDataDirectory,
                                    kMobileNetQuantizedWithMetadata)
                               .data();
  TfLiteImageClassifierOptions options = {0};
  options.base_options.model_file.file_path = model_path;
  TfLiteImageClassifier* image_classifier =
      TfLiteImageClassifierFromOptions(&options);
  EXPECT_NE(image_classifier, nullptr);
  TfLiteImageClassifierDelete(image_classifier);
}

TEST(CImageClassifierFromOptionsTest, SucceedsWithNumberOfThreads) {
  const char* model_path = JoinPath("./" /*test src dir*/, kTestDataDirectory,
                                    kMobileNetQuantizedWithMetadata)
                               .data();

  TfLiteImageClassifierOptions options = {0};
  options.base_options.model_file.file_path = model_path;
  options.base_options.compute_settings.tflite_settings.cpu_settings.num_threads = 3;
  TfLiteImageClassifier* image_classifier =
      TfLiteImageClassifierFromOptions(&options);
  EXPECT_NE(image_classifier, nullptr);
  TfLiteImageClassifierDelete(image_classifier);
}

class CImageClassifierClassifyTest : public ::testing::Test {
 protected:
  void SetUp() override {

 TfLiteImageClassifierOptions options = {0};
  options.base_options.model_file.file_path = JoinPath("./" /*test src dir*/, kTestDataDirectory,
                 kMobileNetQuantizedWithMetadata)
            .data();
  image_classifier =
      TfLiteImageClassifierFromOptions(&options);

    ASSERT_NE(image_classifier, nullptr);
  }

  void TearDown() override { TfLiteImageClassifierDelete(image_classifier); }
  TfLiteImageClassifier* image_classifier;
};

TEST_F(CImageClassifierClassifyTest, SucceedsWithImageData) {
  CImageData image_data = LoadImage("burger-224.png");<commit_msg>Added test for mutual exclusivity of class name blacklist and whiitelist<commit_after>/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow_lite_support/c/task/vision/image_classifier.h"

#include "tensorflow_lite_support/c/task/processor/classification_result.h"
#include "tensorflow_lite_support/c/task/vision/core/frame_buffer.h"
#include "tensorflow_lite_support/c/test/task/vision/utils/image_utils.h"
#include "tensorflow_lite_support/cc/port/gmock.h"
#include "tensorflow_lite_support/cc/port/gtest.h"
#include "tensorflow_lite_support/cc/test/test_utils.h"

namespace tflite {
namespace task {
namespace vision {
namespace {

using ::tflite::task::JoinPath;

constexpr char kTestDataDirectory[] =
    "tensorflow_lite_support/cc/test/testdata/task/vision/";
// Quantized model.
constexpr char kMobileNetQuantizedWithMetadata[] =
    "mobilenet_v1_0.25_224_quant.tflite";

CImageData LoadImage(const char* image_name) {
  return CDecodeImageFromFile(
      JoinPath("./" /*test src dir*/, kTestDataDirectory, image_name).data());
}

TEST(CImageClassifierFromOptionsTest, FailsWithMissingModelPath) {
  TfLiteImageClassifierOptions options = {0};
  TfLiteImageClassifier* image_classifier =
      TfLiteImageClassifierFromOptions(&options);
  ASSERT_EQ(image_classifier, nullptr);
}

TEST(CImageClassifierFromOptionsTest, SucceedsWithModelPath) {
  //   TfLiteImageClassifierOptions* options =
  //   TfLiteImageClassifierOptionsCreate();
  const char* model_path = JoinPath("./" /*test src dir*/, kTestDataDirectory,
                                    kMobileNetQuantizedWithMetadata)
                               .data();
  TfLiteImageClassifierOptions options = {0};
  options.base_options.model_file.file_path = model_path;
  TfLiteImageClassifier* image_classifier =
      TfLiteImageClassifierFromOptions(&options);
  EXPECT_NE(image_classifier, nullptr);
  TfLiteImageClassifierDelete(image_classifier);
}

TEST(CImageClassifierFromOptionsTest, SucceedsWithNumberOfThreads) {
  const char* model_path = JoinPath("./" /*test src dir*/, kTestDataDirectory,
                                    kMobileNetQuantizedWithMetadata)
                               .data();

  TfLiteImageClassifierOptions options = {0};
  options.base_options.model_file.file_path = model_path;
  options.base_options.compute_settings.tflite_settings.cpu_settings
      .num_threads = 3;
  TfLiteImageClassifier* image_classifier =
      TfLiteImageClassifierFromOptions(&options);
  EXPECT_NE(image_classifier, nullptr);
  TfLiteImageClassifierDelete(image_classifier);
}

TEST(CImageClassifierFromOptionsTest,
     FailsWithClassNameBlackListAndClassNameWhiteList) {
  const char* model_path = JoinPath("./" /*test src dir*/, kTestDataDirectory,
                                    kMobileNetQuantizedWithMetadata)
                               .data();

  TfLiteImageClassifierOptions options = {0};
  options.base_options.model_file.file_path = model_path;

  char* class_name_blacklist[] = {"brambling"};
  options.classification_options.class_name_blacklist.list =
      class_name_blacklist;
  options.classification_options.class_name_blacklist.length = 1;

  char* class_name_whitelist[] = {"cheeseburger"};
  options.classification_options.class_name_whitelist.list =
      class_name_whitelist;
  options.classification_options.class_name_whitelist.length = 1;

  TfLiteImageClassifier* image_classifier =
      TfLiteImageClassifierFromOptions(&options);
  EXPECT_EQ(image_classifier, nullptr);
  if (image_classifier) TfLiteImageClassifierDelete(image_classifier);
}

class CImageClassifierClassifyTest : public ::testing::Test {
 protected:
  void SetUp() override {
    TfLiteImageClassifierOptions options = {0};
    options.base_options.model_file.file_path =
        JoinPath("./" /*test src dir*/, kTestDataDirectory,
                 kMobileNetQuantizedWithMetadata)
            .data();
    image_classifier = TfLiteImageClassifierFromOptions(&options);

    ASSERT_NE(image_classifier, nullptr);
  }

  void TearDown() override { TfLiteImageClassifierDelete(image_classifier); }
  TfLiteImageClassifier* image_classifier;
};

TEST_F(CImageClassifierClassifyTest, SucceedsWithImageData) {
  CImageData image_data = LoadImage("burger-224.png");
{'fraction_non_alphanumeric': 0.10525710341654204, 'fraction_numerical': 0.005636719199355804, 'mean_word_length': 4.731048121292024, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3956899', 'n_tokens_mistral': 2606, 'n_tokens_neox': 2570, 'n_words': 503}