51758e2a6cb08178c7fe7007e3068458405f5818
[platform/core/ml/nnfw.git] / runtime / contrib / tflite_classify / src / tflite_classify.cc
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
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
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "ImageClassifier.h"
18
19 #include <iostream>
20
21 #include <boost/filesystem.hpp>
22 #include <opencv2/opencv.hpp>
23
24 namespace fs = boost::filesystem;
25
26 int main(const int argc, char **argv)
27 {
28   const std::string MODEL_FILE = "tensorflow_inception_graph.tflite";
29   const std::string LABEL_FILE = "imagenet_comp_graph_label_strings.txt";
30
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;
37
38   const int FRAME_WIDTH = 640;
39   const int FRAME_HEIGHT = 480;
40
41   const bool use_nnapi = nnfw::misc::EnvVar("USE_NNAPI").asBool(false);
42   const bool debug_mode = nnfw::misc::EnvVar("DEBUG_MODE").asBool(false);
43
44   std::cout << "USE_NNAPI : " << use_nnapi << std::endl;
45   std::cout << "DEBUG_MODE : " << debug_mode << std::endl;
46
47   std::cout << "Model : " << MODEL_FILE << std::endl;
48   std::cout << "Label : " << LABEL_FILE << std::endl;
49
50   if (!fs::exists(MODEL_FILE))
51   {
52     std::cerr << "model file not found: " << MODEL_FILE << std::endl;
53     exit(1);
54   }
55
56   if (!fs::exists(LABEL_FILE))
57   {
58     std::cerr << "label file not found: " << LABEL_FILE << std::endl;
59     exit(1);
60   }
61
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));
66
67   // Cam setting
68   cv::VideoCapture cap(0);
69   cv::Mat frame;
70
71   // Initialize camera
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);
75
76   std::vector<Recognition> results;
77   clock_t begin, end;
78   while (cap.isOpened())
79   {
80     // Get image data
81     if (!cap.read(frame))
82     {
83       std::cout << "Frame is null..." << std::endl;
84       break;
85     }
86
87     if (debug_mode)
88     {
89       begin = clock();
90     }
91     // Recognize image
92     results = classifier->recognizeImage(frame);
93     if (debug_mode)
94     {
95       end = clock();
96     }
97
98     // Show result data
99     std::cout << std::endl;
100     if (results.size() > 0)
101     {
102       for (int i = 0; i < results.size(); ++i)
103       {
104         std::cout << results[i].title << ": " << results[i].confidence << std::endl;
105       }
106     }
107     else
108     {
109       std::cout << "." << std::endl;
110     }
111     if (debug_mode)
112     {
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;
116     }
117   }
118
119   cap.release();
120
121   return 0;
122 }