Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Can I upload OpenCV Mat with curl?

Can I upload OpenCV Mat with curl? Without save on drive?

Can I upload OpenCV Mat with curl?

Can I upload OpenCV Mat with curl? Without save on drive?drive? I have Mat image and want upload him like I sent a picture from drive with C++ curl:

curl_formadd(&formpost,
    &lastptr,
    CURLFORM_COPYNAME, "picture",
    CURLFORM_COPYCONTENTS, "sample.jpg"
    CURLFORM_END);

Can I upload OpenCV Mat with curl?

Can I upload OpenCV Mat with curl? Without save on drive? I have Mat image and want upload him like I sent a picture from drive with C++ curl:

curl_formadd(&formpost,
    &lastptr,
    CURLFORM_COPYNAME, "picture",
    CURLFORM_COPYCONTENTS, "sample.jpg"
    CURLFORM_END);

Solution:

int writer(char *data, size_t size, size_t nmemb, string *writerData)
{
    if (writerData == NULL)
        return 0;
    int len = size*nmemb;
    writerData->append(data, len);
    return len;
}

 void upLoad(Mat img){
vector<uchar> vec_Img;

vector<int> vecCompression_params;

vecCompression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
vecCompression_params.push_back(90);
imencode(".jpg", img, vec_Img, vecCompression_params);

CURL *curl;
CURLcode res;

string buffer;

struct curl_httppost *post=NULL;
struct curl_httppost *last=NULL;

curl = curl_easy_init();
if (curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "URL");
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    struct curl_slist *chunk = NULL;
    chunk = curl_slist_append(chunk, "Accept:");

    chunk = curl_slist_append(chunk, "Authorization: Token ");

    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);


    curl_formadd(&post, &last, 
        CURLFORM_PTRNAME, "photo",
        CURLFORM_BUFFER, "photo.jpg",
        CURLFORM_BUFFERPTR, vec_Img.data(),
        CURLFORM_BUFFERLENGTH, vec_Img.size(),
        CURLFORM_CONTENTTYPE, "image/jpg",
            CURLFORM_END);


    curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);

    curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    res = curl_easy_perform(curl);

    if (res != CURLE_OK)
        fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));

    curl_easy_cleanup(curl);
}

Can I upload OpenCV Mat with curl?

Can I upload OpenCV Mat with curl? Without save on drive? I have Mat image and want upload him like I sent a picture from drive with C++ curl:

curl_formadd(&formpost,
    &lastptr,
    CURLFORM_COPYNAME, "picture",
    CURLFORM_COPYCONTENTS, "sample.jpg"
    CURLFORM_END);

Solution:

int writer(char *data, size_t size, size_t nmemb, string *writerData)
{
    if (writerData == NULL)
        return 0;
    int len = size*nmemb;
    writerData->append(data, len);
    return len;
}

 void upLoad(Mat img){
vector<uchar> vec_Img;

vector<int> vecCompression_params;

vecCompression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
vecCompression_params.push_back(90);
imencode(".jpg", img, vec_Img, vecCompression_params);

CURL *curl;
CURLcode res;

string buffer;

struct curl_httppost *post=NULL;
struct curl_httppost *last=NULL;

curl = curl_easy_init();
if (curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "URL");
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    struct curl_slist *chunk = NULL;
    chunk = curl_slist_append(chunk, "Accept:");

    chunk = curl_slist_append(chunk, "Authorization: Token ");

    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);


    curl_formadd(&post, &last, 
        CURLFORM_PTRNAME, "photo",
        CURLFORM_BUFFER, "photo.jpg",
        CURLFORM_BUFFERPTR, vec_Img.data(),
        CURLFORM_BUFFERLENGTH, vec_Img.size(),
        CURLFORM_CONTENTTYPE, "image/jpg",
            CURLFORM_END);


    curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);

    curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    res = curl_easy_perform(curl);

    if (res != CURLE_OK)
        fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));

    curl_easy_cleanup(curl);
}