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
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
17 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,18 +19,49 @@ import java.nio.MappedByteBuffer;
...
@@ -19,18 +19,49 @@ 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
)
public
static
MappedByteBuffer
loadModelFile
(
AssetManager
assets
,
String
filePath
)
throws
IOException
{
throws
IOException
{
AssetFileDescriptor
fileDescriptor
=
assets
.
openFd
(
filePath
);
AssetFileDescriptor
fileDescriptor
=
assets
.
openFd
(
modelFilename
);
MappedByteBuffer
result
=
null
;
try
{
FileInputStream
inputStream
=
new
FileInputStream
(
fileDescriptor
.
getFileDescriptor
());
FileInputStream
inputStream
=
new
FileInputStream
(
fileDescriptor
.
getFileDescriptor
());
try
{
FileChannel
fileChannel
=
inputStream
.
getChannel
();
FileChannel
fileChannel
=
inputStream
.
getChannel
();
long
startOffset
=
fileDescriptor
.
getStartOffset
();
long
startOffset
=
fileDescriptor
.
getStartOffset
();
long
declaredLength
=
fileDescriptor
.
getDeclaredLength
();
long
declaredLength
=
fileDescriptor
.
getDeclaredLength
();
return
fileChannel
.
map
(
FileChannel
.
MapMode
.
READ_ONLY
,
startOffset
,
declaredLength
);
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
)
{
public
static
void
softmax
(
final
float
[]
vals
)
{
...
@@ -94,19 +125,14 @@ public class Utils {
...
@@ -94,19 +125,14 @@ public class Utils {
* @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
int
dstWidth
,
final
int
dstHeight
,
final
int
applyRotation
,
final
boolean
maintainAspectRatio
)
{
final
Matrix
matrix
=
new
Matrix
();
final
Matrix
matrix
=
new
Matrix
();
if
(
applyRotation
!=
0
)
{
if
(
applyRotation
!=
0
)
{
...
@@ -148,14 +174,14 @@ public class Utils {
...
@@ -148,14 +174,14 @@ public class Utils {
return
matrix
;
return
matrix
;
}
}
public
static
Bitmap
processBitmap
(
Bitmap
source
,
int
size
)
{
public
static
Bitmap
processBitmap
(
Bitmap
source
,
int
size
)
{
int
image_height
=
source
.
getHeight
();
int
image_height
=
source
.
getHeight
();
int
image_width
=
source
.
getWidth
();
int
image_width
=
source
.
getWidth
();
Bitmap
croppedBitmap
=
Bitmap
.
createBitmap
(
size
,
size
,
Bitmap
.
Config
.
ARGB_8888
);
Bitmap
croppedBitmap
=
Bitmap
.
createBitmap
(
size
,
size
,
Bitmap
.
Config
.
ARGB_8888
);
Matrix
frameToCropTransformations
=
getTransformationMatrix
(
image_width
,
image_height
,
size
,
size
,
0
,
false
);
Matrix
frameToCropTransformations
=
getTransformationMatrix
(
image_width
,
image_height
,
size
,
size
,
0
,
false
);
Matrix
cropToFrameTransformations
=
new
Matrix
();
Matrix
cropToFrameTransformations
=
new
Matrix
();
frameToCropTransformations
.
invert
(
cropToFrameTransformations
);
frameToCropTransformations
.
invert
(
cropToFrameTransformations
);
...
...
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