Skip to content

Commit f4d87b0

Browse files
Removed example tests, corrected private variable naming
1 parent bdc163e commit f4d87b0

File tree

9 files changed

+60
-105
lines changed

9 files changed

+60
-105
lines changed

android/app/detector/src/androidTest/java/org/tensorflow/lite/examples/detector/ExampleInstrumentedTest.kt

-24
This file was deleted.

android/app/detector/src/test/java/org/tensorflow/lite/examples/detector/ExampleUnitTest.kt

-17
This file was deleted.

android/app/src/main/java/org/tensorflow/lite/examples/detector/activities/MainActivity.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ open class MainActivity : AppCompatActivity() {
3939
private lateinit var mDetector: Detector
4040
private var mDetectionProcessor: DetectionProcessor? = null
4141

42-
private lateinit var sourceBitmap: Bitmap
43-
private lateinit var cropBitmap: Bitmap
42+
private lateinit var mSourceBitmap: Bitmap
43+
private lateinit var mCropBitmap: Bitmap
4444

4545
override fun onCreate(savedInstanceState: Bundle?) {
4646
super.onCreate(savedInstanceState)
4747
mBinding = ActivityMainBinding.inflate(layoutInflater)
4848
setContentView(mBinding.root)
4949

50-
sourceBitmap = assets.open("kite.jpg").use { inputStream ->
50+
mSourceBitmap = assets.open("kite.jpg").use { inputStream ->
5151
BitmapFactory.decodeStream(inputStream)
5252
}
5353

54-
cropBitmap = processBitmap(sourceBitmap, DETECTION_MODEL.inputSize)
55-
mBinding.imageView.setImageBitmap(cropBitmap)
54+
mCropBitmap = processBitmap(mSourceBitmap, DETECTION_MODEL.inputSize)
55+
mBinding.imageView.setImageBitmap(mCropBitmap)
5656

5757
setUpDetector()
5858
lifecycleScope.launch(Dispatchers.Main) {
@@ -104,7 +104,7 @@ open class MainActivity : AppCompatActivity() {
104104

105105
mBinding.detectButton.setOnClickListener {
106106
lifecycleScope.launch(Dispatchers.Default) {
107-
mDetectionProcessor?.processImage(cropBitmap)
107+
mDetectionProcessor?.processImage(mCropBitmap)
108108
}
109109
}
110110
}

android/app/src/main/java/org/tensorflow/lite/examples/detector/helpers/DetectionProcessor.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class DetectionProcessor(
5757
previewWidth,
5858
previewHeight,
5959
((rotation + 1) % 4) * 90,
60-
showScore = SHOW_SCORE
60+
mShowScore = SHOW_SCORE
6161
)
6262
trackingOverlay.setTracker(mTracker)
6363
}

android/detector/src/main/java/org/tensorflow/lite/examples/detector/Detector.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ interface Detector {
2020
* A unique identifier for what has been recognized. Specific to the detected class,
2121
* not the instance of the [Detection] object.
2222
*/
23-
private val id: String,
23+
private val mId: String,
2424
/**
2525
* Display name for the [Detection].
2626
*/
@@ -36,7 +36,7 @@ interface Detector {
3636
val detectedClass: Int
3737
) : Comparable<Detection> {
3838
override fun toString(): String {
39-
var resultString = "[$id] $className "
39+
var resultString = "[$mId] $className "
4040
resultString += "(%.1f%%) ".format(score * 100.0f)
4141
resultString += "$boundingBox"
4242
return resultString

android/detector/src/main/java/org/tensorflow/lite/examples/detector/YoloV4Detector.kt

+34-37
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import kotlin.math.min
2121
@Suppress("UNNECESSARY_NOT_NULL_ASSERTION")
2222
internal class YoloV4Detector(
2323
assetManager: AssetManager,
24-
private val detectionModel: DetectionModel,
25-
private val minimumScore: Float,
24+
private val mDetectionModel: DetectionModel,
25+
private val mMinimumScore: Float,
2626
) : Detector {
2727

2828
private companion object {
@@ -33,53 +33,50 @@ internal class YoloV4Detector(
3333

3434
}
3535

36-
private val inputSize: Int = detectionModel.inputSize
36+
private val mInputSize: Int = mDetectionModel.inputSize
3737

3838
// Config values.
39-
// Pre-allocated buffers.
40-
private val labels: List<String>
41-
private val interpreter: Interpreter
39+
private val mLabels: List<String>
40+
private val mInterpreter: Interpreter
4241
private val mNmsThresh = 0.6f
4342

44-
private val intValues = IntArray(inputSize * inputSize)
45-
46-
private val byteBuffer: Array<ByteBuffer>
47-
48-
private val outputMap: MutableMap<Int, Array<Array<FloatArray>>> = HashMap()
43+
// Pre-allocated buffers.
44+
private val intValues = IntArray(mInputSize * mInputSize)
45+
private val mByteBuffer: Array<ByteBuffer>
46+
private val mOutputMap: MutableMap<Int, Array<Array<FloatArray>>> = HashMap()
4947

5048
init {
51-
52-
val labelsFilename = detectionModel.labelFilePath
49+
val labelsFilename = mDetectionModel.labelFilePath
5350
.split("file:///android_asset/")
5451
.toTypedArray()[1]
5552

56-
labels = assetManager.open(labelsFilename)
53+
mLabels = assetManager.open(labelsFilename)
5754
.use { it.readBytes() }
5855
.decodeToString()
5956
.trim()
6057
.split("\n")
6158
.map { it.trim() }
6259

63-
interpreter = initializeInterpreter(assetManager)
60+
mInterpreter = initializeInterpreter(assetManager)
6461

65-
val numBytesPerChannel = if (detectionModel.isQuantized) {
62+
val numBytesPerChannel = if (mDetectionModel.isQuantized) {
6663
1 // Quantized (int8)
6764
} else {
6865
4 // Floating point (fp32)
6966
}
7067

7168
// input size * input size * pixel count (RGB) * pixel size (int8/fp32)
72-
byteBuffer = arrayOf(
73-
ByteBuffer.allocateDirect(inputSize * inputSize * 3 * numBytesPerChannel)
69+
mByteBuffer = arrayOf(
70+
ByteBuffer.allocateDirect(mInputSize * mInputSize * 3 * numBytesPerChannel)
7471
)
75-
byteBuffer[0].order(ByteOrder.nativeOrder())
72+
mByteBuffer[0].order(ByteOrder.nativeOrder())
7673

77-
outputMap[0] = arrayOf(Array(detectionModel.outputSize) { FloatArray(numBytesPerChannel) })
78-
outputMap[1] = arrayOf(Array(detectionModel.outputSize) { FloatArray(labels.size) })
74+
mOutputMap[0] = arrayOf(Array(mDetectionModel.outputSize) { FloatArray(numBytesPerChannel) })
75+
mOutputMap[1] = arrayOf(Array(mDetectionModel.outputSize) { FloatArray(mLabels.size) })
7976
}
8077

8178
override fun getDetectionModel(): DetectionModel {
82-
return detectionModel
79+
return mDetectionModel
8380
}
8481

8582
override fun runDetection(bitmap: Bitmap): List<Detection> {
@@ -105,7 +102,7 @@ internal class YoloV4Detector(
105102
}
106103
}
107104

108-
return assetManager.openFd(detectionModel.modelFilename).use { fileDescriptor ->
105+
return assetManager.openFd(mDetectionModel.modelFilename).use { fileDescriptor ->
109106
val fileInputStream = FileInputStream(fileDescriptor.fileDescriptor)
110107
val fileByteBuffer = fileInputStream.channel.map(
111108
FileChannel.MapMode.READ_ONLY,
@@ -122,36 +119,36 @@ internal class YoloV4Detector(
122119
*/
123120
private fun convertBitmapToByteBuffer(bitmap: Bitmap) {
124121
val startTime = SystemClock.uptimeMillis()
125-
val scaledBitmap = Bitmap.createScaledBitmap(bitmap, inputSize, inputSize, true)
122+
val scaledBitmap = Bitmap.createScaledBitmap(bitmap, mInputSize, mInputSize, true)
126123

127-
scaledBitmap.getPixels(intValues, 0, inputSize, 0, 0, inputSize, inputSize)
124+
scaledBitmap.getPixels(intValues, 0, mInputSize, 0, 0, mInputSize, mInputSize)
128125
scaledBitmap.recycle()
129126

130-
byteBuffer[0].clear()
127+
mByteBuffer[0].clear()
131128
for (pixel in intValues) {
132129
val r = (pixel and 0xFF) / 255.0f
133130
val g = (pixel shr 8 and 0xFF) / 255.0f
134131
val b = (pixel shr 16 and 0xFF) / 255.0f
135132

136-
byteBuffer[0].putFloat(r)
137-
byteBuffer[0].putFloat(g)
138-
byteBuffer[0].putFloat(b)
133+
mByteBuffer[0].putFloat(r)
134+
mByteBuffer[0].putFloat(g)
135+
mByteBuffer[0].putFloat(b)
139136
}
140137
Log.v(TAG, "ByteBuffer conversion time : ${SystemClock.uptimeMillis() - startTime} ms")
141138
}
142139

143140
private fun getDetections(imageWidth: Int, imageHeight: Int): List<Detection> {
144-
interpreter.runForMultipleInputsOutputs(byteBuffer, outputMap as Map<Int, Any>)
141+
mInterpreter.runForMultipleInputsOutputs(mByteBuffer, mOutputMap as Map<Int, Any>)
145142

146-
val boundingBoxes = outputMap[0]!![0]
147-
val outScore = outputMap[1]!![0]
143+
val boundingBoxes = mOutputMap[0]!![0]
144+
val outScore = mOutputMap[1]!![0]
148145

149146
return outScore.zip(boundingBoxes)
150147
.mapIndexedNotNull { index, (classScores, boundingBoxes) ->
151-
val bestClassIndex: Int = labels.indices.maxByOrNull { classScores[it] }!!
148+
val bestClassIndex: Int = mLabels.indices.maxByOrNull { classScores[it] }!!
152149
val bestScore = classScores[bestClassIndex]
153150

154-
if (bestScore <= minimumScore) {
151+
if (bestScore <= mMinimumScore) {
155152
return@mapIndexedNotNull null
156153
}
157154

@@ -167,8 +164,8 @@ internal class YoloV4Detector(
167164
)
168165

169166
return@mapIndexedNotNull Detection(
170-
id = index.toString(),
171-
className = labels[bestClassIndex],
167+
mId = index.toString(),
168+
className = mLabels[bestClassIndex],
172169
detectedClass = bestClassIndex,
173170
score = bestScore,
174171
boundingBox = rectF
@@ -179,7 +176,7 @@ internal class YoloV4Detector(
179176
private fun nms(detections: List<Detection>): List<Detection> {
180177
val nmsList: MutableList<Detection> = mutableListOf()
181178

182-
for (labelIndex in labels.indices) {
179+
for (labelIndex in mLabels.indices) {
183180
val priorityQueue = PriorityQueue<Detection>(50)
184181
priorityQueue.addAll(detections.filter { it.detectedClass == labelIndex })
185182

android/detector/src/main/java/org/tensorflow/lite/examples/detector/utils/ImageToBitmapConverter.kt

+14-14
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,29 @@ import java.nio.ByteBuffer
1111
* Utility class for converting [Image] to [Bitmap].
1212
*/
1313
class ImageToBitmapConverter(context: Context, image: Image) {
14-
private val bitmap: Bitmap =
14+
private val mBitmap: Bitmap =
1515
Bitmap.createBitmap(image.width, image.height, Bitmap.Config.ARGB_8888)
1616

17-
private val renderScript: RenderScript = RenderScript.create(context)
17+
private val mRenderScript: RenderScript = RenderScript.create(context)
1818

19-
private val scriptYuvToRgb: ScriptIntrinsicYuvToRGB =
20-
ScriptIntrinsicYuvToRGB.create(renderScript, Element.U8_3(renderScript))
19+
private val mScriptYuvToRgb: ScriptIntrinsicYuvToRGB =
20+
ScriptIntrinsicYuvToRGB.create(mRenderScript, Element.U8_3(mRenderScript))
2121

22-
private val elemType = Type.Builder(renderScript, Element.YUV(renderScript))
22+
private val mElemType = Type.Builder(mRenderScript, Element.YUV(mRenderScript))
2323
.setYuvFormat(ImageFormat.YUV_420_888)
2424
.create()
2525

26-
private val inputAllocation: Allocation =
26+
private val mInputAllocation: Allocation =
2727
Allocation.createSized(
28-
renderScript,
29-
elemType.element,
28+
mRenderScript,
29+
mElemType.element,
3030
image.planes.sumOf { it.buffer.remaining() }
3131
)
3232

33-
private val outputAllocation: Allocation = Allocation.createFromBitmap(renderScript, bitmap)
33+
private val mOutputAllocation: Allocation = Allocation.createFromBitmap(mRenderScript, mBitmap)
3434

3535
init {
36-
scriptYuvToRgb.setInput(inputAllocation)
36+
mScriptYuvToRgb.setInput(mInputAllocation)
3737
}
3838

3939
/**
@@ -42,11 +42,11 @@ class ImageToBitmapConverter(context: Context, image: Image) {
4242
fun imageToBitmap(image: Image): Bitmap {
4343
val yuvBuffer: ByteArray = yuv420ToByteArray(image)
4444

45-
inputAllocation.copyFrom(yuvBuffer)
46-
scriptYuvToRgb.forEach(outputAllocation)
47-
outputAllocation.copyTo(bitmap)
45+
mInputAllocation.copyFrom(yuvBuffer)
46+
mScriptYuvToRgb.forEach(mOutputAllocation)
47+
mOutputAllocation.copyTo(mBitmap)
4848

49-
return bitmap
49+
return mBitmap
5050
}
5151

5252
private fun yuv420ToByteArray(image: Image): ByteArray {

android/detector/src/main/java/org/tensorflow/lite/examples/detector/visualization/MultiBoxTracker.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class MultiBoxTracker(
1919
private val mFrameWidth: Int,
2020
private val mFrameHeight: Int,
2121
private val mOrientation: Int,
22-
private val showScore: Boolean = true
22+
private val mShowScore: Boolean = true
2323
) {
2424

2525
private companion object {
@@ -120,14 +120,14 @@ class MultiBoxTracker(
120120
trackedDetection.boxPaint
121121
)
122122

123-
val labelString = if (showScore && trackedDetection.title.isNotBlank()) {
123+
val labelString = if (mShowScore && trackedDetection.title.isNotBlank()) {
124124
"%s %.2f%%".format(
125125
trackedDetection.title,
126126
100 * trackedDetection.score
127127
)
128128
} else if (trackedDetection.title.isNotBlank()) {
129129
trackedDetection.title
130-
} else if (showScore) {
130+
} else if (mShowScore) {
131131
"%.2f%%".format(100 * trackedDetection.score)
132132
} else ""
133133

android/detector/src/main/java/org/tensorflow/lite/examples/detector/visualization/TrackingOverlayView.kt

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import android.view.View
1111
class TrackingOverlayView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
1212
private var mTracker: MultiBoxTracker? = null
1313

14-
1514
override fun draw(canvas: Canvas) {
1615
super.draw(canvas)
1716
mTracker?.draw(canvas)

0 commit comments

Comments
 (0)