-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTFLite.kt
99 lines (88 loc) · 4.36 KB
/
TFLite.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
package org.firstinspires.ftc.teamcode
import org.firstinspires.ftc.robotcore.external.ClassFactory
import org.firstinspires.ftc.robotcore.external.tfod.TFObjectDetector
import org.firstinspires.ftc.teamcode.RobotProcessor.sampleRandomPos
/**
* Created by khadija on 1/5/2019.
*/
class TFLite(private val master: masterV) {
companion object {
private const val TFOD_MODEL_ASSET = "RoverRuckus.tflite"
private const val LABEL_GOLD_MINERAL = "Gold Mineral"
private const val LABEL_SILVER_MINERAL = "Silver Mineral"
}
private var tfod: TFObjectDetector? = null
private val tfodMoniterViewId = master.hMap.appContext.resources.getIdentifier("tfodMonitorViewId", "id", master.hMap.appContext.packageName)
private val parameters = TFObjectDetector.Parameters(tfodMoniterViewId)
fun init() {
if (tfod == null) {
tfod = ClassFactory.getInstance().createTFObjectDetector(parameters, master.vuforiaLocalizer)
tfod?.loadModelFromAsset(TFOD_MODEL_ASSET, LABEL_GOLD_MINERAL, LABEL_SILVER_MINERAL)
}
}
var lastKnownSampleOrder = sampleRandomPos.UNKNOWN
internal fun updateSampleOrder() {
if (tfod != null) {
val updatedRecognitions = tfod?.updatedRecognitions
if (updatedRecognitions != null) {
if (updatedRecognitions.size == 3 || updatedRecognitions.size == 2) {
var goldMineralX: Int? = null
var silverMineral1X: Int? = null
var silverMineral2X: Int? = null
for (recognition in updatedRecognitions) {
if (recognition.label == LABEL_GOLD_MINERAL)
goldMineralX = recognition.left.toInt()
else if (silverMineral1X == null)
silverMineral1X = recognition.left.toInt()
else
silverMineral2X = recognition.left.toInt()
}
when (master.tfLiteAlgorithm) {
masterV.TFLiteAlgorithm.INFER_NONE -> if (goldMineralX != null && silverMineral1X != null && silverMineral2X != null)
if (updatedRecognitions.size == 3)
lastKnownSampleOrder =
if (goldMineralX < silverMineral1X && goldMineralX < silverMineral2X)
sampleRandomPos.LEFT
else if (goldMineralX > silverMineral1X && goldMineralX > silverMineral2X)
sampleRandomPos.RIGHT
else
sampleRandomPos.CENTER
masterV.TFLiteAlgorithm.INFER_LEFT -> {
if(updatedRecognitions.size == 2) {
if (goldMineralX == null)
lastKnownSampleOrder = sampleRandomPos.LEFT
else if (silverMineral1X != null)
lastKnownSampleOrder =
if (goldMineralX < silverMineral1X)
sampleRandomPos.CENTER
else
sampleRandomPos.RIGHT
}
}
masterV.TFLiteAlgorithm.INFER_RIGHT -> {
if(updatedRecognitions.size == 2) {
if (goldMineralX == null)
lastKnownSampleOrder = sampleRandomPos.RIGHT
else if (silverMineral1X != null)
lastKnownSampleOrder =
if (goldMineralX < silverMineral1X)
sampleRandomPos.CENTER
else
sampleRandomPos.LEFT
}
}
}
}
}
}
}
fun enable() {
tfod?.activate()
}
fun disable() {
tfod?.deactivate()
}
fun shutdown() {
tfod?.shutdown()
}
}