Skip to content

Commit

Permalink
implemented multiprocessing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacky K. Lai committed Jul 3, 2020
1 parent 6f0cf48 commit 3de41d8
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 63 deletions.
120 changes: 120 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 0 additions & 31 deletions SpeechReg.py

This file was deleted.

Binary file removed UI.jpg
Binary file not shown.
1 change: 0 additions & 1 deletion convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ def change_path(files, newPath):
result.append(newPath + "/" + name + ".wav")
return result


54 changes: 24 additions & 30 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
import SpeechReg
from speech_reg import Sr
import convert
import similarity
import PyQt5
import multiprocessing
import os
from PyQt5 import QtCore, QtGui, QtWidgets

PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)



class Ui_SlateInPost(object):
def setupUi(self, SlateInPost):
self.data = {}
Expand Down Expand Up @@ -78,7 +77,6 @@ def setupUi(self, SlateInPost):
self.speechAPIcombo.setGeometry(QtCore.QRect(320, 250, 151, 22))
self.speechAPIcombo.setObjectName("speechAPIcombo")
self.speechAPIcombo.setFont(font)
self.speechAPIcombo.addItem("Sphinx")
self.speechAPIcombo.addItem("Google Speech Recognition")
self.speechAPIcombo.addItem("Wit.ai")
self.speechAPIcombo.addItem("Bing")
Expand Down Expand Up @@ -115,7 +113,7 @@ def setupUi(self, SlateInPost):

def retranslateUi(self, SlateInPost):
_translate = QtCore.QCoreApplication.translate
SlateInPost.setWindowTitle(_translate("SlateInPost", "MainWindow"))
SlateInPost.setWindowTitle(_translate("SlateInPost", "SlateInPost"))
self.importBtn.setText(_translate("SlateInPost", "Import Media Files"))
self.outputBtn.setText(_translate("SlateInPost", "Save output to..."))
self.stepOne.setText(_translate("SlateInPost", "Step 1: select media files for syncing"))
Expand Down Expand Up @@ -196,37 +194,32 @@ def show_popup(self, name, text):
x = msg.exec_()

def _run_reg(self):
errors = []
i = 0
progress_dialog = QtWidgets.QProgressDialog(self.centralwidget)
progress_dialog.setWindowModality(QtCore.Qt.WindowModal)
progress_dialog.setWindowTitle("Transcribing your files")
progress_dialog.setCancelButtonText("Cancel")
progress_dialog.setCancelButton(None)
progress_dialog.setMaximum(100)
progress_dialog.setMinimum(0)
progress_dialog.forceShow()
for file in self.files:
i += 1
progress_dialog.setValue(int(i/len(self.files)*100))
_, name = os.path.split(file)
if name[name.find("."):] not in [".wav", ".WAV"]:
continue
name = name[:name.find(".")]
progress_dialog.setLabelText("Transcribing {}...".format(name))
if progress_dialog.wasCanceled():
progress_dialog.close()
break
QtCore.QCoreApplication.processEvents()
text, success = SpeechReg.transcribe_file(file, self.speechAPIcombo.currentText(), self.info)
if progress_dialog.wasCanceled():
progress_dialog.close()
break
QtCore.QCoreApplication.processEvents()
if not success:
errors.append(name)
else:
self.data[text] = name
regconizer = Sr(self.speechAPIcombo.currentText(), self.info)
p = multiprocessing.Pool()
result = []

progress_dialog.setValue(1)
progress_dialog.setLabelText("Starting transcription...")
QtCore.QCoreApplication.processEvents()
for i, r in enumerate(p.imap(regconizer.transcribe_file, self.files), 1):
progress_dialog.setLabelText("Transcribing {}...".format(r[1]))
progress = (i / len(self.files)) * 100
result.append(r)
progress_dialog.setValue(int(progress))
progress_dialog.close()
for tup in result:
sus, file, transcript = tup
if sus:
self.data[transcript] = file
errors = [tup[1] for tup in result if not tup[0]]
p.close()
return errors

def action(self):
Expand Down Expand Up @@ -284,13 +277,14 @@ def action(self):
":" + files)



if __name__ == "__main__":
import sys
multiprocessing.freeze_support()
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_SlateInPost()
ui.setupUi(MainWindow)
MainWindow.setWindowTitle("SlateInPost (v0.0.1)")
MainWindow.show()
sys.exit(app.exec_())

2 changes: 1 addition & 1 deletion similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ def process_list(dic, sim):
for other in dic.keys():
similarity = _similar(string, other)
if other != string and similarity >= sim:
data.append("{} :{}% similar".format(dic[other], str(similarity)))
data.append("{}: {}% similar".format(dic[other], str(int(similarity))))
result[dic[string]] = data
return result
36 changes: 36 additions & 0 deletions speech_reg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import speech_recognition as sr
import time
import os


class Sr:
def __init__(self, api, info):
self.api = api
self.info = info

def transcribe_file(self, file):
time.sleep(.1)
r = sr.Recognizer()
_, name = os.path.split(file)
with sr.AudioFile(file) as source:
audio = r.record(source)
try:
if self.api == "Google Speech Recognition":
transcript = r.recognize_google(audio)
elif self.api == "Wit.ai":
key = self.info[0]
transcript = r.recognize_wit(audio, key=key)
elif self.api == "Bing":
key = self.info[0]
transcript = r.recognize_bing(audio, key=key)
elif self.api == "Houndify":
client_id, key = self.info[0], self.info[1]
transcript = r.recognize_houndify(audio, client_id=client_id, client_key=key)
else:
username, password = self.info[0], self.info[1]
transcript = r.recognize_ibm(audio, username=username, password=password)
time.sleep(.1)
return True, name, transcript
except Exception as e:
print(e)
return False, name, ""

0 comments on commit 3de41d8

Please sign in to comment.