Commit 202c5cfa authored by 马乐's avatar 马乐

1.增加日志保存功能

parent e4b07a73
......@@ -35,6 +35,18 @@ import com.intergration.test.dms.setDmsAlarmYawnParams
import com.intergration.test.dms.setDmsDistractScope
import com.intergration.test.dms.setDmsDrivingStatus
import com.intergration.test.dms.startDms
import com.intergration.test.fr.COROUTINE_FR
import com.intergration.test.fr.FrCoroutineContext
import com.intergration.test.fr.FrCoroutineScope
import com.intergration.test.fr.compareFeatureWithScore
import com.intergration.test.fr.extractFeatureFromDir
import com.intergration.test.fr.extractRecognizeFeatureFromByteArray
import com.intergration.test.fr.featureList
import com.intergration.test.fr.frCameraDevice
import com.intergration.test.fr.initializeFr
import com.intergration.test.fr.startFr
import com.intergration.test.utils.DMS_PIC_HEIGHT
import com.intergration.test.utils.DMS_PIC_WIDTH
import com.intergration.test.utils.DOT_PREFACE_DURATION
import com.intergration.test.utils.TTS_ENGINE
import com.intergration.test.utils.VIDEO_FOLDER
......@@ -50,7 +62,9 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.takeWhile
import kotlinx.coroutines.launch
import java.io.File
import java.util.Locale
import java.util.concurrent.CancellationException
......@@ -58,6 +72,8 @@ class MainActivity : AppCompatActivity(),TextToSpeech.OnInitListener,VideoCallba
private var dmsCoroutineScope: DmsCoroutineScope? =null
private var frCoroutineScope: FrCoroutineScope? =null
private lateinit var binding: ActivityMainBinding
private val smartPlatformManager by lazy {
......@@ -74,6 +90,8 @@ class MainActivity : AppCompatActivity(),TextToSpeech.OnInitListener,VideoCallba
private var dmsJob:Job? = null
private var frJob:Job? = null
private val requestCameraPermission = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()){ granted->
if (granted.values.any().not()) {
Toast.makeText(this@MainActivity, "请开启权限", Toast.LENGTH_SHORT).show()
......@@ -100,6 +118,7 @@ class MainActivity : AppCompatActivity(),TextToSpeech.OnInitListener,VideoCallba
requestCameraPermission.launch(REQUIRED_PERMISSIONS)
}
binding.startDms.setOnClickListener {
LogUtils.file("点击开始DMS按钮")
if (dmsCoroutineScope == null) {
dmsCameraDevice = SmartPlatformManager.get().openCameraDevice(CAMERA_ID_DMS)
dmsCoroutineScope = DmsCoroutineScope(DmsCoroutineContext(engine = dmsEngine,dmsCameraDevice!!) + Dispatchers.IO + COROUTINE_DMS)
......@@ -119,15 +138,19 @@ class MainActivity : AppCompatActivity(),TextToSpeech.OnInitListener,VideoCallba
})
if (result == ArcErrorInfo.ARC_ERROR_OK) {
LogUtils.d("DMS,FR激活成功")
LogUtils.file("DMS,FR激活成功")
} else {
LogUtils.e("DMS,FR激活失败")
LogUtils.file("DMS,FR激活失败")
return@launch
}
result = coroutineContext[DmsCoroutineContext]?.engine?.initializeDms()?:ArcErrorInfo.ARC_ERROR_NOT_ACTIVATED
if (result == ArcErrorInfo.ARC_ERROR_OK) {
LogUtils.d("DMS初始化成功")
LogUtils.file("DMS初始化成功")
} else {
LogUtils.e("DMS初始化失败")
LogUtils.file("DMS初始化失败")
return@launch
}
coroutineContext[DmsCoroutineContext]?.engine?.let {engine->
......@@ -168,19 +191,100 @@ class MainActivity : AppCompatActivity(),TextToSpeech.OnInitListener,VideoCallba
dmsCameraDevice?.stopPreview()
dmsCameraDevice?.stopRecord(RecordSource.GENERAL_CAMERA)
dmsCameraDevice?.let { smartPlatformManager.closeCameraDevice(it)}
dmsCameraDevice = null
} catch (e: Exception) {
e.printStackTrace()
}
}
}
binding.stopDms.setOnClickListener {
LogUtils.file("点击停止DMS按钮")
if (dmsJob?.isActive == true) {
dmsJob?.cancel(CancellationException("停止DMS检测"))
dmsCoroutineScope = null
} else {
LogUtils.d("DMS协程已经执行完成")
dmsCameraDevice?.stopPreview()
dmsCameraDevice?.stopRecord(RecordSource.GENERAL_CAMERA)
dmsCameraDevice?.let { smartPlatformManager.closeCameraDevice(it)}
}
}
binding.startFr.setOnClickListener {
LogUtils.file("点击开始人脸识别按钮")
if (frCoroutineScope == null) {
dmsCameraDevice = SmartPlatformManager.get().openCameraDevice(CAMERA_ID_DMS)
frCoroutineScope = FrCoroutineScope(FrCoroutineContext(engine = dmsEngine) + Dispatchers.Default + COROUTINE_FR)
}
frJob = frCoroutineScope?.launch {
try {
val activateResult = ArcVisDriveEngine.activate(APP_ID, APP_SECRET, arrayOf(ArcModType.TYPE_DMS,ArcModType.TYPE_FR),
ArcActiveEnvParam().apply {
this.IMEI = DEVICE_IMEI
this.storagePath = getExternalFilesDir("")?.path
})
if (activateResult == ArcErrorInfo.ARC_ERROR_OK) {
LogUtils.d("DMS,FR激活成功")
} else {
LogUtils.e("DMS,FR激活失败")
return@launch
}
val engine = coroutineContext[FrCoroutineContext]?.engine
frCameraDevice.setPreviewSurface(binding.dmsPreview.holder.surface,
PreviewSource.GENERAL_CAMERA)
frCameraDevice.startPreview()
val result = engine?.initializeFr()
LogUtils.eTag("fr","初始化结果:$result")
if (result == ArcErrorInfo.ARC_ERROR_OK) {
if (featureList.isNotEmpty()) {
engine?.extractFeatureFromDir(
File(
Environment.getExternalStorageDirectory(),
"fr/"
).path
)
}
var startTime = System.currentTimeMillis()
frCameraDevice.startFr().takeWhile { _ ->
System.currentTimeMillis() - startTime > 5000
}.collect {
ensureActive()
val liveExtractResult = engine.extractRecognizeFeatureFromByteArray(
DMS_PIC_WIDTH,
DMS_PIC_HEIGHT,
it
)
val isRecognition = engine.compareFeatureWithScore(liveExtractResult)
if (isRecognition) {
tts.speak("人脸识别成功", TextToSpeech.QUEUE_ADD, null, null)
binding.detectResult.text = "人脸识别成功"
} else {
tts.speak("人脸识别失败", TextToSpeech.QUEUE_ADD, null, null)
binding.detectResult.text = "人脸识别失败"
}
startTime = System.currentTimeMillis()
}
} else {
LogUtils.eTag("fr", "人脸算法初始化失败")
}
} catch (e: CancellationException) {
LogUtils.eTag("fr","人脸识别error:${e.message}")
} catch (e: Exception) {
e.printStackTrace()
}
}
}
binding.stopFr.setOnClickListener {
LogUtils.file("点击停止人脸识别按钮")
if (frJob?.isActive == true) {
frJob?.cancel(CancellationException("停止人脸识别"))
frCoroutineScope = null
} else {
LogUtils.file("人脸识别协程已经执行完成或被取消")
LogUtils.dTag("fr","人脸识别协程已经执行完成或被取消")
}
}
}
......@@ -189,21 +293,28 @@ class MainActivity : AppCompatActivity(),TextToSpeech.OnInitListener,VideoCallba
when(videoInfo?.getInt(VideoInfoMap.KEY_RECORDER_EVENT_TYPE)){
VideoCallback.VIDEO_EVENT_SDCARD_FULL-> {
LogUtils.e("SDCard已满")
LogUtils.file("SDCard已满")
dmsCameraDevice?.stopRecord(RecordSource.GENERAL_CAMERA)
}
VideoCallback.VIDEO_EVENT_RECORD_SDCARD_DAMAGED-> {
LogUtils.e("SDCard损坏")
LogUtils.file("SDCard损坏")
dmsCameraDevice?.stopRecord(RecordSource.GENERAL_CAMERA)
}
VideoCallback.VIDEO_EVENT_RECORD_RECORDING_ERROR-> {
LogUtils.e("录制视频过程中发生错误")
LogUtils.file("录制视频过程中发生错误")
dmsCameraDevice?.stopRecord(RecordSource.GENERAL_CAMERA)
}
VideoCallback.VIDEO_EVENT_KEYPOINT_STOP ->{
LogUtils.d("录制打点视频结束")
}
}
LogUtils.file("录制打点视频结束")
dmsCameraDevice?.stopRecord(RecordSource.GENERAL_CAMERA)
dmsCameraDevice?.startRecord(RecordSource.GENERAL_CAMERA,recordConfiguration)
}
}
}
}
override fun onVideoFrame(p0: ByteArray?, p1: Int, p2: Int, p3: String?, p4: Int) {
......@@ -231,6 +342,7 @@ class MainActivity : AppCompatActivity(),TextToSpeech.OnInitListener,VideoCallba
override fun onDestroy() {
tts.stop()
tts.shutdown()
dmsJob?.cancel(CancellationException("退出MainActivity"))
super.onDestroy()
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment