server_tflite.cpp 17 KB

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