上一篇入口
- 下载源-53.8 MB
这是有关使用Android和Tensorflow Lite构建实时危险检测器的系列文章中的第五篇。在这一部分中,我们将向应用程序添加崩溃检测,并使应用程序能够向紧急联系人发送通知。
利用大多数Android设备的功能,我们可以向设备添加崩溃检测。在大多数Android设备中,该设备具有加速度计和GPS。如果我们认为发生了崩溃,可以一起使用这些消息发送消息,并发送带有位置的紧急消息。
为了检测崩溃,我创建了一个名为CrashDetector的新类。该类将监测来自加速度计的读数。当其接收到高于某个水平的读数时,则认为车辆受到了冲击。我在这里选择的用于指示影响的值是基于对车祸的阅读的最佳估计。
var coolDownExpiry:Long = 0
val COOL_DOWN_TIME = 10000
fun resetCooldown() {
coolDownExpiry = Date().time + COOL_DOWN_TIME
}
fun hasCooldownExpired():Boolean {
val now = Date().time
return now > coolDownExpiry
}
fun alert(direction: Int) {
// using the when statement to filter out invalid
// values should they be passed to this function
if(hasCooldownExpired() && currentSpeedMPS>MIN_ALERT_MPS) {
when (direction) {
ALARM_CENTER, ALARM_RIGHT, ALARM_LEFT -> {
soundPlayer = MediaPlayer.create(context, direction)
soundPlayer!!.start()
}
}
}
resetCooldown()
}
还有其他一些情况可能会导致加速度计的读数突然升高。设备可能掉落,用户开车驶过坑洼或用户发生了碰撞而不想发送警报的情况。为了降低发送意外紧急消息的机会,在发送消息之前会有一定的延迟。在此延迟期间,设备会显示提示,允许用户取消消息或将其发送出去。如果对话框到期后仍未选择任何内容,它将向用户已选择其紧急联系人的电话号码发送一条消息。如果驱动程序有一个最后已知的位置,它将作为链接发送到Google Maps。
fun sendEmergencyMessage() {
var msg = crashMessage
if(this.location != null) {
msg = msg + " https://www.google.com/maps/@${location!!.latitude},${location!!.longitude},15z"
}
val smsManager = SmsManager.getDefault() as SmsManager
smsManager.sendTextMessage(crashPhoneNumber, null,msg , null, null)
}
既然已经构建了所有主要功能,我们就可以准备好最后一块,并让应用程序使用实时视频流而不是静态图像。在本系列的下一部分中,我们将为应用程序处理实时数据。
https://www.codeproject.com/Articles/5291392/Crash-Detection-Using-the-Accelerometer