aws / aws-sdk-cpp

AWS SDK for C++
Apache License 2.0
1.97k stars 1.06k forks source link

Index Faces giving segfault #827

Closed mschaudhari closed 6 years ago

mschaudhari commented 6 years ago

I'm having a problem with Index_Faces using Amazon aws-cpp-sdk. I'm getting segmentation fault in following program.

Image *fileData;

Image& imageToBytes(const char* str)
{
    FILE *fp;
    size_t length = 0;

    fp= fopen(str, "rb");
    if(fp == NULL)
    {
        exit(0);
    }
    fseek(fp, 0, SEEK_END);
    length= ftell(fp);
    rewind(fp);
    fileData= (Image*)malloc((length+1)*sizeof(char));
    fread(fileData, length, 1, fp);
    return *fileData;
}

int main()
{
   Aws::SDKOptions options;

   Aws::InitAPI(options);
   {
       RekognitionClient *rekClient = new RekognitionClient();
       CreateCollectionRequest *clRequest = new CreateCollectionRequest();

       CreateCollectionRequest str = clRequest->WithCollectionId("collection7981");

       CreateCollectionOutcome respose = rekClient->CreateCollection(str);
       std::cout<<"Collection Successfully Created..."<<std::endl;

       IndexFacesRequest iFaceRequest;

       iFaceRequest.WithImage(imageToBytes("/home/msc/Profile_Pics/ms.JPG"));

   }
   Aws::ShutdownAPI(options);
   return 0;
}

So, how to provide image file from my local system to amazon aws-cpp-sdk?

bretambrose commented 6 years ago

You can't just cast raw bytes into an Image object.

Replace:

fileData= (Image*)malloc((length+1)*sizeof(char));
fread(fileData, length, 1, fp);

With (completely untested):

Aws::Utils::ByteBuffer rawFileData(length);
fread(rawFileData.GetUnderlyingData(), 1, length, fp);
fileData = new Image;
fileData->SetBytes(rawFileData);
mschaudhari commented 6 years ago

Thanks for your response. It's working fine.