Skip to content

Commit

Permalink
Add OpenCV to the project and convert bitmap to ByteBuffer with this …
Browse files Browse the repository at this point in the history
…library.
  • Loading branch information
farmaker47 committed Nov 2, 2021
1 parent 38aaf2e commit 6294d75
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ class ImageSegmentationModelExecutor(
imageSize, imageSize
)

/*val contentArray =
val contentArray =
ImageUtils.bitmapToByteBuffer(
scaledBitmap,
imageSize,
imageSize,
IMAGE_MEAN,
IMAGE_STD
)*/
)

val contentArray = ImageUtils.convertBitmapToByteBuffer(scaledBitmap)
//val contentArray = ImageUtils.bitmapToBytebufferWithOpenCV(scaledBitmap)
preprocessTime = SystemClock.uptimeMillis() - preprocessTime

imageSegmentationTime = SystemClock.uptimeMillis()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import android.os.SystemClock
import org.opencv.android.Utils

import org.opencv.core.Core

import org.opencv.core.CvType.CV_32F
import org.opencv.core.CvType.*

import org.opencv.core.Mat

Expand Down Expand Up @@ -169,8 +168,8 @@ abstract class ImageUtils {
mean: Float = 0.0f,
std: Float = 255.0f
): ByteBuffer {
val startTime = SystemClock.uptimeMillis()
val bitmap = scaleBitmapAndKeepRatio(bitmapIn, width, height)
val startTime = SystemClock.uptimeMillis()
val inputImage = ByteBuffer.allocateDirect(1 * width * height * 3 * 4)
inputImage.order(ByteOrder.nativeOrder())
inputImage.rewind()
Expand All @@ -193,12 +192,12 @@ abstract class ImageUtils {

inputImage.rewind()
val endTime = SystemClock.uptimeMillis()
Log.v("BitWise", (endTime-startTime).toString())
Log.v("Bitwise", (endTime - startTime).toString())
return inputImage
}

/** Writes Image data into a `ByteBuffer`. */
fun convertBitmapToByteBuffer(bitmap: Bitmap): ByteBuffer {
fun bitmapToBytebufferWithOpenCV(bitmap: Bitmap): ByteBuffer {
val startTime = SystemClock.uptimeMillis()
val imgData = ByteBuffer.allocateDirect(1 * 257 * 257 * 3 * 4)
imgData.order(ByteOrder.nativeOrder())
Expand All @@ -218,12 +217,12 @@ abstract class ImageUtils {
val buf = FloatArray(257 * 257 * 3)
newmat.get(0, 0, buf)

for (i in buf.indices){
imgData.putFloat(buf[i])
for (i in buf.indices) {
imgData.putFloat(buf[i])
}
imgData.rewind()
val endTime = SystemClock.uptimeMillis()
Log.v("BitWise", (endTime-startTime).toString())
Log.v("Bitwise", (endTime - startTime).toString())
return imgData
}

Expand Down

0 comments on commit 6294d75

Please sign in to comment.