Android 快速集成文檔校訂能力 超簡單

前言

在以前的《超簡單集成華爲HMS ML Kit文本識別SDK,一鍵實現帳單號自動錄入》文章中,咱們給你們介紹了華爲HMS ML Kit文本識別技術如何經過拍照自動識別照片中的文本信息。那麼有的小夥伴可能會問,若是拍照時不是正對着文本拍攝,拍出來的照片是歪斜的,那麼還能準確識別文本嗎?固然能夠啦。HMS ML Kit文檔校訂技術能夠自動識別文檔位置,校訂拍攝角度,而且支持用戶自定義邊界點位置,即便在傾斜角度也可以拍攝出文檔的正面圖像。java

在這裏插入圖片描述

應用場景

文檔校訂技術在生活中有不少的應用場景。好比說在拍攝紙質文檔時,相機處於傾斜的角度,致使閱讀文檔很是不方便。使用文檔校訂技術能夠把文檔調整到正對的視角,這樣閱讀起來就順利多了。android

在這裏插入圖片描述

再好比在記錄卡證信息時,使用文檔校訂技術,不須要調整到正對卡證的視角,也能夠拍攝出卡證的正面照片。git

在這裏插入圖片描述

另外,在行程中由於身處於傾斜位置,道路旁的路牌難以準確識別,這時能夠經過文檔校訂技術拍攝到路牌正面圖片。github

在這裏插入圖片描述

怎麼樣,是否是很方便呢?那咱們接下來詳細給你們介紹安卓如何快速集成文檔校訂技術。app

開發實戰

詳細的準備步驟能夠參考華爲開發者聯盟: https://developer.huawei.com/consumer/cn/doc/development/HMS-Guides/ml-process-4 這裏列舉關鍵的開發步驟。機器學習

1.1 項目級gradle裏配置Maven倉地址

buildscript {
        repositories {
             ...
            maven {url 'https://developer.huawei.com/repo/'}
        }
    }
    dependencies {
             ...
            classpath 'com.huawei.agconnect:agcp:1.3.1.300'
    }
    allprojects {
            repositories {
                    ...
                    maven {url 'https://developer.huawei.com/repo/'}
            }
    }

1.2 應用級gradle裏配置SDK依賴

dependencies{
             // 引入基礎SDK
            implementation 'com.huawei.hms:ml-computer-vision-documentskew:2.0.2.300'
            // 引入文檔檢測/校訂模型包
           implementation 'com.huawei.hms:ml-computer-vision-documentskew-model:2.0.2.300'
    }

1.3 在文件頭添加配置

apply plugin: 'com.huawei.agconnect'
    apply plugin: 'com.android.application'

1.4 添加以下語句到AndroidManifest.xml文件中,自動更新機器學習模型到設備

<meta-data
    android:name="com.huawei.hms.ml.DEPENDENCY" 
    android:value= "dsc"/>

1.5 申請攝像機權限和讀本地圖片權限

<uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

2. 代碼開發

2.1 建立文本框檢測/校訂分析器

MLDocumentSkewCorrectionAnalyzerSetting setting = new MLDocumentSkewCorrectionAnalyzerSetting.Factory().create();
MLDocumentSkewCorrectionAnalyzer analyzer =                 MLDocumentSkewCorrectionAnalyzerFactory.getInstance().getDocumentSkewCorrectionAnalyzer(setting);

2.2 經過android.graphics.Bitmap建立MLFrame對象用於分析器檢測圖片,支持的圖片格式包括:jpg/jpeg/png,建議圖片尺寸不小於320320像素,不大於19201920像素。

MLFrame frame = MLFrame.fromBitmap(bitmap);

2.3 調用asyncDocumentSkewDetect異步方法或analyseFrame同步方法進行文本框的檢測。當返回碼是MLDocumentSkewCorrectionConstant.SUCCESS時,將會返回文本框的四個頂點的座標值,該座標值是相對於傳入圖像的座標,若與設備座標不一致,需調用者進行轉換;不然,返回的數據沒有意義。

