Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
TMP-23-105
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
1
Merge Requests
1
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
K.Tharmikan
TMP-23-105
Commits
2c735b9d
Commit
2c735b9d
authored
Oct 31, 2023
by
Heisapirashoban Nadarajah
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload Frondend python file
parent
56114cb7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
112 additions
and
0 deletions
+112
-0
speak.py
speak.py
+112
-0
No files found.
speak.py
0 → 100644
View file @
2c735b9d
import
os
import
numpy
as
np
import
librosa
import
tensorflow
as
tf
import
pyaudio
import
wave
import
threading
from
kivy.lang
import
Builder
from
kivy.uix.screenmanager
import
Screen
,
ScreenManager
from
kivymd.app
import
MDApp
from
kivymd.uix.button
import
MDRaisedButton
from
kivymd.uix.label
import
MDLabel
from
main_imports
import
MDScreen
from
ProjectFiles.applibs
import
utils
import
warnings
import
pandas
as
pd
from
sklearn
import
metrics
from
sklearn.model_selection
import
train_test_split
from
sklearn.tree
import
export_graphviz
from
six
import
StringIO
import
pydotplus
from
sklearn.tree
import
DecisionTreeClassifier
warnings
.
filterwarnings
(
'ignore'
)
utils
.
load_kv
(
"speak.kv"
)
# Load the saved model
model
=
tf
.
keras
.
models
.
load_model
(
'model.h5'
)
# Define a function to extract features from audio data
def
extract_features
(
audio_data
,
sr
=
22050
):
mfcc
=
librosa
.
feature
.
mfcc
(
y
=
audio_data
,
sr
=
sr
,
n_mfcc
=
20
)
mfcc_mean
=
np
.
mean
(
mfcc
,
axis
=
1
)
mfcc_delta
=
librosa
.
feature
.
delta
(
mfcc
,
width
=
3
)
mfcc_delta2
=
librosa
.
feature
.
delta
(
mfcc
,
width
=
3
,
order
=
2
)
mfcc_delta_mean
=
np
.
mean
(
mfcc_delta
,
axis
=
1
)
mfcc_delta2_mean
=
np
.
mean
(
mfcc_delta2
,
axis
=
1
)
features
=
np
.
concatenate
((
mfcc_mean
,
mfcc_delta_mean
,
mfcc_delta2_mean
))
return
features
# Define a function to predict the mood from audio data
def
predict_mood
(
audio_data
):
features
=
extract_features
(
audio_data
)
features
=
features
.
reshape
(
1
,
-
1
)
predictions
=
model
.
predict
(
features
)
emotion_labels
=
[
'angry'
,
'calm'
,
'disgust'
,
'fearful'
,
'happy'
,
'neutral'
,
'sad'
]
predicted_label
=
emotion_labels
[
np
.
argmax
(
predictions
)]
return
predicted_label
class
speak_Screen
(
Screen
):
def
__init__
(
self
,
**
kwargs
):
super
()
.
__init__
(
**
kwargs
)
self
.
recording_thread
=
None
self
.
recording
=
False
self
.
audio_buffer
=
[]
def
btnA
(
self
):
if
not
self
.
recording
:
self
.
start_recording
()
self
.
ids
.
l1
.
text
=
"Recording started"
else
:
self
.
ids
.
l1
.
text
=
"Please wait, text is getting ready"
self
.
analyze_audio
()
def
start_recording
(
self
):
self
.
recording
=
True
print
(
"Recording started"
)
self
.
audio_buffer
=
[]
# Clear the audio buffer
self
.
recording_thread
=
threading
.
Thread
(
target
=
self
.
record_audio
)
self
.
recording_thread
.
start
()
def
record_audio
(
self
):
chunk
=
1024
p
=
pyaudio
.
PyAudio
()
stream
=
p
.
open
(
format
=
pyaudio
.
paInt16
,
channels
=
1
,
rate
=
44100
,
input
=
True
,
frames_per_buffer
=
1024
)
frames
=
[]
while
self
.
recording
:
data
=
stream
.
read
(
1024
)
frames
.
append
(
data
)
self
.
audio_buffer
.
append
(
data
)
stream
.
stop_stream
()
stream
.
close
()
p
.
terminate
()
def
analyze_audio
(
self
):
if
not
self
.
audio_buffer
:
return
audio_data
=
np
.
frombuffer
(
b
''
.
join
(
self
.
audio_buffer
),
dtype
=
np
.
int16
)
# Normalize audio data to the range [-1, 1]
audio_data
=
audio_data
.
astype
(
np
.
float32
)
/
32768.0
temp_audio_file
=
"temp_audio.wav"
wf
=
wave
.
open
(
temp_audio_file
,
'wb'
)
wf
.
setnchannels
(
1
)
wf
.
setsampwidth
(
2
)
wf
.
setframerate
(
44100
)
wf
.
writeframes
(
audio_data
.
tobytes
())
wf
.
close
()
predicted_mood
=
predict_mood
(
audio_data
)
print
(
'Predicted mood:'
,
predicted_mood
)
self
.
ids
.
l1
.
text
=
predicted_mood
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