diff --git a/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt b/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt deleted file mode 100644 index c57452b..0000000 --- a/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.casic.br.ar.app.extensions - -import com.casic.br.ar.app.external.YoloResult - -/** - * 判断集合中是否含有某一类元素 - * */ -fun ArrayList.isContains(target: Int): Boolean { - var result = false - for (yolo in this) { - if (yolo.type == target) { - result = true - break - } - } - return result -} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt b/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt deleted file mode 100644 index c57452b..0000000 --- a/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.casic.br.ar.app.extensions - -import com.casic.br.ar.app.external.YoloResult - -/** - * 判断集合中是否含有某一类元素 - * */ -fun ArrayList.isContains(target: Int): Boolean { - var result = false - for (yolo in this) { - if (yolo.type == target) { - result = true - break - } - } - return result -} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/ar/app/extensions/List.kt b/app/src/main/java/com/casic/br/ar/app/extensions/List.kt new file mode 100644 index 0000000..28faaf7 --- /dev/null +++ b/app/src/main/java/com/casic/br/ar/app/extensions/List.kt @@ -0,0 +1,17 @@ +package com.casic.br.ar.app.extensions + +import com.casic.br.ar.app.external.YoloResult + +/** + * 判断集合中是否含有某一类元素 + * */ +fun List.isContains(target: Int): Boolean { + var result = false + for (yolo in this) { + if (yolo.type == target) { + result = true + break + } + } + return result +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt b/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt deleted file mode 100644 index c57452b..0000000 --- a/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.casic.br.ar.app.extensions - -import com.casic.br.ar.app.external.YoloResult - -/** - * 判断集合中是否含有某一类元素 - * */ -fun ArrayList.isContains(target: Int): Boolean { - var result = false - for (yolo in this) { - if (yolo.type == target) { - result = true - break - } - } - return result -} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/ar/app/extensions/List.kt b/app/src/main/java/com/casic/br/ar/app/extensions/List.kt new file mode 100644 index 0000000..28faaf7 --- /dev/null +++ b/app/src/main/java/com/casic/br/ar/app/extensions/List.kt @@ -0,0 +1,17 @@ +package com.casic.br.ar.app.extensions + +import com.casic.br.ar.app.external.YoloResult + +/** + * 判断集合中是否含有某一类元素 + * */ +fun List.isContains(target: Int): Boolean { + var result = false + for (yolo in this) { + if (yolo.type == target) { + result = true + break + } + } + return result +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/ar/app/utils/LocaleConstant.kt b/app/src/main/java/com/casic/br/ar/app/utils/LocaleConstant.kt index d6fd2e7..8a71f8f 100644 --- a/app/src/main/java/com/casic/br/ar/app/utils/LocaleConstant.kt +++ b/app/src/main/java/com/casic/br/ar/app/utils/LocaleConstant.kt @@ -90,6 +90,29 @@ val SEGMENTATION_ARRAY = arrayOf("弯折", "断裂", "烧焦", "磨损", "铁锈", "龟裂") + val HIDDEN_TROUBLE_ARRAY = arrayOf( + "配电箱无警示标识", + "配电箱内无电路图", + "配电箱内部电线裸露", + "配电箱有跨电线", + "未佩戴安全帽", + "未着工服", + "没有佩戴安全带、安全绳", + "未发现呼吸防护设备", + "未发现路锥、警戒线", + "未发现安全告知牌", + "未发现通风设备", + "未发现井下照明设备", + "未发现井下对讲设备", + "未发现施工三脚架", + "未发现气体检测仪", + "非专用软管", + "软管有接头或三通", + "未发现切断阀", + "未发现熄火保护装置", + "未发现燃气泄漏报警装置" + ) + //配电箱 val DISTRIBUTION_BOX_SCENE_ARRAY = arrayListOf( "安全标识", "电路图", "电线暴露", "跨电线", "配电箱", "贴纸", "开关" diff --git a/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt b/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt deleted file mode 100644 index c57452b..0000000 --- a/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.casic.br.ar.app.extensions - -import com.casic.br.ar.app.external.YoloResult - -/** - * 判断集合中是否含有某一类元素 - * */ -fun ArrayList.isContains(target: Int): Boolean { - var result = false - for (yolo in this) { - if (yolo.type == target) { - result = true - break - } - } - return result -} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/ar/app/extensions/List.kt b/app/src/main/java/com/casic/br/ar/app/extensions/List.kt new file mode 100644 index 0000000..28faaf7 --- /dev/null +++ b/app/src/main/java/com/casic/br/ar/app/extensions/List.kt @@ -0,0 +1,17 @@ +package com.casic.br.ar.app.extensions + +import com.casic.br.ar.app.external.YoloResult + +/** + * 判断集合中是否含有某一类元素 + * */ +fun List.isContains(target: Int): Boolean { + var result = false + for (yolo in this) { + if (yolo.type == target) { + result = true + break + } + } + return result +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/ar/app/utils/LocaleConstant.kt b/app/src/main/java/com/casic/br/ar/app/utils/LocaleConstant.kt index d6fd2e7..8a71f8f 100644 --- a/app/src/main/java/com/casic/br/ar/app/utils/LocaleConstant.kt +++ b/app/src/main/java/com/casic/br/ar/app/utils/LocaleConstant.kt @@ -90,6 +90,29 @@ val SEGMENTATION_ARRAY = arrayOf("弯折", "断裂", "烧焦", "磨损", "铁锈", "龟裂") + val HIDDEN_TROUBLE_ARRAY = arrayOf( + "配电箱无警示标识", + "配电箱内无电路图", + "配电箱内部电线裸露", + "配电箱有跨电线", + "未佩戴安全帽", + "未着工服", + "没有佩戴安全带、安全绳", + "未发现呼吸防护设备", + "未发现路锥、警戒线", + "未发现安全告知牌", + "未发现通风设备", + "未发现井下照明设备", + "未发现井下对讲设备", + "未发现施工三脚架", + "未发现气体检测仪", + "非专用软管", + "软管有接头或三通", + "未发现切断阀", + "未发现熄火保护装置", + "未发现燃气泄漏报警装置" + ) + //配电箱 val DISTRIBUTION_BOX_SCENE_ARRAY = arrayListOf( "安全标识", "电路图", "电线暴露", "跨电线", "配电箱", "贴纸", "开关" diff --git a/app/src/main/java/com/casic/br/ar/app/view/CheckModeActivity.kt b/app/src/main/java/com/casic/br/ar/app/view/CheckModeActivity.kt index 69090db..fb6fa58 100644 --- a/app/src/main/java/com/casic/br/ar/app/view/CheckModeActivity.kt +++ b/app/src/main/java/com/casic/br/ar/app/view/CheckModeActivity.kt @@ -452,12 +452,12 @@ alarmCode = "NonResidentUserHasNoShutoffValve" } - if (detectResults.isContains(34)) { + if (!detectResults.isContains(34)) { warnings = "未发现熄火保护装置" alarmCode = "NonResidentUserHasNoStoveFlameoutProtection" } - if (detectResults.isContains(22)) { + if (!detectResults.isContains(22)) { warnings = "未发现燃气泄漏报警装置" alarmCode = "NonResidentUserHasNoAlarmDevice" } @@ -489,12 +489,12 @@ alarmCode = "NonResidentUserHasNoShutoffValve" } - if (detectResults.isContains(34)) { + if (!detectResults.isContains(34)) { warnings = "未发现熄火保护装置" alarmCode = "NonResidentUserHasNoStoveFlameoutProtection" } - if (detectResults.isContains(22)) { + if (!detectResults.isContains(22)) { warnings = "未发现燃气泄漏报警装置" alarmCode = "NonResidentUserHasNoAlarmDevice" } diff --git a/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt b/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt deleted file mode 100644 index c57452b..0000000 --- a/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.casic.br.ar.app.extensions - -import com.casic.br.ar.app.external.YoloResult - -/** - * 判断集合中是否含有某一类元素 - * */ -fun ArrayList.isContains(target: Int): Boolean { - var result = false - for (yolo in this) { - if (yolo.type == target) { - result = true - break - } - } - return result -} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/ar/app/extensions/List.kt b/app/src/main/java/com/casic/br/ar/app/extensions/List.kt new file mode 100644 index 0000000..28faaf7 --- /dev/null +++ b/app/src/main/java/com/casic/br/ar/app/extensions/List.kt @@ -0,0 +1,17 @@ +package com.casic.br.ar.app.extensions + +import com.casic.br.ar.app.external.YoloResult + +/** + * 判断集合中是否含有某一类元素 + * */ +fun List.isContains(target: Int): Boolean { + var result = false + for (yolo in this) { + if (yolo.type == target) { + result = true + break + } + } + return result +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/ar/app/utils/LocaleConstant.kt b/app/src/main/java/com/casic/br/ar/app/utils/LocaleConstant.kt index d6fd2e7..8a71f8f 100644 --- a/app/src/main/java/com/casic/br/ar/app/utils/LocaleConstant.kt +++ b/app/src/main/java/com/casic/br/ar/app/utils/LocaleConstant.kt @@ -90,6 +90,29 @@ val SEGMENTATION_ARRAY = arrayOf("弯折", "断裂", "烧焦", "磨损", "铁锈", "龟裂") + val HIDDEN_TROUBLE_ARRAY = arrayOf( + "配电箱无警示标识", + "配电箱内无电路图", + "配电箱内部电线裸露", + "配电箱有跨电线", + "未佩戴安全帽", + "未着工服", + "没有佩戴安全带、安全绳", + "未发现呼吸防护设备", + "未发现路锥、警戒线", + "未发现安全告知牌", + "未发现通风设备", + "未发现井下照明设备", + "未发现井下对讲设备", + "未发现施工三脚架", + "未发现气体检测仪", + "非专用软管", + "软管有接头或三通", + "未发现切断阀", + "未发现熄火保护装置", + "未发现燃气泄漏报警装置" + ) + //配电箱 val DISTRIBUTION_BOX_SCENE_ARRAY = arrayListOf( "安全标识", "电路图", "电线暴露", "跨电线", "配电箱", "贴纸", "开关" diff --git a/app/src/main/java/com/casic/br/ar/app/view/CheckModeActivity.kt b/app/src/main/java/com/casic/br/ar/app/view/CheckModeActivity.kt index 69090db..fb6fa58 100644 --- a/app/src/main/java/com/casic/br/ar/app/view/CheckModeActivity.kt +++ b/app/src/main/java/com/casic/br/ar/app/view/CheckModeActivity.kt @@ -452,12 +452,12 @@ alarmCode = "NonResidentUserHasNoShutoffValve" } - if (detectResults.isContains(34)) { + if (!detectResults.isContains(34)) { warnings = "未发现熄火保护装置" alarmCode = "NonResidentUserHasNoStoveFlameoutProtection" } - if (detectResults.isContains(22)) { + if (!detectResults.isContains(22)) { warnings = "未发现燃气泄漏报警装置" alarmCode = "NonResidentUserHasNoAlarmDevice" } @@ -489,12 +489,12 @@ alarmCode = "NonResidentUserHasNoShutoffValve" } - if (detectResults.isContains(34)) { + if (!detectResults.isContains(34)) { warnings = "未发现熄火保护装置" alarmCode = "NonResidentUserHasNoStoveFlameoutProtection" } - if (detectResults.isContains(22)) { + if (!detectResults.isContains(22)) { warnings = "未发现燃气泄漏报警装置" alarmCode = "NonResidentUserHasNoAlarmDevice" } diff --git a/app/src/main/java/com/casic/br/ar/app/widgets/FreeModeYoloDetectView.kt b/app/src/main/java/com/casic/br/ar/app/widgets/FreeModeYoloDetectView.kt new file mode 100644 index 0000000..7d441a7 --- /dev/null +++ b/app/src/main/java/com/casic/br/ar/app/widgets/FreeModeYoloDetectView.kt @@ -0,0 +1,274 @@ +package com.casic.br.ar.app.widgets + +import android.content.Context +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Paint +import android.graphics.Rect +import android.text.TextPaint +import android.util.AttributeSet +import android.view.View +import com.casic.br.ar.app.extensions.isContains +import com.casic.br.ar.app.external.YoloResult +import com.casic.br.ar.app.utils.LocaleConstant +import com.pengxh.kt.lite.extensions.dp2px +import com.pengxh.kt.lite.extensions.sp2px + +class FreeModeYoloDetectView constructor(context: Context, attrs: AttributeSet) : + View(context, attrs) { + + private val kTag = "DetectView" + private val textPaint by lazy { TextPaint() } + private val backgroundPaint by lazy { Paint() } + private val borderPaint by lazy { Paint() } + private val textRect by lazy { Rect() } + private val rect by lazy { Rect() } + private var detectResults: MutableList = ArrayList() + private var segmentationResults: MutableList = ArrayList() + private var hiddenTroubles: MutableList = ArrayList() + private var textHeight = 0 + + init { + textPaint.color = Color.WHITE + textPaint.isAntiAlias = true + textPaint.textAlign = Paint.Align.CENTER + textPaint.textSize = 16f.sp2px(context) + val fontMetrics = textPaint.fontMetrics + textHeight = (fontMetrics.bottom - fontMetrics.top).toInt() + + backgroundPaint.style = Paint.Style.FILL + backgroundPaint.isAntiAlias = true + + borderPaint.style = Paint.Style.STROKE + borderPaint.strokeWidth = 2f.dp2px(context) //设置线宽 + borderPaint.isAntiAlias = true + } + + fun updateTargetPosition( + segmentationResults: MutableList, detectResults: MutableList + ) { + this.segmentationResults = segmentationResults + this.detectResults = detectResults + + //筛选出隐患 + val hiddenTroubles = ArrayList() + if (detectResults.isNotEmpty()) { + //要先判断场景。用第一个结果作为场景判断依据 + val firstTarget = LocaleConstant.CLASS_NAMES_ARRAY[detectResults.first().type] + val sceneName = if (LocaleConstant.DISTRIBUTION_BOX_SCENE_ARRAY.contains(firstTarget)) { + "配电箱" + } else if (LocaleConstant.CONFINED_SPACES_SCENE_ARRAY.contains(firstTarget)) { + "有限空间作业" + } else if (LocaleConstant.NON_RESIDENTIAL_SCENE_ARRAY.contains(firstTarget)) { + "非居民用户" + } else { + "" + } + + val result = YoloResult() + result.position = detectResults.first().position + when (sceneName) { + "配电箱" -> { + //配电箱位置即为隐患位置 + if (!detectResults.isContains(16)) { + result.type = 0 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(36)) { + result.type = 1 + hiddenTroubles.add(result) + } + + if (detectResults.isContains(35)) { + result.type = 2 + hiddenTroubles.add(result) + } + + if (detectResults.isContains(41)) { + result.type = 3 + hiddenTroubles.add(result) + } + } + + "有限空间作业" -> { + if (detectResults.isContains(3)) { + if (!detectResults.isContains(15)) { + result.type = 4 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(20)) { + result.type = 5 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(15) && !detectResults.isContains(17)) { + result.type = 6 + hiddenTroubles.add(result) + } + } + + if (!detectResults.isContains(9)) { + result.type = 7 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(42) && !detectResults.isContains(37)) { + result.type = 8 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(14)) { + result.type = 9 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(51)) { + result.type = 10 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(33)) { + result.type = 11 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(18)) { + result.type = 12 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(0)) { + result.type = 13 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(25)) { + result.type = 14 + hiddenTroubles.add(result) + } + } + + "非居民用户" -> { + if (detectResults.isContains(49)) { + result.type = 15 + hiddenTroubles.add(result) + } + + if (detectResults.isContains(1)) { + result.type = 16 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(4)) { + result.type = 17 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(34)) { + result.type = 18 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(22)) { + result.type = 19 + hiddenTroubles.add(result) + } + } + } + } + this.hiddenTroubles = hiddenTroubles + + postInvalidate() + } + + override fun onDraw(canvas: Canvas) { + super.onDraw(canvas) + detectResults.forEach { + drawTarget(canvas, it, LocaleConstant.CLASS_NAMES_ARRAY[it.type]) + } + + segmentationResults.forEach { + drawAlarmTips(canvas, it, LocaleConstant.SEGMENTATION_ARRAY[it.type]) + } + + hiddenTroubles.forEach { + drawAlarmTips(canvas, it, LocaleConstant.HIDDEN_TROUBLE_ARRAY[it.type]) + } + } + + private fun drawTarget(canvas: Canvas, it: YoloResult, label: String) { + val textLength = textPaint.measureText(label) + val textRectWidth = textLength * 1.1 //文字背景宽度 + val textRectHeight = textHeight * 1.1 //文字背景高度 + + textRect.set( + it.position[0].toInt(), + it.position[1].toInt(), + (it.position[0] + textRectWidth).toInt(), + (it.position[1] - textRectHeight).toInt() + ) + if (label == "非专用软管") { + backgroundPaint.color = Color.RED + } else { + backgroundPaint.color = Color.GREEN + } + canvas.drawRect(textRect, backgroundPaint) + + //画文字 + canvas.drawText( + label, + (it.position[0] + textRectWidth / 2).toFloat(), + (it.position[1] - textRectHeight / 4).toFloat(), + textPaint + ) + + //画框 + rect.set( + it.position[0].toInt(), + it.position[1].toInt(), + it.position[2].toInt(), + it.position[3].toInt() + ) + if (label == "非专用软管") { + borderPaint.color = Color.RED + } else { + borderPaint.color = Color.GREEN + } + canvas.drawRect(rect, borderPaint) + } + + private fun drawAlarmTips(canvas: Canvas, it: YoloResult, label: String) { + val textLength = textPaint.measureText(label) + val textRectWidth = textLength * 1.1 //文字背景宽度 + val textRectHeight = textHeight * 1.1 //文字背景高度 + + textRect.set( + it.position[0].toInt(), + it.position[1].toInt(), + (it.position[0] + textRectWidth).toInt(), + (it.position[1] - textRectHeight).toInt() + ) + backgroundPaint.color = Color.RED + canvas.drawRect(textRect, backgroundPaint) + + //画文字 + canvas.drawText( + label, + (it.position[0] + textRectWidth / 2).toFloat(), + (it.position[1] - textRectHeight / 4).toFloat(), + textPaint + ) + + //画框 + rect.set( + it.position[0].toInt(), + it.position[1].toInt(), + it.position[2].toInt(), + it.position[3].toInt() + ) + borderPaint.color = Color.RED + canvas.drawRect(rect, borderPaint) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt b/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt deleted file mode 100644 index c57452b..0000000 --- a/app/src/main/java/com/casic/br/ar/app/extensions/ArrayList.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.casic.br.ar.app.extensions - -import com.casic.br.ar.app.external.YoloResult - -/** - * 判断集合中是否含有某一类元素 - * */ -fun ArrayList.isContains(target: Int): Boolean { - var result = false - for (yolo in this) { - if (yolo.type == target) { - result = true - break - } - } - return result -} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/ar/app/extensions/List.kt b/app/src/main/java/com/casic/br/ar/app/extensions/List.kt new file mode 100644 index 0000000..28faaf7 --- /dev/null +++ b/app/src/main/java/com/casic/br/ar/app/extensions/List.kt @@ -0,0 +1,17 @@ +package com.casic.br.ar.app.extensions + +import com.casic.br.ar.app.external.YoloResult + +/** + * 判断集合中是否含有某一类元素 + * */ +fun List.isContains(target: Int): Boolean { + var result = false + for (yolo in this) { + if (yolo.type == target) { + result = true + break + } + } + return result +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/ar/app/utils/LocaleConstant.kt b/app/src/main/java/com/casic/br/ar/app/utils/LocaleConstant.kt index d6fd2e7..8a71f8f 100644 --- a/app/src/main/java/com/casic/br/ar/app/utils/LocaleConstant.kt +++ b/app/src/main/java/com/casic/br/ar/app/utils/LocaleConstant.kt @@ -90,6 +90,29 @@ val SEGMENTATION_ARRAY = arrayOf("弯折", "断裂", "烧焦", "磨损", "铁锈", "龟裂") + val HIDDEN_TROUBLE_ARRAY = arrayOf( + "配电箱无警示标识", + "配电箱内无电路图", + "配电箱内部电线裸露", + "配电箱有跨电线", + "未佩戴安全帽", + "未着工服", + "没有佩戴安全带、安全绳", + "未发现呼吸防护设备", + "未发现路锥、警戒线", + "未发现安全告知牌", + "未发现通风设备", + "未发现井下照明设备", + "未发现井下对讲设备", + "未发现施工三脚架", + "未发现气体检测仪", + "非专用软管", + "软管有接头或三通", + "未发现切断阀", + "未发现熄火保护装置", + "未发现燃气泄漏报警装置" + ) + //配电箱 val DISTRIBUTION_BOX_SCENE_ARRAY = arrayListOf( "安全标识", "电路图", "电线暴露", "跨电线", "配电箱", "贴纸", "开关" diff --git a/app/src/main/java/com/casic/br/ar/app/view/CheckModeActivity.kt b/app/src/main/java/com/casic/br/ar/app/view/CheckModeActivity.kt index 69090db..fb6fa58 100644 --- a/app/src/main/java/com/casic/br/ar/app/view/CheckModeActivity.kt +++ b/app/src/main/java/com/casic/br/ar/app/view/CheckModeActivity.kt @@ -452,12 +452,12 @@ alarmCode = "NonResidentUserHasNoShutoffValve" } - if (detectResults.isContains(34)) { + if (!detectResults.isContains(34)) { warnings = "未发现熄火保护装置" alarmCode = "NonResidentUserHasNoStoveFlameoutProtection" } - if (detectResults.isContains(22)) { + if (!detectResults.isContains(22)) { warnings = "未发现燃气泄漏报警装置" alarmCode = "NonResidentUserHasNoAlarmDevice" } @@ -489,12 +489,12 @@ alarmCode = "NonResidentUserHasNoShutoffValve" } - if (detectResults.isContains(34)) { + if (!detectResults.isContains(34)) { warnings = "未发现熄火保护装置" alarmCode = "NonResidentUserHasNoStoveFlameoutProtection" } - if (detectResults.isContains(22)) { + if (!detectResults.isContains(22)) { warnings = "未发现燃气泄漏报警装置" alarmCode = "NonResidentUserHasNoAlarmDevice" } diff --git a/app/src/main/java/com/casic/br/ar/app/widgets/FreeModeYoloDetectView.kt b/app/src/main/java/com/casic/br/ar/app/widgets/FreeModeYoloDetectView.kt new file mode 100644 index 0000000..7d441a7 --- /dev/null +++ b/app/src/main/java/com/casic/br/ar/app/widgets/FreeModeYoloDetectView.kt @@ -0,0 +1,274 @@ +package com.casic.br.ar.app.widgets + +import android.content.Context +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Paint +import android.graphics.Rect +import android.text.TextPaint +import android.util.AttributeSet +import android.view.View +import com.casic.br.ar.app.extensions.isContains +import com.casic.br.ar.app.external.YoloResult +import com.casic.br.ar.app.utils.LocaleConstant +import com.pengxh.kt.lite.extensions.dp2px +import com.pengxh.kt.lite.extensions.sp2px + +class FreeModeYoloDetectView constructor(context: Context, attrs: AttributeSet) : + View(context, attrs) { + + private val kTag = "DetectView" + private val textPaint by lazy { TextPaint() } + private val backgroundPaint by lazy { Paint() } + private val borderPaint by lazy { Paint() } + private val textRect by lazy { Rect() } + private val rect by lazy { Rect() } + private var detectResults: MutableList = ArrayList() + private var segmentationResults: MutableList = ArrayList() + private var hiddenTroubles: MutableList = ArrayList() + private var textHeight = 0 + + init { + textPaint.color = Color.WHITE + textPaint.isAntiAlias = true + textPaint.textAlign = Paint.Align.CENTER + textPaint.textSize = 16f.sp2px(context) + val fontMetrics = textPaint.fontMetrics + textHeight = (fontMetrics.bottom - fontMetrics.top).toInt() + + backgroundPaint.style = Paint.Style.FILL + backgroundPaint.isAntiAlias = true + + borderPaint.style = Paint.Style.STROKE + borderPaint.strokeWidth = 2f.dp2px(context) //设置线宽 + borderPaint.isAntiAlias = true + } + + fun updateTargetPosition( + segmentationResults: MutableList, detectResults: MutableList + ) { + this.segmentationResults = segmentationResults + this.detectResults = detectResults + + //筛选出隐患 + val hiddenTroubles = ArrayList() + if (detectResults.isNotEmpty()) { + //要先判断场景。用第一个结果作为场景判断依据 + val firstTarget = LocaleConstant.CLASS_NAMES_ARRAY[detectResults.first().type] + val sceneName = if (LocaleConstant.DISTRIBUTION_BOX_SCENE_ARRAY.contains(firstTarget)) { + "配电箱" + } else if (LocaleConstant.CONFINED_SPACES_SCENE_ARRAY.contains(firstTarget)) { + "有限空间作业" + } else if (LocaleConstant.NON_RESIDENTIAL_SCENE_ARRAY.contains(firstTarget)) { + "非居民用户" + } else { + "" + } + + val result = YoloResult() + result.position = detectResults.first().position + when (sceneName) { + "配电箱" -> { + //配电箱位置即为隐患位置 + if (!detectResults.isContains(16)) { + result.type = 0 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(36)) { + result.type = 1 + hiddenTroubles.add(result) + } + + if (detectResults.isContains(35)) { + result.type = 2 + hiddenTroubles.add(result) + } + + if (detectResults.isContains(41)) { + result.type = 3 + hiddenTroubles.add(result) + } + } + + "有限空间作业" -> { + if (detectResults.isContains(3)) { + if (!detectResults.isContains(15)) { + result.type = 4 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(20)) { + result.type = 5 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(15) && !detectResults.isContains(17)) { + result.type = 6 + hiddenTroubles.add(result) + } + } + + if (!detectResults.isContains(9)) { + result.type = 7 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(42) && !detectResults.isContains(37)) { + result.type = 8 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(14)) { + result.type = 9 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(51)) { + result.type = 10 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(33)) { + result.type = 11 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(18)) { + result.type = 12 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(0)) { + result.type = 13 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(25)) { + result.type = 14 + hiddenTroubles.add(result) + } + } + + "非居民用户" -> { + if (detectResults.isContains(49)) { + result.type = 15 + hiddenTroubles.add(result) + } + + if (detectResults.isContains(1)) { + result.type = 16 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(4)) { + result.type = 17 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(34)) { + result.type = 18 + hiddenTroubles.add(result) + } + + if (!detectResults.isContains(22)) { + result.type = 19 + hiddenTroubles.add(result) + } + } + } + } + this.hiddenTroubles = hiddenTroubles + + postInvalidate() + } + + override fun onDraw(canvas: Canvas) { + super.onDraw(canvas) + detectResults.forEach { + drawTarget(canvas, it, LocaleConstant.CLASS_NAMES_ARRAY[it.type]) + } + + segmentationResults.forEach { + drawAlarmTips(canvas, it, LocaleConstant.SEGMENTATION_ARRAY[it.type]) + } + + hiddenTroubles.forEach { + drawAlarmTips(canvas, it, LocaleConstant.HIDDEN_TROUBLE_ARRAY[it.type]) + } + } + + private fun drawTarget(canvas: Canvas, it: YoloResult, label: String) { + val textLength = textPaint.measureText(label) + val textRectWidth = textLength * 1.1 //文字背景宽度 + val textRectHeight = textHeight * 1.1 //文字背景高度 + + textRect.set( + it.position[0].toInt(), + it.position[1].toInt(), + (it.position[0] + textRectWidth).toInt(), + (it.position[1] - textRectHeight).toInt() + ) + if (label == "非专用软管") { + backgroundPaint.color = Color.RED + } else { + backgroundPaint.color = Color.GREEN + } + canvas.drawRect(textRect, backgroundPaint) + + //画文字 + canvas.drawText( + label, + (it.position[0] + textRectWidth / 2).toFloat(), + (it.position[1] - textRectHeight / 4).toFloat(), + textPaint + ) + + //画框 + rect.set( + it.position[0].toInt(), + it.position[1].toInt(), + it.position[2].toInt(), + it.position[3].toInt() + ) + if (label == "非专用软管") { + borderPaint.color = Color.RED + } else { + borderPaint.color = Color.GREEN + } + canvas.drawRect(rect, borderPaint) + } + + private fun drawAlarmTips(canvas: Canvas, it: YoloResult, label: String) { + val textLength = textPaint.measureText(label) + val textRectWidth = textLength * 1.1 //文字背景宽度 + val textRectHeight = textHeight * 1.1 //文字背景高度 + + textRect.set( + it.position[0].toInt(), + it.position[1].toInt(), + (it.position[0] + textRectWidth).toInt(), + (it.position[1] - textRectHeight).toInt() + ) + backgroundPaint.color = Color.RED + canvas.drawRect(textRect, backgroundPaint) + + //画文字 + canvas.drawText( + label, + (it.position[0] + textRectWidth / 2).toFloat(), + (it.position[1] - textRectHeight / 4).toFloat(), + textPaint + ) + + //画框 + rect.set( + it.position[0].toInt(), + it.position[1].toInt(), + it.position[2].toInt(), + it.position[3].toInt() + ) + borderPaint.color = Color.RED + canvas.drawRect(rect, borderPaint) + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_free_mode.xml b/app/src/main/res/layout/activity_free_mode.xml index 0d90313..02fc596 100644 --- a/app/src/main/res/layout/activity_free_mode.xml +++ b/app/src/main/res/layout/activity_free_mode.xml @@ -8,7 +8,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" /> -