1. 口罩识别 android SDK 简介
Android SDK以aar包形式提供,封装了口罩识别等相关API,移动应用只需要修改少量代码,即可快速实现图片人物的口罩识别功能。
Android平台上的应用,要使用Android SDK,请先进行license 注册。
主要提供的功能接口有:模型初始化,推理, 模型释放,license初始化等,更多详细信息请查看API文档。
2. 下载Android SDK
请到SDK下载页面下载Android SDK和示例代码。
3. 创建工程及引用SDK源码文件
以下以Android Studio为开发的IDE进行范例说明:
(1)创建一个工程,点击File->New→New Module→Import .jar/.aar Package导入facemask.aar,如下图所示。
默认Subproject name为facemask。
(2) settings.gradle文件include中添加 ':facemask'
(3) 在引用facemask.aar的module的build.gradle文件dependencies里添加 api project(':facemask'),同时添加implementation
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'com.google.android.material:material:1.0.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' implementation 'com.didichuxing.omega:omegasdk:2.2.99.1' implementation 'com.squareup.okhttp3:okhttp:3.10.0' implementation 'com.google.code.gson:gson:2.8.6' api project(':facemask') }
4. Android SDK使用
(1)配置AndroidManifest
在应用的AndroidManifest.xml增加配置的<application>节点下增加以下配置
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application android:name=".IFXApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:networkSecurityConfig="@xml/network_security_config" android:theme="@style/AppTheme"> <activity android:name="com.didi.ifx.face.StartActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.didi.ifx.face.MainActivity"> <intent-filter> <data android:scheme="omega8ed3059906" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> </intent-filter> </activity> </application>
(2)获取argb格式图片数据
private int width; private int height; private int[] argb; private final String TAG = "FaceMask_Main"; // read test image BitmapFactory.Options options = new BitmapFactory.Options(); try { options.inJustDecodeBounds = true; BitmapFactory.decodeStream(this.getAssets().open("mask_test.jpg"), null, options); width = options.outWidth; height = options.outHeight; Log.d(TAG, "img size: " + height + ", "+ width); argb = new int[width * height]; options.inJustDecodeBounds = false; Bitmap img = BitmapFactory.decodeStream(this.getAssets().open ("mask_test.jpg"), null, options); img.getPixels(argb, 0, width, 0, 0, width, height); } catch (Throwable e) { Log.e(TAG, e.getMessage()); }
(3)创建实例并初始化
SDK主要实现类包括FaceMask类和MultiLicenseManager类,FaceMask用于处理业务相关逻辑,MultiLicenseManager用于license认证。
创建Application实现license初始化。
import com.didi.ifx.license.MultiLicenseManager; public class IFXApplication extends Application { public void onCreate() { super.onCreate(); MultiLicenseManager.APPLicenseConfig(this); } }
创建FaceMask实例并完成初始化。
import com.didi.ifx.facemask.FaceMask; Context context = MainActivity.this.getApplicationContext(); final FaceMask faceMask = new FaceMask(context); if (!faceMask.init(context)) { Log.d(TAG, "Init fail"); return; }
(4)调用推理接口
final Handler handler = new Handler(); handler.post(new Runnable() { @Override public void run() { float[] result = faceMask.inference(argb, height, width); long det_t = faceMask.getDetTime(); long cls_t = faceMask.getClsTime(); Log.d(TAG, "Det inference time(ms):" + det_t); Log.d(TAG, "Cls inference time(ms):" + cls_t); processModelOutput(result); handler.postDelayed(this, delayTime); } });
(5)推理结果再利用
public void processModelOutput(float[] model_output) { /* modelput = [x0, y0, x1, y1, ... , ] 其中,xi/yi 分别表示 第二个模型输出的两个值 */ String display = ""; for (int i = 0; i<model_output.length; ++i) { display += model_output[i] + "\n"; } textView.setText(display); }