// asyncDocumentSkewDetect異步調用。
        Task<MLDocumentSkewDetectResult> detectTask = analyzer.asyncDocumentSkewDetect(mlFrame);
        detectTask.addOnSuccessListener(new OnSuccessListener<MLDocumentSkewDetectResult>() {
                @Override
                public void onSuccess(MLDocumentSkewDetectResult detectResult) {
                        // 檢測成功。
                }
        }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(Exception e) {
                      // 檢測失敗。
                 }
         })
   
          // analyseFrame同步調用。
          SparseArray<MLDocumentSkewDetectResult> detect = analyzer.analyseFrame(mlFrame);
          if (detect != null && detect.get(0).getResultCode() == MLDocumentSkewCorrectionConstant.SUCCESS) {
                  // 檢測成功。
          } else {
                  // 檢測失敗。
          }

2.4 檢測成功後,分別獲取文本框四個頂點的座標數據,而後以左上角爲起點,按順時針方向,分別把左上角、右上角、右下角、左下角加入到列表(List<Point>)中,最後構建MLDocumentSkewCorrectionCoordinateInput對象。

2.4.1 若是使用analyseFrame同步調用,先獲取到檢測結果,以下所示(使用asyncDocumentSkewDetect異步調用可忽略此步驟直接進行步驟2.4.2):異步

MLDocumentSkewDetectResult detectResult = detect.get(0);

2.4.2 獲取文本框四個頂點的座標數據並構建MLDocumentSkewCorrectionCoordinateInput對象:async

Point leftTop = detectResult.getLeftTopPosition();
    Point rightTop = detectResult.getRightTopPosition();
    Point leftBottom = detectResult.getLeftBottomPosition();
    Point rightBottom = detectResult.getRightBottomPosition();
    List<Point> coordinates = new ArrayList<>();
    coordinates.add(leftTop);
    coordinates.add(rightTop);
    coordinates.add(rightBottom);
    coordinates.add(leftBottom);
    MLDocumentSkewCorrectionCoordinateInput coordinateData = new MLDocumentSkewCorrectionCoordinateInput(coordinates);

2.5 調用asyncDocumentSkewCorrect異步方法或syncDocumentSkewCorrect同步方法進行文本框的校訂。

// asyncDocumentSkewCorrect異步調用。
Task<MLDocumentSkewCorrectionResult> correctionTask = analyzer.asyncDocumentSkewCorrect(mlFrame, coordinateData);
 correctionTask.addOnSuccessListener(new OnSuccessListener<MLDocumentSkewCorrectionResult>() {
        @Override
        public void onSuccess(MLDocumentSkewCorrectionResult refineResult) {
                // 檢測成功。
         }
 }).addOnFailureListener(new OnFailureListener() {
         @Override
         public void onFailure(Exception e) {
                // 檢測失敗。
          }
  });
  
 // syncDocumentSkewCorrect同步調用。
SparseArray<MLDocumentSkewCorrectionResult> correct= analyzer.syncDocumentSkewCorrect(mlFrame, coordinateData);
 if (correct != null && correct.get(0).getResultCode() == MLDocumentSkewCorrectionConstant.SUCCESS) {
           // 校訂成功。
} else {
          // 校訂失敗。
}

2.6 檢測完成,中止分析器,釋放檢測資源。

if (analyzer != null) {
        analyzer.stop();
}

Demo效果

下面這個demo展現了在傾斜角度掃描文檔,文檔校訂技術能夠把文檔調整到正對視角。效果是否是很棒?maven

在這裏插入圖片描述

文檔校訂技術還能夠輔助文檔識別技術,將傾斜的文檔調整到正面視角,快速實現從紙質文件到電子文件的轉化,大幅度提高信息的錄入效率。ide

Github源碼

https://github.com/HMS-Core/hms-ml-demo/blob/master/MLKit-Sample/module-text/src/main/java/com/mlkit/sample/activity/DocumentSkewCorretionActivity.java

更詳細的開發指南參考華爲開發者聯盟官網

https://developer.huawei.com/consumer/cn/hms/huawei-mlkit

欲瞭解更多詳情,請參閱: 華爲開發者聯盟官網:https://developer.huawei.com/consumer/cn/hms 獲取開發指導文檔:https://developer.huawei.com/consumer/cn/doc/development 參與開發者討論請到Reddit社區:https://www.reddit.com/r/HMSCore/ 下載demo和示例代碼請到Github:https://github.com/HMS-Core 解決集成問題請到Stack Overflow:https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest


文章來源:https://developer.huawei.com/consumer/cn/forum/topicview?tid=0202344452930050418&fid=18

做者:留下落葉
相關文章
相關標籤/搜索