Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
DAN
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Container Registry
Operate
Terraform modules
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Automatic Text Recognition
DAN
Merge requests
!76
Add predicted objects to predict command
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Add predicted objects to predict command
36-add-predicted-objects-to-predict-command
into
main
Overview
8
Commits
16
Pipelines
0
Changes
1
Merged
Thibault Lavigne
requested to merge
36-add-predicted-objects-to-predict-command
into
main
2 years ago
Overview
2
Commits
16
Pipelines
0
Changes
1
Expand
Closes
#36 (closed)
0
0
Merge request reports
Viewing commit
19f6aefa
Prev
Next
Show latest version
1 file
+
49
−
49
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
19f6aefa
reformat attention.py
· 19f6aefa
Solene Tarride
authored
1 year ago
dan/predict/attention.py
+
49
−
49
Options
@@ -9,55 +9,6 @@ from dan import logger
from
dan.utils
import
round_floats
def
compute_coverage
(
text
:
str
,
max_value
:
float
,
offset
:
int
,
attentions
,
size
:
tuple
):
"""
Aggregates attention maps for the current text piece (char, word, line)
:param text: Text piece selected with offset after splitting DAN prediction
:param max_value: Maximum
"
attention intensity
"
for parts of a text piece, used for normalization
:param offset: Offset value to get the relevant part of text piece
:param attentions: Attention weights of size (n_char, feature_height, feature_width)
:param size: Target size (width, height) to resize the coverage vector
"""
_
,
height
,
width
=
attentions
.
shape
# blank vector to accumulate weights for the current text
coverage_vector
=
np
.
zeros
((
height
,
width
))
for
i
in
range
(
len
(
text
)):
local_weight
=
cv2
.
resize
(
attentions
[
i
+
offset
],
(
width
,
height
))
coverage_vector
=
np
.
clip
(
coverage_vector
+
local_weight
,
0
,
1
)
# Normalize coverage vector
coverage_vector
=
(
coverage_vector
/
max_value
*
255
).
astype
(
np
.
uint8
)
# Resize it
if
size
:
coverage_vector
=
cv2
.
resize
(
coverage_vector
,
size
)
return
coverage_vector
def
blend_coverage
(
coverage_vector
,
image
,
mask
,
scale
):
"""
Blends current coverage_vector over original image, used to make an attention map.
:param coverage_vector: Aggregated attention weights of the current text piece, resized to image. size: (n_char, image_height, image_width)
:param image: Input image in PIL format
:param mask: Mask of the image (of any color)
:param scale: Scaling factor for the output gif image
"""
height
,
width
=
coverage_vector
.
shape
# Blend coverage vector with original image
blank_array
=
np
.
zeros
((
height
,
width
)).
astype
(
np
.
uint8
)
coverage_vector
=
Image
.
fromarray
(
np
.
stack
([
coverage_vector
,
blank_array
,
blank_array
],
axis
=
2
),
"
RGB
"
)
blend
=
Image
.
composite
(
image
,
coverage_vector
,
mask
)
# Resize to save time
blend
=
blend
.
resize
((
int
(
width
*
scale
),
int
(
height
*
scale
)),
Image
.
ANTIALIAS
)
return
blend
def
parse_delimiters
(
delimiters
):
return
re
.
compile
(
r
"
|
"
.
join
(
delimiters
))
@@ -170,6 +121,55 @@ def get_predicted_polygons_with_confidence(
return
polygons
def
compute_coverage
(
text
:
str
,
max_value
:
float
,
offset
:
int
,
attentions
,
size
:
tuple
):
"""
Aggregates attention maps for the current text piece (char, word, line)
:param text: Text piece selected with offset after splitting DAN prediction
:param max_value: Maximum
"
attention intensity
"
for parts of a text piece, used for normalization
:param offset: Offset value to get the relevant part of text piece
:param attentions: Attention weights of size (n_char, feature_height, feature_width)
:param size: Target size (width, height) to resize the coverage vector
"""
_
,
height
,
width
=
attentions
.
shape
# blank vector to accumulate weights for the current text
coverage_vector
=
np
.
zeros
((
height
,
width
))
for
i
in
range
(
len
(
text
)):
local_weight
=
cv2
.
resize
(
attentions
[
i
+
offset
],
(
width
,
height
))
coverage_vector
=
np
.
clip
(
coverage_vector
+
local_weight
,
0
,
1
)
# Normalize coverage vector
coverage_vector
=
(
coverage_vector
/
max_value
*
255
).
astype
(
np
.
uint8
)
# Resize it
if
size
:
coverage_vector
=
cv2
.
resize
(
coverage_vector
,
size
)
return
coverage_vector
def
blend_coverage
(
coverage_vector
,
image
,
mask
,
scale
):
"""
Blends current coverage_vector over original image, used to make an attention map.
:param coverage_vector: Aggregated attention weights of the current text piece, resized to image. size: (n_char, image_height, image_width)
:param image: Input image in PIL format
:param mask: Mask of the image (of any color)
:param scale: Scaling factor for the output gif image
"""
height
,
width
=
coverage_vector
.
shape
# Blend coverage vector with original image
blank_array
=
np
.
zeros
((
height
,
width
)).
astype
(
np
.
uint8
)
coverage_vector
=
Image
.
fromarray
(
np
.
stack
([
coverage_vector
,
blank_array
,
blank_array
],
axis
=
2
),
"
RGB
"
)
blend
=
Image
.
composite
(
image
,
coverage_vector
,
mask
)
# Resize to save time
blend
=
blend
.
resize
((
int
(
width
*
scale
),
int
(
height
*
scale
)),
Image
.
ANTIALIAS
)
return
blend
def
compute_contour_metrics
(
coverage_vector
,
contour
):
"""
Compute the contours
'
s area and the mean value inside it.
Loading