亚马逊aws-cpp-sdk Index Faces给出了段错误

时间:2018-03-23 07:13:50

标签: c++ c++11 segmentation-fault aws-sdk-cpp

我使用亚马逊 aws-cpp-sdk Index_Faces时出现问题。我在以下程序中遇到分段错误。

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;
}

那么,如何从我的本地系统提供图像文件到amazon aws-cpp-sdk?

1 个答案:

答案 0 :(得分:3)

您无法使用malloc创建c ++类(实际上您可以,但之后需要调用构造函数)。您需要使用new而不是为您调用构造函数。

在aws-cpp-sdk中有4个不同的类叫做“Image”,它们都不适合直接传递给fread