Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Salamander - API
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
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
Herman Andersen Dyrkorn
Salamander - API
Commits
9afa0c18
Commit
9afa0c18
authored
Jun 9, 2021
by
Herman Andersen Dyrkorn
Browse files
Options
Downloads
Patches
Plain Diff
downscaling images an converting to base64 string
parent
c6f352a7
No related branches found
No related tags found
1 merge request
!44
Resolve "downscale images when returning from server"
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
api/endpoints/salamander.py
+15
-1
15 additions, 1 deletion
api/endpoints/salamander.py
api/endpoints/salamanderimageid.py
+26
-4
26 additions, 4 deletions
api/endpoints/salamanderimageid.py
with
41 additions
and
5 deletions
api/endpoints/salamander.py
+
15
−
1
View file @
9afa0c18
...
...
@@ -4,6 +4,7 @@ from flask_restful import Resource
from
api
import
db
,
limiter
from
api.models.dbmodels
import
Location
,
User
,
Salamander
import
os
import
cv2
import
glob
from
image_encoder.image_encoder
import
*
from
path_constants
import
_ACCESS_DATABASE
...
...
@@ -28,7 +29,20 @@ class SalamanderEndpoint(Resource):
list_of_paths
=
glob
.
glob
(
os
.
path
.
join
(
path_to_salamander_images
,
'
*.*
'
))
for
path
in
list_of_paths
:
if
not
path
.
__contains__
(
"
_str
"
):
encoded
=
encode
(
path
)[
2
:
-
1
]
image
=
cv2
.
imread
(
path
)
# percent of original size
scale_percent
=
40
width
=
int
(
image
.
shape
[
1
]
*
scale_percent
/
100
)
height
=
int
(
image
.
shape
[
0
]
*
scale_percent
/
100
)
dim
=
(
width
,
height
)
# resize image
resized
=
cv2
.
resize
(
image
,
dim
,
interpolation
=
cv2
.
INTER_AREA
)
encoded
=
base64
.
b64encode
(
cv2
.
imencode
(
"
.jpg
"
,
resized
)[
1
]).
decode
()
image_id
=
os
.
path
.
basename
(
path
)[
0
]
image_data
=
{
"
id
"
:
image_id
,
"
url
"
:
"
data:image/png;base64,
"
+
encoded
}
salamander_images
.
append
(
image_data
)
...
...
This diff is collapsed.
Click to expand it.
api/endpoints/salamanderimageid.py
+
26
−
4
View file @
9afa0c18
import
sys
from
flask
import
jsonify
from
flask_jwt_extended
import
jwt_required
,
get_jwt_identity
from
flask_restful
import
Resource
...
...
@@ -7,6 +9,8 @@ import os
import
glob
from
image_encoder.image_encoder
import
*
from
path_constants
import
_ACCESS_DATABASE
import
cv2
# get one specific image on a specific salamander based on s_id and i_id
class
SalamanderImageEndpoint
(
Resource
):
...
...
@@ -20,20 +24,38 @@ class SalamanderImageEndpoint(Resource):
if
user
.
admin
:
with
_ACCESS_DATABASE
:
salamander
=
db
.
session
.
query
(
Salamander
).
filter_by
(
id
=
salamander_id
).
first
()
salamander_growth
=
db
.
session
.
query
(
SalamanderGrowth
).
filter_by
(
salamander_id
=
salamander
.
id
,
image_id
=
image_id
).
first
()
salamander_growth
=
db
.
session
.
query
(
SalamanderGrowth
).
filter_by
(
salamander_id
=
salamander
.
id
,
image_id
=
image_id
).
first
()
if
salamander
and
salamander_growth
:
location
=
db
.
session
.
query
(
Location
).
filter_by
(
id
=
salamander
.
location_id
).
first
()
salamander_images
=
[]
path_to_salamander_images
=
os
.
path
.
join
(
"
./images
"
,
location
.
name
,
salamander
.
species
,
salamander
.
sex
,
str
(
salamander
.
id
))
path_to_salamander_images
=
os
.
path
.
join
(
"
./images
"
,
location
.
name
,
salamander
.
species
,
salamander
.
sex
,
str
(
salamander
.
id
))
list_of_paths
=
glob
.
glob
(
os
.
path
.
join
(
path_to_salamander_images
,
'
*.*
'
))
for
path
in
list_of_paths
:
basename
=
os
.
path
.
basename
(
path
)
if
basename
.
__contains__
(
str
(
image_id
)):
encoded
=
encode
(
path
)[
2
:
-
1
]
image
=
cv2
.
imread
(
path
)
# percent of original size
scale_percent
=
20
width
=
int
(
image
.
shape
[
1
]
*
scale_percent
/
100
)
height
=
int
(
image
.
shape
[
0
]
*
scale_percent
/
100
)
dim
=
(
width
,
height
)
# resize image
resized
=
cv2
.
resize
(
image
,
dim
,
interpolation
=
cv2
.
INTER_AREA
)
encoded
=
base64
.
b64encode
(
cv2
.
imencode
(
"
.jpg
"
,
resized
)[
1
]).
decode
()
image_data
=
{
"
url
"
:
"
data:image/png;base64,
"
+
encoded
}
salamander_images
.
append
(
image_data
)
return
jsonify
({
"
images
"
:
salamander_images
,
"
length
"
:
salamander_growth
.
length
,
"
weight
"
:
salamander_growth
.
weight
,
"
imageId
"
:
image_id
,
"
location
"
:
location
.
name
,
"
salamanderId
"
:
salamander
.
id
,
"
sex
"
:
salamander
.
sex
,
"
species
"
:
salamander
.
species
,
'
status
'
:
200
})
return
jsonify
({
"
images
"
:
salamander_images
,
"
length
"
:
salamander_growth
.
length
,
"
weight
"
:
salamander_growth
.
weight
,
"
imageId
"
:
image_id
,
"
location
"
:
location
.
name
,
"
salamanderId
"
:
salamander
.
id
,
"
sex
"
:
salamander
.
sex
,
"
species
"
:
salamander
.
species
,
'
status
'
:
200
})
else
:
return
jsonify
({
"
message
"
:
"
no salamander with this id or no growth data
"
,
'
status
'
:
400
})
else
:
...
...
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