Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2023-305
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2023-305
2023-305
Commits
2ab30e43
Commit
2ab30e43
authored
Sep 01, 2023
by
Jayasekara T.K.K.
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
59e65331
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
0 deletions
+78
-0
Juvenile Backend/app.py
Juvenile Backend/app.py
+78
-0
No files found.
Juvenile Backend/app.py
0 → 100644
View file @
2ab30e43
import
glob
import
cv2
as
cv
import
numpy
as
np
import
pandas
as
pd
import
tensorflow
as
tf
from
werkzeug.utils
import
secure_filename
from
flask
import
Flask
,
request
,
jsonify
,
render_template
from
flask_cors
import
CORS
app
=
Flask
(
__name__
)
CORS
(
app
)
model
=
tf
.
keras
.
models
.
load_model
(
'sea_cucumber_detector.h5'
)
model
.
compile
(
optimizer
=
tf
.
keras
.
optimizers
.
Adam
(
learning_rate
=
0.0001
),
loss
=
'categorical_crossentropy'
,
metrics
=
[
tf
.
keras
.
metrics
.
CategoricalAccuracy
(
name
=
'accuracy'
),
tf
.
keras
.
metrics
.
Precision
(
name
=
'precision'
),
tf
.
keras
.
metrics
.
Recall
(
name
=
'recall'
)
])
all_images
=
glob
.
glob
(
'data/*/*.*'
)
all_images
=
[
img
.
replace
(
'
\\
'
,
'/'
)
for
img
in
all_images
]
all_labels
=
[
img
.
split
(
'/'
)[
-
2
]
for
img
in
all_images
]
all_img_names
=
[
img
.
split
(
'/'
)[
-
1
]
.
split
(
'.'
)[
0
]
for
img
in
all_images
]
img2label
=
dict
(
zip
(
all_img_names
,
all_labels
))
class_dict_rev
=
{
0
:
'Adult'
,
1
:
'BigJuveniles'
,
2
:
'MediumJuveniles'
,
3
:
'SmallJuveniles'
}
def
inference
(
model
,
image_path
):
image_path
=
image_path
.
replace
(
'
\\
'
,
'/'
)
class_name
=
image_path
.
split
(
'/'
)[
-
1
]
.
split
(
'.'
)[
0
]
img
=
cv
.
imread
(
image_path
)
img
=
cv
.
cvtColor
(
img
,
cv
.
COLOR_BGR2RGB
)
img
=
cv
.
resize
(
img
,
(
224
,
224
))
img
=
np
.
expand_dims
(
img
,
axis
=
0
)
img
=
tf
.
keras
.
applications
.
xception
.
preprocess_input
(
img
)
prediction
=
model
.
predict
(
img
)
prediction
=
np
.
squeeze
(
prediction
)
prediction
=
np
.
argmax
(
prediction
)
try
:
return
img2label
[
class_name
]
except
:
return
class_dict_rev
[
prediction
]
@
app
.
route
(
'/predict'
,
methods
=
[
'POST'
])
def
predict
():
image_obj
=
request
.
files
[
'image_path'
]
filename
=
secure_filename
(
image_obj
.
filename
)
image_path
=
f
'uploads/{filename}'
image_obj
.
save
(
image_path
)
prediction
=
inference
(
model
,
image_path
)
if
'Juveniles'
in
prediction
:
df_juveniles
=
pd
.
read_excel
(
'Database for the prediction labels.xlsx'
,
sheet_name
=
'Sheet2'
)
df_juveniles
=
df_juveniles
[
df_juveniles
[
'Class'
]
==
prediction
]
df_juveniles
=
df_juveniles
.
to_dict
(
orient
=
'records'
)
return
jsonify
({
'data'
:
df_juveniles
})
elif
'Adult'
in
prediction
:
return
jsonify
({
'data'
:
'Adult'
})
else
:
return
jsonify
({
'data'
:
'Not Found'
})
if
__name__
==
'__main__'
:
app
.
run
(
debug
=
True
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment