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
58874145
Commit
58874145
authored
Jan 05, 2022
by
Lihinikaduwa D.N.R.
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
prepared dataset
parent
c4a823af
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
0 deletions
+56
-0
backend/IT18257632/prepare_dataset.py
backend/IT18257632/prepare_dataset.py
+56
-0
No files found.
backend/IT18257632/prepare_dataset.py
0 → 100644
View file @
58874145
import
json
import
librosa
import
os
DATASET_PATH
=
"dataset"
JSON_PATH
=
"data.json"
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 sub-dirs
for
i
,
(
dirpath
,
dirnames
,
filenames
)
in
enumerate
(
os
.
walk
(
dataset_path
)):
if
dirpath
is
not
dataset_path
:
# update mapping
category
=
dirpath
.
split
(
"/"
)
data
[
"mappings"
]
.
append
(
category
)
print
(
f
"Processing{category}"
)
# loop through aii the file name 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
)
# ensure the audio file is at least 1 sec
if
len
(
signal
)
>=
SAMPLES_TO_CONSIDER
:
# enforce 1 sec signal
signal
=
signal
[:
SAMPLES_TO_CONSIDER
]
# extract the MFCCs
MFCCs
=
librosa
.
feature
.
mfcc
(
signal
,
sr
,
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
)
with
open
(
json_path
,
"w"
)
as
fp
:
json
.
dump
(
data
,
fp
,
indent
=
4
)
if
__name__
==
"__main__"
:
prepare_dataset
(
DATASET_PATH
,
JSON_PATH
)
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