Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
primordial-positron
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Rob Kooper
primordial-positron
Commits
6bcd6fc6
Commit
6bcd6fc6
authored
1 year ago
by
Nattapon Jaroenchai
Browse files
Options
Downloads
Patches
Plain Diff
Update inference.py
parent
f7247d3b
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
inference.py
+9
-12
9 additions, 12 deletions
inference.py
with
9 additions
and
12 deletions
inference.py
+
9
−
12
View file @
6bcd6fc6
...
...
@@ -18,7 +18,7 @@ from unet_util import (UNET_224, Residual_CNN_block,
# Declare h5_image as a global variable to streamline data access across functions
h5_image
=
None
def
prediction_mask
(
prediction_result
,
map_name
,
outputPath
):
def
prediction_mask
(
prediction_result
,
map_name
,
legend
,
outputPath
):
"""
Apply a mask to the prediction image to isolate the area of interest.
...
...
@@ -62,20 +62,17 @@ def prediction_mask(prediction_result, map_name, outputPath):
contour
=
sorted
(
contours
,
key
=
cv2
.
contourArea
,
reverse
=
True
)[
0
]
wid
,
hight
=
prediction_result
.
shape
[
0
],
prediction_result
.
shape
[
1
]
mask
=
np
.
zeros
([
wid
,
hight
])
mask
=
cv2
.
fillPoly
(
mask
,
pts
=
[
contour
],
color
=
(
1
,
1
,
1
)).
astype
(
int
)
mask
=
cv2
.
fillPoly
(
mask
,
pts
=
[
contour
],
color
=
(
1
)).
astype
(
int
)
# Normalize the float image to be in the range [0, 255] if it's not already
prediction_result_normalized
=
cv2
.
normalize
(
prediction_result
,
None
,
0
,
255
,
cv2
.
NORM_MINMAX
)
prediction_result_uint8
=
prediction_result_normalized
.
astype
(
np
.
uint8
)
# Threshold prediction results and convert to int
prediction_result_int
=
(
prediction_result
>
0.5
).
astype
(
int
)
# Perform the bitwise operation with the mask also converted to uint8
mask_uint8
=
mask
.
astype
(
np
.
uint8
)
masked_img
=
cv2
.
bitwise_and
(
prediction_result_uint8
,
mask_uint8
)
masked_img
=
cv2
.
bitwise_and
(
prediction_result_int
,
mask
)
# Save the intermediate images using PIL
Image
.
fromarray
((
prediction_result
*
255
).
astype
(
np
.
uint8
)).
save
(
os
.
path
.
join
(
outputPath
,
f
"
{
map_name
}
_prediction_result_x255.tif
"
))
Image
.
fromarray
(
prediction_result_normalized
).
save
(
os
.
path
.
join
(
outputPath
,
f
"
{
map_name
}
_prediction_result_normalized.tif
"
))
Image
.
fromarray
(
mask_uint8
).
save
(
os
.
path
.
join
(
outputPath
,
f
"
{
map_name
}
_mask_uint8.tif
"
))
Image
.
fromarray
((
prediction_result
*
255
).
astype
(
np
.
uint8
)).
save
(
os
.
path
.
join
(
outputPath
,
f
"
{
map_name
}
__
{
legend
}
_prediction_result_x255.tif
"
))
Image
.
fromarray
(
mask
).
save
(
os
.
path
.
join
(
outputPath
,
f
"
{
map_name
}
__
{
legend
}
_mask_uint8.tif
"
))
return
masked_img
...
...
@@ -126,7 +123,7 @@ def save_results(prediction, outputPath, map_name, legend):
# Convert the prediction to an image
# Note: The prediction array may need to be scaled or converted before saving as an image
prediction_image
=
Image
.
fromarray
(
prediction
.
astype
(
np
.
uint8
))
prediction_image
=
Image
.
fromarray
(
(
prediction
*
255
)
.
astype
(
np
.
uint8
))
# Save the prediction as a tiff image
prediction_image
.
save
(
output_image_path
,
'
TIFF
'
)
...
...
@@ -242,7 +239,7 @@ def main(args):
# Mask out the map background pixels from the prediction
print
(
"
Applying mask to the full prediction.
"
)
masked_prediction
=
prediction_mask
(
full_prediction
,
map_name
,
args
.
outputPath
)
masked_prediction
=
prediction_mask
(
full_prediction
,
map_name
,
legend
,
args
.
outputPath
)
# Save the results
print
(
"
Saving results.
"
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment