]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - tidl/tidl-api.git/blob - examples/common/video_utils.cpp
Add option to specify object classes list file
[tidl/tidl-api.git] / examples / common / video_utils.cpp
1 /******************************************************************************
2  * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Texas Instruments Incorporated nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26  * THE POSSIBILITY OF SUCH DAMAGE.
27  *****************************************************************************/
29 #include <getopt.h>
30 #include <cassert>
31 #include "video_utils.h"
33 using namespace std;
34 using namespace tidl;
37 bool ProcessArgs(int argc, char *argv[], cmdline_opts_t& opts)
38 {
39     opts.num_frames      = 0;
40     opts.num_layers_groups = 1;
41     opts.output_width    = 0;
42     opts.verbose         = false;
43     opts.is_camera_input = false;
44     opts.is_video_input  = false;
46     const struct option long_options[] =
47     {
48         {"config",       required_argument, 0, 'c'},
49         {"num_dsps",     required_argument, 0, 'd'},
50         {"num_eves",     required_argument, 0, 'e'},
51         {"num_layers_groups", required_argument, 0, 'g'},
52         {"num_frames",   required_argument, 0, 'f'},
53         {"input_file",   required_argument, 0, 'i'},
54         {"object_classes_list_file",   required_argument, 0, 'l'},
55         {"output_width", required_argument, 0, 'w'},
56         {"help",         no_argument,       0, 'h'},
57         {"verbose",      no_argument,       0, 'v'},
58         {0, 0, 0, 0}
59     };
61     int option_index = 0;
63     while (true)
64     {
65         int c = getopt_long(argc, argv, "c:d:e:g:f:i:l:w:hv", long_options,
66                             &option_index);
68         if (c == -1)
69             break;
71         switch (c)
72         {
73             case 'c': opts.config = optarg;
74                       break;
76             case 'd': opts.num_dsps = atoi(optarg);
77                       assert(opts.num_dsps >= 0 && opts.num_dsps <=
78                                      Executor::GetNumDevices(DeviceType::DSP));
79                       break;
81             case 'e': opts.num_eves = atoi(optarg);
82                       assert(opts.num_eves >= 0 && opts.num_eves <=
83                                      Executor::GetNumDevices(DeviceType::EVE));
84                       break;
86             case 'g': opts.num_layers_groups = atoi(optarg);
87                       assert((opts.num_layers_groups == 1) || (opts.num_layers_groups == 2));
88                       break;
90             case 'f': opts.num_frames = atoi(optarg);
91                       assert (opts.num_frames > 0);
92                       break;
94             case 'i': opts.input_file = optarg;
95                       break;
97             case 'l': opts.object_classes_list_file = optarg;
98                       break;
100             case 'w': opts.output_width = atoi(optarg);
101                       assert (opts.output_width > 0);
102                       break;
104             case 'v': opts.verbose = true;
105                       break;
107             case 'h': return false;
108                       break;
110             case '?': // Error in getopt_long
111                       exit(EXIT_FAILURE);
112                       break;
114             default:
115                       cerr << "Unsupported option: " << c << endl;
116                       return false;
117                       break;
118         }
119     }
121     opts.is_camera_input = (opts.input_file.size() > 5 &&
122                             opts.input_file.substr(0, 6) == "camera");
123     if (opts.input_file.size() > 4)
124     {
125         string suffix = opts.input_file.substr(opts.input_file.size() - 4, 4);
126         opts.is_video_input = (suffix == ".mp4") || (suffix == ".avi") ||
127                               (suffix == ".mov");
128     }
130     return true;
133 // Set Video Input and Output
134 bool SetVideoInputOutput(VideoCapture &cap, const cmdline_opts_t& opts,
135                          const char* window_name)
137     if (opts.is_camera_input || opts.is_video_input)
138     {
139         if (opts.is_camera_input)
140         {
141             int port_num = 1;  // if TMDSCM572X camera module on AM57x EVM
142             if (opts.input_file.size() > 6)  // "camera#"
143                 port_num = stoi(opts.input_file.substr(6,
144                                                   opts.input_file.size() - 6));
145             cap = VideoCapture(port_num);
146         }
147         else
148             cap = VideoCapture(opts.input_file);
149         if (! cap.isOpened())
150         {
151             cerr << "Cannot open video input: " << opts.input_file << endl;
152             return false;
153         }
154         namedWindow(window_name, WINDOW_AUTOSIZE | CV_GUI_NORMAL);
155     }
157     return true;