summaryrefslogtreecommitdiff
path: root/sci_gateway1/cpp/opencv_peopleDetector.cpp
blob: ef8dd5fb4c66d893655cc020f94944564b67298b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/********************************************************
Author: Suraj Prakash
[bboxes] = peopleDetector(image)

// 1->image

Optional arguments :

// 2->hitThreshold 
// 3->winStride
// 4->padding
// 5->scale
// 6->finalThreshold
// 7->useMeanshiftGrouping

********************************************************/

#include <numeric>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>
#include <vector>

using namespace cv;
using namespace std;

extern "C"{
  #include "api_scilab.h"
  #include "Scierror.h"
  #include "BOOL.h"
  #include <localization.h>
  #include <sciprint.h>
  #include "../common.h"
  
  int opencv_peopleDetector(char *fname, unsigned long fname_len){

    /// Error management variable
    SciErr sciErr;
    
    /// Variables
    int i, j;
    int intErr;
    int iRows = 0;
    int iCols = 0;
    int iRows1 = 0;
    int iCols1 = 0;
    
    int *piLen = NULL;
    int *piAddr = NULL;
    int *piAddr2 = NULL;
    int *piAddr3 = NULL;
    char **pstData = NULL;
    char *currentArg = NULL;
    bool *providedArgs = NULL; 
    
    double hitThreshold = 0;
    double scale = 1.05; 
    double *winStride = NULL;
    double *padding = NULL;
    double finalThreshold = 2.0;
    int useMeanshiftGrouping = 0;

    double *bboxes = NULL;


    /// for the Hog_SVM_Detector
    HOGDescriptor hog;

    /// found           -> for the found rectangles 
    /// found_filtered  -> the size of the rectangles found is usually greater so, the rectangles are filtered out
    /// bboxes in matlab correspond to found_filtered in this code
    /// answer to hold the modified rectangle after filtering
    vector< Rect > found, found_filtered, answer;

    /// checking input argument
    // 1->image
    // 2->hitThreshold 
    // 3->winStride
    // 4->padding
    // 5->scale
    // 6->finalThreshold
    // 7->useMeanshiftGrouping
    

    CheckInputArgument(pvApiCtx, 1, 13);
    CheckOutputArgument(pvApiCtx, 1, 1);

    /// retrieve image 
    Mat image;
    retrieveImage(image, 1);

    /// Train hog detector for people
    /// Default training is 48 * 96 people images
    /// can be used for detection of larger instances


    /// For the optional arguments
    int nbInputArguments = *getNbInputArgument(pvApiCtx);

    providedArgs = (bool*) malloc(sizeof(bool) * 5); 
    memset(providedArgs, 0, 5);

    winStride = (double*)malloc(sizeof(double) * 2);
    winStride[0] = 8;
    winStride[1] = 8;

    padding = (double*)malloc(sizeof(double) * 2);
    padding[0] = 16;
    padding[1] = 16;


    for(int iter = 2; iter <= nbInputArguments; ++iter){

        /// Address of next argument
        
        sciErr = getVarAddressFromPosition(pvApiCtx, iter, &piAddr); 
        if (sciErr.iErr)
        {
            printError(&sciErr, 0); 
            return 0; 
        }

        // Three calls to getMatrixOfString
        sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, NULL, NULL); 
        if (sciErr.iErr)
        {
            printError(&sciErr, 0); 
            return 0; 
        }
        
        piLen = (int*) malloc(sizeof(int) * iRows * iCols); 
        
        sciErr = getMatrixOfString(pvApiCtx,  piAddr,  &iRows,  &iCols,  piLen,  NULL); 
        if (sciErr.iErr)
        {
            printError(&sciErr, 0); 
            return 0; 
        }
        
        pstData = (char**) malloc(sizeof(char*) * iRows * iCols); 
        for(int k = 0; k < iRows * iCols; ++k)
        {
            pstData[k] = (char*) malloc(sizeof(char) * piLen[k] + 1); 
        }
        
        sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, pstData); 
        if (sciErr.iErr)
        {
            printError(&sciErr, 0); 
            return 0; 
        }

        currentArg = pstData[0];         

            /// getting hit_threshold
            if(strcmp(currentArg, "hitThreshold") == 0)
            {
                if(iter + 1 <= nbInputArguments and !providedArgs[0])
                {
                    sciErr = getVarAddressFromPosition(pvApiCtx, ++iter, &piAddr); 
                    if (sciErr.iErr)
                    {
                        printError(&sciErr, 0); 
                        return 0; 
                    }
                    intErr = getScalarDouble(pvApiCtx, piAddr, &hitThreshold); 
                    if(intErr)
                    {
                        return intErr; 
                    }
                    // Checking if values are in proper range. Same for all optional arguments
                    if(hitThreshold < 0)
                    {
                        Scierror(999, "Error: Nonnegative hitThreshold only allowed\n"); 
                        return 0; 
                    }
                    providedArgs[0] = 1; 
                }
                else if(providedArgs[0]) // Send an error message if an argument is provided more than once. Same for all optional arguments.
                {
                    Scierror(999, "Please provide optional arguments only once.\n"); 
                    return 0; 
                }
                else // Send an error message if name of argument is given but type is incorrect. Same for all optional arguments.
                {
                    Scierror(999, "Incorrect number of arguments provided. Please check the documentation for more information.\n"); 
                    return 0; 
                }
            }

            /// WindowStride
            if(strcmp(currentArg, "winStride") == 0)
            {
                if(iter+1 <= nbInputArguments && !providedArgs[1])
                {
                    sciErr = getVarAddressFromPosition(pvApiCtx, ++iter, &piAddr); 
                    if (sciErr.iErr)
                    {
                        printError(&sciErr, 0); 
                        return 0; 
                    }
                    sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows1, &iCols1, &winStride); 
                    if(sciErr.iErr)
                    {
                        printError(&sciErr, 0); 
                        return 0; 
                    }
                    if(iRows1 != 1 or iCols1 != 2)
                    {
                        Scierror(999, "Incorrect dimension of matrix for argument .\n"); 
                        return 0; 
                    }
                    
                    // Checking if values are in proper range. Same for all optional arguments
                    providedArgs[1] = 1;
                }
                else if(providedArgs[1]) // Send an error message if an argument is provided more than once. Same for all optional arguments.
                {
                    Scierror(999, "Please provide optional arguments only once.\n"); 
                    return 0; 
                }
                else // Send an error message if name of argument is given but type is incorrect. Same for all optional arguments.
                {
                    Scierror(999, "Incorrect number of arguments provided. Please check the documentation for more information.\n"); 
                    return 0; 
                }
            }

            /// Padding
            if(strcmp(currentArg, "padding") == 0)
            {
                if(iter+1 <= nbInputArguments and !providedArgs[2])
                {
                    sciErr = getVarAddressFromPosition(pvApiCtx, ++iter, &piAddr); 
                    if (sciErr.iErr)
                    {
                        printError(&sciErr, 0); 
                        return 0; 
                    }
                    sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows1, &iCols1, &padding); 
                    if(sciErr.iErr)
                    {
                        printError(&sciErr, 0); 
                        return 0; 
                    }
                    if(iRows1 != 1 or iCols1 != 2)
                    {
                        Scierror(999, "Incorrect dimension of matrix for argument .\n"); 
                        return 0; 
                    }
                    
                    // Checking if values are in proper range. Same for all optional arguments
                    providedArgs[2] = 1;
                }
                else if(providedArgs[2]) // Send an error message if an argument is provided more than once. Same for all optional arguments.
                {
                    Scierror(999, "Please provide optional arguments only once.\n"); 
                    return 0; 
                }
                else // Send an error message if name of argument is given but type is incorrect. Same for all optional arguments.
                {
                    Scierror(999, "Incorrect number of arguments provided. Please check the documentation for more information.\n"); 
                    return 0; 
                }
            }

            /// Scalefactor
            if(strcmp(currentArg, "scale") == 0)
            {
                if(iter + 1 <= nbInputArguments and !providedArgs[3])
                {
                    sciErr = getVarAddressFromPosition(pvApiCtx, ++iter, &piAddr); 
                    if (sciErr.iErr)
                    {
                        printError(&sciErr, 0); 
                        return 0; 
                    }
                    intErr = getScalarDouble(pvApiCtx, piAddr, &scale); 
                    if(intErr)
                    {
                        return intErr; 
                    }
                    // Checking if values are in proper range. Same for all optional arguments
                    if(scale < 1.0001)
                    {
                        Scierror(999, "Error: Wrong ScaleFactor given. Cannot be less than 1.0001\n"); 

                        return 0; 
                    }
                    providedArgs[3] = 1; 
                }
                else if(providedArgs[3]) // Send an error message if an argument is provided more than once. Same for all optional arguments.
                {
                    Scierror(999, "Please provide optional arguments only once.\n"); 
                    return 0; 
                }
                else // Send an error message if name of argument is given but type is incorrect. Same for all optional arguments.
                {
                    Scierror(999, "Incorrect number of arguments provided. Please check the documentation for more information.\n"); 
                    return 0; 
                }
            }

            

            /// finalThreshold
            if(strcmp(currentArg, "finalThreshold") == 0)
            {
                if(iter + 1 <= nbInputArguments and !providedArgs[4])
                {
                    sciErr = getVarAddressFromPosition(pvApiCtx, ++iter, &piAddr); 
                    if (sciErr.iErr)
                    {
                        printError(&sciErr, 0); 
                        return 0; 
                    }
                    intErr = getScalarDouble(pvApiCtx, piAddr, &finalThreshold); 
                    if(intErr)
                    {
                        return intErr; 
                    }

                    if(finalThreshold < 0)
                    {
                        Scierror(999, "Incorrect value for argument .\n"); 
                        return 0; 
                    }
                    
                    // Checking if values are in proper range. Same for all optional arguments
                    providedArgs[4] = 1;
                }
                else if(providedArgs[4]) // Send an error message if an argument is provided more than once. Same for all optional arguments.
                {
                    Scierror(999, "Please provide optional arguments only once.\n"); 
                    return 0; 
                }
                else // Send an error message if name of argument is given but type is incorrect. Same for all optional arguments.
                {
                    Scierror(999, "Incorrect number of arguments provided. Please check the documentation for more information.\n"); 
                    return 0; 
                }
            }

            /// UseMeanshiftGrouping
            if(strcmp(currentArg, "useMeanshiftGrouping")==0)
            {
                if(iter + 1 <= nbInputArguments and !providedArgs[5])
                {
                    sciErr = getVarAddressFromPosition(pvApiCtx, ++iter, &piAddr); 
                    if (sciErr.iErr)
                    {
                        printError(&sciErr, 0); 
                        return 0; 
                    }
                    intErr = getScalarBoolean(pvApiCtx, piAddr, &useMeanshiftGrouping);  
                    if(sciErr.iErr)
                    {
                        printError(&sciErr, 0); 
                        return 0; 
                    }
                    if(useMeanshiftGrouping > 1 or useMeanshiftGrouping < 0)
                    {
                        Scierror(999, "Incorrect value for argument .\n"); 
                        return 0; 
                    }
                    
                    // Checking if values are in proper range. Same for all optional arguments
                    providedArgs[5] = 1;
                }
                else if(providedArgs[5]) // Send an error message if an argument is provided more than once. Same for all optional arguments.
                {
                    Scierror(999, "Please provide optional arguments only once.\n"); 
                    return 0; 
                }
                else // Send an error message if name of argument is given but type is incorrect. Same for all optional arguments.
                {
                    Scierror(999, "Incorrect number of arguments provided. Please check the documentation for more information.\n"); 
                    return 0; 
                }
            }

    }   

    /// End of error check and input get;

    hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());

    ///virtual void cv::HOGDescriptor::detectMultiScale 
        // ( InputArray  img, 
        //   std::vector< Rect > &   foundLocations,
        //   double  hitThreshold = 0,
        //   Size  winStride = Size(),
        //   Size  padding = Size(),
        //   double  scale = 1.05,
        //   double  finalThreshold = 2.0,
        //   bool  useMeanshiftGrouping = false 
        // ) 

    hog.detectMultiScale(image, found, hitThreshold, Size(winStride[0], winStride[1]), Size(padding[0], padding[0]), scale, finalThreshold, useMeanshiftGrouping);
    
    /// finding out the rectangles
    
    for (i = 0; i < found.size(); ++i){
        Rect r = found[i];
        for (j = 0; j < found.size(); ++j)
            /// removing same identified people
            if (j != i and (r & found[j]) == r)
                break;

        /// if no duplicate found
        if (j == found.size())
            found_filtered.push_back(r);
    }

    /// filtering out the rectangles
    for (i=0; i<found_filtered.size(); i++){
        Rect r = found_filtered[i];
        
        r.x += cvRound(r.width*0.1);
        r.y += cvRound(r.height*0.06);

        r.width = cvRound(r.width*0.8);
        r.height = cvRound(r.height*0.9);

        answer.push_back(r);
    }

    ///  bboxex[i] = [x y width height];
    bboxes = (double*)malloc(sizeof(double) * (int)answer.size() * 4);

    int total_boxes = (int)answer.size();
    
    for(i = 0; i < answer.size(); ++i){
      Rect temp = answer[i];

      // x coordinate
      bboxes[i + 0 * total_boxes] = temp.x;

      // y coordinate
      bboxes[i + 1 * total_boxes] = temp.y;

      // width
      bboxes[i + 2 * total_boxes] = temp.width;

      // height
      bboxes[i + 3 * total_boxes] = temp.height;
    }

   

                                                                                            // rows  // columns
    sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, total_boxes, 4, bboxes);
    if(sciErr.iErr){
      printError(&sciErr, 0);
      return 0;
    }

    //Assigning the list as the Output Variable
    AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;

    //Returning the Output Variables as arguments to the Scilab environment
    ReturnArguments(pvApiCtx);
    return 0;

  }
/* ==================================================================== */
}