-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
executable file
·150 lines (123 loc) · 3.49 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3.5
import hatesonar as hs
import matplotlib.pyplot as plt
import multiprocessing
import os
from pydub import AudioSegment
import speech_recognition as sr
import scipy.io.wavfile as wavfile
import subprocess
import sys
import wave
mic = sr.Microphone()
mic.CHUNK = 4096
r = sr.Recognizer()
def splitAudio(inputFile):
TIME_FRAME = 5
if not os.path.isfile(inputFile):
raise Exception("Input audio file not found!")
wav_input_file = wave.open(inputFile)
duration = wav_input_file.getnframes() / wav_input_file.getframerate()
wav_input_file.close()
marker = 0
i = 0
while marker < duration:
t1 = marker * 1000
t2 = min((marker + TIME_FRAME) , duration) * 1000
newAudio = AudioSegment.from_wav('audio.wav')
newAudio = newAudio[t1:t2]
exportfName = './data/' + str(i) + '.wav'
newAudio.export(exportfName , format = 'wav')
marker += TIME_FRAME
i += 1
def get_audio_from_file(inputFile):
audioFile = sr.AudioFile(inputFile)
with audioFile as source:
audio = r.record(source)
return audio
def get_hatepercent(text):
sonar = hs.Sonar()
x = sonar.ping(text=text)
return x['classes'][0]['confidence'] + x['classes'][1]['confidence']
def hplot(x_list, y_list):
try:
if y_list[-1] < 0.5:
col = 'green'
elif y_list[-1] < 0.8:
col = 'orange'
else: col = 'red'
except:
col = 'green'
plt.plot(x_list, y_list, color = col)
plt.ylabel("hate %")
plt.xlabel("time -->")
plt.draw()
plt.pause(0.05)
def rec():
with mic as source:
audio = r.listen(source)
return audio
def process_speech(buff):
i = 1
x_axis = []
y_axis = []
audio = buff.get()
while type(audio) != type("string"):
try:
x = r.recognize_google(audio)
print(x)
except:
print("Sorry, didn't caught that, try again")
x = ""
hate = get_hatepercent(x)
x_axis.append(i)
y_axis.append(hate)
hplot(x_axis, y_axis)
i += 1
audio = buff.get()
plt.show()
def rec_from_file():
try:
fileName = sys.argv[2]
except:
print("usage: ./main file <filename>")
splitAudio(fileName)
audio_file_list = os.listdir('./data/')
rFileList = []
for x in audio_file_list:
rFileList.append(int(x[:-4]))
rFileList.sort()
for f in rFileList:
ff = './data/' + str(f) + '.wav'
buff.put(get_audio_from_file(ff))
buff.put("END")
for f in audio_file_list:
ff = './data/' + f
os.remove(ff)
def rec_from_video():
try:
fileName = sys.argv[2]
print("sys.argv[2]: " + fileName)
except:
print("usage: ./main video <filename>")
command = "ffmpeg -i " + fileName + " -ab 160k -ac 2 -ar 44100 -vn ./audio.wav"
subprocess.call(command, shell=True)
sys.argv[2] = "./audio.wav"
rec_from_file()
os.remove('./audio.wav')
def rec_from_mic():
while True:
buff.put(rec())
def choose_task_and_execute(command):
tasks = {
'mic' : rec_from_mic,
'file' : rec_from_file,
'video' : rec_from_video
}
tasks[command]()
if __name__ == "__main__":
buff = multiprocessing.Queue()
proc = multiprocessing.Process(target=process_speech, args=(buff,), )
proc.start()
choose_task_and_execute(sys.argv[1])
print("ALL DONE!")