server_tflite.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. #include "server_tflite.h"
  2. #include <string>
  3. #include <vector>
  4. #include "string.h"
  5. #include "esp_log.h"
  6. #include <iomanip>
  7. #include <sstream>
  8. #include "Helper.h"
  9. #include "esp_camera.h"
  10. #include "time_sntp.h"
  11. #include "ClassControllCamera.h"
  12. #include "ClassFlowControll.h"
  13. #include "ClassLogFile.h"
  14. //#define DEBUG_DETAIL_ON
  15. ClassFlowControll tfliteflow;
  16. TaskHandle_t xHandleblink_task_doFlow = NULL;
  17. TaskHandle_t xHandletask_autodoFlow = NULL;
  18. bool flowisrunning = false;
  19. long auto_intervall = 0;
  20. bool auto_isrunning = false;
  21. int countRounds = 0;
  22. int getCountFlowRounds() {
  23. return countRounds;
  24. }
  25. esp_err_t GetJPG(std::string _filename, httpd_req_t *req)
  26. {
  27. return tfliteflow.GetJPGStream(_filename, req);
  28. }
  29. esp_err_t GetRawJPG(httpd_req_t *req)
  30. {
  31. return tfliteflow.SendRawJPG(req);
  32. }
  33. bool isSetupModusActive() {
  34. return tfliteflow.getStatusSetupModus();
  35. return false;
  36. }
  37. void KillTFliteTasks()
  38. {
  39. #ifdef DEBUG_DETAIL_ON
  40. printf("Handle: xHandleblink_task_doFlow: %ld\n", (long) xHandleblink_task_doFlow);
  41. #endif
  42. if (xHandleblink_task_doFlow)
  43. {
  44. vTaskDelete(xHandleblink_task_doFlow);
  45. #ifdef DEBUG_DETAIL_ON
  46. printf("Killed: xHandleblink_task_doFlow\n");
  47. #endif
  48. }
  49. #ifdef DEBUG_DETAIL_ON
  50. printf("Handle: xHandletask_autodoFlow: %ld\n", (long) xHandletask_autodoFlow);
  51. #endif
  52. if (xHandletask_autodoFlow)
  53. {
  54. vTaskDelete(xHandletask_autodoFlow);
  55. #ifdef DEBUG_DETAIL_ON
  56. printf("Killed: xHandletask_autodoFlow\n");
  57. #endif
  58. }
  59. }
  60. void doInit(void)
  61. {
  62. string config = "/sdcard/config/config.ini";
  63. #ifdef DEBUG_DETAIL_ON
  64. printf("Start tfliteflow.InitFlow(config);\n");
  65. #endif
  66. tfliteflow.InitFlow(config);
  67. #ifdef DEBUG_DETAIL_ON
  68. printf("Finished tfliteflow.InitFlow(config);\n");
  69. #endif
  70. }
  71. bool doflow(void)
  72. {
  73. std::string zw_time = gettimestring(LOGFILE_TIME_FORMAT);
  74. printf("doflow - start %s\n", zw_time.c_str());
  75. flowisrunning = true;
  76. tfliteflow.doFlow(zw_time);
  77. flowisrunning = false;
  78. #ifdef DEBUG_DETAIL_ON
  79. printf("doflow - end %s\n", zw_time.c_str());
  80. #endif
  81. return true;
  82. }
  83. void blink_task_doFlow(void *pvParameter)
  84. {
  85. #ifdef DEBUG_DETAIL_ON
  86. printf("blink_task_doFlow\n");
  87. #endif
  88. if (!flowisrunning)
  89. {
  90. flowisrunning = true;
  91. doflow();
  92. flowisrunning = false;
  93. }
  94. vTaskDelete(NULL); //Delete this task if it exits from the loop above
  95. xHandleblink_task_doFlow = NULL;
  96. }
  97. esp_err_t handler_init(httpd_req_t *req)
  98. {
  99. #ifdef DEBUG_DETAIL_ON
  100. LogFile.WriteHeapInfo("handler_init - Start");
  101. printf("handler_doinit uri:\n"); printf(req->uri); printf("\n");
  102. #endif
  103. char* resp_str = "Init started<br>";
  104. httpd_resp_send(req, resp_str, strlen(resp_str));
  105. doInit();
  106. resp_str = "Init done<br>";
  107. httpd_resp_send(req, resp_str, strlen(resp_str));
  108. /* Respond with an empty chunk to signal HTTP response completion */
  109. httpd_resp_send_chunk(req, NULL, 0);
  110. #ifdef DEBUG_DETAIL_ON
  111. LogFile.WriteHeapInfo("handler_init - Done");
  112. #endif
  113. return ESP_OK;
  114. };
  115. esp_err_t handler_doflow(httpd_req_t *req)
  116. {
  117. #ifdef DEBUG_DETAIL_ON
  118. LogFile.WriteHeapInfo("handler_doflow - Start");
  119. #endif
  120. char* resp_str;
  121. printf("handler_doFlow uri: "); printf(req->uri); printf("\n");
  122. if (flowisrunning)
  123. {
  124. const char* resp_str = "doFlow läuft bereits und kann nicht nochmal gestartet werden";
  125. httpd_resp_send(req, resp_str, strlen(resp_str));
  126. return 2;
  127. }
  128. else
  129. {
  130. xTaskCreate(&blink_task_doFlow, "blink_doFlow", configMINIMAL_STACK_SIZE * 64, NULL, tskIDLE_PRIORITY+1, &xHandleblink_task_doFlow);
  131. }
  132. resp_str = "doFlow gestartet - dauert ca. 60 Sekunden";
  133. httpd_resp_send(req, resp_str, strlen(resp_str));
  134. /* Respond with an empty chunk to signal HTTP response completion */
  135. httpd_resp_send_chunk(req, NULL, 0);
  136. #ifdef DEBUG_DETAIL_ON
  137. LogFile.WriteHeapInfo("handler_doflow - Done");
  138. #endif
  139. return ESP_OK;
  140. };
  141. esp_err_t handler_wasserzaehler(httpd_req_t *req)
  142. {
  143. #ifdef DEBUG_DETAIL_ON
  144. LogFile.WriteHeapInfo("handler_wasserzaehler - Start");
  145. #endif
  146. bool _rawValue = false;
  147. bool _noerror = false;
  148. string zw;
  149. printf("handler_wasserzaehler uri:\n"); printf(req->uri); printf("\n");
  150. char _query[100];
  151. char _size[10];
  152. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  153. {
  154. // printf("Query: "); printf(_query); printf("\n");
  155. if (httpd_query_key_value(_query, "rawvalue", _size, 10) == ESP_OK)
  156. {
  157. #ifdef DEBUG_DETAIL_ON
  158. printf("rawvalue is found"); printf(_size); printf("\n");
  159. #endif
  160. _rawValue = true;
  161. }
  162. if (httpd_query_key_value(_query, "noerror", _size, 10) == ESP_OK)
  163. {
  164. #ifdef DEBUG_DETAIL_ON
  165. printf("noerror is found"); printf(_size); printf("\n");
  166. #endif
  167. _noerror = true;
  168. }
  169. }
  170. zw = tfliteflow.getReadout(_rawValue, _noerror);
  171. if (zw.length() > 0)
  172. httpd_resp_sendstr_chunk(req, zw.c_str());
  173. string query = std::string(_query);
  174. // printf("Query: %s\n", query.c_str());
  175. if (query.find("full") != std::string::npos)
  176. {
  177. string txt, zw;
  178. txt = "<p>Aligned Image: <p><img src=\"/img_tmp/alg_roi.jpg\"> <p>\n";
  179. txt = txt + "Digital Counter: <p> ";
  180. httpd_resp_sendstr_chunk(req, txt.c_str());
  181. std::vector<HTMLInfo*> htmlinfo;
  182. htmlinfo = tfliteflow.GetAllDigital();
  183. for (int i = 0; i < htmlinfo.size(); ++i)
  184. {
  185. if (htmlinfo[i]->val == 10)
  186. zw = "NaN";
  187. else
  188. {
  189. zw = to_string((int) htmlinfo[i]->val);
  190. }
  191. txt = "<img src=\"/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  192. httpd_resp_sendstr_chunk(req, txt.c_str());
  193. delete htmlinfo[i];
  194. }
  195. htmlinfo.clear();
  196. txt = " <p> Analog Meter: <p> ";
  197. httpd_resp_sendstr_chunk(req, txt.c_str());
  198. htmlinfo = tfliteflow.GetAllAnalog();
  199. for (int i = 0; i < htmlinfo.size(); ++i)
  200. {
  201. std::stringstream stream;
  202. stream << std::fixed << std::setprecision(1) << htmlinfo[i]->val;
  203. zw = stream.str();
  204. txt = "<img src=\"/img_tmp/" + htmlinfo[i]->filename + "\"> " + zw;
  205. httpd_resp_sendstr_chunk(req, txt.c_str());
  206. delete htmlinfo[i];
  207. }
  208. htmlinfo.clear();
  209. }
  210. /* Respond with an empty chunk to signal HTTP response completion */
  211. httpd_resp_sendstr_chunk(req, NULL);
  212. #ifdef DEBUG_DETAIL_ON
  213. LogFile.WriteHeapInfo("handler_wasserzaehler - Done");
  214. #endif
  215. return ESP_OK;
  216. };
  217. esp_err_t handler_editflow(httpd_req_t *req)
  218. {
  219. #ifdef DEBUG_DETAIL_ON
  220. LogFile.WriteHeapInfo("handler_editflow - Start");
  221. #endif
  222. printf("handler_editflow uri: "); printf(req->uri); printf("\n");
  223. char _query[200];
  224. char _valuechar[30];
  225. string _task;
  226. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
  227. {
  228. if (httpd_query_key_value(_query, "task", _valuechar, 30) == ESP_OK)
  229. {
  230. #ifdef DEBUG_DETAIL_ON
  231. printf("task is found: %s\n", _valuechar);
  232. #endif
  233. _task = string(_valuechar);
  234. }
  235. }
  236. if (_task.compare("copy") == 0)
  237. {
  238. string in, out, zw;
  239. httpd_query_key_value(_query, "in", _valuechar, 30);
  240. in = string(_valuechar);
  241. httpd_query_key_value(_query, "out", _valuechar, 30);
  242. out = string(_valuechar);
  243. #ifdef DEBUG_DETAIL_ON
  244. printf("in: "); printf(in.c_str()); printf("\n");
  245. printf("out: "); printf(out.c_str()); printf("\n");
  246. #endif
  247. in = "/sdcard" + in;
  248. out = "/sdcard" + out;
  249. CopyFile(in, out);
  250. zw = "Copy Done";
  251. httpd_resp_sendstr_chunk(req, zw.c_str());
  252. }
  253. if (_task.compare("cutref") == 0)
  254. {
  255. string in, out, zw;
  256. int x, y, dx, dy;
  257. bool enhance = false;
  258. httpd_query_key_value(_query, "in", _valuechar, 30);
  259. in = string(_valuechar);
  260. httpd_query_key_value(_query, "out", _valuechar, 30);
  261. out = string(_valuechar);
  262. httpd_query_key_value(_query, "x", _valuechar, 30);
  263. zw = string(_valuechar);
  264. x = stoi(zw);
  265. httpd_query_key_value(_query, "y", _valuechar, 30);
  266. zw = string(_valuechar);
  267. y = stoi(zw);
  268. httpd_query_key_value(_query, "dx", _valuechar, 30);
  269. zw = string(_valuechar);
  270. dx = stoi(zw);
  271. httpd_query_key_value(_query, "dy", _valuechar, 30);
  272. zw = string(_valuechar);
  273. dy = stoi(zw);
  274. #ifdef DEBUG_DETAIL_ON
  275. printf("in: "); printf(in.c_str()); printf("\n");
  276. printf("out: "); printf(out.c_str()); printf("\n");
  277. printf("x: "); printf(zw.c_str()); printf("\n");
  278. printf("y: "); printf(zw.c_str()); printf("\n");
  279. printf("dx: "); printf(zw.c_str()); printf("\n");
  280. printf("dy: "); printf(zw.c_str()); printf("\n");
  281. #endif
  282. if (httpd_query_key_value(_query, "enhance", _valuechar, 10) == ESP_OK)
  283. {
  284. zw = string(_valuechar);
  285. if (zw.compare("true") == 0)
  286. {
  287. enhance = true;
  288. }
  289. }
  290. in = "/sdcard" + in;
  291. out = "/sdcard" + out;
  292. string out2 = out.substr(0, out.length() - 4) + "_org.jpg";
  293. CAlignAndCutImage *caic = new CAlignAndCutImage(in);
  294. caic->CutAndSave(out2, x, y, dx, dy);
  295. delete caic;
  296. CImageBasis *cim = new CImageBasis(out2);
  297. if (enhance)
  298. {
  299. cim->Contrast(90);
  300. }
  301. cim->SaveToFile(out);
  302. delete cim;
  303. zw = "CutImage Done";
  304. httpd_resp_sendstr_chunk(req, zw.c_str());
  305. }
  306. if (_task.compare("test_take") == 0)
  307. {
  308. std::string _host = "";
  309. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  310. _host = std::string(_valuechar);
  311. }
  312. // printf("Parameter host: "); printf(_host.c_str()); printf("\n");
  313. // string zwzw = "Do " + _task + " start\n"; printf(zwzw.c_str());
  314. std::string zw = tfliteflow.doSingleStep("[MakeImage]", _host);
  315. httpd_resp_sendstr_chunk(req, zw.c_str());
  316. }
  317. if (_task.compare("test_align") == 0)
  318. {
  319. std::string _host = "";
  320. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  321. _host = std::string(_valuechar);
  322. }
  323. // printf("Parameter host: "); printf(_host.c_str()); printf("\n");
  324. // string zwzw = "Do " + _task + " start\n"; printf(zwzw.c_str());
  325. std::string zw = tfliteflow.doSingleStep("[Alignment]", _host);
  326. httpd_resp_sendstr_chunk(req, zw.c_str());
  327. }
  328. if (_task.compare("test_analog") == 0)
  329. {
  330. std::string _host = "";
  331. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  332. _host = std::string(_valuechar);
  333. }
  334. // printf("Parameter host: "); printf(_host.c_str()); printf("\n");
  335. // string zwzw = "Do " + _task + " start\n"; printf(zwzw.c_str());
  336. std::string zw = tfliteflow.doSingleStep("[Analog]", _host);
  337. httpd_resp_sendstr_chunk(req, zw.c_str());
  338. }
  339. if (_task.compare("test_digits") == 0)
  340. {
  341. std::string _host = "";
  342. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  343. _host = std::string(_valuechar);
  344. }
  345. // printf("Parameter host: "); printf(_host.c_str()); printf("\n");
  346. // string zwzw = "Do " + _task + " start\n"; printf(zwzw.c_str());
  347. std::string zw = tfliteflow.doSingleStep("[Digits]", _host);
  348. httpd_resp_sendstr_chunk(req, zw.c_str());
  349. }
  350. /* Respond with an empty chunk to signal HTTP response completion */
  351. httpd_resp_sendstr_chunk(req, NULL);
  352. #ifdef DEBUG_DETAIL_ON
  353. LogFile.WriteHeapInfo("handler_editflow - Done");
  354. #endif
  355. return ESP_OK;
  356. };
  357. esp_err_t handler_prevalue(httpd_req_t *req)
  358. {
  359. #ifdef DEBUG_DETAIL_ON
  360. LogFile.WriteHeapInfo("handler_prevalue - Start");
  361. #endif
  362. const char* resp_str;
  363. string zw;
  364. // printf("handler_prevalue:\n"); printf(req->uri); printf("\n");
  365. char _query[100];
  366. char _size[10] = "";
  367. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  368. {
  369. // printf("Query: "); printf(_query); printf("\n");
  370. if (httpd_query_key_value(_query, "value", _size, 10) == ESP_OK)
  371. {
  372. #ifdef DEBUG_DETAIL_ON
  373. printf("Value: "); printf(_size); printf("\n");
  374. #endif
  375. }
  376. }
  377. if (strlen(_size) == 0)
  378. zw = tfliteflow.GetPrevalue();
  379. else
  380. zw = "SetPrevalue to " + tfliteflow.UpdatePrevalue(_size);
  381. resp_str = zw.c_str();
  382. httpd_resp_send(req, resp_str, strlen(resp_str));
  383. /* Respond with an empty chunk to signal HTTP response completion */
  384. httpd_resp_send_chunk(req, NULL, 0);
  385. #ifdef DEBUG_DETAIL_ON
  386. LogFile.WriteHeapInfo("handler_prevalue - Start");
  387. #endif
  388. return ESP_OK;
  389. };
  390. void task_autodoFlow(void *pvParameter)
  391. {
  392. int64_t fr_start, fr_delta_ms;
  393. doInit();
  394. auto_isrunning = tfliteflow.isAutoStart(auto_intervall);
  395. if (isSetupModusActive()) {
  396. auto_isrunning = false;
  397. std::string zw_time = gettimestring(LOGFILE_TIME_FORMAT);
  398. tfliteflow.doFlowMakeImageOnly(zw_time);
  399. }
  400. while (auto_isrunning)
  401. {
  402. std::string _zw = "task_autodoFlow - next round - Round #" + std::to_string(++countRounds);
  403. LogFile.WriteToFile(_zw);
  404. printf("Autoflow: start\n");
  405. fr_start = esp_timer_get_time();
  406. if (flowisrunning)
  407. {
  408. #ifdef DEBUG_DETAIL_ON
  409. printf("Autoflow: doFLow laeuft bereits!\n");
  410. #endif
  411. }
  412. else
  413. {
  414. #ifdef DEBUG_DETAIL_ON
  415. printf("Autoflow: doFLow wird gestartet\n");
  416. #endif
  417. flowisrunning = true;
  418. doflow();
  419. #ifdef DEBUG_DETAIL_ON
  420. printf("Remove older log files\n");
  421. #endif
  422. LogFile.RemoveOld();
  423. }
  424. LogFile.WriteToFile("task_autodoFlow - round done");
  425. //CPU Temp
  426. float cputmp = temperatureRead();
  427. std::stringstream stream;
  428. stream << std::fixed << std::setprecision(1) << cputmp;
  429. string zwtemp = "CPU Temperature: " + stream.str();
  430. LogFile.WriteToFile(zwtemp);
  431. printf("CPU Temperature: %.2f\n", cputmp);
  432. fr_delta_ms = (esp_timer_get_time() - fr_start) / 1000;
  433. const TickType_t xDelay = (auto_intervall - fr_delta_ms) / portTICK_PERIOD_MS;
  434. printf("Autoflow: sleep for : %ldms\n", (long) xDelay);
  435. vTaskDelay( xDelay );
  436. }
  437. vTaskDelete(NULL); //Delete this task if it exits from the loop above
  438. xHandletask_autodoFlow = NULL;
  439. }
  440. void TFliteDoAutoStart()
  441. {
  442. xTaskCreate(&task_autodoFlow, "task_autodoFlow", configMINIMAL_STACK_SIZE * 64, NULL, tskIDLE_PRIORITY+1, &xHandletask_autodoFlow);
  443. }
  444. void register_server_tflite_uri(httpd_handle_t server)
  445. {
  446. ESP_LOGI(TAGTFLITE, "server_part_camera - Registering URI handlers");
  447. httpd_uri_t camuri = { };
  448. camuri.method = HTTP_GET;
  449. camuri.uri = "/doinit";
  450. camuri.handler = handler_init;
  451. camuri.user_ctx = (void*) "Light On";
  452. httpd_register_uri_handler(server, &camuri);
  453. camuri.uri = "/setPreValue.html";
  454. camuri.handler = handler_prevalue;
  455. camuri.user_ctx = (void*) "Prevalue";
  456. httpd_register_uri_handler(server, &camuri);
  457. camuri.uri = "/doflow";
  458. camuri.handler = handler_doflow;
  459. camuri.user_ctx = (void*) "Light Off";
  460. httpd_register_uri_handler(server, &camuri);
  461. camuri.uri = "/editflow.html";
  462. camuri.handler = handler_editflow;
  463. camuri.user_ctx = (void*) "EditFlow";
  464. httpd_register_uri_handler(server, &camuri);
  465. camuri.uri = "/wasserzaehler.html";
  466. camuri.handler = handler_wasserzaehler;
  467. camuri.user_ctx = (void*) "Wasserzaehler";
  468. httpd_register_uri_handler(server, &camuri);
  469. }