@@ -16,34 +16,41 @@ import java.nio.FloatBuffer
16
16
* [android.media.Image] source.
17
17
*/
18
18
object TensorImageUtils {
19
- @JvmField
20
- var TORCHVISION_NORM_MEAN_RGB : FloatArray = floatArrayOf(0.485f , 0.456f , 0.406f )
21
- @JvmField
22
- var TORCHVISION_NORM_STD_RGB : FloatArray = floatArrayOf(0.229f , 0.224f , 0.225f )
19
+ @JvmField var TORCHVISION_NORM_MEAN_RGB : FloatArray = floatArrayOf(0.485f , 0.456f , 0.406f )
20
+
21
+ @JvmField var TORCHVISION_NORM_STD_RGB : FloatArray = floatArrayOf(0.229f , 0.224f , 0.225f )
23
22
24
23
/* *
25
- * Creates new [Tensor] from full [android.graphics.Bitmap], normalized with specified
26
- * in parameters mean and std.
24
+ * Creates new [Tensor] from full [android.graphics.Bitmap], normalized with specified in
25
+ * parameters mean and std.
27
26
*
28
27
* @param normMeanRGB means for RGB channels normalization, length must equal 3, RGB order
29
28
* @param normStdRGB standard deviation for RGB channels normalization, length must equal 3, RGB
30
- * order
29
+ * order
31
30
*/
32
31
@JvmStatic
33
32
fun bitmapToFloat32Tensor (
34
- bitmap : Bitmap , normMeanRGB : FloatArray , normStdRGB : FloatArray
33
+ bitmap : Bitmap ,
34
+ normMeanRGB : FloatArray ,
35
+ normStdRGB : FloatArray ,
35
36
): Tensor {
36
37
checkNormMeanArg(normMeanRGB)
37
38
checkNormStdArg(normStdRGB)
38
39
39
40
return bitmapToFloat32Tensor(
40
- bitmap, 0 , 0 , bitmap.width, bitmap.height, normMeanRGB, normStdRGB
41
+ bitmap,
42
+ 0 ,
43
+ 0 ,
44
+ bitmap.width,
45
+ bitmap.height,
46
+ normMeanRGB,
47
+ normStdRGB,
41
48
)
42
49
}
43
50
44
51
/* *
45
- * Writes tensor content from specified [android.graphics.Bitmap], normalized with specified
46
- * in parameters mean and std to specified [java.nio.FloatBuffer] with specified offset.
52
+ * Writes tensor content from specified [android.graphics.Bitmap], normalized with specified in
53
+ * parameters mean and std to specified [java.nio.FloatBuffer] with specified offset.
47
54
*
48
55
* @param bitmap [android.graphics.Bitmap] as a source for Tensor data
49
56
* @param x - x coordinate of top left corner of bitmap's area
@@ -52,7 +59,7 @@ object TensorImageUtils {
52
59
* @param height - height of bitmap's area
53
60
* @param normMeanRGB means for RGB channels normalization, length must equal 3, RGB order
54
61
* @param normStdRGB standard deviation for RGB channels normalization, length must equal 3, RGB
55
- * order
62
+ * order
56
63
*/
57
64
fun bitmapToFloatBuffer (
58
65
bitmap : Bitmap ,
@@ -63,7 +70,7 @@ object TensorImageUtils {
63
70
normMeanRGB : FloatArray ,
64
71
normStdRGB : FloatArray ,
65
72
outBuffer : FloatBuffer ,
66
- outBufferOffset : Int
73
+ outBufferOffset : Int ,
67
74
) {
68
75
checkOutBufferCapacity(outBuffer, outBufferOffset, width, height)
69
76
checkNormMeanArg(normMeanRGB)
@@ -88,8 +95,8 @@ object TensorImageUtils {
88
95
}
89
96
90
97
/* *
91
- * Creates new [Tensor] from specified area of [android.graphics.Bitmap], normalized
92
- * with specified in parameters mean and std.
98
+ * Creates new [Tensor] from specified area of [android.graphics.Bitmap], normalized with
99
+ * specified in parameters mean and std.
93
100
*
94
101
* @param bitmap [android.graphics.Bitmap] as a source for Tensor data
95
102
* @param x - x coordinate of top left corner of bitmap's area
@@ -98,7 +105,7 @@ object TensorImageUtils {
98
105
* @param height - height of bitmap's area
99
106
* @param normMeanRGB means for RGB channels normalization, length must equal 3, RGB order
100
107
* @param normStdRGB standard deviation for RGB channels normalization, length must equal 3, RGB
101
- * order
108
+ * order
102
109
*/
103
110
fun bitmapToFloat32Tensor (
104
111
bitmap : Bitmap ,
@@ -107,7 +114,7 @@ object TensorImageUtils {
107
114
width : Int ,
108
115
height : Int ,
109
116
normMeanRGB : FloatArray ,
110
- normStdRGB : FloatArray
117
+ normStdRGB : FloatArray ,
111
118
): Tensor {
112
119
checkNormMeanArg(normMeanRGB)
113
120
checkNormStdArg(normStdRGB)
@@ -118,17 +125,31 @@ object TensorImageUtils {
118
125
}
119
126
120
127
private fun checkOutBufferCapacity (
121
- outBuffer : FloatBuffer , outBufferOffset : Int , tensorWidth : Int , tensorHeight : Int
128
+ outBuffer : FloatBuffer ,
129
+ outBufferOffset : Int ,
130
+ tensorWidth : Int ,
131
+ tensorHeight : Int ,
122
132
) {
123
- check(outBufferOffset + 3 * tensorWidth * tensorHeight <= outBuffer.capacity()) { " Buffer underflow" }
133
+ check(outBufferOffset + 3 * tensorWidth * tensorHeight <= outBuffer.capacity()) {
134
+ " Buffer underflow"
135
+ }
124
136
}
125
137
126
138
private fun checkTensorSize (tensorWidth : Int , tensorHeight : Int ) {
127
- require(! (tensorHeight <= 0 || tensorWidth <= 0 )) { " tensorHeight and tensorWidth must be positive" }
139
+ require(! (tensorHeight <= 0 || tensorWidth <= 0 )) {
140
+ " tensorHeight and tensorWidth must be positive"
141
+ }
128
142
}
129
143
130
144
private fun checkRotateCWDegrees (rotateCWDegrees : Int ) {
131
- require(! (rotateCWDegrees != 0 && rotateCWDegrees != 90 && rotateCWDegrees != 180 && rotateCWDegrees != 270 )) { " rotateCWDegrees must be one of 0, 90, 180, 270" }
145
+ require(
146
+ ! (rotateCWDegrees != 0 &&
147
+ rotateCWDegrees != 90 &&
148
+ rotateCWDegrees != 180 &&
149
+ rotateCWDegrees != 270 )
150
+ ) {
151
+ " rotateCWDegrees must be one of 0, 90, 180, 270"
152
+ }
132
153
}
133
154
134
155
private fun checkNormStdArg (normStdRGB : FloatArray ) {
0 commit comments