Skip to content

Commit c53a6ed

Browse files
authored
Merge pull request #66 from mlbo/master
Check whether the model exists before copying and update InsightFace api
2 parents d8dd943 + 84c1f0a commit c53a6ed

File tree

4 files changed

+97
-36
lines changed

4 files changed

+97
-36
lines changed

Android/docs/Android_api.md

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,40 @@ We merge all the functions into one interface
4646
TengineKitSdk.getInstance().releaseFaceDetect()
4747
```
4848

49+
## ***3. InsightFace***
50+
### init
51+
``` kotlin
52+
TengineKitSdk.getInstance().initInsightFace()
53+
```
4954

55+
### predict
56+
We merge all the functions into one interface
57+
58+
``` kotlin
59+
val byte = ImageUtils.bitmap2RGB(bitmap)
60+
val config = InsightFaceConfig().apply {
61+
scrfd = true
62+
recognition = true
63+
registered = false
64+
video = false
65+
}
66+
val imageConfig = ImageConfig().apply {
67+
data = byte
68+
degree = 0
69+
mirror = false
70+
height = it.height
71+
width = it.width
72+
format = ImageConfig.FaceImageFormat.RGB
73+
}
74+
val faces = TengineKitSdk.getInstance().detectInsightFace(imageConfig, config)
75+
```
76+
77+
### release
78+
```kotlin
79+
TengineKitSdk.getInstance().releaseInsightFace()
80+
```
5081

51-
## ***3. SegBody***
82+
## ***4. SegBody***
5283

5384
### init
5485

@@ -78,7 +109,7 @@ directly return a mask, the mask is a android bitmap, the mask's width is 398, t
78109
TengineKitSdk.getInstance().releaseSegBody()
79110
```
80111

81-
## ***4. BodyDetect***
112+
## ***5. BodyDetect***
82113

83114
### init
84115
``` kotlin
@@ -89,17 +120,17 @@ directly return a mask, the mask is a android bitmap, the mask's width is 398, t
89120
We merge all the functions into one interface
90121

91122
``` kotlin
92-
val data = ImageUtils.bitmap2RGB(bitmap)
93-
val imageConfig = ImageConfig().apply {
94-
this.data = data
95-
this.format = ImageConfig.FaceImageFormat.RGB
96-
this.height = it.height
97-
this.width = it.width
98-
this.mirror = false
99-
this.degree = 0
100-
}
101-
val bodyConfig = BodyConfig()
102-
val bodyS = TengineKitSdk.getInstance().bodyDetect(imageConfig, bodyConfig)
123+
val data = ImageUtils.bitmap2RGB(bitmap)
124+
val imageConfig = ImageConfig().apply {
125+
this.data = data
126+
this.format = ImageConfig.FaceImageFormat.RGB
127+
this.height = it.height
128+
this.width = it.width
129+
this.mirror = false
130+
this.degree = 0
131+
}
132+
val bodyConfig = BodyConfig()
133+
val bodyS = TengineKitSdk.getInstance().bodyDetect(imageConfig, bodyConfig)
103134
```
104135

105136
### release
@@ -108,27 +139,36 @@ We merge all the functions into one interface
108139
```
109140

110141

111-
## ***5.Release Context***
142+
## ***6.Release Context***
112143
```
113144
TengineKitSdk.getInstance().release()
114145
```
115146

116147
## DataStruct
148+
### Config
149+
#### ImageConfig
150+
* data(byte[]): set image data byte array of image raw data
151+
* degree(int): set rotate degree need if in camera mode, need to rotate the right angle to detect the face
152+
* width(int): set bitmap width or preview width
153+
* height(int): set bitmap height or preview height
154+
* format(enum ImageConfig.FaceImageFormat): set image format, support RGB format and NV21 format current now
117155

118156
#### FaceConfig
157+
* detect(boolean): set true if need detect face rect
119158
* landmark2d(boolean): set true if need landmark2d except detect face rect
120159
* video(boolean): set true if in camera mode
121160

161+
#### InsightFaceConfig
162+
* scrfd(boolean): set true if need do scrfd
163+
* recognition(boolean): set true if need do arcface
164+
* registered(boolean): set true if already registered face
165+
122166
#### SegConfig
123167
* default portrait segmentation config current
168+
#### BodyConfig
169+
* landmark(boolean): set true if need body landmark
124170

125-
#### ImageConfig
126-
* data(byte[]): set image data byte array of image raw data
127-
* degree(int): set rotate degree need if in camera mode, need to rotate the right angle to detect the face
128-
* width(int): set bitmap width or preview width
129-
* height(int): set bitmap height or preview height
130-
* format(enum ImageConfig.FaceImageFormat): set image format, support RGB format and NV21 format current now
131-
171+
### Info
132172
#### Face
133173

134174
all detect values ​​are normalized from 0 to 1
@@ -146,6 +186,15 @@ all detect values ​​are normalized from 0 to 1
146186
* mouthClose: Mouth closure confidence 0~1
147187
* mouthBigOpen: Open mouth Big confidence 0~1
148188

