-
Notifications
You must be signed in to change notification settings - Fork 2
/
pro.py
302 lines (223 loc) · 10.9 KB
/
pro.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#encoding:utf-8
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPalette,QBrush,QPixmap,QColor, QIcon,QPixmap
from PyQt5.QtWidgets import QFileDialog,QWidget
from baidutranslate import requests_for_dst
import requests
import string
import time
import hashlib
import json
import sys
import os
import wave
import pyaudio
import voice
import recordvoice
import s
import GenAudio
import xunfeitts
from log import Logger
from Baidutts import BaiduRest
from bdtranslate import translate1,translate2
from baiduasr import asr
class Ui_MainWindow2(QWidget):
def setupUi(self, MainWindow2):
MainWindow2.setObjectName("MainWindow2")
MainWindow2.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow2)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(60, 100, 181, 111))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setObjectName("verticalLayout")
self.textEdit = QtWidgets.QTextEdit(self.verticalLayoutWidget)
self.textEdit.setObjectName("textEdit")
self.verticalLayout.addWidget(self.textEdit)
self.verticalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(370, 100, 191, 111))
self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.textEdit_2 = QtWidgets.QTextEdit(self.verticalLayoutWidget_2)
self.textEdit_2.setObjectName("textEdit_2")
self.verticalLayout_2.addWidget(self.textEdit_2)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(160, 220, 51, 27))
self.pushButton.setObjectName("pushButton")
#self.pushButton.setStyleSheet('QPushButton{background-image:url(record.png)}')
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(270, 140, 81, 27))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(410, 220, 51, 27))
self.pushButton_3.setObjectName("pushButton_3")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(250, 40, 211, 20))
self.label.setObjectName("label")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(80, 220, 51, 27))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_7.setGeometry(QtCore.QRect(480, 220, 60, 27))
self.pushButton_7.setObjectName("pushButton_7")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(100, 70, 85, 27))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_2.setGeometry(QtCore.QRect(420, 70, 85, 27))
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
MainWindow2.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow2)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 31))
self.menubar.setObjectName("menubar")
MainWindow2.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow2)
self.statusbar.setObjectName("statusbar")
MainWindow2.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow2)
QtCore.QMetaObject.connectSlotsByName(MainWindow2)
def retranslateUi(self, MainWindow2):
_translate = QtCore.QCoreApplication.translate
MainWindow2.setWindowTitle(_translate("MainWindow2", "translate system"))
self.pushButton.setText(_translate("MainWindow2", "录音"))
self.pushButton.setToolTip("请说话")
self.pushButton_2.setText(_translate("MainWindow2", "翻译"))
self.pushButton_2.setToolTip("中英互译")
self.pushButton_3.setText(_translate("MainWindow2", "播报"))
self.pushButton_3.setToolTip("文字朗读")
self.label.setText(_translate("MainWindow2", "多功能翻译系统"))
self.pushButton_4.setText(_translate("MainWindow2", "打开"))
self.pushButton_4.setToolTip("打开文本/音视频")
self.pushButton_7.setText(_translate("MainWindow2", "合成视频"))
self.pushButton_7.setToolTip("将音频和视频组合")
self.comboBox.setItemText(0, _translate("MainWindow2", "源语言"))
self.comboBox_2.setItemText(0, _translate("MainWindow2", "目标语言"))
def setupFunction(self):
self.pushButton.pressed.connect(self.Record)
#self.pushButton.released.connect(self.record_stop) #set function for the button
self.pushButton_2.clicked.connect(self.translateapi)
self.pushButton_4.clicked.connect(self.openfile)
self.pushButton_3.clicked.connect(self.tts)
self.pushButton_7.clicked.connect(self.mix)
#self.pushButton.released.connect(self.stopRecord)
#self.textEdit_2.setText(baidutranslate.requests_for_dst(self.textEdit.toPlainText()))
#self.textEdit_2.setText(baidutranslate.requests_for_dst())
def translateapi(self): #baiduapi translate mode
low = 'abcdefghijklmnopqrstuvwxyz'
up = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
#yourAppId = '20170221000039563'
yourAppId = '20180430000151935'
#yourSecret = 'nIyk6j2N4pOIc3PpE9tY'
yourSecret ='fVWFdsmEiv74MwcQVnbO'
word = self.textEdit.toPlainText()
if (str(word)[0] in low)|(str(word)[0] in up):
self.textEdit_2.setText(translate1(yourAppId, yourSecret, "en", "zh",word))
else:
word2 = str(word)
self.textEdit_2.setText(translate2(yourAppId, yourSecret, "zh", "en",word2))
#requests_for_dst(word)
#str1 = (baidutranslate.requests_for_dst(word))
#baidutranslate.requests_for_dst(word)
#baidutranslate.requests_for_dst(word)
#sys.stdout = Logger("1.txt")
#self.textEdit_2.setText
#self.textEdit_2.setText(baidutranslate.requests_for_dst(word))
def openfile(self):
filename = QFileDialog.getOpenFileName(self, 'Open file', './')
file_path = os.path.split(filename[0]) #分割出目录与文件
lists = file_path[1].split('.') #分割出文件与文件扩展名
#print lists[1]
if lists[1] == 'txt': #open the txt
if filename[0]:
with open(filename[0], 'r') as f:
self.textEdit.setText(f.read())
if lists[1] == 'wav': #open the sound
voice.playvoice(filename[0])
#os.system('play test2.mp3')
self.textEdit.setText(s.recognize(filename[0]))
if lists[1] == 'mp3': #open the sound
#voice.playvoice(filename[0])
cmd6 = "ffmpeg -i "+''+ file_path[1] +' '+" mp3out.wav "
os.system(cmd6)
os.system ('play'+' '+filename[0])
self.textEdit.setText(asr("mp3out.wav"))
if lists[1] == 'mp4': #open the video
cmd = "mplayer"+ ' '+file_path[1]
os.system(cmd)
#dtVideoFile = "d:\\workroom\\testroom\\dt-program.mp4"
cmd2 = "ffmpeg -i " + file_path[1] + " -vn -sn -dn -f wav output.wav "
os.system(cmd2)
os.system('play test2.mp3')
self.textEdit.setText(s.recognize("output.wav"))
#dt_sr(pcmFile)
#os.system('paly filename[0]')
#os.system('mplayer L9.mp4')
#if os.path.splitext(filename)[1] == '.txt':
#with open(filename[0], 'r') as f:
#self.textEdit.setText(f.read())
#elif os.path.splitext(filename)[1] == '.mp3':
#os.system('play filename')
#else:
#a = filename[0]
#os.system('mplayer a')
#print filename[1]
#print type(filename)
#print type(filename[0])
#print (filename)
#print (filename[0])
#print file_path
#print lists
#if filename[0]:
#with open(filename[0], 'r') as f:
#self.textEdit.setText(f.read())
#def startRecord(self):
#recordvoice.my_record()
#def stopRecord(self):
#recordvoice.paly()
def mix(self):
filename = QFileDialog.getOpenFileName(self, 'Open file', './')
file_path = os.path.split(filename[0]) #分割出目录与文件
#lists = file_path[1].split('.') #分割出文件与文件扩展名
#if lists[1] == 'mp4': #open the video
#dtVideoFile = "d:\\workroom\\testroom\\dt-program.mp4"
cmd3 = "ffmpeg -i "+' '+ file_path[1] + ' '+" -vcodec copy -an test.mp4"
os.system(cmd3)
time.sleep(3)
cmd4 = "ffmpeg -i test.mp4 -i test0.mp3 -vcodec copy -acodec copy output.avi "
os.system(cmd4)
time.sleep(3)
cmd5 = "mplayer output.avi"
os.system(cmd5)
def Record(self):
os.system("play start.mp3")
os.system("python GenAudio.py")
os.system("play test.wav")
result = asr("test.wav")
self.textEdit.setText(result)
def tts(self):
word = str(self.textEdit_2.toPlainText())
#xunfeitts.text_to_speech(str(word),'xx.wav')
api_key = "SrhYKqzl3SE1URnAEuZ0FKdT"
api_secert = "hGqeCkaMPb0ELMqtRGc2VjWdmjo7T89d"
bdr = BaiduRest("test_python", api_key, api_secert)
# 将字符串语音合成并保存为out.mp3
bdr.getVoice(word, "test0.mp3")
os.system('play test0.mp3')
def record_stop(self):
# stop record the wave
# stop stream (4)
pass
if __name__ == '__main__':
#baidutranslate.requests_for_dst('word')
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow2()
ui.setupUi(MainWindow)
ui.setupFunction()
#QtGui.QApplication.processEvents()
MainWindow.show()
sys.exit(app.exec_())