native_vision.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <stdint.h>
  2. #include <opencv2/opencv.hpp>
  3. #include "core/doc_img_cleaner.hpp"
  4. #import "utils/log_util.h"
  5. extern "C" __attribute__((visibility("default"))) __attribute__((used))
  6. int32_t native_add(uint8_t *src, uchar **dst, int32_t width, int32_t height) {
  7. cv::Mat matImg = cv::imdecode(cv::Mat(1, width, CV_8UC1, src), cv::IMREAD_UNCHANGED);
  8. int channels = matImg.channels();
  9. cv::Mat gray;
  10. cv::cvtColor(matImg, gray, cv::COLOR_BGRA2GRAY);
  11. std::vector<uchar> buf;
  12. cv::imencode(".png", gray, buf);
  13. size_t size = buf.size();
  14. *dst = (uchar *)malloc(size * sizeof(uchar));
  15. for (size_t i = 0; i < size; i++) {
  16. (*dst)[i] = buf[i];
  17. }
  18. return size;
  19. }
  20. extern "C" __attribute__((visibility("default"))) __attribute__((used))
  21. intptr_t init(const char* modelPath) {
  22. auto *docImgCleanerPtr = new DocImgCleaner(modelPath);
  23. return (intptr_t)docImgCleanerPtr;
  24. }
  25. extern "C" __attribute__((visibility("default"))) __attribute__((used))
  26. void release(intptr_t docImgCleanerAdr) {
  27. auto *docImgCleanerPtr = (DocImgCleaner *)docImgCleanerAdr;
  28. delete docImgCleanerPtr;
  29. }
  30. extern "C" __attribute__((visibility("default"))) __attribute__((used))
  31. bool process(const char* imgPath, const char* dstImgPath, intptr_t docImgCleanerAdr) {
  32. #ifdef DEBUG
  33. auto start_time = cv::getTickCount();
  34. #endif
  35. cv::Mat bgrImg = cv::imread(imgPath);
  36. int maxWidth = 2560;
  37. if (bgrImg.size().width > maxWidth) {
  38. float aspectRatio = bgrImg.size().width / (float)bgrImg.size().height;
  39. int width = maxWidth;
  40. int height = width / aspectRatio;
  41. cv::resize(bgrImg, bgrImg, cv::Size(width, height));
  42. }
  43. auto *docimgCleaner = (DocImgCleaner *)docImgCleanerAdr;
  44. cv::Mat processedImg = docimgCleaner->process(bgrImg);
  45. // auto _imgPath = std::string(imgPath);
  46. // auto processedImgPath = _imgPath.substr(0, _imgPath.find_last_of("/")) + "/processedImg.jpg";
  47. cv::cvtColor(processedImg, processedImg, cv::COLOR_RGB2BGR);
  48. auto succ = cv::imwrite(dstImgPath, processedImg);
  49. #ifdef DEBUG
  50. LOGW("doc_image_cleaner", "total time:%f", (cv::getTickCount() - start_time) / cv::getTickFrequency());
  51. #endif
  52. return succ;
  53. }