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
|
/***************************************************
Author : Tanmay Chaudhari, Shashank Shekhar
**************************************************/
#include <numeric>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>
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"
//#include "../common.cpp"
Ptr<VideoCapture> capture;
double videoCurrentTime = 0;
double videoDuration = 0;
int videoFPS;
char *videoFileName = NULL;
bool videoObjectSt = false;
int opencv_VideoReader(char *fname, unsigned long fname_len)
{
SciErr sciErr;
int *piAddr = NULL;
int *piAddrVal = NULL;
int *piLen = NULL;
int iRows, iCols = 0;
char ** pstData1 = NULL;
char ** pstData2 = NULL;
double frames;
double width;
double height;
double CT;
char full_path[PATH_MAX];
bool stillLeft = false;
CheckInputArgument(pvApiCtx,1,3);
CheckOutputArgument(pvApiCtx,1,10);
int noOfarguments = *getNbInputArgument(pvApiCtx);
if(noOfarguments==2)
{
Scierror(999,"Invalid number of Arguments\n");
return 0;
}
sciErr = getVarAddressFromPosition(pvApiCtx,1,&piAddr);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
if(!isStringType(pvApiCtx, piAddr))
{
Scierror(999,"Invalid Argument\n");
return 0;
}
//first call to get rows and columns
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, NULL, NULL);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
piLen = (int*)malloc(sizeof(int) * iRows * iCols);
//second call to retrieve length of each string
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, NULL);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
pstData1 = (char**)malloc(sizeof(char*) * iRows * iCols);
for(int j = 0 ; j < iRows * iCols ; j++)
{
pstData1[j] = (char*)malloc(sizeof(char) * (piLen[j] + 1));//+ 1 for null termination
}
//third call to retrieve data
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, pstData1);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
if(noOfarguments>1)
{
sciErr = getVarAddressFromPosition(pvApiCtx,2,&piAddr);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
if(!isStringType(pvApiCtx, piAddr))
{
Scierror(999,"Invalid Argument\n");
return 0;
}
//first call to get rows and columns
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, NULL, NULL);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
piLen = (int*)malloc(sizeof(int) * iRows * iCols);
//second call to retrieve length of each string
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, NULL);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
pstData2 = (char**)malloc(sizeof(char*) * iRows * iCols);
for(int j = 0 ; j < iRows * iCols ; j++)
{
pstData2[j] = (char*)malloc(sizeof(char) * (piLen[j] + 1));//+ 1 for null termination
}
//third call to retrieve data
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, pstData2);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
if(strcmp(pstData2[0], "CurrentTime")==0)
{
sciErr = getVarAddressFromPosition(pvApiCtx,3,&piAddrVal);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
if(!isDoubleType(pvApiCtx, piAddrVal) || isVarComplex(pvApiCtx,piAddrVal) || !isScalar(pvApiCtx, piAddrVal))
{
Scierror(999," Invalid Value for Current Time.\n");
return 0;
}
getScalarDouble(pvApiCtx, piAddrVal, &CT);
if(CT<0)
{
Scierror(999,"Invalid Input for CurrentTime.\n");
return 0;
}
CT*=1000;
}
else
{
Scierror(999,"Invalid Argument\n");
return 0;
}
}
if(videoObjectSt==true && strcmp(videoFileName, pstData1[0])==0)
{
if(int(videoCurrentTime) >= int(videoDuration))
{
stillLeft = false;
}
else if(int(videoCurrentTime) < int(videoDuration))
stillLeft = true;
}
if(!stillLeft)
{
capture = new VideoCapture(pstData1[0]);
if(!capture->isOpened())
{
Scierror(999,"Could not open file\n");
return 0;
}
}
videoFileName = pstData1[0];
videoFPS = capture->get(CV_CAP_PROP_FPS);
frames = capture->get(CV_CAP_PROP_FRAME_COUNT);
if(noOfarguments!=1)
{
if(int(CT) >= int(videoDuration))
{
Scierror(999,"Current Time should be less than Video Duration\n");
return 0;
}
capture->set(CV_CAP_PROP_POS_MSEC, CT);
}
videoObjectSt = true;
videoCurrentTime = capture->get(CV_CAP_PROP_POS_MSEC);
videoDuration = (1000/double(videoFPS))*double(frames);
width = capture->get(CV_CAP_PROP_FRAME_WIDTH);
height = capture->get(CV_CAP_PROP_FRAME_HEIGHT);
realpath(pstData1[0], full_path);
createSingleString(pvApiCtx, nbInputArgument(pvApiCtx) + 1, pstData1[0]);
createScalarDouble(pvApiCtx,nbInputArgument(pvApiCtx) + 2, videoDuration);
createScalarDouble(pvApiCtx,nbInputArgument(pvApiCtx) + 3, videoFPS);
createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx)+4, videoCurrentTime);
createScalarDouble(pvApiCtx,nbInputArgument(pvApiCtx) + 5, frames);
createScalarDouble(pvApiCtx,nbInputArgument(pvApiCtx) + 6, width);
createScalarDouble(pvApiCtx,nbInputArgument(pvApiCtx) + 7, height);
createSingleString(pvApiCtx, nbInputArgument(pvApiCtx)+8, full_path);
for(int i=0;i<=8;i++)
{
AssignOutputVariable(pvApiCtx, i) = nbInputArgument(pvApiCtx) + i;
}
ReturnArguments(pvApiCtx);
return 0;
}
int has_frame(char *fname, unsigned long fname_len)
{
SciErr sciErr;
if(videoFileName==NULL)
{
Scierror(999,"No video has been read yet.\n");
return 0;
}
int *piAddr = NULL;
int *piLen = NULL;
int iRows, iCols = 0;
char ** pstData = NULL;
CheckInputArgument(pvApiCtx,0,0);
CheckOutputArgument(pvApiCtx,0,1);
if(videoObjectSt == false)
{
Scierror(999,"Invalid Video File. You need to read the video using VideoReader.\n");
return 0;
}
if(videoCurrentTime>videoDuration)
createScalarBoolean(pvApiCtx,nbInputArgument(pvApiCtx) + 1, false);
else if(int(videoCurrentTime) == int(videoDuration))
createScalarBoolean(pvApiCtx,nbInputArgument(pvApiCtx) + 1, false);
else
createScalarBoolean(pvApiCtx,nbInputArgument(pvApiCtx) + 1, true);
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
ReturnArguments(pvApiCtx);
return 0;
}
int read_frame(char *fname, unsigned long fname_len)
{
SciErr sciErr;
if(videoFileName==NULL)
{
Scierror(999,"No video has been read yet.\n");
return 0;
}
if(int(videoCurrentTime)>=int(videoDuration))
{
Scierror(999,"No more frames left to be read\n");
return 0;
}
//sciprint("Current Time of the Video is : %f\n", videoCurrentTime);
int *piAddr = NULL;
int *piLen = NULL;
int iRows, iCols = 0;
char ** pstData = NULL;
int *singleFrameList = NULL;
unsigned char *red = NULL;
unsigned char *green = NULL;
unsigned char *blue = NULL;
CheckInputArgument(pvApiCtx,0,0);
CheckOutputArgument(pvApiCtx,0,1);
if(videoObjectSt == false)
{
Scierror(999,"Invalid Video File. You need to read the video using VideoReader.\n");
return 0;
}
//--------------------------------------- Processing Frame at the particular frame -----------------------------------
Mat frame;
capture->set(CV_CAP_PROP_POS_MSEC, videoCurrentTime);
if(!capture->read(frame))
{
Scierror(999, "Can't read Frame");
return 0;
}
//--------------------------------------------------------------------------------------------------------------------
if(frame.channels()==1)
{
sciErr = createList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, &singleFrameList);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
red = (unsigned char *)malloc(sizeof(unsigned char)*frame.rows*frame.cols);
for(int k=0;k<frame.rows;k++)
for(int p=0;p<frame.cols;p++)
red[k+frame.rows*p]=frame.at<uchar>(k, p);
sciErr = createMatrixOfUnsignedInteger8InList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, singleFrameList, 1, frame.rows, frame.cols, red);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
free(red);
}
else
{
sciErr = createList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 3, &singleFrameList);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
red = (unsigned char *)malloc(sizeof(unsigned char)*frame.rows*frame.cols);
green = (unsigned char *)malloc(sizeof(unsigned char)*frame.rows*frame.cols);
blue = (unsigned char *)malloc(sizeof(unsigned char)*frame.rows*frame.cols);
for(int k=0;k<frame.rows;k++)
{
for(int p=0;p<frame.cols;p++)
{
Vec3b intensity = frame.at<Vec3b>(k, p);
red[k+frame.rows*p]=intensity.val[2];
green[k+frame.rows*p]=intensity.val[1];
blue[k+frame.rows*p]=intensity.val[0];
}
}
sciErr = createMatrixOfUnsignedInteger8InList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, singleFrameList, 1, frame.rows, frame.cols, red);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = createMatrixOfUnsignedInteger8InList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, singleFrameList, 2, frame.rows, frame.cols, green);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = createMatrixOfUnsignedInteger8InList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, singleFrameList, 3, frame.rows, frame.cols, blue);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
free(red);
free(green);
free(blue);
}
videoCurrentTime+=(1000/double(videoFPS));
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
ReturnArguments(pvApiCtx);
return 0;
}
}
|