Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
21_22-J 38
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
21_22-J 38
21_22-J 38
Commits
45d89364
Commit
45d89364
authored
May 03, 2022
by
Lihinikaduwa D.N.R.
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
some changes
parent
cb3a6ded
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
0 deletions
+79
-0
API/service/reading/model.h5
API/service/reading/model.h5
+0
-0
API/service/reading/pronounce_spotting_service.py
API/service/reading/pronounce_spotting_service.py
+79
-0
API/service/reading/temp/no.wav
API/service/reading/temp/no.wav
+0
-0
No files found.
API/service/reading/model.h5
0 → 100755
View file @
45d89364
File added
API/service/reading/pronounce_spotting_service.py
0 → 100755
View file @
45d89364
import
tensorflow.keras
as
keras
import
numpy
as
np
import
librosa
MODEL_PATH
=
"model.h5"
NUM_SAMPLES_TO_CONSIDER
=
22050
# 1 sec
class
_Keyword_Spotting_Service
:
model
=
None
_mappings
=
[
"no"
,
"go"
,
"he"
,
"dog"
,
"bird"
,
"fish"
,
]
_instance
=
None
def
predict
(
self
,
file_path
):
# extract MFCCs
MFCCs
=
self
.
preprocess
(
file_path
)
# (# segment, # coefficients)
# convert 2d MFCCs array into 4d array -> (# samples, # segment, # coefficients, # channels)
MFCCs
=
MFCCs
[
np
.
newaxis
,
...
,
np
.
newaxis
]
# make prediction
predictions
=
self
.
model
.
predict
(
MFCCs
)
predicted_index
=
np
.
argmax
(
predictions
)
predicted_keyword
=
self
.
_mappings
[
predicted_index
]
# return predicted_keyword
new_file_path
=
file_path
.
split
(
"/"
)
new_file_paths
=
new_file_path
[
1
]
.
split
(
"."
)
print
(
f
"1-{predicted_keyword}"
)
print
(
f
"2-{new_file_paths[0]}"
)
if
predicted_keyword
==
new_file_paths
[
0
]:
return
predicted_keyword
else
:
return
"No Prediction"
def
preprocess
(
self
,
file_path
,
n_mfcc
=
13
,
n_fft
=
2048
,
hop_length
=
512
):
# load audio file
signal
,
sr
=
librosa
.
load
(
file_path
)
# ensure consistency in the audio file length
if
len
(
signal
)
>
NUM_SAMPLES_TO_CONSIDER
:
signal
=
signal
[:
NUM_SAMPLES_TO_CONSIDER
]
# extract MFCCs
MFCCs
=
librosa
.
feature
.
mfcc
(
signal
,
n_mfcc
=
n_mfcc
,
n_fft
=
n_fft
,
hop_length
=
hop_length
)
return
MFCCs
.
T
def
Keyword_Spotting_service
():
# ensure that we only have 1 instance of KSS
if
_Keyword_Spotting_Service
.
_instance
is
None
:
_Keyword_Spotting_Service
.
_instance
=
_Keyword_Spotting_Service
()
_Keyword_Spotting_Service
.
model
=
keras
.
models
.
load_model
(
MODEL_PATH
)
return
_Keyword_Spotting_Service
.
_instance
if
__name__
==
"__main__"
:
kss
=
Keyword_Spotting_service
()
keyword1
=
kss
.
predict
(
"test/bed.wav"
)
# keyword2 = kss.predict("test/blu7.wav")
print
(
f
"Predicted Keywords: {keyword1}"
)
# print(f"Predicted Keywords: {keyword2}")
\ No newline at end of file
API/service/reading/temp/no.wav
0 → 100644
View file @
45d89364
File added
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