]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - tidl/tidl-api.git/blobdiff - examples/segmentation/main.cpp
TIDL-API docu update: time, probability
[tidl/tidl-api.git] / examples / segmentation / main.cpp
index 0f7d4cc90a7704591d5294a5adda0a1d62a42f66..86f81e55cd8c21f464ef19320ff86ef83f5bf073 100644 (file)
@@ -44,6 +44,7 @@
 #include "executor.h"
 #include "execution_object.h"
 #include "configuration.h"
+#include "object_classes.h"
 
 #include "opencv2/core.hpp"
 #include "opencv2/imgproc.hpp"
@@ -58,8 +59,11 @@ bool __TI_show_debug_ = false;
 bool is_default_input = false;
 bool is_preprocessed_input = false;
 bool is_camera_input       = false;
+int  orig_width;
+int  orig_height;
+object_class_table_t *object_class_table;
 
-using namespace tinn;
+using namespace tidl;
 using namespace cv;
 
 
@@ -71,7 +75,8 @@ bool ReadFrame(ExecutionObject& eo, int frame_idx,
                const Configuration& configuration, int num_frames,
                std::string& image_file, VideoCapture &cap);
 
-bool WriteFrameOutput(const ExecutionObject &eo);
+bool WriteFrameOutput(const ExecutionObject &eo,
+                      const Configuration& configuration);
 
 static void ProcessArgs(int argc, char *argv[],
                         std::string& config,
@@ -92,9 +97,9 @@ int main(int argc, char *argv[])
     signal(SIGTERM, exit);
 
     // If there are no devices capable of offloading TIDL on the SoC, exit
-    uint32_t num_dla = Executor::GetNumDevices(DeviceType::DLA);
+    uint32_t num_eve = Executor::GetNumDevices(DeviceType::EVE);
     uint32_t num_dsp = Executor::GetNumDevices(DeviceType::DSP);
-    if (num_dla == 0 && num_dsp == 0)
+    if (num_eve == 0 && num_dsp == 0)
     {
         std::cout << "TI DL not supported on this SoC." << std::endl;
         return EXIT_SUCCESS;
@@ -104,9 +109,15 @@ int main(int argc, char *argv[])
     std::string config      = DEFAULT_CONFIG;
     std::string input_file  = DEFAULT_INPUT;
     int         num_devices = 1;
-    DeviceType  device_type = DeviceType::DLA;
+    DeviceType  device_type = (num_eve > 0 ? DeviceType::EVE:DeviceType::DSP);
     ProcessArgs(argc, argv, config, num_devices, device_type, input_file);
 
+    if ((object_class_table = GetObjectClassTable(config)) == nullptr)
+    {
+        std::cout << "No object classes defined for this config." << std::endl;
+        return EXIT_FAILURE;
+    }
+
     if (input_file == DEFAULT_INPUT)  is_default_input = true;
     if (input_file == "camera")       is_camera_input = true;
     if (input_file.length() > 2 &&
@@ -165,10 +176,6 @@ bool RunConfiguration(const std::string& config_file, int num_devices,
         image_file = input_file;
     }
 
-    // Determine input frame size from configuration
-    size_t frame_sz = configuration.inWidth * configuration.inHeight *
-                      configuration.inNumChannels;
-
     try
     {
         // Create a executor with the approriate core type, number of cores
@@ -184,8 +191,10 @@ bool RunConfiguration(const std::string& config_file, int num_devices,
         std::vector<void *> buffers;
         for (auto &eo : execution_objects)
         {
-            ArgInfo in  = { ArgInfo(malloc(frame_sz), frame_sz)};
-            ArgInfo out = { ArgInfo(malloc(frame_sz), frame_sz)};
+            size_t in_size  = eo->GetInputBufferSizeInBytes();
+            size_t out_size = eo->GetOutputBufferSizeInBytes();
+            ArgInfo in  = { ArgInfo(malloc(in_size),  in_size)};
+            ArgInfo out = { ArgInfo(malloc(out_size), out_size)};
             eo->SetInputOutputBuffer(in, out);
 
             buffers.push_back(in.ptr());
@@ -222,7 +231,7 @@ bool RunConfiguration(const std::string& config_file, int num_devices,
                           << std::setw(6) << std::setprecision(3)
                           << overhead << " %" << std::endl;
 
-                WriteFrameOutput(*eo);
+                WriteFrameOutput(*eo, configuration);
             }
 
             // Read a frame and start processing it with current eo
@@ -238,7 +247,7 @@ bool RunConfiguration(const std::string& config_file, int num_devices,
             free(b);
 
     }
-    catch (tinn::Exception &e)
+    catch (tidl::Exception &e)
     {
         std::cerr << e.what() << std::endl;
         status = false;
@@ -270,6 +279,8 @@ bool ReadFrame(ExecutionObject &eo, int frame_idx,
             ifs.read(frame_buffer, channel_size * 3);
             bool ifs_status = ifs.good();
             ifs.close();
+            orig_width  = configuration.inWidth;
+            orig_height = configuration.inHeight;
             return ifs_status;  // already PreProc-ed
         }
         else
@@ -296,6 +307,8 @@ bool ReadFrame(ExecutionObject &eo, int frame_idx,
 
     // scale to network input size 1024 x 512
     Mat s_image, bgr_frames[3];
+    orig_width  = image.cols;
+    orig_height = image.rows;
     cv::resize(image, s_image,
                Size(configuration.inWidth, configuration.inHeight),
                0, 0, cv::INTER_AREA);
@@ -307,51 +320,50 @@ bool ReadFrame(ExecutionObject &eo, int frame_idx,
 }
 
 // Create Overlay mask for pixel-level segmentation
-void CreateMask(uchar *classes, uchar *mb, uchar *mg, uchar* mr)
+void CreateMask(uchar *classes, uchar *mb, uchar *mg, uchar* mr,
+                int channel_size)
 {
-  for (int i = 0; i < 1024 * 512; i++)
-  {
-    switch(classes[i])
+    for (int i = 0; i < channel_size; i++)
     {
-      case 0x00: mb[i] = 0xFF; mg[i] = 0xFF; mr[i] = 0xFF; break;
-      case 0x01: mb[i] = 0xFF; mg[i] = 0x00; mr[i] = 0x00; break;
-      case 0x02: mb[i] = 0x00; mg[i] = 0x00; mr[i] = 0xFF; break;
-      case 0x03: mb[i] = 0x00; mg[i] = 0xFF; mr[i] = 0x00; break;
-      case 0x04: mb[i] = 0x00; mg[i] = 0xFF; mr[i] = 0xFF; break;
-      default:   mb[i] = 0x00; mg[i] = 0x00; mr[i] = 0x00; break;
+        object_class_t *object_class = GetObjectClass(object_class_table,
+                                                      classes[i]);
+        mb[i] = object_class->color.blue;
+        mg[i] = object_class->color.green;
+        mr[i] = object_class->color.red;
     }
-  }
 }
 
 // Create frame overlayed with pixel-level segmentation
-bool WriteFrameOutput(const ExecutionObject &eo)
+bool WriteFrameOutput(const ExecutionObject &eo,
+                      const Configuration& configuration)
 {
-    const int k = 5;
     unsigned char *out = (unsigned char *) eo.GetOutputBufferPtr();
-    int out_size = eo.GetOutputBufferSizeInBytes();
+    int out_size       = eo.GetOutputBufferSizeInBytes();
+    int width          = configuration.inWidth;
+    int height         = configuration.inHeight;
+    int channel_size   = width * height;
 
-    Mat mask, frame, blend, bgr[3];
+    Mat mask, frame, blend, r_blend, bgr[3];
     // Create overlay mask
-    bgr[0] = Mat(512, 1024, CV_8UC(1));
-    bgr[1] = Mat(512, 1024, CV_8UC(1));
-    bgr[2] = Mat(512, 1024, CV_8UC(1));
-    CreateMask(out, bgr[0].ptr(), bgr[1].ptr(), bgr[2].ptr());
+    bgr[0] = Mat(height, width, CV_8UC(1));
+    bgr[1] = Mat(height, width, CV_8UC(1));
+    bgr[2] = Mat(height, width, CV_8UC(1));
+    CreateMask(out, bgr[0].ptr(), bgr[1].ptr(), bgr[2].ptr(), channel_size);
     cv::merge(bgr, 3, mask);
 
     // Asseembly original frame
     unsigned char *in = (unsigned char *) eo.GetInputBufferPtr();
-    bgr[0] = Mat(512, 1024, CV_8UC(1), in);
-    bgr[1] = Mat(512, 1024, CV_8UC(1), in + 512*1024);
-    bgr[2] = Mat(512, 1024, CV_8UC(1), in + 512*1024*2);
+    bgr[0] = Mat(height, width, CV_8UC(1), in);
+    bgr[1] = Mat(height, width, CV_8UC(1), in + channel_size);
+    bgr[2] = Mat(height, width, CV_8UC(1), in + channel_size*2);
     cv::merge(bgr, 3, frame);
 
     // Create overlayed frame
     cv::addWeighted(frame, 0.7, mask, 0.3, 0.0, blend);
 
+    cv::resize(blend, r_blend, Size(orig_width, orig_height));
     if (is_camera_input)
     {
-        Mat r_blend;
-        cv::resize(blend, r_blend, Size(640, 480));
         cv::imshow("Segmentation", r_blend);
         waitKey(1);
     }
@@ -367,7 +379,7 @@ bool WriteFrameOutput(const ExecutionObject &eo)
         }
 
         snprintf(outfile_name, 64, "overlay_%d.png", frame_index);
-        cv::imwrite(outfile_name, blend);
+        cv::imwrite(outfile_name, r_blend);
         printf("Saving frame %d overlayed with segmentation to: %s\n",
                frame_index, outfile_name);
     }
@@ -410,7 +422,7 @@ void ProcessArgs(int argc, char *argv[], std::string& config,
                       break;
 
             case 't': if (*optarg == 'e')
-                          device_type = DeviceType::DLA;
+                          device_type = DeviceType::EVE;
                       else if (*optarg == 'd')
                           device_type = DeviceType::DSP;
                       else
@@ -451,7 +463,7 @@ void DisplayHelp()
                  "Optional arguments:\n"
                  " -c <config>          Valid configs: jseg21_tiscapes, jseg21\n"
                  " -n <number of cores> Number of cores to use (1 - 4)\n"
-                 " -t <d|e>             Type of core. d -> DSP, e -> DLA\n"
+                 " -t <d|e>             Type of core. d -> DSP, e -> EVE\n"
                  " -i <image>           Path to the image file\n"
                  "                      Default are 3 frames in testvecs\n"
                  " -i camera            Use camera as input\n"