Skip to content

Commit 040b327

Browse files
...Internal refactor...
PiperOrigin-RevId: 414839941
1 parent 1e62452 commit 040b327

2 files changed

Lines changed: 57 additions & 64 deletions

File tree

  • lite/examples

lite/examples/image_segmentation/android/lib_interpreter/src/main/java/org/tensorflow/lite/examples/imagesegmentation/tflite/ImageSegmentationModelExecutor.kt

Lines changed: 53 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import android.content.Context
2020
import android.graphics.Bitmap
2121
import android.graphics.Color
2222
import android.os.SystemClock
23-
import androidx.core.graphics.ColorUtils
2423
import android.util.Log
24+
import androidx.core.graphics.ColorUtils
2525
import java.io.FileInputStream
2626
import java.io.IOException
2727
import java.nio.ByteBuffer
@@ -34,21 +34,17 @@ import org.tensorflow.lite.examples.imagesegmentation.utils.ImageUtils
3434
import org.tensorflow.lite.gpu.GpuDelegate
3535

3636
/**
37-
* Class responsible to run the Image Segmentation model.
38-
* more information about the DeepLab model being used can
39-
* be found here:
37+
* Class responsible to run the Image Segmentation model. more information about the DeepLab model
38+
* being used can be found here:
4039
* https://ai.googleblog.com/2018/03/semantic-image-segmentation-with.html
4140
* https://www.tensorflow.org/lite/models/segmentation/overview
4241
* https://github.com/tensorflow/models/tree/master/research/deeplab
4342
*
44-
* Label names: 'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus',
45-
* 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike',
46-
* 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tv'
43+
* Label names: 'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat',
44+
* 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep',
45+
* 'sofa', 'train', 'tv'
4746
*/
48-
class ImageSegmentationModelExecutor(
49-
context: Context,
50-
private var useGPU: Boolean = false
51-
) {
47+
class ImageSegmentationModelExecutor(context: Context, private var useGPU: Boolean = false) {
5248
private var gpuDelegate: GpuDelegate? = null
5349

5450
private val segmentationMasks: ByteBuffer
@@ -73,20 +69,10 @@ class ImageSegmentationModelExecutor(
7369
fullTimeExecutionTime = SystemClock.uptimeMillis()
7470

7571
preprocessTime = SystemClock.uptimeMillis()
76-
val scaledBitmap =
77-
ImageUtils.scaleBitmapAndKeepRatio(
78-
data,
79-
imageSize, imageSize
80-
)
72+
val scaledBitmap = ImageUtils.scaleBitmapAndKeepRatio(data, imageSize, imageSize)
8173

8274
val contentArray =
83-
ImageUtils.bitmapToByteBuffer(
84-
scaledBitmap,
85-
imageSize,
86-
imageSize,
87-
IMAGE_MEAN,
88-
IMAGE_STD
89-
)
75+
ImageUtils.bitmapToByteBuffer(scaledBitmap, imageSize, imageSize, IMAGE_MEAN, IMAGE_STD)
9076
preprocessTime = SystemClock.uptimeMillis() - preprocessTime
9177

9278
imageSegmentationTime = SystemClock.uptimeMillis()
@@ -97,7 +83,10 @@ class ImageSegmentationModelExecutor(
9783
maskFlatteningTime = SystemClock.uptimeMillis()
9884
val (maskImageApplied, maskOnly, itemsFound) =
9985
convertBytebufferMaskToBitmap(
100-
segmentationMasks, imageSize, imageSize, scaledBitmap,
86+
segmentationMasks,
87+
imageSize,
88+
imageSize,
89+
scaledBitmap,
10190
segmentColors
10291
)
10392
maskFlatteningTime = SystemClock.uptimeMillis() - maskFlatteningTime
@@ -117,11 +106,7 @@ class ImageSegmentationModelExecutor(
117106
val exceptionLog = "something went wrong: ${e.message}"
118107
Log.d(TAG, exceptionLog)
119108

120-
val emptyBitmap =
121-
ImageUtils.createEmptyBitmap(
122-
imageSize,
123-
imageSize
124-
)
109+
val emptyBitmap = ImageUtils.createEmptyBitmap(imageSize, imageSize)
125110
return ModelExecutionResult(
126111
emptyBitmap,
127112
emptyBitmap,
@@ -132,7 +117,8 @@ class ImageSegmentationModelExecutor(
132117
}
133118
}
134119

135-
// base: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/java/demo/app/src/main/java/com/example/android/tflitecamerademo/ImageClassifier.java
120+
// base:
121+
// https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/java/demo/app/src/main/java/com/example/android/tflitecamerademo/ImageClassifier.java
136122
@Throws(IOException::class)
137123
private fun loadModelFile(context: Context, modelFile: String): MappedByteBuffer {
138124
val fileDescriptor = context.assets.openFd(modelFile)
@@ -193,11 +179,7 @@ class ImageSegmentationModelExecutor(
193179
val maskBitmap = Bitmap.createBitmap(imageWidth, imageHeight, conf)
194180
val resultBitmap = Bitmap.createBitmap(imageWidth, imageHeight, conf)
195181
val scaledBackgroundImage =
196-
ImageUtils.scaleBitmapAndKeepRatio(
197-
backgroundImage,
198-
imageWidth,
199-
imageHeight
200-
)
182+
ImageUtils.scaleBitmapAndKeepRatio(backgroundImage, imageWidth, imageHeight)
201183
val mSegmentBits = Array(imageWidth) { IntArray(imageHeight) }
202184
val itemsFound = HashMap<String, Int>()
203185
inputBuffer.rewind()
@@ -208,8 +190,7 @@ class ImageSegmentationModelExecutor(
208190
mSegmentBits[x][y] = 0
209191

210192
for (c in 0 until NUM_CLASSES) {
211-
val value = inputBuffer
212-
.getFloat((y * imageWidth * NUM_CLASSES + x * NUM_CLASSES + c) * 4)
193+
val value = inputBuffer.getFloat((y * imageWidth * NUM_CLASSES + x * NUM_CLASSES + c) * 4)
213194
if (c == 0 || value > maxVal) {
214195
maxVal = value
215196
mSegmentBits[x][y] = c
@@ -218,10 +199,11 @@ class ImageSegmentationModelExecutor(
218199
val label = labelsArrays[mSegmentBits[x][y]]
219200
val color = colors[mSegmentBits[x][y]]
220201
itemsFound.put(label, color)
221-
val newPixelColor = ColorUtils.compositeColors(
222-
colors[mSegmentBits[x][y]],
223-
scaledBackgroundImage.getPixel(x, y)
224-
)
202+
val newPixelColor =
203+
ColorUtils.compositeColors(
204+
colors[mSegmentBits[x][y]],
205+
scaledBackgroundImage.getPixel(x, y)
206+
)
225207
resultBitmap.setPixel(x, y, newPixelColor)
226208
maskBitmap.setPixel(x, y, colors[mSegmentBits[x][y]])
227209
}
@@ -240,29 +222,43 @@ class ImageSegmentationModelExecutor(
240222
private const val IMAGE_STD = 127.5f
241223

242224
val segmentColors = IntArray(NUM_CLASSES)
243-
val labelsArrays = arrayOf(
244-
"background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus",
245-
"car", "cat", "chair", "cow", "dining table", "dog", "horse", "motorbike",
246-
"person", "potted plant", "sheep", "sofa", "train", "tv"
247-
)
225+
val labelsArrays =
226+
arrayOf(
227+
"background",
228+
"aeroplane",
229+
"bicycle",
230+
"bird",
231+
"boat",
232+
"bottle",
233+
"bus",
234+
"car",
235+
"cat",
236+
"chair",
237+
"cow",
238+
"dining table",
239+
"dog",
240+
"horse",
241+
"motorbike",
242+
"person",
243+
"potted plant",
244+
"sheep",
245+
"sofa",
246+
"train",
247+
"tv"
248+
)
248249

249250
init {
250251

251252
val random = Random(System.currentTimeMillis())
252253
segmentColors[0] = Color.TRANSPARENT
253254
for (i in 1 until NUM_CLASSES) {
254-
segmentColors[i] = Color.argb(
255-
(128),
256-
getRandomRGBInt(
257-
random
258-
),
259-
getRandomRGBInt(
260-
random
261-
),
262-
getRandomRGBInt(
263-
random
255+
segmentColors[i] =
256+
Color.argb(
257+
(128),
258+
getRandomRGBInt(random),
259+
getRandomRGBInt(random),
260+
getRandomRGBInt(random)
264261
)
265-
)
266262
}
267263
}
268264

lite/examples/posenet/android/app/src/main/java/org/tensorflow/lite/examples/posenet/TestActivity.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ import android.graphics.Color
2222
import android.graphics.Paint
2323
import android.graphics.drawable.Drawable
2424
import android.os.Bundle
25-
import androidx.core.content.res.ResourcesCompat
2625
import androidx.appcompat.app.AppCompatActivity
2726
import android.widget.ImageView
27+
import androidx.core.content.res.ResourcesCompat
2828
import org.tensorflow.lite.examples.posenet.lib.Posenet as Posenet
2929

3030
class TestActivity : AppCompatActivity() {
31-
/** Returns a resized bitmap of the drawable image. */
31+
/** Returns a resized bitmap of the drawable image. */
3232
private fun drawableToBitmap(drawable: Drawable): Bitmap {
3333
val bitmap = Bitmap.createBitmap(257, 257, Bitmap.Config.ARGB_8888)
3434
val canvas = Canvas(bitmap)
@@ -39,7 +39,7 @@ class TestActivity : AppCompatActivity() {
3939
return bitmap
4040
}
4141

42-
/** Calls the Posenet library functions. */
42+
/** Calls the Posenet library functions. */
4343
override fun onCreate(savedInstanceState: Bundle?) {
4444
super.onCreate(savedInstanceState)
4545
setContentView(R.layout.tfe_pn_activity_test)
@@ -59,10 +59,7 @@ class TestActivity : AppCompatActivity() {
5959
val mutableBitmap = imageBitmap.copy(Bitmap.Config.ARGB_8888, true)
6060
val canvas = Canvas(mutableBitmap)
6161
for (keypoint in person.keyPoints) {
62-
canvas.drawCircle(
63-
keypoint.position.x.toFloat(),
64-
keypoint.position.y.toFloat(), size, paint
65-
)
62+
canvas.drawCircle(keypoint.position.x.toFloat(), keypoint.position.y.toFloat(), size, paint)
6663
}
6764
sampleImageView.adjustViewBounds = true
6865
sampleImageView.setImageBitmap(mutableBitmap)

0 commit comments

Comments
 (0)