-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
1,434 additions
and
22 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
.../io/getstream/video/flutter/stream_video_flutter/videoFilters/common/BitmapVideoFilter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.getstream.video.flutter.stream_video_flutter.videoFilters.common | ||
|
||
import android.graphics.Bitmap | ||
|
||
/** | ||
* A filter that provides a Bitmap of each frame. It's less performant than using the | ||
* RawVideoFilter because we do YUV<->ARGB conversions internally. | ||
*/ | ||
abstract class BitmapVideoFilter { | ||
abstract fun applyFilter(videoFrameBitmap: Bitmap) | ||
} |
67 changes: 67 additions & 0 deletions
67
...kotlin/io/getstream/video/flutter/stream_video_flutter/videoFilters/common/FilterUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package io.getstream.video.flutter.stream_video_flutter.videoFilters.common | ||
|
||
import android.graphics.Bitmap | ||
import android.graphics.Matrix | ||
import com.google.mlkit.vision.segmentation.SegmentationMask | ||
|
||
internal fun copySegment( | ||
segment: Segment, | ||
source: Bitmap, | ||
destination: Bitmap, | ||
segmentationMask: SegmentationMask, | ||
confidenceThreshold: Double, | ||
) { | ||
val scaleBetweenSourceAndMask = getScalingFactors( | ||
widths = Pair(source.width, segmentationMask.width), | ||
heights = Pair(source.height, segmentationMask.height), | ||
) | ||
|
||
segmentationMask.buffer.rewind() | ||
|
||
val sourcePixels = IntArray(source.width * source.height) | ||
source.getPixels(sourcePixels, 0, source.width, 0, 0, source.width, source.height) | ||
val destinationPixels = IntArray(destination.width * destination.height) | ||
|
||
for (y in 0 until segmentationMask.height) { | ||
for (x in 0 until segmentationMask.width) { | ||
val confidence = segmentationMask.buffer.float | ||
|
||
if (((segment == Segment.BACKGROUND) && confidence < confidenceThreshold) || | ||
((segment == Segment.FOREGROUND) && confidence >= confidenceThreshold) | ||
) { | ||
val scaledX = (x * scaleBetweenSourceAndMask.first).toInt() | ||
val scaledY = (y * scaleBetweenSourceAndMask.second).toInt() | ||
destinationPixels[y * destination.width + x] = | ||
sourcePixels[scaledY * source.width + scaledX] | ||
} | ||
} | ||
} | ||
|
||
destination.setPixels( | ||
destinationPixels, | ||
0, | ||
destination.width, | ||
0, | ||
0, | ||
destination.width, | ||
destination.height, | ||
) | ||
} | ||
|
||
internal enum class Segment { | ||
FOREGROUND, BACKGROUND | ||
} | ||
|
||
private fun getScalingFactors(widths: Pair<Int, Int>, heights: Pair<Int, Int>) = | ||
Pair(widths.first.toFloat() / widths.second, heights.first.toFloat() / heights.second) | ||
|
||
internal fun newSegmentationMaskMatrix(bitmap: Bitmap, mask: SegmentationMask): Matrix { | ||
val isRawSizeMaskEnabled = mask.width != bitmap.width || mask.height != bitmap.height | ||
return if (!isRawSizeMaskEnabled) { | ||
Matrix() | ||
} else { | ||
val scale = | ||
getScalingFactors(Pair(bitmap.width, mask.width), Pair(bitmap.height, mask.height)) | ||
Matrix().apply { preScale(scale.first, scale.second) } | ||
} | ||
} |
Oops, something went wrong.