doc_scan.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <stdio.h>
  2. #include "core/doc_scanner.hpp"
  3. #include "native_structs.h"
  4. //typedef struct {
  5. // int32_t x;
  6. // int32_t y;
  7. //} Coordinate;
  8. //
  9. //typedef struct {
  10. // Coordinate* coordsPtr;
  11. // uint32_t length;
  12. //} CoordinateArray;
  13. typedef struct {
  14. int32_t left;
  15. int32_t top;
  16. int32_t width;
  17. int32_t height;
  18. } ROI;
  19. typedef struct {
  20. uint8_t *buf;
  21. int32_t height;
  22. int32_t width;
  23. const char *format;
  24. int32_t bytesPerRow;
  25. ROI *roiPtr;
  26. } NativeCameraImage;
  27. extern "C" __attribute__((visibility("default"))) __attribute__((used))
  28. intptr_t initDocFinder(const char* doc_model_path, const char* corner_model_path, float precise = 0.85) {
  29. auto *docFinderPtr = new DocScanner(doc_model_path, corner_model_path, precise);
  30. return (intptr_t)docFinderPtr;
  31. }
  32. extern "C" __attribute__((visibility("default"))) __attribute__((used))
  33. int findDocWithCameraImg(CoordinateArray *coordList, NativeCameraImage *cameraImgPtr, intptr_t _docScannerPtr, const char *tmpDir) {
  34. //#ifdef DEBUG
  35. // int64_t start_time = cv::getTickCount();
  36. //#endif
  37. cv::Mat rgbaMat = cv::Mat(cameraImgPtr->height, cameraImgPtr->width, CV_8UC4, cameraImgPtr->buf, cameraImgPtr->bytesPerRow);
  38. ROI *roiPtr = cameraImgPtr->roiPtr;
  39. cv::Rect roi(roiPtr->left, roiPtr->top, roiPtr->width, roiPtr->height);
  40. rgbaMat = rgbaMat(roi);
  41. // std::string rootDir(tmpDir);
  42. // std::string dstPath = rootDir + "/test.jpg";
  43. // std::cout << "ret:" << cv::imwrite(dstPath.c_str(), rgbaMat) << std::endl;
  44. auto *docScannerPtr = (DocScanner *)_docScannerPtr;
  45. std::vector<cv::Point> points;
  46. docScannerPtr->scan(rgbaMat, points);
  47. bool succ = points.size() == coordList->length;
  48. if (succ) {
  49. float width = rgbaMat.size().width;
  50. float height = rgbaMat.size().height;
  51. for (size_t i = 0; i < coordList->length; i++) {
  52. coordList->coordsPtr[i].x = points[i].x / width;
  53. coordList->coordsPtr[i].y = points[i].y / height;
  54. }
  55. }
  56. //#ifdef DEBUG
  57. // float elapsedTime = (cv::getTickCount() - start_time) / cv::getTickFrequency();
  58. // float fps = 1 / elapsedTime;
  59. // std::cout << "findDocPoints fps:" << fps << std::endl;
  60. //#endif
  61. return succ;
  62. }
  63. extern "C" __attribute__((visibility("default"))) __attribute__((used))
  64. int findDocWithImgPath(const char *imgPath, const char *dstPath, intptr_t _docFinderPtr, CoordinateArray *coordList, ROI *roiPtr) {
  65. auto *docScannerPtr = (DocScanner *)_docFinderPtr;
  66. cv::Mat bgr = cv::imread(imgPath), rgb;
  67. cv::Rect roi(roiPtr->left, roiPtr->top, roiPtr->width, roiPtr->height);
  68. bgr = bgr(roi);
  69. cv::cvtColor(bgr, rgb, cv::COLOR_BGR2RGB);
  70. std::vector<cv::Point> points;
  71. docScannerPtr->scan(rgb, points);
  72. bool succ = points.size() == coordList->length;
  73. if (succ) {
  74. float width = rgb.size().width;
  75. float height = rgb.size().height;
  76. for (size_t i = 0; i < coordList->length; i++) {
  77. coordList->coordsPtr[i].x = points[i].x / width;
  78. coordList->coordsPtr[i].y = points[i].y / height;
  79. }
  80. cv::imwrite(dstPath, bgr);
  81. }
  82. return succ;
  83. }
  84. extern "C" __attribute__((visibility("default"))) __attribute__((used))
  85. void deleteDocFinder(intptr_t _docFinderPtr) {
  86. #ifdef DEBUG
  87. std::cout << "deleteDocFinder" << std::endl;
  88. #endif
  89. auto *docFinderPtr = (DocScanner *)_docFinderPtr;
  90. delete docFinderPtr;
  91. }