Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
parkpow
GitHub Repository: parkpow/deep-license-plate-recognition
Path: blob/master/cpp/linux/numberPlate.cpp
643 views
1
#include "curl/curl.h"
2
#include <iostream>
3
#include <string>
4
#include "json/json/json.h"
5
#include "json/jsoncpp.cpp"
6
#include <fstream>
7
8
using namespace std;
9
10
namespace
11
{
12
size_t callback(
13
const char* in,
14
std::size_t size,
15
std::size_t num,
16
std::string* out)
17
{
18
const size_t totalBytes(size * num);
19
out->clear();
20
out->append(in, totalBytes);
21
return totalBytes;
22
}
23
}
24
25
Json::Value sendRequest(string auth_token, string fileName, string mode) {
26
27
curl_global_init(CURL_GLOBAL_ALL);
28
29
30
curl_off_t speed_upload, total_time;
31
curl_mime *form = NULL;
32
curl_mimepart *field = NULL;
33
34
CURL *hnd = curl_easy_init();
35
36
//curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
37
curl_easy_setopt(hnd, CURLOPT_URL, "https://api.platerecognizer.com/v1/plate-reader/");
38
39
form = curl_mime_init(hnd); //initialize form fields
40
41
/* Fill in the file upload field */
42
field = curl_mime_addpart(form);
43
curl_mime_name(field, "upload");
44
curl_mime_filedata(field, fileName.c_str());
45
46
// Add more fields
47
// config mode
48
if(mode.length()) {
49
curl_mimepart *part = NULL;
50
part = curl_mime_addpart(form);
51
curl_mime_name(part, "config");
52
53
if (strcmp(mode.c_str(),"redaction") == 0){
54
curl_mime_data(part, "{\"mode\":\"redaction\"}", CURL_ZERO_TERMINATED);
55
} else if (strcmp(mode.c_str(),"fast") == 0){
56
curl_mime_data(part, "{\"mode\":\"fast\"}", CURL_ZERO_TERMINATED);
57
} else{
58
cout << "Unknown config mode : "+mode+"\n Valid Options: fast, redaction\n";
59
exit(1);
60
}
61
}
62
63
curl_easy_setopt(hnd, CURLOPT_MIMEPOST, form);
64
65
struct curl_slist *headers = NULL;
66
headers = curl_slist_append(headers, "cache-control: no-cache");
67
headers = curl_slist_append(headers, ("Authorization: Token "+ auth_token).c_str());
68
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
69
unique_ptr<std::string> httpData(new std::string()); //initializing string pointer to get data
70
// Hook up data handling function.
71
curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, callback);
72
73
// Hook up data container (will be passed as the last parameter to the
74
// callback handling function). Can be any pointer type, since it will
75
// internally be passed as a void pointer.
76
curl_easy_setopt(hnd, CURLOPT_WRITEDATA, httpData.get());
77
78
CURLcode ret = curl_easy_perform(hnd); //perform the request
79
80
if (ret != CURLE_OK) { //failed case
81
fprintf(stderr, "curl_easy_perform() failed: %s\n",
82
curl_easy_strerror(ret));
83
}
84
else {
85
/* now extract transfer info */
86
curl_easy_getinfo(hnd, CURLINFO_SPEED_UPLOAD_T, &speed_upload);
87
curl_easy_getinfo(hnd, CURLINFO_TOTAL_TIME_T, &total_time);
88
89
fprintf(stderr, "Speed: %" CURL_FORMAT_CURL_OFF_T " bytes/sec during %"
90
CURL_FORMAT_CURL_OFF_T ".%06ld seconds\n",
91
speed_upload,
92
(total_time / 1000000), (long)(total_time % 1000000));
93
}
94
95
curl_easy_cleanup(hnd);
96
curl_mime_free(form);
97
98
Json::Value jsonData;
99
Json::Reader jsonReader;
100
101
if (jsonReader.parse(*httpData, jsonData))
102
{
103
cout << jsonData;
104
}
105
else
106
{
107
std::cout << "Could not parse HTTP data as JSON" << std::endl;
108
std::cout << "HTTP data was:\n" << *httpData.get() << std::endl;
109
}
110
111
return jsonData;
112
}
113
114
115
116
int main(int argc, char *argv[])
117
{
118
string token = "MY_API_KEY";
119
Json::Value data;
120
if (argc >= 2) {
121
data = sendRequest(token, argv[1], argv[2]);
122
}
123
else {
124
cout << "Error:Invalid Arguments!\nUsage: program.exe <Image>\n program.exe <Image> <ConfigMode>";
125
}
126
127
ofstream file;
128
file.open("responce.txt", ios::app);
129
file << data;
130
file << "\n\n";
131
file.close();
132
cout << "\n\n";
133
//system("PAUSE");
134
return 0;
135
}
136
137