inferthread.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  1. #include "inferthread.h"
  2. #include <QTimer>
  3. #include <ctime>
  4. void InferThread::setStopBtn(QPushButton *btn)
  5. {
  6. btnStop = btn;
  7. }
  8. void InferThread::setInferBtn(QPushButton *btn)
  9. {
  10. btnInfer = btn;
  11. }
  12. void InferThread::setDetThreshold(float threshold)
  13. {
  14. det_Threshold = threshold;
  15. }
  16. void InferThread::setInferDelay(int delay)
  17. {
  18. infer_Delay = delay;
  19. }
  20. uchar *InferThread::get_color_map_list(int num_classes)
  21. {
  22. uchar *color_list = new uchar[num_classes * 3];
  23. num_classes += 1;
  24. for (int i = 1; i < num_classes; i++)
  25. {
  26. int j = 0;
  27. int lab = i;
  28. while (lab != 0)
  29. {
  30. color_list[(i-1) * 3] |= (uchar)(((lab >> 0) & 1) << (7 - j));
  31. color_list[(i-1) * 3 + 1] |= (uchar)(((lab >> 1) & 1) << (7 - j));
  32. color_list[(i-1) * 3 + 2] |= (uchar)(((lab >> 2) & 1) << (7 - j));
  33. j += 1;
  34. lab >>= 3;
  35. }
  36. }
  37. return color_list;
  38. }
  39. InferThread::InferThread(QObject *parent) : QThread(parent)
  40. {
  41. doing_Infer = false;
  42. break_Infer = false;
  43. dataLoaded = false; // false: Unloaded data
  44. color_map = get_color_map_list();
  45. model_Type = "det";
  46. image_path = "";
  47. images_path = QStringList();
  48. video_path = "";
  49. label1_image = nullptr;
  50. label2_image = nullptr;
  51. image1 = nullptr;
  52. image2 = nullptr;
  53. }
  54. void InferThread::setModelType(QString &model_type)
  55. {
  56. if (model_type=="det") // Check whether the type is met, otherwise set ""
  57. {
  58. model_Type = model_type;
  59. return;
  60. }
  61. else if (model_type=="seg")
  62. {
  63. model_Type = model_type;
  64. return;
  65. }
  66. else if (model_type=="clas")
  67. {
  68. model_Type = model_type;
  69. return;
  70. }
  71. else if (model_type=="mask")
  72. {
  73. model_Type = model_type;
  74. return;
  75. }
  76. else
  77. {
  78. // set empty
  79. model_Type = "";
  80. }
  81. }
  82. void InferThread::setInputImage(QString &image_path)
  83. {
  84. this->image_path = image_path;
  85. this->images_path = QStringList();
  86. this->video_path = "";
  87. dataLoaded = true;
  88. }
  89. void InferThread::setInputImages(QStringList &images_path)
  90. {
  91. this->images_path = images_path;
  92. this->image_path = "";
  93. this->video_path = "";
  94. dataLoaded = true;
  95. }
  96. void InferThread::setInputVideo(QString &video_path)
  97. {
  98. this->video_path = video_path;
  99. this->image_path = "";
  100. this->images_path = QStringList();
  101. dataLoaded = true;
  102. }
  103. void InferThread::setInferFuncs(Det_ModelPredict det_Inferfunc, Seg_ModelPredict seg_Inferfunc, Cls_ModelPredict cls_Inferfunc, Mask_ModelPredict mask_Inferfunc)
  104. {
  105. det_ModelPredict = det_Inferfunc;
  106. seg_ModelPredict = seg_Inferfunc;
  107. cls_ModelPredict = cls_Inferfunc;
  108. mask_ModelPredict = mask_Inferfunc;
  109. }
  110. void InferThread::runInferDet()
  111. {
  112. if (doing_Infer == false)
  113. {
  114. if (is_InferImage())
  115. {
  116. Det_Image();
  117. }
  118. else if (is_InferImages())
  119. {
  120. Det_Images();
  121. }
  122. else if (is_InferVideo())
  123. {
  124. Det_Video();
  125. }
  126. }
  127. else
  128. {
  129. // TODO
  130. }
  131. }
  132. void InferThread::runInferSeg()
  133. {
  134. if (doing_Infer == false)
  135. {
  136. if (is_InferImage())
  137. {
  138. Seg_Image();
  139. }
  140. else if (is_InferImages())
  141. {
  142. Seg_Images();
  143. }
  144. else if (is_InferVideo())
  145. {
  146. Seg_Video();
  147. }
  148. }
  149. else
  150. {
  151. // TODO
  152. }
  153. }
  154. void InferThread::runInferCls()
  155. {
  156. if (doing_Infer == false)
  157. {
  158. if (is_InferImage())
  159. {
  160. Cls_Image();
  161. }
  162. else if (is_InferImages())
  163. {
  164. Cls_Images();
  165. }
  166. else if (is_InferVideo())
  167. {
  168. Cls_Video();
  169. }
  170. }
  171. else
  172. {
  173. // TODO
  174. }
  175. }
  176. void InferThread::runInferMask()
  177. {
  178. if (doing_Infer == false)
  179. {
  180. if (is_InferImage())
  181. {
  182. Mask_Image();
  183. }
  184. else if (is_InferImages())
  185. {
  186. Mask_Images();
  187. }
  188. else if (is_InferVideo())
  189. {
  190. Mask_Video();
  191. }
  192. }
  193. else
  194. {
  195. // TODO
  196. }
  197. }
  198. // The thread actually runs the configuration
  199. void InferThread::run()
  200. {
  201. if (model_Type == "det")
  202. {
  203. runInferDet();
  204. }
  205. else if (model_Type == "seg")
  206. {
  207. runInferSeg();
  208. }
  209. else if (model_Type == "clas")
  210. {
  211. runInferCls();
  212. }
  213. else if (model_Type == "mask")
  214. {
  215. runInferMask();
  216. }
  217. }
  218. bool InferThread::is_InferImage()
  219. {
  220. if (image_path.isEmpty()) return false;
  221. else return true;
  222. }
  223. bool InferThread::is_InferImages()
  224. {
  225. if (images_path.isEmpty()) return false;
  226. else return true;
  227. }
  228. bool InferThread::is_InferVideo()
  229. {
  230. if (video_path.isEmpty()) return false;
  231. else return true;
  232. }
  233. QString InferThread::makeLabelInfo(QString label, int id, float score)
  234. {
  235. QString describe_str = QString::number(id) + ":";
  236. describe_str += label + "-";
  237. describe_str += QString::number(score);
  238. return describe_str;
  239. }
  240. void InferThread::Det_Image()
  241. {
  242. // Read the picture
  243. Mat image = imread(image_path.toLocal8Bit().toStdString()); //BGR
  244. if (image.cols > 512 || image.rows > 512)
  245. {
  246. float ratio = min(image.cols, image.rows) / 512.;
  247. int new_h = image.cols / ratio;
  248. int new_w = image.rows / ratio;
  249. cv::resize(image, image, cv::Size(new_h/4*4,new_w/4*4)); // Make sure pixMap displays properly - cut to scale images
  250. }
  251. // Predict output result
  252. float bboxs[600];
  253. int bbox_num[1];
  254. char labellist[1000];
  255. // Set the start reasoning state
  256. doing_Infer = true;
  257. try {
  258. clock_t start_infer_time = clock();
  259. // Perform reasoning and get results
  260. qDebug() << "Doing Det-Infer." << "\n";
  261. det_ModelPredict((const uchar*)image.data, image.cols, image.rows, 3, bboxs, bbox_num, labellist);
  262. double cost_time = 1000 * (clock() - start_infer_time) / (double)CLOCKS_PER_SEC;
  263. emit SetCostTime(cost_time);
  264. } catch (QException &e) {
  265. // Set the end reasoning state
  266. doing_Infer = false;
  267. qDebug() << "Finished Det-Infer, but it is raise a exception." << "\n";
  268. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  269. // btnStop->setEnabled(false); // When reasoning is complete, close the button of reasoning interruption to prevent late point
  270. // btnInfer->setEnabled(true); // When the reasoning is complete, the button for the execution of the reasoning is opened to allow the reasoning again
  271. return;
  272. }
  273. // Post-processing
  274. cvtColor(image, image, COLOR_BGR2RGB);
  275. if (image1 == nullptr)
  276. {
  277. image1 = new Mat(image.clone());
  278. }
  279. else
  280. {
  281. delete image1;
  282. image1 = new Mat(image.clone());
  283. }
  284. if (label1_image == nullptr)
  285. {
  286. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  287. image1->step, QImage::Format_RGB888);
  288. }
  289. else
  290. {
  291. delete label1_image;
  292. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  293. image1->step, QImage::Format_RGB888);
  294. }
  295. QString labels(labellist);
  296. QStringList label_list = labels.split(' '); // Get Label
  297. for (int i = 0; i < bbox_num[0]; i++)
  298. {
  299. int categry_id = (int)bboxs[i*6];
  300. float score = bboxs[i*6 + 1];
  301. int left_topx = (int)bboxs[i*6 + 2];
  302. int left_topy = (int)bboxs[i*6 + 3];
  303. int right_downx = left_topx + (int)bboxs[i*6 + 4]; // Parameters 4 and 5 are width and height, but the same DLL using c# is the lower right vertex
  304. int right_downy = left_topy + (int)bboxs[i*6 + 5];
  305. if (score >= det_Threshold)
  306. {
  307. int color_[3] = { (int)(color_map[(categry_id % 256) * 3]),
  308. (int)(color_map[(categry_id % 256) * 3 + 1]),
  309. (int)(color_map[(categry_id % 256) * 3 + 2]) };
  310. QString disscribe_str = makeLabelInfo(label_list[i], categry_id, score);
  311. int baseline[1];
  312. auto text_size = getTextSize(disscribe_str.toStdString(), FONT_HERSHEY_SIMPLEX,
  313. 1.0, 2, baseline);
  314. int text_left_downx = left_topx; // Small offset adjustment: (int)(text_size.Width/10)
  315. int text_left_downy = left_topy + text_size.height;
  316. rectangle(image, Point(left_topx, left_topy),
  317. Point(right_downx, right_downy),
  318. Scalar(color_[0], color_[1], color_[2]), 2, LINE_8);
  319. putText(image, disscribe_str.toStdString(), Point(text_left_downx, text_left_downy),
  320. FONT_HERSHEY_SIMPLEX, 1.0,
  321. Scalar(color_[0], color_[1], color_[2]), 2, LINE_8);
  322. }
  323. }
  324. if (image2 == nullptr)
  325. {
  326. image2 = new Mat(image.clone());
  327. }
  328. else
  329. {
  330. delete image2;
  331. image2 = new Mat(image.clone());
  332. }
  333. if (label2_image == nullptr)
  334. {
  335. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  336. image2->step, QImage::Format_RGB888);
  337. }
  338. else
  339. {
  340. delete label2_image;
  341. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  342. image2->step, QImage::Format_RGB888);
  343. }
  344. emit InferFinished(label1_image, label2_image);
  345. // Set the end reasoning state
  346. doing_Infer = false;
  347. qDebug() << "Finished Det-Infer." << "\n";
  348. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  349. // btnStop->setEnabled(false);
  350. // btnInfer->setEnabled(true);
  351. }
  352. void InferThread::Det_Images()
  353. {
  354. doing_Infer = true;
  355. for (int j = 0; j < images_path.count(); j++)
  356. {
  357. if (break_Infer) // Exit continuous detection
  358. {
  359. doing_Infer = false;
  360. break_Infer = false;
  361. qDebug() << "Det-Infer has Break." << "\n";
  362. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  363. return;
  364. }
  365. QString img_file = images_path[j]; // Get image path
  366. Mat image = imread(img_file.toLocal8Bit().toStdString()); // Help with Chinese paths
  367. if (image.cols > 512 || image.rows > 512)
  368. {
  369. float ratio = min(image.cols, image.rows) / 512.;
  370. int new_h = image.cols / ratio;
  371. int new_w = image.rows / ratio;
  372. cv::resize(image, image, cv::Size(new_h/4*4,new_w/4*4));
  373. }
  374. float bboxs[600];
  375. int bbox_num[1];
  376. char labellist[1000];
  377. try {
  378. clock_t start_infer_time = clock();
  379. qDebug() << "Doing Det-Infer." << "\n";
  380. det_ModelPredict((const uchar*)image.data, image.cols, image.rows, 3, bboxs, bbox_num, labellist);
  381. double cost_time = 1000 * (clock() - start_infer_time) / (double)CLOCKS_PER_SEC;
  382. emit SetCostTime(cost_time);
  383. } catch (QException &e) {
  384. doing_Infer = false;
  385. qDebug() << "Finished Det-Infer, but it is raise a exception." << "\n";
  386. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  387. return;
  388. }
  389. cvtColor(image, image, COLOR_BGR2RGB);
  390. if (image1 == nullptr)
  391. {
  392. image1 = new Mat(image.clone());
  393. }
  394. else
  395. {
  396. delete image1;
  397. image1 = new Mat(image.clone());
  398. }
  399. if (label1_image == nullptr)
  400. {
  401. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  402. image1->step, QImage::Format_RGB888);
  403. }
  404. else
  405. {
  406. delete label1_image;
  407. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  408. image1->step, QImage::Format_RGB888);
  409. }
  410. QString labels(labellist);
  411. QStringList label_list = labels.split(' ');
  412. for (int i = 0; i < bbox_num[0]; i++)
  413. {
  414. int categry_id = (int)bboxs[i*6];
  415. float score = bboxs[i*6 + 1];
  416. int left_topx = (int)bboxs[i*6 + 2];
  417. int left_topy = (int)bboxs[i*6 + 3];
  418. int right_downx = left_topx + (int)bboxs[i*6 + 4];
  419. int right_downy = left_topy + (int)bboxs[i*6 + 5];
  420. if (score >= det_Threshold)
  421. {
  422. int color_[3] = { (int)(color_map[(categry_id % 256) * 3]),
  423. (int)(color_map[(categry_id % 256) * 3 + 1]),
  424. (int)(color_map[(categry_id % 256) * 3 + 2]) };
  425. QString disscribe_str = makeLabelInfo(label_list[i], categry_id, score);
  426. int baseline[1];
  427. auto text_size = getTextSize(disscribe_str.toStdString(), FONT_HERSHEY_SIMPLEX,
  428. 1.0, 2, baseline);
  429. int text_left_downx = left_topx;
  430. int text_left_downy = left_topy + text_size.height;
  431. rectangle(image, Point(left_topx, left_topy),
  432. Point(right_downx, right_downy),
  433. Scalar(color_[0], color_[1], color_[2]), 2, LINE_8);
  434. putText(image, disscribe_str.toStdString(), Point(text_left_downx, text_left_downy),
  435. FONT_HERSHEY_SIMPLEX, 1.0,
  436. Scalar(color_[0], color_[1], color_[2]), 2, LINE_8);
  437. }
  438. }
  439. if (image2 == nullptr)
  440. {
  441. image2 = new Mat(image.clone());
  442. }
  443. else
  444. {
  445. delete image2;
  446. image2 = new Mat(image.clone());
  447. }
  448. if (label2_image == nullptr)
  449. {
  450. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  451. image2->step, QImage::Format_RGB888);
  452. }
  453. else
  454. {
  455. delete label2_image;
  456. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  457. image2->step, QImage::Format_RGB888);
  458. }
  459. emit InferFinished(label1_image, label2_image);
  460. this->msleep(infer_Delay); // Thread sleep wait
  461. }
  462. doing_Infer = false;
  463. qDebug() << "Finished Det-Infer." << "\n";
  464. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  465. }
  466. void InferThread::Det_Video()
  467. {
  468. doing_Infer = true;
  469. VideoCapture cap = VideoCapture(video_path.toLocal8Bit().toStdString());
  470. if(!cap.isOpened()) return; // Return if the video does not open properly
  471. Mat frame;
  472. cap >> frame;
  473. while(!frame.empty()) // Exit the loop if a frame is empty
  474. {
  475. if (frame.cols > 512 || frame.rows > 512)
  476. {
  477. float ratio = min(frame.cols, frame.rows) / 512.;
  478. int new_h = frame.cols / ratio;
  479. int new_w = frame.rows / ratio;
  480. cv::resize(frame, frame, cv::Size(new_h/4*4,new_w/4*4));
  481. }
  482. if (break_Infer)
  483. {
  484. doing_Infer = false;
  485. break_Infer = false;
  486. qDebug() << "Det-Infer has Break." << "\n";
  487. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  488. return;
  489. }
  490. float bboxs[600];
  491. int bbox_num[1];
  492. char labellist[1000];
  493. try {
  494. clock_t start_infer_time = clock();
  495. qDebug() << "Doing Det-Infer." << "\n";
  496. det_ModelPredict((const uchar*)frame.data, frame.cols, frame.rows, 3, bboxs, bbox_num, labellist);
  497. double cost_time = 1000 * (clock() - start_infer_time) / (double)CLOCKS_PER_SEC;
  498. emit SetCostTime(cost_time);
  499. } catch (QException &e) {
  500. doing_Infer = false;
  501. qDebug() << "Finished Det-Infer, but it is raise a exception." << "\n";
  502. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  503. return;
  504. }
  505. cvtColor(frame, frame, COLOR_BGR2RGB);
  506. if (image1 == nullptr)
  507. {
  508. image1 = new Mat(frame.clone());
  509. }
  510. else
  511. {
  512. delete image1;
  513. image1 = new Mat(frame.clone());
  514. }
  515. if (label1_image == nullptr)
  516. {
  517. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  518. image1->step, QImage::Format_RGB888);
  519. }
  520. else
  521. {
  522. delete label1_image;
  523. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  524. image1->step, QImage::Format_RGB888);
  525. }
  526. QString labels(labellist);
  527. QStringList label_list = labels.split(' ');
  528. for (int i = 0; i < bbox_num[0]; i++)
  529. {
  530. int categry_id = (int)bboxs[i*6];
  531. float score = bboxs[i*6 + 1];
  532. int left_topx = (int)bboxs[i*6 + 2];
  533. int left_topy = (int)bboxs[i*6 + 3];
  534. int right_downx = left_topx + (int)bboxs[i*6 + 4];
  535. int right_downy = left_topy + (int)bboxs[i*6 + 5];
  536. if (score >= det_Threshold)
  537. {
  538. int color_[3] = { (int)(color_map[(categry_id % 256) * 3]),
  539. (int)(color_map[(categry_id % 256) * 3 + 1]),
  540. (int)(color_map[(categry_id % 256) * 3 + 2]) };
  541. QString disscribe_str = makeLabelInfo(label_list[i], categry_id, score);
  542. int baseline[1];
  543. auto text_size = getTextSize(disscribe_str.toStdString(), FONT_HERSHEY_SIMPLEX,
  544. 1.0, 2, baseline);
  545. int text_left_downx = left_topx;
  546. int text_left_downy = left_topy + text_size.height;
  547. rectangle(frame, Point(left_topx, left_topy),
  548. Point(right_downx, right_downy),
  549. Scalar(color_[0], color_[1], color_[2]), 2, LINE_8);
  550. putText(frame, disscribe_str.toStdString(), Point(text_left_downx, text_left_downy),
  551. FONT_HERSHEY_SIMPLEX, 1.0,
  552. Scalar(color_[0], color_[1], color_[2]), 2, LINE_8);
  553. }
  554. }
  555. if (image2 == nullptr)
  556. {
  557. image2 = new Mat(frame.clone());
  558. }
  559. else
  560. {
  561. delete image2;
  562. image2 = new Mat(frame.clone());
  563. }
  564. if (label2_image == nullptr)
  565. {
  566. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  567. image2->step, QImage::Format_RGB888);
  568. }
  569. else
  570. {
  571. delete label2_image;
  572. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  573. image2->step, QImage::Format_RGB888);
  574. }
  575. emit InferFinished(label1_image, label2_image);
  576. cap >> frame;
  577. }
  578. doing_Infer = false;
  579. qDebug() << "Finished Det-Infer." << "\n";
  580. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  581. }
  582. void InferThread::Seg_Image()
  583. {
  584. Mat image = imread(image_path.toLocal8Bit().toStdString()); //BGR
  585. if (image.cols > 512 || image.rows > 512)
  586. {
  587. float ratio = min(image.cols, image.rows) / 512.;
  588. int new_h = image.cols / ratio;
  589. int new_w = image.rows / ratio;
  590. cv::resize(image, image, cv::Size(new_h/4*4,new_w/4*4));
  591. }
  592. // Predict output result
  593. unsigned char out_image[image.cols * image.rows];
  594. doing_Infer = true;
  595. try {
  596. clock_t start_infer_time = clock();
  597. qDebug() << "Doing Seg-Infer." << "\n";
  598. seg_ModelPredict((const uchar*)image.data, image.cols, image.rows, 3, out_image);
  599. double cost_time = 1000 * (clock() - start_infer_time) / (double)CLOCKS_PER_SEC;
  600. emit SetCostTime(cost_time);
  601. } catch (QException &e) {
  602. doing_Infer = false;
  603. qDebug() << "Finished Seg-Infer, but it is raise a exception." << "\n";
  604. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  605. return;
  606. }
  607. // Generate the mask three-channel image
  608. Mat out3c_image = Mat(image.clone());
  609. for (int i = 0; i < out3c_image.rows; i++) // height
  610. {
  611. for (int j = 0; j < out3c_image.cols; j++) // width
  612. {
  613. int indexSrc = i*out3c_image.cols + j;
  614. unsigned char color_id = (int)out_image[indexSrc] % 256; // Pixel category ID
  615. if (color_id == 0)
  616. out3c_image.at<Vec3b>(i, j) = Vec3b(0, 0, 0);
  617. else
  618. out3c_image.at<Vec3b>(i, j) = Vec3b(color_map[color_id * 3], color_map[color_id * 3 + 1], color_map[color_id * 3 + 2]);
  619. }
  620. }
  621. cvtColor(image, image, COLOR_BGR2RGB);
  622. if (image1 == nullptr)
  623. {
  624. image1 = new Mat(image.clone());
  625. }
  626. else
  627. {
  628. delete image1;
  629. image1 = new Mat(image.clone());
  630. }
  631. if (label1_image == nullptr)
  632. {
  633. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  634. image1->step, QImage::Format_RGB888);
  635. }
  636. else
  637. {
  638. delete label1_image;
  639. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  640. image1->step, QImage::Format_RGB888);
  641. }
  642. // merge images
  643. addWeighted(image, 0.5, out3c_image, 0.5, 0, image);
  644. if (image2 == nullptr)
  645. {
  646. image2 = new Mat(image.clone());
  647. }
  648. else
  649. {
  650. delete image2;
  651. image2 = new Mat(image.clone());
  652. }
  653. if (label2_image == nullptr)
  654. {
  655. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  656. image2->step, QImage::Format_RGB888);
  657. }
  658. else
  659. {
  660. delete label2_image;
  661. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  662. image2->step, QImage::Format_RGB888);
  663. }
  664. emit InferFinished(label1_image, label2_image);
  665. doing_Infer = false;
  666. qDebug() << "Finished Seg-Infer." << "\n";
  667. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  668. }
  669. void InferThread::Seg_Images()
  670. {
  671. doing_Infer = true;
  672. for (int j = 0; j < images_path.count(); j++)
  673. {
  674. if (break_Infer)
  675. {
  676. doing_Infer = false;
  677. break_Infer = false;
  678. qDebug() << "Seg-Infer has Break." << "\n";
  679. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  680. return;
  681. }
  682. QString img_file = images_path[j];
  683. Mat image = imread(img_file.toLocal8Bit().toStdString());
  684. if (image.cols > 512 || image.rows > 512)
  685. {
  686. float ratio = min(image.cols, image.rows) / 512.;
  687. int new_h = image.cols / ratio;
  688. int new_w = image.rows / ratio;
  689. cv::resize(image, image, cv::Size(new_h/4*4,new_w/4*4));
  690. }
  691. unsigned char out_image[image.cols * image.rows];
  692. memset(out_image, 0, sizeof (out_image));
  693. try {
  694. clock_t start_infer_time = clock();
  695. qDebug() << "Doing --Seg Infer." << "\n";
  696. seg_ModelPredict((const uchar*)image.data, image.cols, image.rows, 3, out_image);
  697. double cost_time = 1000 * (clock() - start_infer_time) / (double)CLOCKS_PER_SEC;
  698. emit SetCostTime(cost_time);
  699. } catch (QException &e) {
  700. doing_Infer = false;
  701. qDebug() << "Finished Seg-Infer, but it is raise a exception." << "\n";
  702. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  703. return;
  704. }
  705. Mat out3c_image = Mat(image.clone());
  706. for (int i = 0; i < out3c_image.rows; i++) // height
  707. {
  708. for (int j = 0; j < out3c_image.cols; j++) // width
  709. {
  710. int indexSrc = i*out3c_image.cols + j;
  711. unsigned char color_id = (int)out_image[indexSrc] % 256;
  712. if (color_id == 0)
  713. out3c_image.at<Vec3b>(i, j) = Vec3b(0, 0, 0);
  714. else
  715. out3c_image.at<Vec3b>(i, j) = Vec3b(color_map[color_id * 3], color_map[color_id * 3 + 1], color_map[color_id * 3 + 2]);
  716. }
  717. }
  718. cvtColor(image, image, COLOR_BGR2RGB);
  719. if (image1 == nullptr)
  720. {
  721. image1 = new Mat(image.clone());
  722. }
  723. else
  724. {
  725. delete image1;
  726. image1 = new Mat(image.clone());
  727. }
  728. if (label1_image == nullptr)
  729. {
  730. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  731. image1->step, QImage::Format_RGB888);
  732. }
  733. else
  734. {
  735. delete label1_image;
  736. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  737. image1->step, QImage::Format_RGB888);
  738. }
  739. addWeighted(image, 0.5, out3c_image, 0.5, 0, image);
  740. if (image2 == nullptr)
  741. {
  742. image2 = new Mat(image.clone());
  743. }
  744. else
  745. {
  746. delete image2;
  747. image2 = new Mat(image.clone());
  748. }
  749. if (label2_image == nullptr)
  750. {
  751. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  752. image2->step, QImage::Format_RGB888);
  753. }
  754. else
  755. {
  756. delete label2_image;
  757. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  758. image2->step, QImage::Format_RGB888);
  759. }
  760. emit InferFinished(label1_image, label2_image);
  761. this->msleep(infer_Delay);
  762. }
  763. doing_Infer = false;
  764. qDebug() << "Finished Seg-Infer." << "\n";
  765. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  766. }
  767. void InferThread::Seg_Video()
  768. {
  769. doing_Infer = true;
  770. VideoCapture cap = VideoCapture(video_path.toLocal8Bit().toStdString());
  771. if(!cap.isOpened()) return;
  772. Mat frame;
  773. cap >> frame;
  774. while(!frame.empty())
  775. {
  776. if (frame.cols > 512 || frame.rows > 512)
  777. {
  778. float ratio = min(frame.cols, frame.rows) / 512.;
  779. int new_h = frame.cols / ratio;
  780. int new_w = frame.rows / ratio;
  781. cv::resize(frame, frame, cv::Size(new_h/4*4,new_w/4*4));
  782. }
  783. if (break_Infer)
  784. {
  785. doing_Infer = false;
  786. break_Infer = false;
  787. qDebug() << "Seg-Infer has Break." << "\n";
  788. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  789. return;
  790. }
  791. unsigned char out_image[frame.cols * frame.rows];
  792. memset(out_image, 0, sizeof (out_image));
  793. try {
  794. clock_t start_infer_time = clock();
  795. qDebug() << "Doing Seg-Infer." << "\n";
  796. seg_ModelPredict((const uchar*)frame.data, frame.cols, frame.rows, 3, out_image);
  797. double cost_time = 1000 * (clock() - start_infer_time) / (double)CLOCKS_PER_SEC;
  798. emit SetCostTime(cost_time);
  799. } catch (QException &e) {
  800. doing_Infer = false;
  801. qDebug() << "Finished Seg-Infer, but it is raise a exception." << "\n";
  802. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  803. return;
  804. }
  805. Mat out3c_image = Mat(frame.clone());
  806. for (int i = 0; i < out3c_image.rows; i++) // height
  807. {
  808. for (int j = 0; j < out3c_image.cols; j++) // width
  809. {
  810. int indexSrc = i*out3c_image.cols + j;
  811. unsigned char color_id = (int)out_image[indexSrc] % 256;
  812. if (color_id == 0)
  813. out3c_image.at<Vec3b>(i, j) = Vec3b(0, 0, 0);
  814. else
  815. out3c_image.at<Vec3b>(i, j) = Vec3b(color_map[color_id * 3], color_map[color_id * 3 + 1], color_map[color_id * 3 + 2]);
  816. }
  817. }
  818. cvtColor(frame, frame, COLOR_BGR2RGB);
  819. if (image1 == nullptr)
  820. {
  821. image1 = new Mat(frame.clone());
  822. }
  823. else
  824. {
  825. delete image1;
  826. image1 = new Mat(frame.clone());
  827. }
  828. if (label1_image == nullptr)
  829. {
  830. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  831. image1->step, QImage::Format_RGB888);
  832. }
  833. else
  834. {
  835. delete label1_image;
  836. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  837. image1->step, QImage::Format_RGB888);
  838. }
  839. addWeighted(frame, 0.5, out3c_image, 0.5, 0, frame);
  840. if (image2 == nullptr)
  841. {
  842. image2 = new Mat(frame.clone());
  843. }
  844. else
  845. {
  846. delete image2;
  847. image2 = new Mat(frame.clone());
  848. }
  849. if (label2_image == nullptr)
  850. {
  851. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  852. image2->step, QImage::Format_RGB888);
  853. }
  854. else
  855. {
  856. delete label2_image;
  857. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  858. image2->step, QImage::Format_RGB888);
  859. }
  860. emit InferFinished(label1_image, label2_image);
  861. cap >> frame;
  862. }
  863. doing_Infer = false;
  864. qDebug() << "Finished Seg-Infer." << "\n";
  865. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  866. }
  867. void InferThread::Cls_Image()
  868. {
  869. Mat image = imread(image_path.toLocal8Bit().toStdString()); //BGR
  870. if (image.cols > 512 || image.rows > 512)
  871. {
  872. float ratio = min(image.cols, image.rows) / 512.;
  873. int new_h = image.cols / ratio;
  874. int new_w = image.rows / ratio;
  875. cv::resize(image, image, cv::Size(new_h/4*4,new_w/4*4));
  876. }
  877. // Predict output result
  878. float pre_score[1];
  879. int pre_category_id[1];
  880. char pre_category[200];
  881. doing_Infer = true;
  882. try {
  883. clock_t start_infer_time = clock();
  884. qDebug() << "Doing Clas-Infer." << "\n";
  885. cls_ModelPredict((const uchar*)image.data, image.cols, image.rows, 3, pre_score, pre_category, pre_category_id);
  886. double cost_time = 1000 * (clock() - start_infer_time) / (double)CLOCKS_PER_SEC;
  887. emit SetCostTime(cost_time);
  888. } catch (QException &e) {
  889. doing_Infer = false;
  890. qDebug() << "Finished Clas-Infer, but it is raise a exception." << "\n";
  891. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  892. return;
  893. }
  894. cvtColor(image, image, COLOR_BGR2RGB);
  895. float ratio = min(image.cols, image.rows) / 512.;
  896. int new_h = image.cols / ratio;
  897. int new_w = image.rows / ratio;
  898. cv::resize(image, image, cv::Size(new_h/4*4,new_w/4*4));
  899. if (image1 == nullptr)
  900. {
  901. image1 = new Mat(image.clone());
  902. }
  903. else
  904. {
  905. delete image1;
  906. image1 = new Mat(image.clone());
  907. }
  908. if (label1_image == nullptr)
  909. {
  910. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  911. image1->step, QImage::Format_RGB888);
  912. }
  913. else
  914. {
  915. delete label1_image;
  916. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  917. image1->step, QImage::Format_RGB888);
  918. }
  919. int color_[3] = { (int)(color_map[(pre_category_id[0] % 256) * 3]),
  920. (int)(color_map[(pre_category_id[0] % 256) * 3 + 1]),
  921. (int)(color_map[(pre_category_id[0] % 256) * 3 + 2]) };
  922. QString disscribe_str = makeLabelInfo(QString(pre_category), pre_category_id[0], pre_score[0]);
  923. int baseline[1];
  924. auto text_size = getTextSize(disscribe_str.toStdString(), FONT_HERSHEY_SIMPLEX,
  925. 1.0, 2, baseline);
  926. int text_left_downx = 0;
  927. int text_left_downy = 0 + text_size.height;
  928. putText(image, disscribe_str.toStdString(), Point(text_left_downx, text_left_downy),
  929. FONT_HERSHEY_SIMPLEX, 1.0,
  930. Scalar(color_[0], color_[1], color_[2]), 2, LINE_8);
  931. if (image2 == nullptr)
  932. {
  933. image2 = new Mat(image.clone());
  934. }
  935. else
  936. {
  937. delete image2;
  938. image2 = new Mat(image.clone());
  939. }
  940. if (label2_image == nullptr)
  941. {
  942. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  943. image2->step, QImage::Format_RGB888);
  944. }
  945. else
  946. {
  947. delete label2_image;
  948. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  949. image2->step, QImage::Format_RGB888);
  950. }
  951. emit InferFinished(label1_image, label2_image);
  952. doing_Infer = false;
  953. qDebug() << "Finished Clas-Infer." << "\n";
  954. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  955. }
  956. void InferThread::Cls_Images()
  957. {
  958. doing_Infer = true;
  959. for (int j = 0; j < images_path.count(); j++)
  960. {
  961. if (break_Infer)
  962. {
  963. doing_Infer = false;
  964. break_Infer = false;
  965. qDebug() << "Clas-Infer has Break." << "\n";
  966. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  967. return;
  968. }
  969. QString img_file = images_path[j];
  970. Mat image = imread(img_file.toLocal8Bit().toStdString());
  971. if (image.cols > 512 || image.rows > 512)
  972. {
  973. float ratio = min(image.cols, image.rows) / 512.;
  974. int new_h = image.cols / ratio;
  975. int new_w = image.rows / ratio;
  976. cv::resize(image, image, cv::Size(new_h/4*4,new_w/4*4));
  977. }
  978. float pre_score[1];
  979. int pre_category_id[1];
  980. char pre_category[200];
  981. try {
  982. clock_t start_infer_time = clock();
  983. qDebug() << "Doing Clas-Infer." << "\n";
  984. cls_ModelPredict((const uchar*)image.data, image.cols, image.rows, 3, pre_score, pre_category, pre_category_id);
  985. double cost_time = 1000 * (clock() - start_infer_time) / (double)CLOCKS_PER_SEC;
  986. emit SetCostTime(cost_time);
  987. } catch (QException &e) {
  988. doing_Infer = false;
  989. qDebug() << "Finished Clas-Infer, but it is raise a exception." << "\n";
  990. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  991. return;
  992. }
  993. cvtColor(image, image, COLOR_BGR2RGB);
  994. float ratio = min(image.cols, image.rows) / 512.;
  995. int new_h = image.cols / ratio;
  996. int new_w = image.rows / ratio;
  997. cv::resize(image, image, cv::Size(new_h/4*4,new_w/4*4));
  998. if (image1 == nullptr)
  999. {
  1000. image1 = new Mat(image.clone());
  1001. }
  1002. else
  1003. {
  1004. delete image1;
  1005. image1 = new Mat(image.clone());
  1006. }
  1007. if (label1_image == nullptr)
  1008. {
  1009. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  1010. image1->step, QImage::Format_RGB888);
  1011. }
  1012. else
  1013. {
  1014. delete label1_image;
  1015. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  1016. image1->step, QImage::Format_RGB888);
  1017. }
  1018. int color_[3] = { (int)(color_map[(pre_category_id[0] % 256) * 3]),
  1019. (int)(color_map[(pre_category_id[0] % 256) * 3 + 1]),
  1020. (int)(color_map[(pre_category_id[0] % 256) * 3 + 2]) };
  1021. QString disscribe_str = makeLabelInfo(QString(pre_category), pre_category_id[0], pre_score[0]);
  1022. int baseline[1];
  1023. auto text_size = getTextSize(disscribe_str.toStdString(), FONT_HERSHEY_SIMPLEX,
  1024. 1.0, 2, baseline);
  1025. int text_left_downx = 0;
  1026. int text_left_downy = 0 + text_size.height;
  1027. putText(image, disscribe_str.toStdString(), Point(text_left_downx, text_left_downy),
  1028. FONT_HERSHEY_SIMPLEX, 1.0,
  1029. Scalar(color_[0], color_[1], color_[2]), 2, LINE_8);
  1030. if (image2 == nullptr)
  1031. {
  1032. image2 = new Mat(image.clone());
  1033. }
  1034. else
  1035. {
  1036. delete image2;
  1037. image2 = new Mat(image.clone());
  1038. }
  1039. if (label2_image == nullptr)
  1040. {
  1041. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  1042. image2->step, QImage::Format_RGB888);
  1043. }
  1044. else
  1045. {
  1046. delete label2_image;
  1047. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  1048. image2->step, QImage::Format_RGB888);
  1049. }
  1050. emit InferFinished(label1_image, label2_image);
  1051. this->msleep(infer_Delay);
  1052. }
  1053. doing_Infer = false;
  1054. qDebug() << "Finished Clas-Infer." << "\n";
  1055. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  1056. }
  1057. void InferThread::Cls_Video()
  1058. {
  1059. doing_Infer = true;
  1060. VideoCapture cap = VideoCapture(video_path.toLocal8Bit().toStdString());
  1061. if(!cap.isOpened()) return;
  1062. Mat frame;
  1063. cap >> frame;
  1064. while(!frame.empty())
  1065. {
  1066. if (frame.cols > 512 || frame.rows > 512)
  1067. {
  1068. float ratio = min(frame.cols, frame.rows) / 512.;
  1069. int new_h = frame.cols / ratio;
  1070. int new_w = frame.rows / ratio;
  1071. cv::resize(frame, frame, cv::Size(new_h/4*4,new_w/4*4));
  1072. }
  1073. if (break_Infer)
  1074. {
  1075. doing_Infer = false;
  1076. break_Infer = false;
  1077. qDebug() << "Clas-Infer has Break." << "\n";
  1078. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  1079. return;
  1080. }
  1081. float pre_score[1];
  1082. int pre_category_id[1];
  1083. char pre_category[200];
  1084. try {
  1085. clock_t start_infer_time = clock();
  1086. qDebug() << "Doing Clas-Infer." << "\n";
  1087. cls_ModelPredict((const uchar*)frame.data, frame.cols, frame.rows, 3, pre_score, pre_category, pre_category_id);
  1088. double cost_time = 1000 * (clock() - start_infer_time) / (double)CLOCKS_PER_SEC;
  1089. emit SetCostTime(cost_time);
  1090. } catch (QException &e) {
  1091. doing_Infer = false;
  1092. qDebug() << "Finished Clas-Infer, but it is raise a exception." << "\n";
  1093. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  1094. return;
  1095. }
  1096. cvtColor(frame, frame, COLOR_BGR2RGB);
  1097. if (image1 == nullptr)
  1098. {
  1099. image1 = new Mat(frame.clone());
  1100. }
  1101. else
  1102. {
  1103. delete image1;
  1104. image1 = new Mat(frame.clone());
  1105. }
  1106. if (label1_image == nullptr)
  1107. {
  1108. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  1109. image1->step, QImage::Format_RGB888);
  1110. }
  1111. else
  1112. {
  1113. delete label1_image;
  1114. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  1115. image1->step, QImage::Format_RGB888);
  1116. }
  1117. int color_[3] = { (int)(color_map[(pre_category_id[0] % 256) * 3]),
  1118. (int)(color_map[(pre_category_id[0] % 256) * 3 + 1]),
  1119. (int)(color_map[(pre_category_id[0] % 256) * 3 + 2]) };
  1120. QString disscribe_str = makeLabelInfo(QString(pre_category), pre_category_id[0], pre_score[0]);
  1121. int baseline[1];
  1122. auto text_size = getTextSize(disscribe_str.toStdString(), FONT_HERSHEY_SIMPLEX,
  1123. 1.0, 2, baseline);
  1124. int text_left_downx = 0;
  1125. int text_left_downy = 0 + text_size.height;
  1126. putText(frame, disscribe_str.toStdString(), Point(text_left_downx, text_left_downy),
  1127. FONT_HERSHEY_SIMPLEX, 1.0,
  1128. Scalar(color_[0], color_[1], color_[2]), 2, LINE_8);
  1129. if (image2 == nullptr)
  1130. {
  1131. image2 = new Mat(frame.clone());
  1132. }
  1133. else
  1134. {
  1135. delete image2;
  1136. image2 = new Mat(frame.clone());
  1137. }
  1138. if (label2_image == nullptr)
  1139. {
  1140. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  1141. image2->step, QImage::Format_RGB888);
  1142. }
  1143. else
  1144. {
  1145. delete label2_image;
  1146. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  1147. image2->step, QImage::Format_RGB888);
  1148. }
  1149. emit InferFinished(label1_image, label2_image);
  1150. cap >> frame;
  1151. }
  1152. doing_Infer = false;
  1153. qDebug() << "Finished Clas-Infer." << "\n";
  1154. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  1155. }
  1156. void InferThread::Mask_Image()
  1157. {
  1158. Mat image = imread(image_path.toLocal8Bit().toStdString()); //BGR
  1159. if (image.cols > 512 || image.rows > 512)
  1160. {
  1161. float ratio = min(image.cols, image.rows) / 512.;
  1162. int new_h = image.cols / ratio;
  1163. int new_w = image.rows / ratio;
  1164. cv::resize(image, image, cv::Size(new_h/4*4,new_w/4*4));
  1165. }
  1166. // Predict output result
  1167. float bboxs[600];
  1168. int bbox_num[1];
  1169. char labellist[1000];
  1170. unsigned char out_image[image.cols * image.rows];
  1171. doing_Infer = true;
  1172. try {
  1173. clock_t start_infer_time = clock();
  1174. qDebug() << "Doing Mask-Infer." << "\n";
  1175. mask_ModelPredict((const uchar*)image.data, image.cols, image.rows, 3, bboxs, out_image, bbox_num, labellist);
  1176. double cost_time = 1000 * (clock() - start_infer_time) / (double)CLOCKS_PER_SEC;
  1177. emit SetCostTime(cost_time);
  1178. } catch (QException &e) {
  1179. doing_Infer = false;
  1180. qDebug() << "Finished Mask-Infer, but it is raise a exception." << "\n";
  1181. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  1182. return;
  1183. }
  1184. Mat out3c_image = Mat(image.clone());
  1185. for (int i = 0; i < out3c_image.rows; i++) // height
  1186. {
  1187. for (int j = 0; j < out3c_image.cols; j++) // width
  1188. {
  1189. int indexSrc = i*out3c_image.cols + j;
  1190. unsigned char color_id = (int)out_image[indexSrc] % 256;
  1191. if (color_id == 0)
  1192. out3c_image.at<Vec3b>(i, j) = Vec3b(0, 0, 0);
  1193. else
  1194. out3c_image.at<Vec3b>(i, j) = Vec3b(color_map[color_id * 3], color_map[color_id * 3 + 1], color_map[color_id * 3 + 2]);
  1195. }
  1196. }
  1197. cvtColor(image, image, COLOR_BGR2RGB);
  1198. if (image1 == nullptr)
  1199. {
  1200. image1 = new Mat(image.clone());
  1201. }
  1202. else
  1203. {
  1204. delete image1;
  1205. image1 = new Mat(image.clone());
  1206. }
  1207. if (label1_image == nullptr)
  1208. {
  1209. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  1210. image1->step, QImage::Format_RGB888);
  1211. }
  1212. else
  1213. {
  1214. delete label1_image;
  1215. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  1216. image1->step, QImage::Format_RGB888);
  1217. }
  1218. addWeighted(image, 0.5, out3c_image, 0.5, 0, image);
  1219. QString labels(labellist);
  1220. QStringList label_list = labels.split(' '); // Get Label
  1221. for (int i = 0; i < bbox_num[0]; i++)
  1222. {
  1223. int categry_id = (int)bboxs[i*6];
  1224. float score = bboxs[i*6 + 1];
  1225. int left_topx = (int)bboxs[i*6 + 2];
  1226. int left_topy = (int)bboxs[i*6 + 3];
  1227. int right_downx = left_topx + (int)bboxs[i*6 + 4];
  1228. int right_downy = left_topy + (int)bboxs[i*6 + 5];
  1229. if (score >= det_Threshold)
  1230. {
  1231. int color_[3] = { (int)(color_map[(categry_id % 256) * 3]),
  1232. (int)(color_map[(categry_id % 256) * 3 + 1]),
  1233. (int)(color_map[(categry_id % 256) * 3 + 2]) };
  1234. QString disscribe_str = makeLabelInfo(label_list[i], categry_id, score);
  1235. int baseline[1];
  1236. auto text_size = getTextSize(disscribe_str.toStdString(), FONT_HERSHEY_SIMPLEX,
  1237. 1.0, 2, baseline);
  1238. int text_left_downx = left_topx;
  1239. int text_left_downy = left_topy + text_size.height;
  1240. rectangle(image, Point(left_topx, left_topy),
  1241. Point(right_downx, right_downy),
  1242. Scalar(color_[0], color_[1], color_[2]), 2, LINE_8);
  1243. putText(image, disscribe_str.toStdString(), Point(text_left_downx, text_left_downy),
  1244. FONT_HERSHEY_SIMPLEX, 1.0,
  1245. Scalar(color_[0], color_[1], color_[2]), 2, LINE_8);
  1246. }
  1247. }
  1248. if (image2 == nullptr)
  1249. {
  1250. image2 = new Mat(image.clone());
  1251. }
  1252. else
  1253. {
  1254. delete image2;
  1255. image2 = new Mat(image.clone());
  1256. }
  1257. if (label2_image == nullptr)
  1258. {
  1259. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  1260. image2->step, QImage::Format_RGB888);
  1261. }
  1262. else
  1263. {
  1264. delete label2_image;
  1265. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  1266. image2->step, QImage::Format_RGB888);
  1267. }
  1268. emit InferFinished(label1_image, label2_image);
  1269. doing_Infer = false;
  1270. qDebug() << "Finished Mask-Infer." << "\n";
  1271. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  1272. }
  1273. void InferThread::Mask_Images()
  1274. {
  1275. doing_Infer = true;
  1276. for (int j = 0; j < images_path.count(); j++)
  1277. {
  1278. if (break_Infer)
  1279. {
  1280. doing_Infer = false;
  1281. break_Infer = false;
  1282. qDebug() << "Mask-Infer has Break." << "\n";
  1283. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  1284. return;
  1285. }
  1286. QString img_file = images_path[j];
  1287. Mat image = imread(img_file.toLocal8Bit().toStdString());
  1288. if (image.cols > 512 || image.rows > 512)
  1289. {
  1290. float ratio = min(image.cols, image.rows) / 512.;
  1291. int new_h = image.cols / ratio;
  1292. int new_w = image.rows / ratio;
  1293. cv::resize(image, image, cv::Size(new_h/4*4,new_w/4*4));
  1294. }
  1295. float bboxs[600];
  1296. int bbox_num[1];
  1297. char labellist[1000];
  1298. unsigned char out_image[image.cols * image.rows];
  1299. memset(out_image, 0, sizeof (out_image));
  1300. try {
  1301. clock_t start_infer_time = clock();
  1302. qDebug() << "Doing Mask-Infer." << "\n";
  1303. mask_ModelPredict((const uchar*)image.data, image.cols, image.rows, 3, bboxs, out_image, bbox_num, labellist);
  1304. double cost_time = 1000 * (clock() - start_infer_time) / (double)CLOCKS_PER_SEC;
  1305. emit SetCostTime(cost_time);
  1306. } catch (QException &e) {
  1307. doing_Infer = false;
  1308. qDebug() << "Finished Mask-Infer, but it is raise a exception." << "\n";
  1309. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  1310. return;
  1311. }
  1312. Mat out3c_image = Mat(image.clone());
  1313. for (int i = 0; i < out3c_image.rows; i++) // height
  1314. {
  1315. for (int j = 0; j < out3c_image.cols; j++) // width
  1316. {
  1317. int indexSrc = i*out3c_image.cols + j;
  1318. unsigned char color_id = (int)out_image[indexSrc] % 256;
  1319. if (color_id == 0)
  1320. out3c_image.at<Vec3b>(i, j) = Vec3b(0, 0, 0);
  1321. else
  1322. out3c_image.at<Vec3b>(i, j) = Vec3b(color_map[color_id * 3], color_map[color_id * 3 + 1], color_map[color_id * 3 + 2]);
  1323. }
  1324. }
  1325. cvtColor(image, image, COLOR_BGR2RGB);
  1326. if (image1 == nullptr)
  1327. {
  1328. image1 = new Mat(image.clone());
  1329. }
  1330. else
  1331. {
  1332. delete image1;
  1333. image1 = new Mat(image.clone());
  1334. }
  1335. if (label1_image == nullptr)
  1336. {
  1337. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  1338. image1->step, QImage::Format_RGB888);
  1339. }
  1340. else
  1341. {
  1342. delete label1_image;
  1343. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  1344. image1->step, QImage::Format_RGB888);
  1345. }
  1346. addWeighted(image, 0.5, out3c_image, 0.5, 0, image);
  1347. QString labels(labellist);
  1348. QStringList label_list = labels.split(' '); // 获取label
  1349. for (int i = 0; i < bbox_num[0]; i++)
  1350. {
  1351. int categry_id = (int)bboxs[i*6];
  1352. float score = bboxs[i*6 + 1];
  1353. int left_topx = (int)bboxs[i*6 + 2];
  1354. int left_topy = (int)bboxs[i*6 + 3];
  1355. int right_downx = left_topx + (int)bboxs[i*6 + 4];
  1356. int right_downy = left_topy + (int)bboxs[i*6 + 5];
  1357. if (score >= det_Threshold)
  1358. {
  1359. int color_[3] = { (int)(color_map[(categry_id % 256) * 3]),
  1360. (int)(color_map[(categry_id % 256) * 3 + 1]),
  1361. (int)(color_map[(categry_id % 256) * 3 + 2]) };
  1362. QString disscribe_str = makeLabelInfo(label_list[i], categry_id, score);
  1363. int baseline[1];
  1364. auto text_size = getTextSize(disscribe_str.toStdString(), FONT_HERSHEY_SIMPLEX,
  1365. 1.0, 2, baseline);
  1366. int text_left_downx = left_topx;
  1367. int text_left_downy = left_topy + text_size.height;
  1368. rectangle(image, Point(left_topx, left_topy),
  1369. Point(right_downx, right_downy),
  1370. Scalar(color_[0], color_[1], color_[2]), 2, LINE_8);
  1371. putText(image, disscribe_str.toStdString(), Point(text_left_downx, text_left_downy),
  1372. FONT_HERSHEY_SIMPLEX, 1.0,
  1373. Scalar(color_[0], color_[1], color_[2]), 2, LINE_8);
  1374. }
  1375. }
  1376. if (image2 == nullptr)
  1377. {
  1378. image2 = new Mat(image.clone());
  1379. }
  1380. else
  1381. {
  1382. delete image2;
  1383. image2 = new Mat(image.clone());
  1384. }
  1385. if (label2_image == nullptr)
  1386. {
  1387. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  1388. image2->step, QImage::Format_RGB888);
  1389. }
  1390. else
  1391. {
  1392. delete label2_image;
  1393. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  1394. image2->step, QImage::Format_RGB888);
  1395. }
  1396. emit InferFinished(label1_image, label2_image);
  1397. this->msleep(infer_Delay);
  1398. }
  1399. doing_Infer = false;
  1400. qDebug() << "Finished Mask-Infer." << "\n";
  1401. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  1402. }
  1403. void InferThread::Mask_Video()
  1404. {
  1405. doing_Infer = true;
  1406. VideoCapture cap = VideoCapture(video_path.toLocal8Bit().toStdString());
  1407. if(!cap.isOpened()) return;
  1408. Mat frame;
  1409. cap >> frame;
  1410. while(!frame.empty())
  1411. {
  1412. if (frame.cols > 512 || frame.rows > 512)
  1413. {
  1414. float ratio = min(frame.cols, frame.rows) / 512.;
  1415. int new_h = frame.cols / ratio;
  1416. int new_w = frame.rows / ratio;
  1417. cv::resize(frame, frame, cv::Size(new_h/4*4,new_w/4*4));
  1418. }
  1419. if (break_Infer)
  1420. {
  1421. doing_Infer = false;
  1422. break_Infer = false;
  1423. qDebug() << "Mask-Infer has Break." << "\n";
  1424. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  1425. return;
  1426. }
  1427. float bboxs[600];
  1428. int bbox_num[1];
  1429. char labellist[1000];
  1430. unsigned char out_image[frame.cols * frame.rows];
  1431. memset(out_image, 0, sizeof (out_image));
  1432. try {
  1433. clock_t start_infer_time = clock();
  1434. qDebug() << "Doing Mask-Infer." << "\n";
  1435. mask_ModelPredict((const uchar*)frame.data, frame.cols, frame.rows, 3, bboxs, out_image, bbox_num, labellist);
  1436. double cost_time = 1000 * (clock() - start_infer_time) / (double)CLOCKS_PER_SEC;
  1437. emit SetCostTime(cost_time);
  1438. } catch (QException &e) {
  1439. doing_Infer = false;
  1440. qDebug() << "Finished Mask-Infer, but it is raise a exception." << "\n";
  1441. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  1442. return;
  1443. }
  1444. Mat out3c_image = Mat(frame.clone());
  1445. for (int i = 0; i < out3c_image.rows; i++) // height
  1446. {
  1447. for (int j = 0; j < out3c_image.cols; j++) // width
  1448. {
  1449. int indexSrc = i*out3c_image.cols + j;
  1450. unsigned char color_id = (int)out_image[indexSrc] % 256;
  1451. if (color_id == 0)
  1452. out3c_image.at<Vec3b>(i, j) = Vec3b(0, 0, 0);
  1453. else
  1454. out3c_image.at<Vec3b>(i, j) = Vec3b(color_map[color_id * 3], color_map[color_id * 3 + 1], color_map[color_id * 3 + 2]);
  1455. }
  1456. }
  1457. cvtColor(frame, frame, COLOR_BGR2RGB);
  1458. if (image1 == nullptr)
  1459. {
  1460. image1 = new Mat(frame.clone());
  1461. }
  1462. else
  1463. {
  1464. delete image1;
  1465. image1 = new Mat(frame.clone());
  1466. }
  1467. if (label1_image == nullptr)
  1468. {
  1469. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  1470. image1->step, QImage::Format_RGB888);
  1471. }
  1472. else
  1473. {
  1474. delete label1_image;
  1475. label1_image = new QImage((const uchar*)image1->data, image1->cols, image1->rows,
  1476. image1->step, QImage::Format_RGB888);
  1477. }
  1478. addWeighted(frame, 0.5, out3c_image, 0.5, 0, frame);
  1479. QString labels(labellist);
  1480. QStringList label_list = labels.split(' ');
  1481. for (int i = 0; i < bbox_num[0]; i++)
  1482. {
  1483. int categry_id = (int)bboxs[i*6];
  1484. float score = bboxs[i*6 + 1];
  1485. int left_topx = (int)bboxs[i*6 + 2];
  1486. int left_topy = (int)bboxs[i*6 + 3];
  1487. int right_downx = left_topx + (int)bboxs[i*6 + 4];
  1488. int right_downy = left_topy + (int)bboxs[i*6 + 5];
  1489. if (score >= det_Threshold)
  1490. {
  1491. int color_[3] = { (int)(color_map[(categry_id % 256) * 3]),
  1492. (int)(color_map[(categry_id % 256) * 3 + 1]),
  1493. (int)(color_map[(categry_id % 256) * 3 + 2]) };
  1494. QString disscribe_str = makeLabelInfo(label_list[i], categry_id, score);
  1495. int baseline[1];
  1496. auto text_size = getTextSize(disscribe_str.toStdString(), FONT_HERSHEY_SIMPLEX,
  1497. 1.0, 2, baseline);
  1498. int text_left_downx = left_topx;
  1499. int text_left_downy = left_topy + text_size.height;
  1500. rectangle(frame, Point(left_topx, left_topy),
  1501. Point(right_downx, right_downy),
  1502. Scalar(color_[0], color_[1], color_[2]), 2, LINE_8);
  1503. putText(frame, disscribe_str.toStdString(), Point(text_left_downx, text_left_downy),
  1504. FONT_HERSHEY_SIMPLEX, 1.0,
  1505. Scalar(color_[0], color_[1], color_[2]), 2, LINE_8);
  1506. }
  1507. }
  1508. if (image2 == nullptr)
  1509. {
  1510. image2 = new Mat(frame.clone());
  1511. }
  1512. else
  1513. {
  1514. delete image2;
  1515. image2 = new Mat(frame.clone());
  1516. }
  1517. if (label2_image == nullptr)
  1518. {
  1519. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  1520. image2->step, QImage::Format_RGB888);
  1521. }
  1522. else
  1523. {
  1524. delete label2_image;
  1525. label2_image = new QImage((const uchar*)image2->data, image2->cols, image2->rows,
  1526. image2->step, QImage::Format_RGB888);
  1527. }
  1528. emit InferFinished(label1_image, label2_image);
  1529. cap >> frame;
  1530. }
  1531. doing_Infer = false;
  1532. qDebug() << "Finished Mask-Infer." << "\n";
  1533. emit SetState_Btn_StopAndInfer(false, true); // first is stop, second is infer
  1534. }