189+
#### InsightFace
190+
191+
* x1: detect body rect left
192+
* y1: detect body rect top
193+
* x2: detect body rect right
194+
* y2: detect body rect bottom
195+
* landmark: if not null landmark contain 5 face key points
196+
* confidence: face detect confidence
197+
* feature: if not null feature contain 512 face feature points
149198
#### Body
150199

151200
* x1: detect body rect left
@@ -157,3 +206,5 @@ all detect values ​​are normalized from 0 to 1
157206

158207

159208

209+
210+

Android/source/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
88
<uses-permission android:name="android.permission.INTERNET" />
99

10+
1011
<application
1112
android:name=".MyApplication"
1213
android:allowBackup="true"
1314
android:icon="@mipmap/ic_launcher"
1415
android:label="@string/app_name"
16+
android:requestLegacyExternalStorage="true"
1517
android:roundIcon="@mipmap/ic_launcher_round"
1618
android:supportsRtl="true"
1719
android:theme="@style/Theme.Tenginedemo">

Android/source/app/src/main/java/com/tenginekit/tenginedemo/insightface/InsightFaceBitmapActivity.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import android.content.Intent
2424
import android.app.Activity
2525
import androidx.activity.result.contract.ActivityResultContracts
2626
import com.tenginekit.engine.core.SdkConfig
27-
import com.tenginekit.engine.insightface.InsightFace
2827
import com.tenginekit.engine.insightface.InsightFaceConfig
2928
import com.tenginekit.tenginedemo.Constant
3029
import com.tenginekit.tenginedemo.R
@@ -181,9 +180,10 @@ class InsightFaceBitmapActivity : AppCompatActivity(), View.OnClickListener {
181180
)
182181
faceLandmarks.add(i, faceLandmarkList)
183182
faceRects[i] = rect
184-
faceFeature = face.Feature
183+
faceFeature = face.feature
185184
val distance = calculSimilar(regFeature!!, faceFeature)
186-
Log.e("distance", distance.toString())
185+
Log.i(Constant.LOG_TAG, "Distance:" + distance.toString())
186+
Log.i(Constant.LOG_TAG, "Confidence:" + face.confidence.toString())
187187
}
188188
overlayView?.onProcessResults(faceRects)
189189
overlayView?.onProcessResults(faceLandmarks)
@@ -254,7 +254,7 @@ class InsightFaceBitmapActivity : AppCompatActivity(), View.OnClickListener {
254254
)
255255
faceLandmarks.add(i, faceLandmarkList)
256256
faceRects[i] = rect
257-
regFeature = face.Feature
257+
regFeature = face.feature
258258
}
259259
overlayView?.onProcessResults(faceRects)
260260
overlayView?.onProcessResults(faceLandmarks)

Android/source/app/src/main/java/com/tenginekit/tenginedemo/utils/copyAssetFolder.kt

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package com.tenginekit.tenginedemo.utils
22

33
import android.content.res.AssetManager
4+
import com.luck.picture.lib.tools.PictureFileUtils.isFileExists
45
import java.io.File
56
import java.io.File.separator
67
import java.io.FileOutputStream
78
import java.io.IOException
89
import java.io.OutputStream
910

10-
fun AssetManager.copyAssetFolder(srcName: String, dstName: String, callback: ModelCopyCallback?): Boolean {
11+
fun AssetManager.copyAssetFolder(
12+
srcName: String,
13+
dstName: String,
14+
callback: ModelCopyCallback?
15+
): Boolean {
1116
return try {
1217
var result = true
1318
val fileList = this.list(srcName) ?: return false
@@ -35,16 +40,19 @@ fun AssetManager.copyAssetFolder(srcName: String, dstName: String, callback: Mod
3540

3641
fun AssetManager.copyAssetFile(srcName: String, dstName: String): Boolean {
3742
return try {
38-
val inStream = this.open(srcName)
39-
val outFile = File(dstName)
40-
val out: OutputStream = FileOutputStream(outFile)
41-
val buffer = ByteArray(1024)
42-
var read: Int
43-
while (inStream.read(buffer).also { read = it } != -1) {
44-
out.write(buffer, 0, read)
43+
if (!isFileExists(dstName)) {
44+
val inStream = this.open(srcName)
45+
val outFile = File(dstName)
46+
val out: OutputStream = FileOutputStream(outFile)
47+
val buffer = ByteArray(1024)
48+
var read: Int
49+
while (inStream.read(buffer).also { read = it } != -1) {
50+
out.write(buffer, 0, read)
51+
}
52+
53+
inStream.close()
54+
out.close()
4555
}
46-
inStream.close()
47-
out.close()
4856
true
4957
} catch (e: IOException) {
5058
e.printStackTrace()
@@ -53,7 +61,7 @@ fun AssetManager.copyAssetFile(srcName: String, dstName: String): Boolean {
5361
}
5462

5563

56-
interface ModelCopyCallback{
64+
interface ModelCopyCallback {
5765
fun copyFinish()
5866
fun copyFail()
5967
}

0 commit comments

Comments
 (0)