-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoMoreCatZ.cpp
374 lines (331 loc) · 14.5 KB
/
NoMoreCatZ.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// NoMoreCatZ.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#define _TFLITE
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#ifdef _TFLITE
//#include "tensorflow/lite/c/c_api.h"
//#include "tensorflow/lite/c/c_api_internal.h"
#include "tensorflow/lite/c/c_api_experimental.h"
/*#include "absl/memory/memory.h"
#include "tensorflow/lite/delegates/nnapi/nnapi_delegate.h"
#include "tensorflow/lite/delegates/xnnpack/xnnpack_delegate.h"
//#include "tensorflow/lite/examples/label_image/bitmap_helpers.h"
#include "tensorflow/lite/examples/label_image/get_top_n.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/optional_debug_tools.h"
#include "tensorflow/lite/profiling/profiler.h"
#include "tensorflow/lite/string_util.h"
#include "tensorflow/lite/tools/command_line_flags.h"
#include "tensorflow/lite/tools/delegates/delegate_provider.h"
#include "tensorflow/lite/tools/evaluation/utils.h"*/
#pragma comment(lib,"tensorflow2/lib/tensorflowlite_c.dll.if.lib")
#else
#include "tensorflow/c/c_api.h"
#pragma comment(lib,"tensorflow2/lib/tensorflow.lib")
#endif
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#ifdef _DEBUG
#pragma comment(lib,"opencv/lib/opencv_world343d.lib")
#else
#pragma comment(lib,"opencv/lib/opencv_world343.lib")
#endif
using namespace std;
using namespace cv;
void NoOpDeallocator(void* data, size_t a, void* b) {}
BYTE* ParseJson(char* json, unsigned *width, unsigned* height, unsigned *type, float *cutLevel) {
BYTE* res = NULL;
// Get height and width
sscanf_s(json, "{\"type\":%d,\"cut\":%f,\"width\":%d,\"height\":%d,", type, cutLevel, width, height);
cerr << "Data: " << *type << ", " << *width << ", " << *height << endl;
// allocate buffer
res = (BYTE*)malloc((*width) * (*height) * 4);
// copy data to buffer
int ind = 1, pos = 0, dpos = 0;
char buffer[4] = { 0,0,0,0 };
while (json[ind] != '{') ind++;
while (json[ind] != '}') {
switch (json[ind+1]) {
case ':': dpos = ind + 2; ind++; break; // start number
case ',': case '}': strncpy_s(buffer, json+dpos, ind+1-dpos);// end number
buffer[ind+1-dpos] = 0;
res[pos++] = atoi(buffer);
ind++;
break;
default: ind++;
}
}
return res;
}
int main(int argc, char* argv[])
{
_setmode(_fileno(stdin), _O_BINARY);
cerr << "NoMoreCatZ v0.3.0\n";
//cout << "Tensorflow " << TF_Version() << endl;
if (argc < 2) {
cout << "Usage:" << endl
<< "NoMoreCatz -register [id]\t- Register app for Chrome" << endl
<< "Don't forget to get plugin ID from Chrome!" << endl;
}
else {
string url = argv[1];
string cpid;
if (argc >= 3)
cpid = argv[2];
string manp2;
string manp1 = "{ \"name\": \"com.ttroll.nomorecatz\",\n\"version\" : \"0.2\",\n\"description\" : \"This extension block images with cats using host-based AI\",\n\"path\" : \"";
if (cpid == "")
manp2 = "\",\n\"type\": \"stdio\",\n\"allowed_origins\" : [ \"chrome-extension://ikkdkejghellfjlnjmfmllhklcgponce/\" ] ,\n\"manifest_version\" : 2\n}";
else
manp2 = "\",\n\"type\": \"stdio\",\n\"allowed_origins\" : [ \"chrome-extension://" + cpid + "/\" ] ,\n\"manifest_version\" : 2\n}";
//cout << "URL: " << url << endl;
if (url == "-register") {
// add to registry
string manp2;
string manp1 = "{ \"name\": \"com.ttroll.nomorecatz\",\n\"version\" : \"0.2\",\n\"description\" : \"This extension block images with cats using host-based AI\",\n\"path\" : \"";
if (cpid == "")
manp2 = "\",\n\"type\": \"stdio\",\n\"allowed_origins\" : [ \"chrome-extension://ikkdkejghellfjlnjmfmllhklcgponce/\" ] ,\n\"manifest_version\" : 2\n}";
else
manp2 = "\",\n\"type\": \"stdio\",\n\"allowed_origins\" : [ \"chrome-extension://" + cpid + "/\" ] ,\n\"manifest_version\" : 2\n}";
cout << "Registering plugin..." << endl;
DWORD dwDisposition;
HKEY hKey1 = NULL;
RegCreateKeyEx(HKEY_CURRENT_USER,
TEXT("SOFTWARE\\Google\\Chrome\\NativeMessagingHosts\\com.ttroll.nomorecatz"),
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,//KEY_WRITE,
NULL,
&hKey1,
&dwDisposition);
char pathBuffer[2048];
GetCurrentDirectoryA(2047, pathBuffer);
CharLowerA(pathBuffer);
string pathMan(pathBuffer);
pathMan += "\\Plugin\\hostmf.json";
GetModuleFileNameA(NULL, pathBuffer, 2047);
CharLowerA(pathBuffer);
string fullPath(pathBuffer);
for (int i = 0; i < fullPath.size(); i++) {
if (fullPath[i] == '\\') {
fullPath[i] = '/';
}
}
cout << "Modifying " << pathMan << "...";
string manfull = manp1 + fullPath + manp2;
HANDLE hFile = CreateFile(pathMan.c_str(), // Filename
GENERIC_WRITE, // Desired access
FILE_SHARE_WRITE, // Share flags
NULL, // Security Attributes
CREATE_ALWAYS, // Creation Disposition
0, // Flags and Attributes
NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Cannot open TestFile\n");
return 0;
}
else
{
DWORD dwRet;
WriteFile(hFile, // Handle
manfull.c_str(), // Data to be written
manfull.length(), // Size of data, in bytes
&dwRet, // Number of bytes written
NULL); // OVERLAPPED pointer
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
}
cout << " Done!" << endl << "Modifying Chrome settings...";
RegSetValueExA(hKey1, "", 0, REG_SZ, (BYTE*)pathMan.c_str(), pathMan.size());
cout << " Done!" << endl << "Plugin registered." << endl;
return 1;
}
#ifdef _TFLITE
cerr << "Tensorflow " << TfLiteVersion() << endl;
//********* Read model
TfLiteModel* model = TfLiteModelCreateFromFile("./model/model.tflite");
TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();
TfLiteInterpreterOptionsSetNumThreads(options, 16);
TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options);
#else
cout << "Tensorflow " << TF_Version() << endl;
//********* Read model
TF_Graph* Graph = TF_NewGraph();
TF_Status* Status = TF_NewStatus();
TF_SessionOptions* SessionOpts = TF_NewSessionOptions();
TF_Buffer* RunOpts = NULL;
const char* saved_model_dir = "./model/"; // Path of the model
const char* tags = "serve"; // default model serving tag; can change in future
int ntags = 1;
TF_Session* Session = TF_LoadSessionFromSavedModel(SessionOpts, RunOpts, saved_model_dir, &tags, ntags, Graph, NULL, Status);
if (TF_GetCode(Status) == TF_OK)
{
printf("TF_LoadSessionFromSavedModel OK\n");
}
else
{
printf("%s", TF_Message(Status));
return 0;
}
int NumInputs = 1;
TF_Output* Input = (TF_Output*) malloc(sizeof(TF_Output) * NumInputs);
TF_Output t0 = { TF_GraphOperationByName(Graph, "serving_default_input_tensor"), 0 };
if (t0.oper == NULL) {
printf("ERROR: Failed TF_GraphOperationByName serving_default_input\n");
return 0;
}
Input[0] = t0;
int NumOutputs = 7;
TF_Output* Output = (TF_Output*) malloc(sizeof(TF_Output) * NumOutputs);
for (int i = 0; i < 7; i++) {
TF_Output t2 = { TF_GraphOperationByName(Graph, "StatefulPartitionedCall"), i };
if (t2.oper == NULL) {
printf("ERROR: Failed TF_GraphOperationByName StatefulPartitionedCall\n");
return 0;
}
Output[i] = t2;
}
// load data....
TF_Tensor** InputValues = (TF_Tensor**)malloc(sizeof(TF_Tensor*) * NumInputs);
TF_Tensor** OutputValues = (TF_Tensor**)malloc(sizeof(TF_Tensor*) * NumOutputs);
#endif
unsigned msgsize, width = 0, height = 0, type = -1;
float cutLevel = 0.4;
DWORD dwRet;
//while (true) {
_read(0, &msgsize, 4);
char* msgbuffer = (char*)malloc(msgsize);
_read(0, msgbuffer, msgsize);
BYTE* imgbuf = ParseJson(msgbuffer, &width, &height, &type, &cutLevel);
/*HANDLE sFile = CreateFile("c:\\temp\\message.json", // Filename
GENERIC_WRITE, // Desired access
FILE_SHARE_WRITE, // Share flags
NULL, // Security Attributes
CREATE_ALWAYS, // Creation Disposition
0, // Flags and Attributes
NULL);
WriteFile(sFile, // Handle
&msgsize, // Data to be written
4, // Size of data, in bytes
&dwRet, // Number of bytes written
NULL);
WriteFile(sFile, // Handle
msgbuffer, // Data to be written
msgsize, // Size of data, in bytes
&dwRet, // Number of bytes written
NULL); // OVERLAPPED pointer
CloseHandle(sFile);*/
Mat img(height, width, CV_8UC4, imgbuf), tim;
free(msgbuffer);
Mat dstp, dst;
cvtColor(img, dstp, CV_RGBA2RGB);
//imwrite("c:\\temp\\testimg.png", dstp);
/*float rcratio = (float)*img.cols / *img.rows;
if (img.cols > img.rows)
resize(dstp, dstp, Size(320, 320 / rcratio));
else
resize(dstp, dstp, Size(320 * rcratio, 320));
if (rcratio != 1.0f)
copyMakeBorder(dstp, dstp, 0, 320 - dstp.rows, 0, 320-dstp.cols, BORDER_CONSTANT, 0);*/
//cv::resize(dstp, dstp, Size(320, 320));
dstp.convertTo(dst, CV_32FC3);// , 1, -127.5f);
//imwrite("c:\\temp\\testimg_tf.png", dst);
cv::normalize(dst, dst, -1.0f, 1.0f, NORM_MINMAX);
int ndata = 3 * dst.cols * dst.rows * sizeof(float);
int ndims = 4;
int dims[] = { 1,dst.rows,dst.cols,3 };
#ifdef _TFLITE
// Will need it later for RCNN
//TfLiteInterpreterAllocateTensors(interpreter);
//TfLiteInterpreterResizeInputTensor(interpreter, 0, dims, ndims);
TfLiteInterpreterAllocateTensors(interpreter);
TfLiteTensor* input_tensor =
TfLiteInterpreterGetInputTensor(interpreter, 0);
TfLiteTensorCopyFromBuffer(input_tensor, dst.ptr(),
ndata);
TfLiteInterpreterInvoke(interpreter);
int oCount = TfLiteInterpreterGetOutputTensorCount(interpreter);
float* scores, *types, *boxes;
float numDetections;
const TfLiteTensor* output_tensor[4];
for (int i = 0; i < oCount; i++)
output_tensor[i] = TfLiteInterpreterGetOutputTensor(interpreter, i);
numDetections = *(float*)TfLiteTensorData(output_tensor[3]);
scores = (float*)TfLiteTensorData(output_tensor[2]);
types = (float*)TfLiteTensorData(output_tensor[1]);
boxes = (float*)TfLiteTensorData(output_tensor[0]);
#else
TF_Tensor* int_tensor = TF_NewTensor(TF_UINT8, dims, ndims, dst.ptr(), ndata, &NoOpDeallocator, 0);
//TF_Tensor* int_tensor = TF_NewTensor(TF_UINT8, dims, ndims, dst->imageData, dst->imageSize, &NoOpDeallocator, 0);
if (int_tensor == NULL) {
printf("ERROR: Failed TF_NewTensor\n");
}
//auto root = tensorflow::Scope::NewRootScope();
InputValues[0] = int_tensor;
// evaluate
TF_SessionRun(Session, NULL, Input, InputValues, NumInputs, Output, OutputValues, NumOutputs, NULL, 0, NULL, Status);
if (TF_GetCode(Status) == TF_OK)
{
printf("Session is OK\n");
}
else
{
printf("%s", TF_Message(Status));
}
// parse results
float numDetections = ((float*)TF_TensorData(OutputValues[5]))[0];
float* boxes = (float*)TF_TensorData(OutputValues[1]);
float* types = (float*)TF_TensorData(OutputValues[2]);
float* scores = (float*)TF_TensorData(OutputValues[4]);
#endif
free(imgbuf);
char buffer[8192];
sprintf_s(buffer, 2047, "{\"size\":%d,\"result\":", msgsize);
int outsize = strlen(buffer);
string outmessage = buffer;
int finResult = 0;
for (int i = 0; i < numDetections; i++)
if (scores[i] > cutLevel) {
if ((int)types[i] == type) {
finResult = 1;
break;
}
/*printf("Type %f - Score %f\n", types[i], scores[i]);
printf(" Box: %f,%f - %f,%f\n", boxes[i * 4], boxes[i * 4 + 1],
boxes[i * 4 + 2], boxes[i * 4 + 3]);
rectangle(img, Point(boxes[i * 4+1] * img.cols, boxes[i * 4] * img.rows),
Point(boxes[i * 4 + 3] * img.cols, boxes[i * 4 + 2] * img.rows),
Scalar(types[i]*2.8, 255 - types[i]*2.8, types[i] * 2.8), 2);
sprintf_s(tid, 255, "Type: %d", (int)types[i] + 1);
putText(img, tid, Point(boxes[i * 4 + 1] * img.cols, boxes[i * 4] * img.rows), FONT_HERSHEY_COMPLEX_SMALL,
1, Scalar(0,0,255));*/
}
else break;
sprintf_s(buffer, 2047, "%d}", finResult);
outmessage += buffer;
outsize = outmessage.length();
cerr << "Result: " << finResult << endl;
_write(1, &outsize, 4);
_write(1, outmessage.c_str(), outsize);
// deinit
#ifdef _TFLITE
// Dispose of the model and interpreter objects.
TfLiteInterpreterDelete(interpreter);
TfLiteInterpreterOptionsDelete(options);
TfLiteModelDelete(model);
#else
TF_DeleteGraph(Graph);
TF_DeleteSession(Session, Status);
TF_DeleteSessionOptions(SessionOpts);
TF_DeleteStatus(Status);
#endif
}
return 1;
}