result.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "ultra_infer/vision/common/result.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. void ClassifyResult::Free() {
  18. std::vector<int32_t>().swap(label_ids);
  19. std::vector<float>().swap(scores);
  20. std::vector<float>().swap(feature);
  21. }
  22. void ClassifyResult::Clear() {
  23. label_ids.clear();
  24. scores.clear();
  25. feature.clear();
  26. }
  27. void ClassifyResult::Resize(int size) {
  28. label_ids.resize(size);
  29. scores.resize(size);
  30. // TODO(qiuyanjun): feature not perform resize now.
  31. // may need the code below for future.
  32. // feature.resize(size);
  33. }
  34. std::string ClassifyResult::Str() {
  35. std::string out;
  36. out = "ClassifyResult(\nlabel_ids: ";
  37. for (size_t i = 0; i < label_ids.size(); ++i) {
  38. out = out + std::to_string(label_ids[i]) + ", ";
  39. }
  40. out += "\nscores: ";
  41. for (size_t i = 0; i < scores.size(); ++i) {
  42. out = out + std::to_string(scores[i]) + ", ";
  43. }
  44. if (!feature.empty()) {
  45. out += "\nfeature: size (";
  46. out += std::to_string(feature.size()) + "), only show first 100 values.\n";
  47. for (size_t i = 0; i < feature.size(); ++i) {
  48. // only show first 100 values.
  49. if ((i + 1) <= 100) {
  50. out = out + std::to_string(feature[i]) + ", ";
  51. if ((i + 1) % 10 == 0 && (i + 1) < 100) {
  52. out += "\n";
  53. }
  54. if ((i + 1) == 100) {
  55. out += "\n......";
  56. }
  57. }
  58. }
  59. }
  60. out += "\n)";
  61. return out;
  62. }
  63. ClassifyResult &ClassifyResult::operator=(ClassifyResult &&other) {
  64. if (&other != this) {
  65. label_ids = std::move(other.label_ids);
  66. scores = std::move(other.scores);
  67. feature = std::move(other.feature);
  68. }
  69. return *this;
  70. }
  71. void Mask::Reserve(int size) { data.reserve(size); }
  72. void Mask::Resize(int size) { data.resize(size); }
  73. void Mask::Free() {
  74. std::vector<uint32_t>().swap(data);
  75. std::vector<int64_t>().swap(shape);
  76. }
  77. void Mask::Clear() {
  78. data.clear();
  79. shape.clear();
  80. }
  81. std::string Mask::Str() {
  82. std::string out = "Mask(";
  83. size_t ndim = shape.size();
  84. for (size_t i = 0; i < ndim; ++i) {
  85. if (i < ndim - 1) {
  86. out += std::to_string(shape[i]) + ",";
  87. } else {
  88. out += std::to_string(shape[i]);
  89. }
  90. }
  91. out += ")\n";
  92. return out;
  93. }
  94. DetectionResult::DetectionResult(const DetectionResult &res) {
  95. boxes.assign(res.boxes.begin(), res.boxes.end());
  96. rotated_boxes.assign(res.rotated_boxes.begin(), res.rotated_boxes.end());
  97. scores.assign(res.scores.begin(), res.scores.end());
  98. label_ids.assign(res.label_ids.begin(), res.label_ids.end());
  99. contain_masks = res.contain_masks;
  100. if (contain_masks) {
  101. masks.clear();
  102. size_t mask_size = res.masks.size();
  103. for (size_t i = 0; i < mask_size; ++i) {
  104. masks.emplace_back(res.masks[i]);
  105. }
  106. }
  107. }
  108. DetectionResult &DetectionResult::operator=(DetectionResult &&other) {
  109. if (&other != this) {
  110. boxes = std::move(other.boxes);
  111. rotated_boxes = std::move(other.rotated_boxes);
  112. scores = std::move(other.scores);
  113. label_ids = std::move(other.label_ids);
  114. contain_masks = std::move(other.contain_masks);
  115. if (contain_masks) {
  116. masks.clear();
  117. masks = std::move(other.masks);
  118. }
  119. }
  120. return *this;
  121. }
  122. void DetectionResult::Free() {
  123. std::vector<std::array<float, 4>>().swap(boxes);
  124. std::vector<std::array<float, 8>>().swap(rotated_boxes);
  125. std::vector<float>().swap(scores);
  126. std::vector<int32_t>().swap(label_ids);
  127. std::vector<Mask>().swap(masks);
  128. contain_masks = false;
  129. }
  130. void DetectionResult::Clear() {
  131. boxes.clear();
  132. rotated_boxes.clear();
  133. scores.clear();
  134. label_ids.clear();
  135. masks.clear();
  136. contain_masks = false;
  137. }
  138. void DetectionResult::Reserve(int size) {
  139. boxes.reserve(size);
  140. rotated_boxes.reserve(size);
  141. scores.reserve(size);
  142. label_ids.reserve(size);
  143. if (contain_masks) {
  144. masks.reserve(size);
  145. }
  146. }
  147. void DetectionResult::Resize(int size) {
  148. boxes.resize(size);
  149. rotated_boxes.resize(size);
  150. scores.resize(size);
  151. label_ids.resize(size);
  152. if (contain_masks) {
  153. masks.resize(size);
  154. }
  155. }
  156. std::string DetectionResult::Str() {
  157. std::string out;
  158. if (!contain_masks) {
  159. out = "DetectionResult: [xmin, ymin, xmax, ymax, score, label_id]\n";
  160. if (!rotated_boxes.empty()) {
  161. out = "DetectionResult: [x1, y1, x2, y2, x3, y3, x4, y4, score, "
  162. "label_id]\n";
  163. }
  164. } else {
  165. out = "DetectionResult: [xmin, ymin, xmax, ymax, score, label_id, "
  166. "mask_shape]\n";
  167. if (!rotated_boxes.empty()) {
  168. out =
  169. "DetectionResult: [x1, y1, x2, y2, x3, y3, x4, y4, score, label_id, "
  170. "mask_shape]\n";
  171. }
  172. }
  173. for (size_t i = 0; i < boxes.size(); ++i) {
  174. out = out + std::to_string(boxes[i][0]) + "," +
  175. std::to_string(boxes[i][1]) + ", " + std::to_string(boxes[i][2]) +
  176. ", " + std::to_string(boxes[i][3]) + ", " +
  177. std::to_string(scores[i]) + ", " + std::to_string(label_ids[i]);
  178. if (!contain_masks) {
  179. out += "\n";
  180. } else {
  181. out += ", " + masks[i].Str();
  182. }
  183. }
  184. for (size_t i = 0; i < rotated_boxes.size(); ++i) {
  185. out = out + std::to_string(rotated_boxes[i][0]) + "," +
  186. std::to_string(rotated_boxes[i][1]) + ", " +
  187. std::to_string(rotated_boxes[i][2]) + ", " +
  188. std::to_string(rotated_boxes[i][3]) + ", " +
  189. std::to_string(rotated_boxes[i][4]) + "," +
  190. std::to_string(rotated_boxes[i][5]) + ", " +
  191. std::to_string(rotated_boxes[i][6]) + ", " +
  192. std::to_string(rotated_boxes[i][7]) + ", " +
  193. std::to_string(scores[i]) + ", " + std::to_string(label_ids[i]);
  194. out += "\n";
  195. }
  196. return out;
  197. }
  198. // PerceptionResult -----------------------------------------------------
  199. PerceptionResult::PerceptionResult(const PerceptionResult &res) {
  200. scores.assign(res.scores.begin(), res.scores.end());
  201. label_ids.assign(res.label_ids.begin(), res.label_ids.end());
  202. boxes.assign(res.boxes.begin(), res.boxes.end());
  203. center.assign(res.center.begin(), res.center.end());
  204. observation_angle.assign(res.observation_angle.begin(),
  205. res.observation_angle.end());
  206. yaw_angle.assign(res.yaw_angle.begin(), res.yaw_angle.end());
  207. velocity.assign(res.velocity.begin(), res.velocity.end());
  208. valid.assign(res.valid.begin(), res.valid.end());
  209. }
  210. PerceptionResult &PerceptionResult::operator=(PerceptionResult &&other) {
  211. if (&other != this) {
  212. scores = std::move(other.scores);
  213. label_ids = std::move(other.label_ids);
  214. boxes = std::move(other.boxes);
  215. center = std::move(other.center);
  216. observation_angle = std::move(other.observation_angle);
  217. yaw_angle = std::move(other.yaw_angle);
  218. velocity = std::move(other.velocity);
  219. valid = std::move(other.valid);
  220. }
  221. return *this;
  222. }
  223. void PerceptionResult::Free() {
  224. std::vector<float>().swap(scores);
  225. std::vector<int32_t>().swap(label_ids);
  226. std::vector<std::array<float, 7>>().swap(boxes);
  227. std::vector<std::array<float, 3>>().swap(center);
  228. std::vector<float>().swap(observation_angle);
  229. std::vector<float>().swap(yaw_angle);
  230. std::vector<std::array<float, 3>>().swap(velocity);
  231. std::vector<bool>().swap(valid);
  232. }
  233. void PerceptionResult::Clear() {
  234. scores.clear();
  235. label_ids.clear();
  236. boxes.clear();
  237. center.clear();
  238. observation_angle.clear();
  239. yaw_angle.clear();
  240. velocity.clear();
  241. valid.clear();
  242. }
  243. void PerceptionResult::Reserve(int size) {
  244. scores.reserve(size);
  245. label_ids.reserve(size);
  246. boxes.reserve(size);
  247. center.reserve(size);
  248. observation_angle.reserve(size);
  249. yaw_angle.reserve(size);
  250. velocity.reserve(size);
  251. }
  252. void PerceptionResult::Resize(int size) {
  253. scores.resize(size);
  254. label_ids.resize(size);
  255. boxes.resize(size);
  256. center.resize(size);
  257. observation_angle.resize(size);
  258. yaw_angle.resize(size);
  259. velocity.resize(size);
  260. }
  261. std::string PerceptionResult::Str() {
  262. std::string out;
  263. out = "PerceptionResult: [";
  264. if (valid[2]) {
  265. out += "xmin, ymin, xmax, ymax, w, h, l,";
  266. }
  267. if (valid[3]) {
  268. out += " cx, cy, cz,";
  269. }
  270. if (valid[5]) {
  271. out += " yaw_angle,";
  272. }
  273. if (valid[4]) {
  274. out += " ob_angle,";
  275. }
  276. if (valid[0]) {
  277. out += " score,";
  278. }
  279. if (valid[1]) {
  280. out += " label_id,";
  281. }
  282. out += "]\n";
  283. for (size_t i = 0; i < boxes.size(); ++i) {
  284. if (valid[2]) {
  285. out = out + std::to_string(boxes[i][0]) + "," +
  286. std::to_string(boxes[i][1]) + ", " + std::to_string(boxes[i][2]) +
  287. ", " + std::to_string(boxes[i][3]) + ", " +
  288. std::to_string(boxes[i][4]) + ", " + std::to_string(boxes[i][5]) +
  289. ", " + std::to_string(boxes[i][6]) + ", ";
  290. }
  291. if (valid[3]) {
  292. out = out + std::to_string(center[i][0]) + ", " +
  293. std::to_string(center[i][1]) + ", " + std::to_string(center[i][2]) +
  294. ", ";
  295. }
  296. if (valid[5]) {
  297. out = out + std::to_string(yaw_angle[i]) + ", ";
  298. }
  299. if (valid[4]) {
  300. out = out + std::to_string(observation_angle[i]) + ", ";
  301. }
  302. if (valid[0]) {
  303. out = out + std::to_string(scores[i]) + ", ";
  304. }
  305. if (valid[1]) {
  306. out = out + std::to_string(label_ids[i]);
  307. }
  308. out += "\n";
  309. }
  310. return out;
  311. }
  312. // PerceptionResult finished
  313. void KeyPointDetectionResult::Free() {
  314. std::vector<std::array<float, 2>>().swap(keypoints);
  315. std::vector<float>().swap(scores);
  316. num_joints = -1;
  317. }
  318. void KeyPointDetectionResult::Clear() {
  319. keypoints.clear();
  320. scores.clear();
  321. num_joints = -1;
  322. }
  323. void KeyPointDetectionResult::Reserve(int size) { keypoints.reserve(size); }
  324. void KeyPointDetectionResult::Resize(int size) { keypoints.resize(size); }
  325. std::string KeyPointDetectionResult::Str() {
  326. std::string out;
  327. out = "KeyPointDetectionResult: [x, y, conf]\n";
  328. for (size_t i = 0; i < keypoints.size(); ++i) {
  329. out = out + std::to_string(keypoints[i][0]) + "," +
  330. std::to_string(keypoints[i][1]) + ", " + std::to_string(scores[i]) +
  331. "\n";
  332. }
  333. out += "num_joints:" + std::to_string(num_joints) + "\n";
  334. return out;
  335. }
  336. void OCRResult::Clear() {
  337. boxes.clear();
  338. text.clear();
  339. rec_scores.clear();
  340. cls_scores.clear();
  341. cls_labels.clear();
  342. }
  343. void OCRCURVEResult::Clear() {
  344. boxes.clear();
  345. text.clear();
  346. rec_scores.clear();
  347. cls_scores.clear();
  348. cls_labels.clear();
  349. }
  350. void MOTResult::Clear() {
  351. boxes.clear();
  352. ids.clear();
  353. scores.clear();
  354. class_ids.clear();
  355. }
  356. std::string MOTResult::Str() {
  357. std::string out;
  358. out = "MOTResult:\nall boxes counts: " + std::to_string(boxes.size()) + "\n";
  359. out += "[xmin\tymin\txmax\tymax\tid\tscore]\n";
  360. for (size_t i = 0; i < boxes.size(); ++i) {
  361. out = out + "[" + std::to_string(boxes[i][0]) + "\t" +
  362. std::to_string(boxes[i][1]) + "\t" + std::to_string(boxes[i][2]) +
  363. "\t" + std::to_string(boxes[i][3]) + "\t" + std::to_string(ids[i]) +
  364. "\t" + std::to_string(scores[i]) + "]\n";
  365. }
  366. return out;
  367. }
  368. FaceDetectionResult::FaceDetectionResult(const FaceDetectionResult &res) {
  369. boxes.assign(res.boxes.begin(), res.boxes.end());
  370. landmarks.assign(res.landmarks.begin(), res.landmarks.end());
  371. scores.assign(res.scores.begin(), res.scores.end());
  372. landmarks_per_face = res.landmarks_per_face;
  373. }
  374. void FaceDetectionResult::Free() {
  375. std::vector<std::array<float, 4>>().swap(boxes);
  376. std::vector<float>().swap(scores);
  377. std::vector<std::array<float, 2>>().swap(landmarks);
  378. landmarks_per_face = 0;
  379. }
  380. void FaceDetectionResult::Clear() {
  381. boxes.clear();
  382. scores.clear();
  383. landmarks.clear();
  384. landmarks_per_face = 0;
  385. }
  386. void FaceDetectionResult::Reserve(int size) {
  387. boxes.reserve(size);
  388. scores.reserve(size);
  389. if (landmarks_per_face > 0) {
  390. landmarks.reserve(size * landmarks_per_face);
  391. }
  392. }
  393. void FaceDetectionResult::Resize(int size) {
  394. boxes.resize(size);
  395. scores.resize(size);
  396. if (landmarks_per_face > 0) {
  397. landmarks.resize(size * landmarks_per_face);
  398. }
  399. }
  400. std::string FaceDetectionResult::Str() {
  401. std::string out;
  402. // format without landmarks
  403. if (landmarks_per_face <= 0) {
  404. out = "FaceDetectionResult: [xmin, ymin, xmax, ymax, score]\n";
  405. for (size_t i = 0; i < boxes.size(); ++i) {
  406. out = out + std::to_string(boxes[i][0]) + "," +
  407. std::to_string(boxes[i][1]) + ", " + std::to_string(boxes[i][2]) +
  408. ", " + std::to_string(boxes[i][3]) + ", " +
  409. std::to_string(scores[i]) + "\n";
  410. }
  411. return out;
  412. }
  413. // format with landmarks
  414. FDASSERT((landmarks.size() == boxes.size() * landmarks_per_face),
  415. "The size of landmarks != boxes.size * landmarks_per_face.");
  416. out = "FaceDetectionResult: [xmin, ymin, xmax, ymax, score, (x, y) x " +
  417. std::to_string(landmarks_per_face) + "]\n";
  418. for (size_t i = 0; i < boxes.size(); ++i) {
  419. out = out + std::to_string(boxes[i][0]) + "," +
  420. std::to_string(boxes[i][1]) + ", " + std::to_string(boxes[i][2]) +
  421. ", " + std::to_string(boxes[i][3]) + ", " +
  422. std::to_string(scores[i]) + ", ";
  423. for (size_t j = 0; j < landmarks_per_face; ++j) {
  424. out = out + "(" +
  425. std::to_string(landmarks[i * landmarks_per_face + j][0]) + "," +
  426. std::to_string(landmarks[i * landmarks_per_face + j][1]);
  427. if (j < landmarks_per_face - 1) {
  428. out = out + "), ";
  429. } else {
  430. out = out + ")\n";
  431. }
  432. }
  433. }
  434. return out;
  435. }
  436. void FaceAlignmentResult::Free() {
  437. std::vector<std::array<float, 2>>().swap(landmarks);
  438. }
  439. void FaceAlignmentResult::Clear() { landmarks.clear(); }
  440. void FaceAlignmentResult::Reserve(int size) { landmarks.resize(size); }
  441. void FaceAlignmentResult::Resize(int size) { landmarks.resize(size); }
  442. std::string FaceAlignmentResult::Str() {
  443. std::string out;
  444. out = "FaceAlignmentResult: [x, y]\n";
  445. out = out + "There are " + std::to_string(landmarks.size()) +
  446. " landmarks, the top 10 are listed as below:\n";
  447. int landmarks_size = landmarks.size();
  448. size_t result_length = std::min(10, landmarks_size);
  449. for (size_t i = 0; i < result_length; ++i) {
  450. out = out + std::to_string(landmarks[i][0]) + "," +
  451. std::to_string(landmarks[i][1]) + "\n";
  452. }
  453. out += "num_landmarks:" + std::to_string(landmarks.size()) + "\n";
  454. return out;
  455. }
  456. void SegmentationResult::Clear() {
  457. label_map.clear();
  458. score_map.clear();
  459. shape.clear();
  460. contain_score_map = false;
  461. }
  462. void SegmentationResult::Free() {
  463. std::vector<uint8_t>().swap(label_map);
  464. std::vector<float>().swap(score_map);
  465. std::vector<int64_t>().swap(shape);
  466. contain_score_map = false;
  467. }
  468. void SegmentationResult::Reserve(int size) {
  469. label_map.reserve(size);
  470. if (contain_score_map) {
  471. score_map.reserve(size);
  472. }
  473. }
  474. void SegmentationResult::Resize(int size) {
  475. label_map.resize(size);
  476. if (contain_score_map) {
  477. score_map.resize(size);
  478. }
  479. }
  480. std::string SegmentationResult::Str() {
  481. std::string out;
  482. out = "SegmentationResult Image masks 10 rows x 10 cols: \n";
  483. for (size_t i = 0; i < 10; ++i) {
  484. out += "[";
  485. for (size_t j = 0; j < 10; ++j) {
  486. out = out + std::to_string(label_map[i * 10 + j]) + ", ";
  487. }
  488. out += ".....]\n";
  489. }
  490. out += "...........\n";
  491. if (contain_score_map) {
  492. out += "SegmentationResult Score map 10 rows x 10 cols: \n";
  493. for (size_t i = 0; i < 10; ++i) {
  494. out += "[";
  495. for (size_t j = 0; j < 10; ++j) {
  496. out = out + std::to_string(score_map[i * 10 + j]) + ", ";
  497. }
  498. out += ".....]\n";
  499. }
  500. out += "...........\n";
  501. }
  502. out += "result shape is: [" + std::to_string(shape[0]) + " " +
  503. std::to_string(shape[1]) + "]";
  504. return out;
  505. }
  506. SegmentationResult &SegmentationResult::operator=(SegmentationResult &&other) {
  507. if (&other != this) {
  508. label_map = std::move(other.label_map);
  509. shape = std::move(other.shape);
  510. contain_score_map = std::move(other.contain_score_map);
  511. if (contain_score_map) {
  512. score_map.clear();
  513. score_map = std::move(other.score_map);
  514. }
  515. }
  516. return *this;
  517. }
  518. FaceRecognitionResult::FaceRecognitionResult(const FaceRecognitionResult &res) {
  519. embedding.assign(res.embedding.begin(), res.embedding.end());
  520. }
  521. void FaceRecognitionResult::Free() { std::vector<float>().swap(embedding); }
  522. void FaceRecognitionResult::Clear() { embedding.clear(); }
  523. void FaceRecognitionResult::Reserve(int size) { embedding.reserve(size); }
  524. void FaceRecognitionResult::Resize(int size) { embedding.resize(size); }
  525. std::string FaceRecognitionResult::Str() {
  526. std::string out;
  527. out = "FaceRecognitionResult: [";
  528. size_t numel = embedding.size();
  529. if (numel <= 0) {
  530. return out + "Empty Result]";
  531. }
  532. // max, min, mean
  533. float min_val = embedding.at(0);
  534. float max_val = embedding.at(0);
  535. float total_val = embedding.at(0);
  536. for (size_t i = 1; i < numel; ++i) {
  537. float val = embedding.at(i);
  538. total_val += val;
  539. if (val < min_val) {
  540. min_val = val;
  541. }
  542. if (val > max_val) {
  543. max_val = val;
  544. }
  545. }
  546. float mean_val = total_val / static_cast<float>(numel);
  547. out = out + "Dim(" + std::to_string(numel) + "), " + "Min(" +
  548. std::to_string(min_val) + "), " + "Max(" + std::to_string(max_val) +
  549. "), " + "Mean(" + std::to_string(mean_val) + ")]\n";
  550. return out;
  551. }
  552. MattingResult::MattingResult(const MattingResult &res) {
  553. alpha.assign(res.alpha.begin(), res.alpha.end());
  554. foreground.assign(res.foreground.begin(), res.foreground.end());
  555. shape.assign(res.shape.begin(), res.shape.end());
  556. contain_foreground = res.contain_foreground;
  557. }
  558. void MattingResult::Clear() {
  559. alpha.clear();
  560. foreground.clear();
  561. shape.clear();
  562. contain_foreground = false;
  563. }
  564. void MattingResult::Free() {
  565. std::vector<float>().swap(alpha);
  566. std::vector<float>().swap(foreground);
  567. std::vector<int64_t>().swap(shape);
  568. contain_foreground = false;
  569. }
  570. void MattingResult::Reserve(int size) {
  571. alpha.reserve(size);
  572. if (contain_foreground) {
  573. FDASSERT((shape.size() == 3),
  574. "Please initial shape (h,w,c) before call Reserve.");
  575. int c = static_cast<int>(shape[2]);
  576. foreground.reserve(size * c);
  577. }
  578. }
  579. void MattingResult::Resize(int size) {
  580. alpha.resize(size);
  581. if (contain_foreground) {
  582. FDASSERT((shape.size() == 3),
  583. "Please initial shape (h,w,c) before call Resize.");
  584. int c = static_cast<int>(shape[2]);
  585. foreground.resize(size * c);
  586. }
  587. }
  588. std::string MattingResult::Str() {
  589. std::string out;
  590. out = "MattingResult[";
  591. if (contain_foreground) {
  592. out += "Foreground(true)";
  593. } else {
  594. out += "Foreground(false)";
  595. }
  596. out += ", Alpha(";
  597. size_t numel = alpha.size();
  598. if (numel <= 0) {
  599. return out + "[Empty Result]";
  600. }
  601. // max, min, mean
  602. float min_val = alpha.at(0);
  603. float max_val = alpha.at(0);
  604. float total_val = alpha.at(0);
  605. for (size_t i = 1; i < numel; ++i) {
  606. float val = alpha.at(i);
  607. total_val += val;
  608. if (val < min_val) {
  609. min_val = val;
  610. }
  611. if (val > max_val) {
  612. max_val = val;
  613. }
  614. }
  615. float mean_val = total_val / static_cast<float>(numel);
  616. // shape
  617. std::string shape_str = "Shape(";
  618. for (size_t i = 0; i < shape.size(); ++i) {
  619. if ((i + 1) != shape.size()) {
  620. shape_str += std::to_string(shape[i]) + ",";
  621. } else {
  622. shape_str += std::to_string(shape[i]) + ")";
  623. }
  624. }
  625. out = out + "Numel(" + std::to_string(numel) + "), " + shape_str + ", Min(" +
  626. std::to_string(min_val) + "), " + "Max(" + std::to_string(max_val) +
  627. "), " + "Mean(" + std::to_string(mean_val) + "))]\n";
  628. return out;
  629. }
  630. std::string OCRResult::Str() {
  631. std::string no_result;
  632. if (boxes.size() > 0) {
  633. std::string out;
  634. for (int n = 0; n < boxes.size(); n++) {
  635. out = out + "det boxes: [";
  636. for (int i = 0; i < 4; i++) {
  637. out = out + "[" + std::to_string(boxes[n][i * 2]) + "," +
  638. std::to_string(boxes[n][i * 2 + 1]) + "]";
  639. if (i != 3) {
  640. out = out + ",";
  641. }
  642. }
  643. out = out + "]";
  644. if (rec_scores.size() > 0) {
  645. out = out + "rec text: " + text[n] +
  646. " rec score:" + std::to_string(rec_scores[n]) + " ";
  647. }
  648. if (cls_labels.size() > 0) {
  649. out = out + "cls label: " + std::to_string(cls_labels[n]) +
  650. " cls score: " + std::to_string(cls_scores[n]);
  651. }
  652. out = out + "\n";
  653. }
  654. if (table_boxes.size() > 0 && table_structure.size() > 0) {
  655. for (int n = 0; n < boxes.size(); n++) {
  656. out = out + "table boxes: [";
  657. for (int i = 0; i < 4; i++) {
  658. out = out + "[" + std::to_string(table_boxes[n][i * 2]) + "," +
  659. std::to_string(table_boxes[n][i * 2 + 1]) + "]";
  660. if (i != 3) {
  661. out = out + ",";
  662. }
  663. }
  664. out = out + "]\n";
  665. }
  666. out = out + "\ntable structure: \n";
  667. for (int m = 0; m < table_structure.size(); m++) {
  668. out += table_structure[m];
  669. }
  670. if (!table_html.empty()) {
  671. out = out + "\n" + "table html: \n" + table_html;
  672. }
  673. }
  674. std::vector<std::array<int, 8>> table_boxes;
  675. std::vector<std::string> table_structure;
  676. return out;
  677. } else if (boxes.size() == 0 && rec_scores.size() > 0 &&
  678. cls_scores.size() > 0) {
  679. std::string out;
  680. for (int i = 0; i < rec_scores.size(); i++) {
  681. out = out + "rec text: " + text[i] +
  682. " rec score:" + std::to_string(rec_scores[i]) + " ";
  683. out = out + "cls label: " + std::to_string(cls_labels[i]) +
  684. " cls score: " + std::to_string(cls_scores[i]);
  685. out = out + "\n";
  686. }
  687. return out;
  688. } else if (boxes.size() == 0 && rec_scores.size() == 0 &&
  689. cls_scores.size() > 0) {
  690. std::string out;
  691. for (int i = 0; i < cls_scores.size(); i++) {
  692. out = out + "cls label: " + std::to_string(cls_labels[i]) +
  693. " cls score: " + std::to_string(cls_scores[i]);
  694. out = out + "\n";
  695. }
  696. return out;
  697. } else if (boxes.size() == 0 && rec_scores.size() > 0 &&
  698. cls_scores.size() == 0) {
  699. std::string out;
  700. for (int i = 0; i < rec_scores.size(); i++) {
  701. out = out + "rec text: " + text[i] +
  702. " rec score:" + std::to_string(rec_scores[i]) + " ";
  703. out = out + "\n";
  704. }
  705. return out;
  706. } else if (boxes.size() == 0 && table_boxes.size() > 0 &&
  707. table_structure.size() > 0) {
  708. std::string out;
  709. for (int n = 0; n < table_boxes.size(); n++) {
  710. out = out + "table boxes: [";
  711. for (int i = 0; i < 4; i++) {
  712. out = out + "[" + std::to_string(table_boxes[n][i * 2]) + "," +
  713. std::to_string(table_boxes[n][i * 2 + 1]) + "]";
  714. if (i != 3) {
  715. out = out + ",";
  716. }
  717. }
  718. out = out + "]\n";
  719. }
  720. out = out + "\ntable structure: \n";
  721. for (int m = 0; m < table_structure.size(); m++) {
  722. out += table_structure[m];
  723. }
  724. if (!table_html.empty()) {
  725. out = out + "\n" + "table html: \n" + table_html;
  726. }
  727. return out;
  728. }
  729. no_result = no_result + "No Results!";
  730. return no_result;
  731. }
  732. std::string OCRCURVEResult::Str() {
  733. std::string no_result;
  734. if (boxes.size() > 0) {
  735. std::string out;
  736. for (int n = 0; n < boxes.size(); n++) {
  737. out = out + "det boxes: [";
  738. for (int i = 0; i < boxes[n].size() / 2; i++) {
  739. out = out + "[" + std::to_string(boxes[n][i * 2]) + "," +
  740. std::to_string(boxes[n][i * 2 + 1]) + "]";
  741. if (i != boxes[n].size() / 2 - 1) {
  742. out = out + ",";
  743. }
  744. }
  745. out = out + "]";
  746. if (rec_scores.size() > 0) {
  747. out = out + "rec text: " + text[n] +
  748. " rec score:" + std::to_string(rec_scores[n]) + " ";
  749. }
  750. if (cls_labels.size() > 0) {
  751. out = out + "cls label: " + std::to_string(cls_labels[n]) +
  752. " cls score: " + std::to_string(cls_scores[n]);
  753. }
  754. out = out + "\n";
  755. }
  756. if (table_boxes.size() > 0 && table_structure.size() > 0) {
  757. for (int n = 0; n < boxes.size(); n++) {
  758. out = out + "table boxes: [";
  759. for (int i = 0; i < 4; i++) {
  760. out = out + "[" + std::to_string(table_boxes[n][i * 2]) + "," +
  761. std::to_string(table_boxes[n][i * 2 + 1]) + "]";
  762. if (i != 3) {
  763. out = out + ",";
  764. }
  765. }
  766. out = out + "]\n";
  767. }
  768. out = out + "\ntable structure: \n";
  769. for (int m = 0; m < table_structure.size(); m++) {
  770. out += table_structure[m];
  771. }
  772. if (!table_html.empty()) {
  773. out = out + "\n" + "table html: \n" + table_html;
  774. }
  775. }
  776. std::vector<std::array<int, 8>> table_boxes;
  777. std::vector<std::string> table_structure;
  778. return out;
  779. } else if (boxes.size() == 0 && rec_scores.size() > 0 &&
  780. cls_scores.size() > 0) {
  781. std::string out;
  782. for (int i = 0; i < rec_scores.size(); i++) {
  783. out = out + "rec text: " + text[i] +
  784. " rec score:" + std::to_string(rec_scores[i]) + " ";
  785. out = out + "cls label: " + std::to_string(cls_labels[i]) +
  786. " cls score: " + std::to_string(cls_scores[i]);
  787. out = out + "\n";
  788. }
  789. return out;
  790. } else if (boxes.size() == 0 && rec_scores.size() == 0 &&
  791. cls_scores.size() > 0) {
  792. std::string out;
  793. for (int i = 0; i < cls_scores.size(); i++) {
  794. out = out + "cls label: " + std::to_string(cls_labels[i]) +
  795. " cls score: " + std::to_string(cls_scores[i]);
  796. out = out + "\n";
  797. }
  798. return out;
  799. } else if (boxes.size() == 0 && rec_scores.size() > 0 &&
  800. cls_scores.size() == 0) {
  801. std::string out;
  802. for (int i = 0; i < rec_scores.size(); i++) {
  803. out = out + "rec text: " + text[i] +
  804. " rec score:" + std::to_string(rec_scores[i]) + " ";
  805. out = out + "\n";
  806. }
  807. return out;
  808. } else if (boxes.size() == 0 && table_boxes.size() > 0 &&
  809. table_structure.size() > 0) {
  810. std::string out;
  811. for (int n = 0; n < table_boxes.size(); n++) {
  812. out = out + "table boxes: [";
  813. for (int i = 0; i < 4; i++) {
  814. out = out + "[" + std::to_string(table_boxes[n][i * 2]) + "," +
  815. std::to_string(table_boxes[n][i * 2 + 1]) + "]";
  816. if (i != 3) {
  817. out = out + ",";
  818. }
  819. }
  820. out = out + "]\n";
  821. }
  822. out = out + "\ntable structure: \n";
  823. for (int m = 0; m < table_structure.size(); m++) {
  824. out += table_structure[m];
  825. }
  826. if (!table_html.empty()) {
  827. out = out + "\n" + "table html: \n" + table_html;
  828. }
  829. return out;
  830. }
  831. no_result = no_result + "No Results!";
  832. return no_result;
  833. }
  834. void HeadPoseResult::Free() { std::vector<float>().swap(euler_angles); }
  835. void HeadPoseResult::Clear() { euler_angles.clear(); }
  836. void HeadPoseResult::Reserve(int size) { euler_angles.resize(size); }
  837. void HeadPoseResult::Resize(int size) { euler_angles.resize(size); }
  838. std::string HeadPoseResult::Str() {
  839. std::string out;
  840. out = "HeadPoseResult: [yaw, pitch, roll]\n";
  841. out = out + "yaw: " + std::to_string(euler_angles[0]) + "\n" +
  842. "pitch: " + std::to_string(euler_angles[1]) + "\n" +
  843. "roll: " + std::to_string(euler_angles[2]) + "\n";
  844. return out;
  845. }
  846. } // namespace vision
  847. } // namespace ultra_infer