server_tflite.cpp 15 KB

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