-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathMainActivity.kt
174 lines (149 loc) · 7.5 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.faceplugin.faceliveness
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.ocp.facesdk.FaceBox
import com.ocp.facesdk.FaceDetectionParam
import com.ocp.facesdk.FaceSDK
import kotlin.random.Random
class MainActivity : AppCompatActivity() {
companion object {
private val SELECT_PHOTO_REQUEST_CODE = 1
private val SELECT_ATTRIBUTE_REQUEST_CODE = 2
}
private lateinit var dbManager: DBManager
private lateinit var textWarning: TextView
private lateinit var personAdapter: PersonAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// textWarning = findViewById<TextView>(R.id.textWarning)
var ret = FaceSDK.setActivation(
"f0Y15I2DXE4BP9g3pkGRzcM7HQatMXrQ3B85fFNSb80QJ7txgdrdWpX8EbRjafdCoPBmv3TqeHRc\n" +
"lTIrQBGF7sQUpe2XdEcrkjZgvQBhD4zFlAqMrBvaRaSiGE+LLGa/5XjrOlFpk+1UAlZcPiq+YrBG\n" +
"eS5zDY7FkhPze1q9nsg19yJ7yDFFkCc04ChKcXyNAk8UipYc4CWWx8xnVticyDQI74bdm273kQJw\n" +
"IhL/lwCspEDEVScFycCSIPPTDf3JX3jmZIjcKk8cQjWcPT00/Bly/eYM3KyDk19QXgQ9w86yCL3S\n" +
"kpS91aW3SY7PqVZR2fK56bqxP4dsagSIUJHO6Q=="
)
if (ret == FaceSDK.SDK_SUCCESS) {
ret = FaceSDK.init(assets)
}
if (ret != FaceSDK.SDK_SUCCESS) {
textWarning.setVisibility(View.VISIBLE)
if (ret == FaceSDK.SDK_LICENSE_KEY_ERROR) {
textWarning.setText("Invalid license!")
} else if (ret == FaceSDK.SDK_LICENSE_APPID_ERROR) {
textWarning.setText("Invalid error!")
} else if (ret == FaceSDK.SDK_LICENSE_EXPIRED) {
textWarning.setText("License expired!")
} else if (ret == FaceSDK.SDK_NO_ACTIVATED) {
textWarning.setText("No activated!")
} else if (ret == FaceSDK.SDK_INIT_ERROR) {
textWarning.setText("Init error!")
}
}
dbManager = DBManager(this)
dbManager.loadPerson()
personAdapter = PersonAdapter(this, DBManager.personList)
// val listView: ListView = findViewById<View>(R.id.listPerson) as ListView
// listView.setAdapter(personAdapter)
//
// findViewById<Button>(R.id.buttonEnroll).setOnClickListener {
// val intent = Intent()
// intent.setType("image/*")
// intent.setAction(Intent.ACTION_PICK)
// startActivityForResult(Intent.createChooser(intent, getString(R.string.select_picture)), SELECT_PHOTO_REQUEST_CODE)
// }
//
findViewById<Button>(R.id.buttonIdentify).setOnClickListener {
startActivity(Intent(this, CameraActivity::class.java))
}
// findViewById<Button>(R.id.buttonCapture).setOnClickListener {
// startActivity(Intent(this, CaptureActivity::class.java))
// }
//
// findViewById<Button>(R.id.buttonAttribute).setOnClickListener {
// val intent = Intent()
// intent.setType("image/*")
// intent.setAction(Intent.ACTION_PICK)
// startActivityForResult(Intent.createChooser(intent, getString(R.string.select_picture)), SELECT_ATTRIBUTE_REQUEST_CODE)
// }
findViewById<Button>(R.id.buttonSettings).setOnClickListener {
startActivity(Intent(this, SettingsActivity::class.java))
}
findViewById<Button>(R.id.buttonAbout).setOnClickListener {
startActivity(Intent(this, AboutActivity::class.java))
}
findViewById<LinearLayout>(R.id.lytBrand).setOnClickListener {
val browse = Intent(Intent.ACTION_VIEW, Uri.parse("https://faceplugin.com"))
startActivity(browse)
}
}
override fun onResume() {
super.onResume()
personAdapter.notifyDataSetChanged()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == SELECT_PHOTO_REQUEST_CODE && resultCode == RESULT_OK) {
try {
var bitmap: Bitmap = Utils.getCorrectlyOrientedImage(this, data?.data!!)
var faceBoxes: List<FaceBox>? = FaceSDK.faceDetection(bitmap, null)
if(faceBoxes.isNullOrEmpty()) {
Toast.makeText(this, getString(R.string.no_face_detected), Toast.LENGTH_SHORT).show()
} else if (faceBoxes.size > 1) {
Toast.makeText(this, getString(R.string.multiple_face_detected), Toast.LENGTH_SHORT).show()
} else {
val faceImage = Utils.cropFace(bitmap, faceBoxes[0])
val templates = FaceSDK.templateExtraction(bitmap, faceBoxes[0])
dbManager.insertPerson("Person" + Random.nextInt(10000, 20000), faceImage, templates)
personAdapter.notifyDataSetChanged()
Toast.makeText(this, getString(R.string.person_enrolled), Toast.LENGTH_SHORT).show()
}
} catch (e: java.lang.Exception) {
//handle exception
e.printStackTrace()
}
} else if (requestCode == SELECT_ATTRIBUTE_REQUEST_CODE && resultCode == RESULT_OK) {
try {
var bitmap: Bitmap = Utils.getCorrectlyOrientedImage(this, data?.data!!)
val param = FaceDetectionParam()
param.check_liveness = true
param.check_liveness_level = SettingsActivity.getLivenessLevel(this)
param.check_eye_closeness = true
param.check_face_occlusion = true
param.check_mouth_opened = true
param.estimate_age_gender = true
var faceBoxes: List<FaceBox>? = FaceSDK.faceDetection(bitmap, param)
if(faceBoxes.isNullOrEmpty()) {
Toast.makeText(this, getString(R.string.no_face_detected), Toast.LENGTH_SHORT).show()
} else if (faceBoxes.size > 1) {
Toast.makeText(this, getString(R.string.multiple_face_detected), Toast.LENGTH_SHORT).show()
} else {
val faceImage = Utils.cropFace(bitmap, faceBoxes[0])
val intent = Intent(this, AttributeActivity::class.java)
intent.putExtra("face_image", faceImage)
intent.putExtra("yaw", faceBoxes[0].yaw)
intent.putExtra("roll", faceBoxes[0].roll)
intent.putExtra("pitch", faceBoxes[0].pitch)
intent.putExtra("face_quality", faceBoxes[0].face_quality)
intent.putExtra("face_luminance", faceBoxes[0].face_luminance)
intent.putExtra("liveness", faceBoxes[0].liveness)
intent.putExtra("left_eye_closed", faceBoxes[0].left_eye_closed)
intent.putExtra("right_eye_closed", faceBoxes[0].right_eye_closed)
intent.putExtra("face_occlusion", faceBoxes[0].face_occlusion)
intent.putExtra("mouth_opened", faceBoxes[0].mouth_opened)
intent.putExtra("age", faceBoxes[0].age)
intent.putExtra("gender", faceBoxes[0].gender)
startActivity(intent)
}
} catch (e: java.lang.Exception) {
//handle exception
e.printStackTrace()
}
}
}
}