[Huawei Developers] Freemind R:
More information like this, you can visit HUAWEI Developer Forum​

Original link: https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201333475840980029&fid=0101187876626530001

You take a photo and you want to automatically identify which scene is by an intelligent recognize, or maybe you want to categorize you photo album by scenes, this is possible by a HIAI engine by a quick scene clasification.

On this post i will show you how you can use this feature on a Java-native app, since the front activities until the methos from HIAI you must call.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200820224453.76552001605660943560510585798686:50510903025438:2800:C2F69D61316D0458F0AA508C50652CFCC19C2F52788B7D4C77096D181F879426.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200820224453.76552001605660943560510585798686:50510903025438:2800:C2F69D61316D0458F0AA508C50652CFCC19C2F52788B7D4C77096D181F879426.png[/IMG]

HiAi Engine can recognize several scenes automatically bby using its methods, here some examples:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200820224752.14369337532415844181630585233087:50510903025438:2800:FF88C585311BD3B9FA30B34BB1675A13056DEEB465B1DA7512F8DC6A6C373DEA.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200820224752.14369337532415844181630585233087:50510903025438:2800:FF88C585311BD3B9FA30B34BB1675A13056DEEB465B1DA7512F8DC6A6C373DEA.png[/IMG]

if you want to get the full information you will get it on the API reference in the next link: https://developer.huawei.com/consumer/en/doc/development/hiai-References/20201204

What do we need :

Java JDK installation package

Android Studio 3.1 or later

Android SDK package

HiAI SDK package

1) Install DevEco IDE Plugins

Into your Android istudio go to File>Settings>Plugins and search for Dev ECO ID and Enable it \m/

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200717165111.88430803503882341598326481661571:50510903025438:2800:47D3889D5A8A127701881E0B1C0A5B07499C303D4F913470A11F7237D4CF494D.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200717165111.88430803503882341598326481661571:50510903025438:2800:47D3889D5A8A127701881E0B1C0A5B07499C303D4F913470A11F7237D4CF494D.png[/IMG]

Once is enable, dont forget to Restart you IDE (Android Studio).

2) Create your APP

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200717165245.55201428211713371729218839533969:50510903025438:2800:40825ED8820C94717EBEA8F0C3439E039CB0B616506B9CECD90D77D160AED592.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200717165245.55201428211713371729218839533969:50510903025438:2800:40825ED8820C94717EBEA8F0C3439E039CB0B616506B9CECD90D77D160AED592.png[/IMG]

3) Add dependency library

· Download the Huawei-hiai-vision.aar package in the Huawei AI Engine SDKs from the Huawei developer community.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200717165351.07196417127694371820275381450706:50510903025438:2800:A188520E4F4FB2C3DDBF34F3C8097E3A1A4C9679328413EF8D8172CA37940316.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200717165351.07196417127694371820275381450706:50510903025438:2800:A188520E4F4FB2C3DDBF34F3C8097E3A1A4C9679328413EF8D8172CA37940316.png[/IMG]

Copy the downloaded vision-release.aar package to the app/libs directory of the project.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200731162352.86874326243820616979061766408941:50510903025438:2800:5AE90900A2910460927E0BFC73CCFFD7EC34C2E3BB9E297807030990E69DD319.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200731162352.86874326243820616979061766408941:50510903025438:2800:5AE90900A2910460927E0BFC73CCFFD7EC34C2E3BB9E297807030990E69DD319.png[/IMG]

4) Lets code

I will create a project on android studio starting with an empty activity, for the visual side i will add an imageView to show the photo, a button to take the photo and a textview to show the different values that i can get for the scene, for the logic side i will use the HiAi methods to make the API call to get the image scene of the photos.

-Define the layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.SceneDetection">

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintlayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_bias="0.5"
app:layout_constraintBottom_toTopOf="@+id/linearlayout">

<com.huawei.hiaicodedemo.widget.TitleBar
android:id="@+id/titleBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleName="@string/as_scene"
app:titleTextColor="@color/black"
app:titleTextSize="18sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>

<ImageView
android:id="@+id/text_origin"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="30dp"
android:src="@color/color_6e"
app:layout_constraintDimensionRatio="h,4:3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintWidth_percent="0.8"
app:layout_constraintTop_toBottomOf="@+id/titleBar"
app:layout_constraintBottom_toTopOf="@+id/after"/>

<LinearLayout
android:id="@+id/after"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="15dp"
android:text="@string/as_scene"
android:textSize="18sp"/>

<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/color_6e"/>
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/color_6e"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_bias="0.5"
app:layout_constraintTop_toBottomOf="@+id/constraintlayout">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">

<androidx.core.widget.NestedScrollView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="h,4:3"
app:layout_constraintWidth_percent="0.8"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:background="@color/color_6e">

<EditText
android:id="@+id/text_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:textColor="@color/black" />
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/takePhoto"
android:layout_width="90dp"
android:layout_height="37dp"
android:layout_marginStart="48dp"
android:layout_marginTop="16dp"
android:background="@drawable/submit_button"
android:text="@string/foto"
android:textAllCaps="false"
android:textColor="@drawable/submit_button_text_color"
android:textSize="15sp"
app:layout_constraintHorizontal_bias="0.031"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/btn_album"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.37" />

