Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
王雷
/
detection
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit e08d779b
authored
Apr 29, 2024
by
wanglei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新读取Assets文件方法
1 parent
00e7d0f7
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
169 additions
and
143 deletions
app/src/main/java/com/agenew/detection/env/Utils.java
app/src/main/java/com/agenew/detection/env/Utils.java
View file @
e08d779
...
...
@@ -19,38 +19,69 @@ import java.nio.MappedByteBuffer;
import
java.nio.channels.FileChannel
;
public
class
Utils
{
/**
* Memory-map the model file in Assets.
*/
public
static
MappedByteBuffer
loadModelFile
(
AssetManager
assets
,
String
modelFilename
)
throws
IOException
{
AssetFileDescriptor
fileDescriptor
=
assets
.
openFd
(
modelFilename
);
FileInputStream
inputStream
=
new
FileInputStream
(
fileDescriptor
.
getFileDescriptor
());
FileChannel
fileChannel
=
inputStream
.
getChannel
();
long
startOffset
=
fileDescriptor
.
getStartOffset
();
long
declaredLength
=
fileDescriptor
.
getDeclaredLength
();
return
fileChannel
.
map
(
FileChannel
.
MapMode
.
READ_ONLY
,
startOffset
,
declaredLength
);
}
public
static
void
softmax
(
final
float
[]
vals
)
{
float
max
=
Float
.
NEGATIVE_INFINITY
;
for
(
final
float
val
:
vals
)
{
max
=
Math
.
max
(
max
,
val
);
}
float
sum
=
0.0f
;
for
(
int
i
=
0
;
i
<
vals
.
length
;
++
i
)
{
vals
[
i
]
=
(
float
)
Math
.
exp
(
vals
[
i
]
-
max
);
sum
+=
vals
[
i
];
}
for
(
int
i
=
0
;
i
<
vals
.
length
;
++
i
)
{
vals
[
i
]
=
vals
[
i
]
/
sum
;
}
}
public
static
float
expit
(
final
float
x
)
{
return
(
float
)
(
1
.
/
(
1
.
+
Math
.
exp
(-
x
)));
}
private
static
final
String
TAG
=
"Utils"
;
/**
* Memory-map the model file in Assets.
*/
public
static
MappedByteBuffer
loadModelFile
(
AssetManager
assets
,
String
filePath
)
throws
IOException
{
AssetFileDescriptor
fileDescriptor
=
assets
.
openFd
(
filePath
);
MappedByteBuffer
result
=
null
;
try
{
FileInputStream
inputStream
=
new
FileInputStream
(
fileDescriptor
.
getFileDescriptor
());
try
{
FileChannel
fileChannel
=
inputStream
.
getChannel
();
long
startOffset
=
fileDescriptor
.
getStartOffset
();
long
declaredLength
=
fileDescriptor
.
getDeclaredLength
();
result
=
fileChannel
.
map
(
FileChannel
.
MapMode
.
READ_ONLY
,
startOffset
,
declaredLength
);
}
catch
(
Throwable
e
)
{
try
{
inputStream
.
close
();
}
catch
(
Throwable
e1
)
{
e
.
addSuppressed
(
e1
);
}
throw
e
;
}
finally
{
inputStream
.
close
();
}
}
catch
(
Throwable
e
)
{
if
(
fileDescriptor
!=
null
)
{
try
{
fileDescriptor
.
close
();
}
catch
(
Throwable
e1
)
{
e
.
addSuppressed
(
e1
);
}
}
Log
.
e
(
TAG
,
"WL_DEBUG loadMappedFile e = "
+
e
,
e
);
}
finally
{
if
(
fileDescriptor
!=
null
)
{
fileDescriptor
.
close
();
}
}
return
result
;
}
public
static
void
softmax
(
final
float
[]
vals
)
{
float
max
=
Float
.
NEGATIVE_INFINITY
;
for
(
final
float
val
:
vals
)
{
max
=
Math
.
max
(
max
,
val
);
}
float
sum
=
0.0f
;
for
(
int
i
=
0
;
i
<
vals
.
length
;
++
i
)
{
vals
[
i
]
=
(
float
)
Math
.
exp
(
vals
[
i
]
-
max
);
sum
+=
vals
[
i
];
}
for
(
int
i
=
0
;
i
<
vals
.
length
;
++
i
)
{
vals
[
i
]
=
vals
[
i
]
/
sum
;
}
}
public
static
float
expit
(
final
float
x
)
{
return
(
float
)
(
1
.
/
(
1
.
+
Math
.
exp
(-
x
)));
}
// public static Bitmap scale(Context context, String filePath) {
// AssetManager assetManager = context.getAssets();
...
...
@@ -69,117 +100,112 @@ public class Utils {
// return bitmap;
// }
public
static
Bitmap
getBitmapFromAsset
(
Context
context
,
String
filePath
)
{
AssetManager
assetManager
=
context
.
getAssets
();
public
static
Bitmap
getBitmapFromAsset
(
Context
context
,
String
filePath
)
{
AssetManager
assetManager
=
context
.
getAssets
();
InputStream
istr
;
Bitmap
bitmap
=
null
;
try
{
istr
=
assetManager
.
open
(
filePath
);
bitmap
=
BitmapFactory
.
decodeStream
(
istr
);
InputStream
istr
;
Bitmap
bitmap
=
null
;
try
{
istr
=
assetManager
.
open
(
filePath
);
bitmap
=
BitmapFactory
.
decodeStream
(
istr
);
// return bitmap.copy(Bitmap.Config.ARGB_8888,true);
}
catch
(
IOException
e
)
{
// handle exception
Log
.
e
(
"getBitmapFromAsset"
,
"getBitmapFromAsset: "
+
e
.
getMessage
());
}
return
bitmap
;
}
/**
* Returns a transformation matrix from one reference frame into another.
* Handles cropping (if maintaining aspect ratio is desired) and rotation.
*
* @param srcWidth Width of source frame.
* @param srcHeight Height of source frame.
* @param dstWidth Width of destination frame.
* @param dstHeight Height of destination frame.
* @param applyRotation Amount of rotation to apply from one frame to another.
* Must be a multiple of 90.
* @param maintainAspectRatio If true, will ensure that scaling in x and y remains constant,
* cropping the image if necessary.
* @return The transformation fulfilling the desired requirements.
*/
public
static
Matrix
getTransformationMatrix
(
final
int
srcWidth
,
final
int
srcHeight
,
final
int
dstWidth
,
final
int
dstHeight
,
final
int
applyRotation
,
final
boolean
maintainAspectRatio
)
{
final
Matrix
matrix
=
new
Matrix
();
if
(
applyRotation
!=
0
)
{
// Translate so center of image is at origin.
matrix
.
postTranslate
(-
srcWidth
/
2.0f
,
-
srcHeight
/
2.0f
);
// Rotate around origin.
matrix
.
postRotate
(
applyRotation
);
}
// Account for the already applied rotation, if any, and then determine how
// much scaling is needed for each axis.
final
boolean
transpose
=
(
Math
.
abs
(
applyRotation
)
+
90
)
%
180
==
0
;
final
int
inWidth
=
transpose
?
srcHeight
:
srcWidth
;
final
int
inHeight
=
transpose
?
srcWidth
:
srcHeight
;
// Apply scaling if necessary.
if
(
inWidth
!=
dstWidth
||
inHeight
!=
dstHeight
)
{
final
float
scaleFactorX
=
dstWidth
/
(
float
)
inWidth
;
final
float
scaleFactorY
=
dstHeight
/
(
float
)
inHeight
;
if
(
maintainAspectRatio
)
{
// Scale by minimum factor so that dst is filled completely while
// maintaining the aspect ratio. Some image may fall off the edge.
final
float
scaleFactor
=
Math
.
max
(
scaleFactorX
,
scaleFactorY
);
matrix
.
postScale
(
scaleFactor
,
scaleFactor
);
}
else
{
// Scale exactly to fill dst from src.
matrix
.
postScale
(
scaleFactorX
,
scaleFactorY
);
}
}
if
(
applyRotation
!=
0
)
{
// Translate back from origin centered reference to destination frame.
matrix
.
postTranslate
(
dstWidth
/
2.0f
,
dstHeight
/
2.0f
);
}
return
matrix
;
}
public
static
Bitmap
processBitmap
(
Bitmap
source
,
int
size
){
int
image_height
=
source
.
getHeight
();
int
image_width
=
source
.
getWidth
();
Bitmap
croppedBitmap
=
Bitmap
.
createBitmap
(
size
,
size
,
Bitmap
.
Config
.
ARGB_8888
);
Matrix
frameToCropTransformations
=
getTransformationMatrix
(
image_width
,
image_height
,
size
,
size
,
0
,
false
);
Matrix
cropToFrameTransformations
=
new
Matrix
();
frameToCropTransformations
.
invert
(
cropToFrameTransformations
);
final
Canvas
canvas
=
new
Canvas
(
croppedBitmap
);
canvas
.
drawBitmap
(
source
,
frameToCropTransformations
,
null
);
return
croppedBitmap
;
}
public
static
void
writeToFile
(
String
data
,
Context
context
)
{
try
{
String
baseDir
=
Environment
.
getExternalStorageDirectory
().
getAbsolutePath
();
String
fileName
=
"myFile.txt"
;
File
file
=
new
File
(
baseDir
+
File
.
separator
+
fileName
);
FileOutputStream
stream
=
new
FileOutputStream
(
file
);
try
{
stream
.
write
(
data
.
getBytes
());
}
finally
{
stream
.
close
();
}
}
catch
(
IOException
e
)
{
Log
.
e
(
"Exception"
,
"File write failed: "
+
e
.
toString
());
}
}
}
catch
(
IOException
e
)
{
// handle exception
Log
.
e
(
"getBitmapFromAsset"
,
"getBitmapFromAsset: "
+
e
.
getMessage
());
}
return
bitmap
;
}
/**
* Returns a transformation matrix from one reference frame into another.
* Handles cropping (if maintaining aspect ratio is desired) and rotation.
*
* @param srcWidth Width of source frame.
* @param srcHeight Height of source frame.
* @param dstWidth Width of destination frame.
* @param dstHeight Height of destination frame.
* @param applyRotation Amount of rotation to apply from one frame to
* another. Must be a multiple of 90.
* @param maintainAspectRatio If true, will ensure that scaling in x and y
* remains constant, cropping the image if necessary.
* @return The transformation fulfilling the desired requirements.
*/
public
static
Matrix
getTransformationMatrix
(
final
int
srcWidth
,
final
int
srcHeight
,
final
int
dstWidth
,
final
int
dstHeight
,
final
int
applyRotation
,
final
boolean
maintainAspectRatio
)
{
final
Matrix
matrix
=
new
Matrix
();
if
(
applyRotation
!=
0
)
{
// Translate so center of image is at origin.
matrix
.
postTranslate
(-
srcWidth
/
2.0f
,
-
srcHeight
/
2.0f
);
// Rotate around origin.
matrix
.
postRotate
(
applyRotation
);
}
// Account for the already applied rotation, if any, and then determine how
// much scaling is needed for each axis.
final
boolean
transpose
=
(
Math
.
abs
(
applyRotation
)
+
90
)
%
180
==
0
;
final
int
inWidth
=
transpose
?
srcHeight
:
srcWidth
;
final
int
inHeight
=
transpose
?
srcWidth
:
srcHeight
;
// Apply scaling if necessary.
if
(
inWidth
!=
dstWidth
||
inHeight
!=
dstHeight
)
{
final
float
scaleFactorX
=
dstWidth
/
(
float
)
inWidth
;
final
float
scaleFactorY
=
dstHeight
/
(
float
)
inHeight
;
if
(
maintainAspectRatio
)
{
// Scale by minimum factor so that dst is filled completely while
// maintaining the aspect ratio. Some image may fall off the edge.
final
float
scaleFactor
=
Math
.
max
(
scaleFactorX
,
scaleFactorY
);
matrix
.
postScale
(
scaleFactor
,
scaleFactor
);
}
else
{
// Scale exactly to fill dst from src.
matrix
.
postScale
(
scaleFactorX
,
scaleFactorY
);
}
}
if
(
applyRotation
!=
0
)
{
// Translate back from origin centered reference to destination frame.
matrix
.
postTranslate
(
dstWidth
/
2.0f
,
dstHeight
/
2.0f
);
}
return
matrix
;
}
public
static
Bitmap
processBitmap
(
Bitmap
source
,
int
size
)
{
int
image_height
=
source
.
getHeight
();
int
image_width
=
source
.
getWidth
();
Bitmap
croppedBitmap
=
Bitmap
.
createBitmap
(
size
,
size
,
Bitmap
.
Config
.
ARGB_8888
);
Matrix
frameToCropTransformations
=
getTransformationMatrix
(
image_width
,
image_height
,
size
,
size
,
0
,
false
);
Matrix
cropToFrameTransformations
=
new
Matrix
();
frameToCropTransformations
.
invert
(
cropToFrameTransformations
);
final
Canvas
canvas
=
new
Canvas
(
croppedBitmap
);
canvas
.
drawBitmap
(
source
,
frameToCropTransformations
,
null
);
return
croppedBitmap
;
}
public
static
void
writeToFile
(
String
data
,
Context
context
)
{
try
{
String
baseDir
=
Environment
.
getExternalStorageDirectory
().
getAbsolutePath
();
String
fileName
=
"myFile.txt"
;
File
file
=
new
File
(
baseDir
+
File
.
separator
+
fileName
);
FileOutputStream
stream
=
new
FileOutputStream
(
file
);
try
{
stream
.
write
(
data
.
getBytes
());
}
finally
{
stream
.
close
();
}
}
catch
(
IOException
e
)
{
Log
.
e
(
"Exception"
,
"File write failed: "
+
e
.
toString
());
}
}
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment