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