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
4227166f
Commit
4227166f
authored
Dec 30, 2021
by
W.D.R.P. Sandeepa
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'it18218640' into 'master'
create prepare_dataset method See merge request
!21
parents
38a4b382
bebad94f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
1 deletion
+49
-1
backend/IT18218640/prepare_dataset.py
backend/IT18218640/prepare_dataset.py
+49
-1
No files found.
backend/IT18218640/prepare_dataset.py
View file @
4227166f
...
...
@@ -4,4 +4,52 @@ import json
DATASET_PATH
=
"dataset"
JSON_PATH
=
"data.json"
SAMPLES_TO_CONSIDER
=
22050
\ No newline at end of file
SAMPLES_TO_CONSIDER
=
22050
def
prepare_dataset
(
dataset_path
,
json_path
,
n_mfcc
=
13
,
hop_length
=
512
,
n_fft
=
2048
):
# data dictionary
data
=
{
"mappings"
:
[],
"labels"
:
[],
"MFCCs"
:
[],
"files"
:
[]
}
# loop through all the sub-dirs
for
i
,
(
dirpath
,
dirnames
,
filenames
)
in
enumerate
(
os
.
walk
(
dataset_path
)):
# we need to ensure that we are not at root level
if
dirpath
is
not
dataset_path
:
# update mapping
category
=
dirpath
.
split
(
"/"
)[
-
1
]
# dataset/down -> [dataset, down]
data
[
"mappings"
]
.
append
(
category
)
print
(
f
"Processing {category}"
)
# loop through all the filenames and extract MFCCs
for
f
in
filenames
:
# get file path
file_path
=
os
.
path
.
join
(
dirpath
,
f
)
# load audio file
signal
,
sr
=
librosa
.
load
(
file_path
)
if
len
(
signal
)
>=
SAMPLES_TO_CONSIDER
:
# enforce 1 sec, long signal
signal
=
signal
[:
SAMPLES_TO_CONSIDER
]
# extract the MFCCs
MFCCs
=
librosa
.
feature
.
mfcc
(
signal
,
n_mfcc
=
n_mfcc
,
hop_length
=
hop_length
,
n_fft
=
n_fft
)
# store data
data
[
"labels"
]
.
append
(
i
-
1
)
data
[
"MFCCs"
]
.
append
(
MFCCs
.
T
.
tolist
())
data
[
"files"
]
.
append
(
file_path
)
print
(
f
"{file_path}:{i-1}"
)
# store in json file
with
open
(
json_path
,
"w"
)
as
fp
:
json
.
dump
(
data
,
fp
,
indent
=
4
)
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