一文搞懂華為ML Kit拍照購,超簡單集成


簡介

華為HMS ML Kit提供拍照購服務,用戶通過拍攝商品圖片,在預先建立的商品圖片庫中在線檢索同款或相似商品,返回相似商品ID和相關信息。

在這里插入圖片描述

應用場景

  1. 使用攝像頭設備從開發的購物APP當中捕捉產品圖像。

  2. 在循環視圖當中顯示返還的產品列表。

開發准備

  1. 推薦使用Java JDK 1.8或者更高版本。

  2. 推薦使用Android Studio。

  3. 搭載HMS Core4.0.0.300 或者更高版本的華為安卓設備。

  4. 在開發APP之前,你需要注冊華為開發者,賬號注冊。

  5. 集成AppGallery Connect SDK, 請訪問AppGallery Connect 服務入門指南。

開發

  1. 在Manage APIs中啟用ML Kit, 可參考開通服務。

  2. 在app-level bulid.gradle 中集成以下依賴項。

// Import the product visual search SDK.



 implementation 'com.huawei.hms:ml-computer-vision-cloud:2.0.1.300'
  1. 在app.gradle文件的頂部添加agc插件。
apply plugin: 'com.huawei.agconnect'
  1. 在清單中添加以下權限。
  • 攝像頭權限android.permission.CAMERA: 從攝像頭中獲取實時圖像或視頻。

  • 網絡連接權限 android.permission.INTERNET:訪問互聯網上的雲服務。

  • 存儲寫權限android.permission.WRITE_EXTERNAL_STORAGE: 升級算法版本。

  • 存儲讀權限android.permission.READ_EXTERNAL_STORAGE: 讀取存儲在設備上的照片。

  1. 實時請求相機權限
private void requestCameraPermission() {
     final String[] permissions = new String[] {Manifest.permission.CAMERA};
 
     if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
         ActivityCompat.requestPermissions(this, permissions, this.CAMERA_PERMISSION_CODE);
         return;
     }
 }
  1. 在Application class 中添加以下代碼
public class MyApplication extends Application {
 
@Override
     public void onCreate() {
         super.onCreate();
         MLApplication.getInstance().setApiKey("API KEY");
     }
 }

可以從AGC或者集成的agconnect-services.json獲得API key。

  1. 為拍照購物創建一個分析器。
private void initializeProductVisionSearch() {
         MLRemoteProductVisionSearchAnalyzerSetting settings = new MLRemoteProductVisionSearchAnalyzerSetting.Factory()
 // Set the maximum number of products that can be returned.
                 .setLargestNumOfReturns(16)
                 // Set the product set ID. (Contact mlkit@huawei.com to obtain the configuration guide.)
 //                .setProductSetId(productSetId)
                 // Set the region.
                 .setRegion(MLRemoteProductVisionSearchAnalyzerSetting.REGION_DR_CHINA)
                 .create();
          analyzer
                 = MLAnalyzerFactory.getInstance().getRemoteProductVisionSearchAnalyzer(settings);
     }
  1. 從相機中捕捉圖像。
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 
 startActivityForResult(intent, REQ_CAMERA_CODE);
  1. 一旦圖像被捕捉,將執行onActivityResult() method。
@Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
         Log.d(TAG, "onActivityResult");
         if(requestCode == 101) {
             if (resultCode == RESULT_OK) {
                 Bitmap bitmap = (Bitmap) data.getExtras().get("data");
                 if (bitmap != null) {
 // Create an MLFrame object using the bitmap, which is the image data in bitmap format.
                     MLFrame mlFrame = new MLFrame.Creator().setBitmap(bitmap).create();
                     mlImageDetection(mlFrame);
                 }
  }
 
 }
     }  
     private void mlImageDetection(MLFrame mlFrame) {
     Task> task = analyzer.asyncAnalyseFrame(mlFrame);
     task.addOnSuccessListener(new OnSuccessListener>() {
         public void onSuccess(List products) {
             // Processing logic for detection success.
      displaySuccess(products);
             
   }})
             .addOnFailureListener(new OnFailureListener() {
                 public void onFailure(Exception e) {
                     // Processing logic for detection failure.
                     // Recognition failure.
                     try {
                         MLException mlException = (MLException)e;
                         // Obtain the result code. You can process the result code and customize respective messages displayed to users.
                         int errorCode = mlException.getErrCode();
                         // Obtain the error information. You can quickly locate the fault based on the result code.
                         String errorMessage = mlException.getMessage();
                     } catch (Exception error) {
                         // Handle the conversion error.
                     }
                 }
             });
              } 
 
   private void displaySuccess(List productVisionSearchList) {
         List productImageList = new ArrayList();
         String prodcutType = "";
         for (MLProductVisionSearch productVisionSearch : productVisionSearchList) {
             Log.d(TAG, "type: " + productVisionSearch.getType() );
             prodcutType =  productVisionSearch.getType();
             for (MLVisionSearchProduct product : productVisionSearch.getProductList()) {
                 productImageList.addAll(product.getImageList());
                 Log.d(TAG, "custom content: " + product.getCustomContent() );
             }
         }
         StringBuffer buffer = new StringBuffer();
         for (MLVisionSearchProductImage productImage : productImageList) {
             String str = "ProductID: " + productImage.getProductId() + "
ImageID: " + productImage.getImageId() + "
Possibility: " + productImage.getPossibility();
             buffer.append(str);
             buffer.append("
");
         }
         Log.d(TAG , "display success: " + buffer.toString());
         FragmentTransaction transaction = getFragmentManager().beginTransaction();
         transaction.replace(R.id.main_fragment_container, new SearchResultFragment(productImageList, prodcutType ));
         transaction.commit();
     }

onSuccess()回調將給我們MLProductVisionSearch.getType()對象列表,可用於獲取每個產品的ID和圖像URL。我們還可以使用productVisionSearch.getType()獲取產品類型,gatType()返回可映射的編號。

在撰寫本文時,產品類型是:

  1. 我們可以使用以下代碼實現產品類型映射。
private String getProductType(String type) {
    switch(type) {
        case "0":
            return "Others";
        case "1":
            return "Clothing";
        case "2":
            return "Shoes";
        case "3":
            return "Bags";
        case "4":
            return "Digital & Home appliances";
        case "5":
            return "Household Products";
        case "6":
            return "Toys";
        case "7":
            return "Cosmetics";
        case "8":
            return "Accessories";
        case "9":
            return "Food";
    }
    return  "Others";
}
  1. 從MLVisionSearchProductImage獲取產品ID和圖像URL。
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final MLVisionSearchProductImage mlProductVisionSearch = productVisionSearchList.get(position);
 
    holder.tvTitle.setText(mlProductVisionSearch.getProductId());
    Glide.with(context)
            .load(mlProductVisionSearch.getImageId())
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(holder.imageView);
 
}

Demo


欲了解更多詳情,請參閱:

華為開發者聯盟官網

獲取開發指導文檔

參與開發者討論請到Reddit社區

下載demo和示例代碼請到Github

解決集成問題請到Stack Overflow


原文鏈接:
https://developer.huawei.com/consumer/cn/forum/topic/0201434135672410084?fid=18

作者:胡椒


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM