server_tflite.cpp 17 KB

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