</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200820225318.80451780713752995924869724394000:50510903025438:2800:FB0A1ABA3D6FB31272EA72AF6003EFC4CA30C0EE76985DAE0EE6DB483625F7F1.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200820225318.80451780713752995924869724394000:50510903025438:2800:FB0A1ABA3D6FB31272EA72AF6003EFC4CA30C0EE76985DAE0EE6DB483625F7F1.png[/IMG]

-Prepare the intent to get the picture from the camera and save it into a Bitmap

public void tomaFotoCategory(CategoryLabel view){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_FOTO);
}

Remember to add the permises to use the periferic in you manifest:

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

-Connect to HiAi Engine to use the scene detection by VisionBase Class

In the class where you are going to call the methos to use the HIAI, we have to init the HIAI capability, i have created a method to call this init as following:

private void initHiAI() {
VisionBase.init(this, new ConnectionCallback() {
@Override
public void onServiceConnect() {
}
@Override
public void onServiceDisconnect() {
}
});
}

-lets get the scene

Now, to get the scene category from your photo, you will have to make an instance from the class SceneDetectior and using its detect() method the value can be identified, in my case i create a method to do this job, every step is decribed in my code bellow:

private void setHiAi() {
/** Define class detector, the context of this project is the input parameter： */
SceneDetector sceneDetector = new SceneDetector(this);
/** define frame class, put the picture which need to be scene detected into the frame */
Frame frame = new Frame();
/** BitmapFactory.decodeFile input resource file path*/
frame.setBitmap(bitmap);
/** Call the detect method to get the result of the scene detection */
/** Note: This line of code must be placed in the worker thread instead of the main thread */
JSONObject jsonScene = sceneDetector.detect(frame, null);
/** Call convertResult() method to convert the json to java class and get the label detection (you can parse the json by yourself, too) */
Scene scene = sceneDetector.convertResult(jsonScene);
/** Get the identified scene type*/
int type = scene.getType();
this.resultado = type;
handler.sendEmptyMessage(TEXTDETECTOR_RESULT);
}

The result is the following (if you need the full project, let me know in the comments):

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200820231044.62955919862606484485594067438160:50510903025438:2800:6A3835675A996BC401084C15875EA27BD82D943C586040358F6567A08D6E4911.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200820231044.62955919862606484485594067438160:50510903025438:2800:6A3835675A996BC401084C15875EA27BD82D943C586040358F6567A08D6E4911.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200820231121.16735892013794283832327268289498:50510903025438:2800:A9D7D8FAFEDD796834E96A2B28E917653524583A6A836777014C8F067830965F.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200820231121.16735892013794283832327268289498:50510903025438:2800:A9D7D8FAFEDD796834E96A2B28E917653524583A6A836777014C8F067830965F.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200820231206.51061449053328466166958237831104:50510903025438:2800:5D69F89E97C89D9B47F08A4D0EE32654B152D8DB76540EB655F1A0DEEAE20442.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200820231206.51061449053328466166958237831104:50510903025438:2800:5D69F89E97C89D9B47F08A4D0EE32654B152D8DB76540EB655F1A0DEEAE20442.png[/IMG]

---------------------- < BONUS TRACK :D /> --------------------

1) Once you have your DevEco IDE plugin Eneable you will see an AMAZING KIT ASSITANT

go to DevECO>EMUI KIT>KIT ASSITANT

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200619180735.63441073705736675412508171806240:50510903025438:2800:296D0DE1DD58E0B1E1C0B60CAF8EBF56499D1AC71D223DF1ACB2141FABBDAF21.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200619180735.63441073705736675412508171806240:50510903025438:2800:296D0DE1DD58E0B1E1C0B60CAF8EBF56499D1AC71D223DF1ACB2141FABBDAF21.png[/IMG]

2) You will see a Menu like this:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200619180817.16285288131275103000237741521565:50510903025438:2800:15D9422577449ECBC1A306789370E4C15844CD2E04836D363F3123F9591FA7E4.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200619180817.16285288131275103000237741521565:50510903025438:2800:15D9422577449ECBC1A306789370E4C15844CD2E04836D363F3123F9591FA7E4.png[/IMG]

3) Select HiAi and search for scene detection:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200731183707.33866241713584470650687715781392:50510903025438:2800:71CAC927691151CFF0BDF1F65C0EBD6BA1C294FE7D1A28AEF3BF982967E91715.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200731183707.33866241713584470650687715781392:50510903025438:2800:71CAC927691151CFF0BDF1F65C0EBD6BA1C294FE7D1A28AEF3BF982967E91715.png[/IMG]

4) BOOM!!! Code Samples, Descriptions, Suggestions, DRAG & DROP CODE!!!!

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200820230356.31641605393105553627864621252970:50510903025438:2800:4848CFAC5E0C5CCC82D9076E0E0FFEAEA72E6E1C2CB6EB465B0EBCF199AE4CDF.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/187/997/2640052000002187997.20200820230356.31641605393105553627864621252970:50510903025438:2800:4848CFAC5E0C5CCC82D9076E0E0FFEAEA72E6E1C2CB6EB465B0EBCF199AE4CDF.png[/IMG]