cocodataset / cocoapi

COCO API - Dataset @ http://cocodataset.org/
Other
6.05k stars 3.75k forks source link

Octave Support #392

Open btalberg opened 4 years ago

btalberg commented 4 years ago

Anyone have experience running the cocoapi with Octave? I'm struggling using the gason library. I'm running on OX 10.15.3, using Octave version 5.2.0 (installed using brew). I'm able to compile the gasonMex binary using:

export CXXFLAGS='-Wno-c++11-narrowing -Wall -Wextra'
mkoctfile --output private/gasonMex -I../common --mex -v private/gasonMex.cpp ../common/gason.cpp

When I try the example in the gason.m file, I'm seeing the following:

MatlabAPI>> o = struct('first',{'piotr','ty'},'last',{'dollar','lin'})
MatlabAPI>> s = gason( o )
s = [{"first":"piotr","last":"dollar
MatlabAPI>> p = gason( s )
error: gasonMex: breaking bad
error: called from
    gason at line 53 column 5

Tried a few other JSON strings as well:

MatlabAPI>> gason('[{"first":"piotr"}]')
error: gasonMex: bad string
error: called from
    gason at line 53 column 5
MatlabAPI>> gason('[{}]')
error: gasonMex: mismatch bracket
error: called from
    gason at line 53 column 5
MatlabAPI>> gason('[]')
error: gasonMex: breaking bad
error: called from
    gason at line 53 column 5
MatlabAPI>> gason('{}')
error: gasonMex: breaking bad
error: called from
    gason at line 53 column 5
MatlabAPI>> gason('{"H":"I"}')
error: gasonMex: unexpected character
error: called from
    gason at line 53 column 5

Any suggestions/insights?

wkt commented 2 years ago

Change mxArrayToStringRobust in gasonMex.cpp to:

char* mxArrayToStringRobust( const mxArray *M ) {
#ifdef HAVE_OCTAVE
  return mxArrayToString(M);
#else
  // convert Matlab string to char* (robust version of mxArrayToString)
  if(!mxIsChar(M)) mexErrMsgTxt("String expected.");
  ushort *c=(ushort*) mxGetData(M); char* str; siz n;
  n=mxGetNumberOfElements(M); str=(char*) mxMalloc(n+1);
  for( siz i=0; i<n; i++ ) str[i]=c[i]; str[n]=0; return str;
#endif
}

then recompile the code:

$ cd path_of_private
$ CXXFLAGS="-std=c++11 -Wall -fPIC -I../../common" mkoctfile  --mex -DMATLAB_MEX_FILE -g gasonMex.cpp ../../common/gason.cpp
$ CFLAGS="-std=c11 -Wall -fPIC -I../../common" mkoctfile  --mex -DMATLAB_MEX_FILE -v -g maskApiMex.c ../../common/maskApi.c

It is work for me,I hope that can help !