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;
...
@@ -19,38 +19,69 @@ import java.nio.MappedByteBuffer;
import
java.nio.channels.FileChannel
;
import
java.nio.channels.FileChannel
;
public
class
Utils
{
public
class
Utils
{
private
static
final
String
TAG
=
"Utils"
;
/**
* Memory-map the model file in Assets.
/**
*/
* Memory-map the model file in Assets.
public
static
MappedByteBuffer
loadModelFile
(
AssetManager
assets
,
String
modelFilename
)
*/
throws
IOException
{
public
static
MappedByteBuffer
loadModelFile
(
AssetManager
assets
,
String
filePath
)
throws
IOException
{
AssetFileDescriptor
fileDescriptor
=
assets
.
openFd
(
modelFilename
);
AssetFileDescriptor
fileDescriptor
=
assets
.
openFd
(
filePath
);
FileInputStream
inputStream
=
new
FileInputStream
(
fileDescriptor
.
getFileDescriptor
());
MappedByteBuffer
result
=
null
;
FileChannel
fileChannel
=
inputStream
.
getChannel
();
try
{
long
startOffset
=
fileDescriptor
.
getStartOffset
();
FileInputStream
inputStream
=
new
FileInputStream
(
fileDescriptor
.
getFileDescriptor
());
long
declaredLength
=
fileDescriptor
.
getDeclaredLength
();
return
fileChannel
.
map
(
FileChannel
.
MapMode
.
READ_ONLY
,
startOffset
,
declaredLength
);
try
{
}
FileChannel
fileChannel
=
inputStream
.
getChannel
();
long
startOffset
=
fileDescriptor
.
getStartOffset
();
public
static
void
softmax
(
final
float
[]
vals
)
{
long
declaredLength
=
fileDescriptor
.
getDeclaredLength
();
float
max
=
Float
.
NEGATIVE_INFINITY
;
result
=
fileChannel
.
map
(
FileChannel
.
MapMode
.
READ_ONLY
,
startOffset
,
declaredLength
);
for
(
final
float
val
:
vals
)
{
}
catch
(
Throwable
e
)
{
max
=
Math
.
max
(
max
,
val
);
try
{
}
inputStream
.
close
();
float
sum
=
0.0f
;
}
catch
(
Throwable
e1
)
{
for
(
int
i
=
0
;
i
<
vals
.
length
;
++
i
)
{
e
.
addSuppressed
(
e1
);
vals
[
i
]
=
(
float
)
Math
.
exp
(
vals
[
i
]
-
max
);
}
sum
+=
vals
[
i
];
}
throw
e
;
for
(
int
i
=
0
;
i
<
vals
.
length
;
++
i
)
{
}
finally
{
vals
[
i
]
=
vals
[
i
]
/
sum
;
inputStream
.
close
();
}
}
}
}
catch
(
Throwable
e
)
{
if
(
fileDescriptor
!=
null
)
{
public
static
float
expit
(
final
float
x
)
{
try
{
return
(
float
)
(
1
.
/
(
1
.
+
Math
.
exp
(-
x
)));
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) {
// public static Bitmap scale(Context context, String filePath) {
// AssetManager assetManager = context.getAssets();
// AssetManager assetManager = context.getAssets();
...
@@ -69,117 +100,112 @@ public class Utils {
...
@@ -69,117 +100,112 @@ public class Utils {
// return bitmap;
// return bitmap;
// }
// }
public
static
Bitmap
getBitmapFromAsset
(
Context
context
,
String
filePath
)
{
public
static
Bitmap
getBitmapFromAsset
(
Context
context
,
String
filePath
)
{
AssetManager
assetManager
=
context
.
getAssets
();
AssetManager
assetManager
=
context
.
getAssets
();
InputStream
istr
;
InputStream
istr
;
Bitmap
bitmap
=
null
;
Bitmap
bitmap
=
null
;
try
{
try
{
istr
=
assetManager
.
open
(
filePath
);
istr
=
assetManager
.
open
(
filePath
);
bitmap
=
BitmapFactory
.
decodeStream
(
istr
);
bitmap
=
BitmapFactory
.
decodeStream
(
istr
);
// return bitmap.copy(Bitmap.Config.ARGB_8888,true);
// return bitmap.copy(Bitmap.Config.ARGB_8888,true);
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
// handle exception
// handle exception
Log
.
e
(
"getBitmapFromAsset"
,
"getBitmapFromAsset: "
+
e
.
getMessage
());
Log
.
e
(
"getBitmapFromAsset"
,
"getBitmapFromAsset: "
+
e
.
getMessage
());
}
}
return
bitmap
;
return
bitmap
;
}
}
/**
/**
* Returns a transformation matrix from one reference frame into another.
* Returns a transformation matrix from one reference frame into another.
* Handles cropping (if maintaining aspect ratio is desired) and rotation.
* Handles cropping (if maintaining aspect ratio is desired) and rotation.
*
*
* @param srcWidth Width of source frame.
* @param srcWidth Width of source frame.
* @param srcHeight Height of source frame.
* @param srcHeight Height of source frame.
* @param dstWidth Width of destination frame.
* @param dstWidth Width of destination frame.
* @param dstHeight Height of destination frame.
* @param dstHeight Height of destination frame.
* @param applyRotation Amount of rotation to apply from one frame to another.
* @param applyRotation Amount of rotation to apply from one frame to
* Must be a multiple of 90.
* another. Must be a multiple of 90.
* @param maintainAspectRatio If true, will ensure that scaling in x and y remains constant,
* @param maintainAspectRatio If true, will ensure that scaling in x and y
* cropping the image if necessary.
* remains constant, cropping the image if necessary.
* @return The transformation fulfilling the desired requirements.
* @return The transformation fulfilling the desired requirements.
*/
*/
public
static
Matrix
getTransformationMatrix
(
public
static
Matrix
getTransformationMatrix
(
final
int
srcWidth
,
final
int
srcHeight
,
final
int
dstWidth
,
final
int
srcWidth
,
final
int
dstHeight
,
final
int
applyRotation
,
final
boolean
maintainAspectRatio
)
{
final
int
srcHeight
,
final
Matrix
matrix
=
new
Matrix
();
final
int
dstWidth
,
final
int
dstHeight
,
if
(
applyRotation
!=
0
)
{
final
int
applyRotation
,
// Translate so center of image is at origin.
final
boolean
maintainAspectRatio
)
{
matrix
.
postTranslate
(-
srcWidth
/
2.0f
,
-
srcHeight
/
2.0f
);
final
Matrix
matrix
=
new
Matrix
();
// Rotate around origin.
if
(
applyRotation
!=
0
)
{
matrix
.
postRotate
(
applyRotation
);
// Translate so center of image is at origin.
}
matrix
.
postTranslate
(-
srcWidth
/
2.0f
,
-
srcHeight
/
2.0f
);
// Account for the already applied rotation, if any, and then determine how
// Rotate around origin.
// much scaling is needed for each axis.
matrix
.
postRotate
(
applyRotation
);
final
boolean
transpose
=
(
Math
.
abs
(
applyRotation
)
+
90
)
%
180
==
0
;
}
final
int
inWidth
=
transpose
?
srcHeight
:
srcWidth
;
// Account for the already applied rotation, if any, and then determine how
final
int
inHeight
=
transpose
?
srcWidth
:
srcHeight
;
// much scaling is needed for each axis.
final
boolean
transpose
=
(
Math
.
abs
(
applyRotation
)
+
90
)
%
180
==
0
;
// Apply scaling if necessary.
if
(
inWidth
!=
dstWidth
||
inHeight
!=
dstHeight
)
{
final
int
inWidth
=
transpose
?
srcHeight
:
srcWidth
;
final
float
scaleFactorX
=
dstWidth
/
(
float
)
inWidth
;
final
int
inHeight
=
transpose
?
srcWidth
:
srcHeight
;
final
float
scaleFactorY
=
dstHeight
/
(
float
)
inHeight
;
// Apply scaling if necessary.
if
(
maintainAspectRatio
)
{
if
(
inWidth
!=
dstWidth
||
inHeight
!=
dstHeight
)
{
// Scale by minimum factor so that dst is filled completely while
final
float
scaleFactorX
=
dstWidth
/
(
float
)
inWidth
;
// maintaining the aspect ratio. Some image may fall off the edge.
final
float
scaleFactorY
=
dstHeight
/
(
float
)
inHeight
;
final
float
scaleFactor
=
Math
.
max
(
scaleFactorX
,
scaleFactorY
);
matrix
.
postScale
(
scaleFactor
,
scaleFactor
);
if
(
maintainAspectRatio
)
{
}
else
{
// Scale by minimum factor so that dst is filled completely while
// Scale exactly to fill dst from src.
// maintaining the aspect ratio. Some image may fall off the edge.
matrix
.
postScale
(
scaleFactorX
,
scaleFactorY
);
final
float
scaleFactor
=
Math
.
max
(
scaleFactorX
,
scaleFactorY
);
}
matrix
.
postScale
(
scaleFactor
,
scaleFactor
);
}
}
else
{
// Scale exactly to fill dst from src.
if
(
applyRotation
!=
0
)
{
matrix
.
postScale
(
scaleFactorX
,
scaleFactorY
);
// Translate back from origin centered reference to destination frame.
}
matrix
.
postTranslate
(
dstWidth
/
2.0f
,
dstHeight
/
2.0f
);
}
}
if
(
applyRotation
!=
0
)
{
return
matrix
;
// Translate back from origin centered reference to destination frame.
}
matrix
.
postTranslate
(
dstWidth
/
2.0f
,
dstHeight
/
2.0f
);
}
public
static
Bitmap
processBitmap
(
Bitmap
source
,
int
size
)
{
return
matrix
;
int
image_height
=
source
.
getHeight
();
}
int
image_width
=
source
.
getWidth
();
public
static
Bitmap
processBitmap
(
Bitmap
source
,
int
size
){
Bitmap
croppedBitmap
=
Bitmap
.
createBitmap
(
size
,
size
,
Bitmap
.
Config
.
ARGB_8888
);
int
image_height
=
source
.
getHeight
();
Matrix
frameToCropTransformations
=
getTransformationMatrix
(
image_width
,
image_height
,
size
,
size
,
0
,
false
);
int
image_width
=
source
.
getWidth
();
Matrix
cropToFrameTransformations
=
new
Matrix
();
frameToCropTransformations
.
invert
(
cropToFrameTransformations
);
Bitmap
croppedBitmap
=
Bitmap
.
createBitmap
(
size
,
size
,
Bitmap
.
Config
.
ARGB_8888
);
final
Canvas
canvas
=
new
Canvas
(
croppedBitmap
);
Matrix
frameToCropTransformations
=
getTransformationMatrix
(
image_width
,
image_height
,
size
,
size
,
0
,
false
);
canvas
.
drawBitmap
(
source
,
frameToCropTransformations
,
null
);
Matrix
cropToFrameTransformations
=
new
Matrix
();
frameToCropTransformations
.
invert
(
cropToFrameTransformations
);
return
croppedBitmap
;
}
final
Canvas
canvas
=
new
Canvas
(
croppedBitmap
);
canvas
.
drawBitmap
(
source
,
frameToCropTransformations
,
null
);
public
static
void
writeToFile
(
String
data
,
Context
context
)
{
try
{
return
croppedBitmap
;
String
baseDir
=
Environment
.
getExternalStorageDirectory
().
getAbsolutePath
();
}
String
fileName
=
"myFile.txt"
;
public
static
void
writeToFile
(
String
data
,
Context
context
)
{
File
file
=
new
File
(
baseDir
+
File
.
separator
+
fileName
);
try
{
String
baseDir
=
Environment
.
getExternalStorageDirectory
().
getAbsolutePath
();
FileOutputStream
stream
=
new
FileOutputStream
(
file
);
String
fileName
=
"myFile.txt"
;
try
{
stream
.
write
(
data
.
getBytes
());
File
file
=
new
File
(
baseDir
+
File
.
separator
+
fileName
);
}
finally
{
stream
.
close
();
FileOutputStream
stream
=
new
FileOutputStream
(
file
);
}
try
{
}
catch
(
IOException
e
)
{
stream
.
write
(
data
.
getBytes
());
Log
.
e
(
"Exception"
,
"File write failed: "
+
e
.
toString
());
}
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