Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I
Intelligent English Tutor
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-24-027
Intelligent English Tutor
Commits
4cab3664
Commit
4cab3664
authored
Nov 07, 2023
by
Piumi Navoda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dataset prepare
parent
ff03cfbb
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
0 deletions
+70
-0
voicerecognizion/prepare_dataset.py
voicerecognizion/prepare_dataset.py
+70
-0
No files found.
voicerecognizion/prepare_dataset.py
0 → 100644
View file @
4cab3664
import
librosa
import
os
import
json
DATASET_PATH
=
"dataset"
JSON_PATH
=
"data.json"
SAMPLES_TO_CONSIDER
=
22050
# 1 sec. of audio
def
preprocess_dataset
(
dataset_path
,
json_path
,
num_mfcc
=
13
,
n_fft
=
2048
,
hop_length
=
512
):
"""Extracts MFCCs from music dataset and saves them into a json file.
:param dataset_path (str): Path to dataset
:param json_path (str): Path to json file used to save MFCCs
:param num_mfcc (int): Number of coefficients to extract
:param n_fft (int): Interval we consider to apply FFT. Measured in # of samples
:param hop_length (int): Sliding window for FFT. Measured in # of samples
:return:
"""
# dictionary where we'll store mapping, labels, MFCCs and filenames
data
=
{
"mapping"
:
[],
"labels"
:
[],
"MFCCs"
:
[],
"files"
:
[]
}
# loop through all sub-dirs
for
i
,
(
dirpath
,
dirnames
,
filenames
)
in
enumerate
(
os
.
walk
(
dataset_path
)):
# ensure we're at sub-folder level
if
dirpath
is
not
dataset_path
:
# save label (i.e., sub-folder name) in the mapping
label
=
dirpath
.
split
(
"/"
)[
-
1
]
data
[
"mapping"
]
.
append
(
label
)
print
(
"
\n
Processing: '{}'"
.
format
(
label
))
# process all audio files in sub-dir and store MFCCs
for
f
in
filenames
:
file_path
=
os
.
path
.
join
(
dirpath
,
f
)
# load audio file and slice it to ensure length consistency among different files
signal
,
sample_rate
=
librosa
.
load
(
file_path
)
# drop audio files with less than pre-decided number of samples
if
len
(
signal
)
>=
SAMPLES_TO_CONSIDER
:
# ensure consistency of the length of the signal
signal
=
signal
[:
SAMPLES_TO_CONSIDER
]
# extract MFCCs
MFCCs
=
librosa
.
feature
.
mfcc
(
y
=
signal
,
sr
=
sample_rate
,
n_mfcc
=
num_mfcc
,
n_fft
=
n_fft
,
hop_length
=
hop_length
)
# store data for analysed track
data
[
"MFCCs"
]
.
append
(
MFCCs
.
T
.
tolist
())
data
[
"labels"
]
.
append
(
i
-
1
)
data
[
"files"
]
.
append
(
file_path
)
print
(
"{}: {}"
.
format
(
file_path
,
i
-
1
))
# save data in json file
with
open
(
json_path
,
"w"
)
as
fp
:
json
.
dump
(
data
,
fp
,
indent
=
4
)
if
__name__
==
"__main__"
:
preprocess_dataset
(
DATASET_PATH
,
JSON_PATH
)
\ No newline at end of file
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