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
Commits
67f28861
Commit
67f28861
authored
1 year ago
by
Marie Generali
Committed by
Yoann Schneider
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Input prediction folder
parent
3a1fea73
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!176
Input prediction folder
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
dan/predict/__init__.py
+13
-4
13 additions, 4 deletions
dan/predict/__init__.py
dan/predict/prediction.py
+94
-33
94 additions, 33 deletions
dan/predict/prediction.py
docs/usage/predict.md
+3
-1
3 additions, 1 deletion
docs/usage/predict.md
with
110 additions
and
38 deletions
dan/predict/__init__.py
+
13
−
4
View file @
67f28861
...
...
@@ -14,13 +14,16 @@ def add_predict_parser(subcommands) -> None:
description
=
__doc__
,
help
=
__doc__
,
)
# Required arguments.
parser
.
add_argument
(
image_or_folder_input
=
parser
.
add_mutually_exclusive_group
(
required
=
True
)
image_or_folder_input
.
add_argument
(
"
--image
"
,
type
=
pathlib
.
Path
,
help
=
"
Path to the image to predict.
"
,
required
=
True
,
)
image_or_folder_input
.
add_argument
(
"
--image-dir
"
,
type
=
pathlib
.
Path
,
help
=
"
Path to the folder where the images to predict are stored.
"
,
)
parser
.
add_argument
(
"
--model
"
,
...
...
@@ -48,6 +51,12 @@ def add_predict_parser(subcommands) -> None:
required
=
True
,
)
# Optional arguments.
parser
.
add_argument
(
"
--image-extension
"
,
type
=
str
,
help
=
"
The extension of the images in the folder.
"
,
default
=
"
.jpg
"
,
)
parser
.
add_argument
(
"
--scale
"
,
type
=
float
,
...
...
This diff is collapsed.
Click to expand it.
dan/predict/prediction.py
+
94
−
33
View file @
67f28861
...
...
@@ -251,11 +251,10 @@ class DAN:
return
out
def
run
(
def
process_image
(
image
,
model
,
parameters
,
charset
,
dan_model
,
device
,
output
,
scale
,
confidence_score
,
...
...
@@ -265,40 +264,11 @@ def run(
attention_map_scale
,
word_separators
,
line_separators
,
temperature
,
image_max_width
,
predict_objects
,
threshold_method
,
threshold_value
,
):
"""
Predict a single image save the output
:param image: Path to the image to predict.
:param model: Path to the model to use for prediction.
:param parameters: Path to the YAML parameters file.
:param charset: Path to the charset.
:param output: Path to the output folder where the results will be saved.
:param scale: Scaling factor to resize the image.
:param confidence_score: Whether to compute confidence score.
:param attention_map: Whether to plot the attention map.
:param attention_map_level: Level of objects to extract.
:param attention_map_scale: Scaling factor for the attention map.
:param word_separators: List of word separators.
:param line_separators: List of line separators.
:param image_max_width: Resize image
:param predict_objects: Whether to extract objects.
:param threshold_method: Thresholding method. Should be in [
"
otsu
"
,
"
simple
"
].
:param threshold_value: Thresholding value to use for the
"
simple
"
thresholding method.
"""
# Create output directory if necessary
if
not
os
.
path
.
exists
(
output
):
os
.
mkdir
(
output
)
# Load model
device
=
"
cuda
"
if
torch
.
cuda
.
is_available
()
else
"
cpu
"
dan_model
=
DAN
(
device
,
temperature
)
dan_model
.
load
(
model
,
parameters
,
charset
,
mode
=
"
eval
"
)
# Load image and pre-process it
if
image_max_width
:
_
,
w
,
_
=
read_image
(
image
,
scale
=
1
).
shape
...
...
@@ -396,3 +366,94 @@ def run(
json_filename
=
f
"
{
output
}
/
{
image
.
stem
}
.json
"
logger
.
info
(
f
"
Saving JSON prediction in
{
json_filename
}
"
)
save_json
(
Path
(
json_filename
),
result
)
def
run
(
image
,
image_dir
,
model
,
parameters
,
charset
,
output
,
scale
,
confidence_score
,
confidence_score_levels
,
attention_map
,
attention_map_level
,
attention_map_scale
,
word_separators
,
line_separators
,
temperature
,
image_max_width
,
predict_objects
,
threshold_method
,
threshold_value
,
image_extension
,
):
"""
Predict a single image save the output
:param image: Path to the image to predict.
:param image_dir: Path to the folder where the images to predict are stored.
:param model: Path to the model to use for prediction.
:param parameters: Path to the YAML parameters file.
:param charset: Path to the charset.
:param output: Path to the output folder where the results will be saved.
:param scale: Scaling factor to resize the image.
:param confidence_score: Whether to compute confidence score.
:param attention_map: Whether to plot the attention map.
:param attention_map_level: Level of objects to extract.
:param attention_map_scale: Scaling factor for the attention map.
:param word_separators: List of word separators.
:param line_separators: List of line separators.
:param image_max_width: Resize image
:param predict_objects: Whether to extract objects.
:param threshold_method: Thresholding method. Should be in [
"
otsu
"
,
"
simple
"
].
:param threshold_value: Thresholding value to use for the
"
simple
"
thresholding method.
"""
# Create output directory if necessary
if
not
os
.
path
.
exists
(
output
):
os
.
makedirs
(
output
,
exist_ok
=
True
)
# Load model
device
=
"
cuda
"
if
torch
.
cuda
.
is_available
()
else
"
cpu
"
dan_model
=
DAN
(
device
,
temperature
)
dan_model
.
load
(
model
,
parameters
,
charset
,
mode
=
"
train
"
)
if
image
:
process_image
(
image
,
dan_model
,
device
,
output
,
scale
,
confidence_score
,
confidence_score_levels
,
attention_map
,
attention_map_level
,
attention_map_scale
,
word_separators
,
line_separators
,
image_max_width
,
predict_objects
,
threshold_method
,
threshold_value
,
)
else
:
for
image_name
in
image_dir
.
rglob
(
f
"
*
{
image_extension
}
"
):
process_image
(
image_name
,
dan_model
,
device
,
output
,
scale
,
confidence_score
,
confidence_score_levels
,
attention_map
,
attention_map_level
,
attention_map_scale
,
word_separators
,
line_separators
,
image_max_width
,
predict_objects
,
threshold_method
,
threshold_value
,
)
This diff is collapsed.
Click to expand it.
docs/usage/predict.md
+
3
−
1
View file @
67f28861
...
...
@@ -6,7 +6,9 @@ Use the `teklia-dan predict` command to predict a trained DAN model on an image.
| Parameter | Description | Type | Default |
| --------------------------- | -------------------------------------------------------------------------------------------- | ------- | ------------- |
|
`--image`
| Path to the image to predict. |
`Path`
| |
|
`--image`
| Path to the image to predict. Must not be provided with
`--image-dir`
. |
`Path`
| |
|
`--image-dir`
| Path to the folder where the images to predict are stored. Must not be provided with
`--image`
. |
`Path`
| |
|
`--image-extension`
| The extension of the images in the folder. Ignored if
`--image-dir`
is not provided. |
`str`
| .jpg |
|
`--model`
| Path to the model to use for prediction |
`Path`
| |
|
`--parameters`
| Path to the YAML parameters file. |
`Path`
| |
|
`--charset`
| Path to the charset file. |
`Path`
| |
...
...
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