Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SimpleHTR
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
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
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Fabian Mersch
SimpleHTR
Commits
2d55c3a5
Commit
2d55c3a5
authored
1 year ago
by
merschie
Browse files
Options
Downloads
Patches
Plain Diff
get Namelist from client
parent
51fec0fd
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/preprocessor.py
+1
-1
1 addition, 1 deletion
src/preprocessor.py
src/webserver.py
+41
-111
41 additions, 111 deletions
src/webserver.py
with
42 additions
and
112 deletions
src/preprocessor.py
+
1
−
1
View file @
2d55c3a5
...
...
@@ -165,7 +165,7 @@ class Preprocessor:
#cut out first horizontal line if it is white
img
=
255
-
img
try
:
while
np
.
sum
(
img
[
0
,:])
<
2048
:
while
np
.
sum
(
img
[
0
,:])
<
1024
:
img
=
img
[
1
:,:]
except
:
return
np
.
ones
((
ht
,
ht
))
...
...
This diff is collapsed.
Click to expand it.
src/webserver.py
+
41
−
111
View file @
2d55c3a5
from
flask
import
Flask
,
request
,
jsonify
import
main
as
htr
import
model
as
htr_model
import
dataloader_iam
as
htr_data_loader
import
preprocessor
as
htr_preprocessor
import
numpy
as
np
import
base64
import
time
import
json
app
=
Flask
(
__name__
)
image_size
=
32
model_name
=
htr_model
.
Model
(
htr
.
char_list_from_file
(),
htr_model
.
DecoderType
.
LexiconSearch
,
must_restore
=
True
)
model_name
.
setup_ctc
csv_path
=
'
../tns.csv
'
char_list
=
htr
.
char_list_from_file
()
chars
=
''
.
join
(
char_list
)
#word_chars = open('../model/wordCharList.txt').read().splitlines()[0]
matrikel_numbers
=
[]
@app.route
(
'
/getNames
'
,
methods
=
[
'
GET
'
])
def
getNames
():
return
jsonify
(
matrikel_numbers
)
def
image_to_base64
(
processed_image
):
processed_image
=
processed_image
+
0.5
processed_image
=
processed_image
*
255
#rotate image 90 degrees
processed_image
=
np
.
rot90
(
processed_image
,
3
)
#mirror image
image
=
np
.
fliplr
(
processed_image
)
height
,
width
=
image
.
shape
cut
=
np
.
reshape
(
image
,(
height
*
width
))
cut
=
np
.
append
(
cut
,
height
)
cut
=
np
.
append
(
cut
,
width
)
cut
=
cut
.
astype
(
np
.
uint64
)
array_bytes
=
cut
.
tobytes
()
image_base64
=
base64
.
b64encode
(
array_bytes
).
decode
(
'
utf-8
'
)
return
image_base64
@app.route
(
'
/predictNachname
'
,
methods
=
[
'
POST
'
])
def
predictNach
():
image_array
=
np
.
frombuffer
(
request
.
data
,
dtype
=
np
.
uint64
)
def
base64_to_image
(
processed_image
):
image
=
base64
.
b64decode
(
processed_image
.
encode
(
'
utf-8
'
))
image_array
=
np
.
frombuffer
(
image
,
dtype
=
np
.
uint64
)
h
=
image_array
[
-
2
]
w
=
image_array
[
-
1
]
image_array
=
image_array
[:
-
2
]
image_array
=
image_array
.
reshape
((
h
,
w
))
return
image_array
@app.route
(
'
/predictNachname
'
,
methods
=
[
'
POST
'
])
def
predictNach
():
image_data
=
json
.
loads
(
request
.
data
)[
'
image
'
]
image_array
=
base64_to_image
(
image_data
)
preprocessor
=
htr_preprocessor
.
Preprocessor
(
htr
.
get_img_size
(),
dynamic_width
=
True
,
padding
=
16
)
processed_image
=
preprocessor
.
process_img
(
image_array
)
batch
=
htr_data_loader
.
Batch
([
processed_image
],
None
,
1
)
#change corpus for name
model_name
.
corpus
=
open
(
'
../data/Nachname.txt
'
).
read
().
split
()[:
200
]
names
=
json
.
loads
(
request
.
data
)[
'
names
'
]
print
(
names
)
model_name
.
corpus
=
names
.
split
(
'
'
)
#check time of infer_batch
recognized
,
probability
=
htr_model
.
Model
.
infer_batch
(
model_name
,
batch
)
processed_image
=
processed_image
+
0.5
processed_image
=
processed_image
*
255
#rotate image 90 degrees
processed_image
=
np
.
rot90
(
processed_image
,
3
)
#mirror image
processed_image
=
np
.
fliplr
(
processed_image
)
height
,
width
=
processed_image
.
shape
image
=
np
.
reshape
(
processed_image
,(
height
*
width
))
image
=
np
.
append
(
image
,
height
)
image
=
np
.
append
(
image
,
width
)
image
=
image
.
astype
(
np
.
uint64
)
array_bytes
=
image
.
tobytes
()
image_base64
=
base64
.
b64encode
(
array_bytes
).
decode
(
'
utf-8
'
)
image_base64
=
image_to_base64
(
processed_image
)
result
=
{
'
recognized
'
:
recognized
,
...
...
@@ -73,36 +72,21 @@ def predictNach():
@app.route
(
'
/predictVorname
'
,
methods
=
[
'
POST
'
])
def
predictVor
():
image_array
=
np
.
frombuffer
(
request
.
data
,
dtype
=
np
.
uint64
)
h
=
image_array
[
-
2
]
w
=
image_array
[
-
1
]
image_array
=
image_array
[:
-
2
]
image_array
=
image_array
.
reshape
((
h
,
w
))
image_data
=
json
.
loads
(
request
.
data
)[
'
image
'
]
image_array
=
base64_to_image
(
image_data
)
preprocessor
=
htr_preprocessor
.
Preprocessor
(
htr
.
get_img_size
(),
dynamic_width
=
True
,
padding
=
16
)
processed_image
=
preprocessor
.
process_img
(
image_array
)
batch
=
htr_data_loader
.
Batch
([
processed_image
],
None
,
1
)
#change corpus for name
model_name
.
corpus
=
open
(
'
../data/Vorname.txt
'
).
read
().
split
()[:
200
]
recognized
,
probability
=
htr_model
.
Model
.
infer_batch
(
model_name
,
batch
)
names
=
json
.
loads
(
request
.
data
)[
'
names
'
]
model_name
.
corpus
=
names
.
split
(
'
'
)
processed_image
=
processed_image
+
0.5
processed_image
=
processed_image
*
255
#rotate image -90 degrees
processed_image
=
np
.
rot90
(
processed_image
,
3
)
#mirror image
processed_image
=
np
.
fliplr
(
processed_image
)
height
,
width
=
processed_image
.
shape
image
=
np
.
reshape
(
processed_image
,(
height
*
width
))
image
=
np
.
append
(
image
,
height
)
image
=
np
.
append
(
image
,
width
)
image
=
image
.
astype
(
np
.
uint64
)
array_bytes
=
image
.
tobytes
()
image_base64
=
base64
.
b64encode
(
array_bytes
).
decode
(
'
utf-8
'
)
recognized
,
probability
=
htr_model
.
Model
.
infer_batch
(
model_name
,
batch
)
image_base64
=
image_to_base64
(
processed_image
)
result
=
{
'
recognized
'
:
recognized
,
...
...
@@ -110,60 +94,6 @@ def predictVor():
}
return
jsonify
(
result
)
def
replace_umlauts
(
text
):
text
=
text
.
replace
(
'
ä
'
,
'
a
'
)
text
=
text
.
replace
(
'
ö
'
,
'
o
'
)
text
=
text
.
replace
(
'
ü
'
,
'
u
'
)
text
=
text
.
replace
(
'
Ä
'
,
'
A
'
)
text
=
text
.
replace
(
'
Ö
'
,
'
O
'
)
text
=
text
.
replace
(
'
Ü
'
,
'
U
'
)
text
=
text
.
replace
(
'
ß
'
,
'
s
'
)
text
=
text
.
replace
(
'
é
'
,
'
e
'
)
text
=
text
.
replace
(
'
è
'
,
'
e
'
)
text
=
text
.
replace
(
'
ê
'
,
'
e
'
)
text
=
text
.
replace
(
'
à
'
,
'
a
'
)
text
=
text
.
replace
(
'
â
'
,
'
a
'
)
text
=
text
.
replace
(
'
á
'
,
'
a
'
)
text
=
text
.
replace
(
'
ô
'
,
'
o
'
)
text
=
text
.
replace
(
'
û
'
,
'
u
'
)
text
=
text
.
replace
(
'
ç
'
,
'
c
'
)
text
=
text
.
replace
(
'
î
'
,
'
i
'
)
text
=
text
.
replace
(
'
ï
'
,
'
i
'
)
text
=
text
.
replace
(
'
ë
'
,
'
e
'
)
text
=
text
.
replace
(
'
ù
'
,
'
u
'
)
text
=
text
.
replace
(
'
'
,
'
-
'
)
return
text
def
split_Student_Names
():
#csv looks like: Vorname;Nachname;Matrikelnummer
#need to put Vorname in one list, Nachname in another
#create /data/Vorname.txt and /data/Nachname.txt
vorname_file
=
open
(
'
../data/Vorname.txt
'
,
'
w
'
)
nachname_file
=
open
(
'
../data/Nachname.txt
'
,
'
w
'
)
matrikelnummer_file
=
open
(
'
../data/Matrikelnummer.txt
'
,
'
w
'
)
teilnehmer
=
[]
with
open
(
csv_path
,
'
r
'
)
as
csv_file
:
lines
=
csv_file
.
readlines
()
for
line
in
lines
[
1
:]:
line
=
line
.
split
(
'
,
'
)
vorname
=
line
[
2
][:
-
1
]
vorname
=
replace_umlauts
(
vorname
)
nachname
=
line
[
1
]
nachname
=
replace_umlauts
(
nachname
)
matrikelnummer
=
line
[
0
]
print
(
vorname
,
nachname
,
matrikelnummer
)
vorname_file
.
write
(
vorname
+
"
"
)
nachname_file
.
write
(
nachname
+
"
"
)
matrikelnummer_file
.
write
(
matrikelnummer
+
"
"
)
teilnehmer
.
append
([
vorname
,
nachname
,
matrikelnummer
])
return
teilnehmer
if
__name__
==
'
__main__
'
:
#split csv file into Vorname, Nachname and Matrikelnummer
matrikel_numbers
=
split_Student_Names
()
app
.
run
(
debug
=
False
,
port
=
8000
)
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