Path: blob/master/cpp/linux/numberPlate.cpp
643 views
#include "curl/curl.h"1#include <iostream>2#include <string>3#include "json/json/json.h"4#include "json/jsoncpp.cpp"5#include <fstream>67using namespace std;89namespace10{11size_t callback(12const char* in,13std::size_t size,14std::size_t num,15std::string* out)16{17const size_t totalBytes(size * num);18out->clear();19out->append(in, totalBytes);20return totalBytes;21}22}2324Json::Value sendRequest(string auth_token, string fileName, string mode) {2526curl_global_init(CURL_GLOBAL_ALL);272829curl_off_t speed_upload, total_time;30curl_mime *form = NULL;31curl_mimepart *field = NULL;3233CURL *hnd = curl_easy_init();3435//curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");36curl_easy_setopt(hnd, CURLOPT_URL, "https://api.platerecognizer.com/v1/plate-reader/");3738form = curl_mime_init(hnd); //initialize form fields3940/* Fill in the file upload field */41field = curl_mime_addpart(form);42curl_mime_name(field, "upload");43curl_mime_filedata(field, fileName.c_str());4445// Add more fields46// config mode47if(mode.length()) {48curl_mimepart *part = NULL;49part = curl_mime_addpart(form);50curl_mime_name(part, "config");5152if (strcmp(mode.c_str(),"redaction") == 0){53curl_mime_data(part, "{\"mode\":\"redaction\"}", CURL_ZERO_TERMINATED);54} else if (strcmp(mode.c_str(),"fast") == 0){55curl_mime_data(part, "{\"mode\":\"fast\"}", CURL_ZERO_TERMINATED);56} else{57cout << "Unknown config mode : "+mode+"\n Valid Options: fast, redaction\n";58exit(1);59}60}6162curl_easy_setopt(hnd, CURLOPT_MIMEPOST, form);6364struct curl_slist *headers = NULL;65headers = curl_slist_append(headers, "cache-control: no-cache");66headers = curl_slist_append(headers, ("Authorization: Token "+ auth_token).c_str());67curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);68unique_ptr<std::string> httpData(new std::string()); //initializing string pointer to get data69// Hook up data handling function.70curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, callback);7172// Hook up data container (will be passed as the last parameter to the73// callback handling function). Can be any pointer type, since it will74// internally be passed as a void pointer.75curl_easy_setopt(hnd, CURLOPT_WRITEDATA, httpData.get());7677CURLcode ret = curl_easy_perform(hnd); //perform the request7879if (ret != CURLE_OK) { //failed case80fprintf(stderr, "curl_easy_perform() failed: %s\n",81curl_easy_strerror(ret));82}83else {84/* now extract transfer info */85curl_easy_getinfo(hnd, CURLINFO_SPEED_UPLOAD_T, &speed_upload);86curl_easy_getinfo(hnd, CURLINFO_TOTAL_TIME_T, &total_time);8788fprintf(stderr, "Speed: %" CURL_FORMAT_CURL_OFF_T " bytes/sec during %"89CURL_FORMAT_CURL_OFF_T ".%06ld seconds\n",90speed_upload,91(total_time / 1000000), (long)(total_time % 1000000));92}9394curl_easy_cleanup(hnd);95curl_mime_free(form);9697Json::Value jsonData;98Json::Reader jsonReader;99100if (jsonReader.parse(*httpData, jsonData))101{102cout << jsonData;103}104else105{106std::cout << "Could not parse HTTP data as JSON" << std::endl;107std::cout << "HTTP data was:\n" << *httpData.get() << std::endl;108}109110return jsonData;111}112113114115int main(int argc, char *argv[])116{117string token = "MY_API_KEY";118Json::Value data;119if (argc >= 2) {120data = sendRequest(token, argv[1], argv[2]);121}122else {123cout << "Error:Invalid Arguments!\nUsage: program.exe <Image>\n program.exe <Image> <ConfigMode>";124}125126ofstream file;127file.open("responce.txt", ios::app);128file << data;129file << "\n\n";130file.close();131cout << "\n\n";132//system("PAUSE");133return 0;134}135136137