2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "ImageClassifier.h"
21 #include <boost/filesystem.hpp>
22 #include <opencv2/opencv.hpp>
24 namespace fs = boost::filesystem;
26 int main(const int argc, char **argv)
28 const std::string MODEL_FILE = "tensorflow_inception_graph.tflite";
29 const std::string LABEL_FILE = "imagenet_comp_graph_label_strings.txt";
31 const std::string INPUT_NAME = "input";
32 const std::string OUTPUT_NAME = "output";
33 const int INPUT_SIZE = 224;
34 const int IMAGE_MEAN = 117;
35 const int IMAGE_STD = 1;
36 const int OUTPUT_SIZE = 1008;
38 const int FRAME_WIDTH = 640;
39 const int FRAME_HEIGHT = 480;
41 const bool use_nnapi = nnfw::misc::EnvVar("USE_NNAPI").asBool(false);
42 const bool debug_mode = nnfw::misc::EnvVar("DEBUG_MODE").asBool(false);
44 std::cout << "USE_NNAPI : " << use_nnapi << std::endl;
45 std::cout << "DEBUG_MODE : " << debug_mode << std::endl;
47 std::cout << "Model : " << MODEL_FILE << std::endl;
48 std::cout << "Label : " << LABEL_FILE << std::endl;
50 if (!fs::exists(MODEL_FILE))
52 std::cerr << "model file not found: " << MODEL_FILE << std::endl;
56 if (!fs::exists(LABEL_FILE))
58 std::cerr << "label file not found: " << LABEL_FILE << std::endl;
62 // Create ImageClassifier
63 std::unique_ptr<ImageClassifier> classifier(
64 new ImageClassifier(MODEL_FILE, LABEL_FILE, INPUT_SIZE, IMAGE_MEAN, IMAGE_STD, INPUT_NAME,
65 OUTPUT_NAME, use_nnapi));
68 cv::VideoCapture cap(0);
72 cap.set(CV_CAP_PROP_FRAME_WIDTH, FRAME_WIDTH);
73 cap.set(CV_CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT);
74 cap.set(CV_CAP_PROP_FPS, 5);
76 std::vector<Recognition> results;
78 while (cap.isOpened())
83 std::cout << "Frame is null..." << std::endl;
92 results = classifier->recognizeImage(frame);
99 std::cout << std::endl;
100 if (results.size() > 0)
102 for (int i = 0; i < results.size(); ++i)
104 std::cout << results[i].title << ": " << results[i].confidence << std::endl;
109 std::cout << "." << std::endl;
113 std::cout << "Frame: " << FRAME_WIDTH << "x" << FRAME_HEIGHT << std::endl;
114 std::cout << "Crop: " << INPUT_SIZE << "x" << INPUT_SIZE << std::endl;
115 std::cout << "Inference time(ms): " << ((end - begin) / (CLOCKS_PER_SEC / 1000)) << std::endl;