print(' 3)inpText = ' + str(inpText)) if os.path.exists(inpText): os.remove(inpText) resText = '' if (os.path.exists(inpWave)): #try: googleAPI = google_api.SpeechAPI() res = googleAPI.authenticate('stt', google_key.getkey('stt'), ) if (res == True): resText, api = googleAPI.recognize(inpWave=inpWave, inpLang=inpLang) #except: # pass print(' ' + resText) if (resText != ''): w = codecs.open(inpText, 'w', 'shift_jis') w.write(resText) w.close() w = None
tts.save(outFile) return outText, 'free' except: pass return '', '' if __name__ == '__main__': #googleAPI = google_api.SpeechAPI() googleAPI = SpeechAPI() res1 = googleAPI.authenticate( 'stt', google_key.getkey('stt'), ) res2 = googleAPI.authenticate( 'tra', google_key.getkey('tra'), ) res3 = googleAPI.authenticate( 'tts', google_key.getkey('tts'), ) print('authenticate:', res1, res2, res3) if (res1 == True) and (res2 == True) and (res3 == True): text = 'Hallo' file = 'temp_voice.mp3'
def qOCR2Trn( useApi='google', inpLang='ja', inpAry=['Hallo'], trnLang='en', apiRecovery=False, ): resText = '' resApi = '' resAry = None api = useApi if (api != 'free') and (api != 'google') \ and (api != 'azure'): api = 'google' if (resText == '') and (api == 'azure'): a_azureAPI = a_azure_api.SpeechAPI() ver, key = a_azure_key.getkey('tra') res = a_azureAPI.authenticate( 'tra', ver, key, ) if (a_res == True): resAry = [] resAry.append('[Translate] ' + trnLang + ' (' + api + ')') l = 0 for text in inpAry: l += 1 if (l > 1): outText, api = a_azureAPI.translate( inpText=text, inpLang=inpLang, outLang=trnLang, ) if (outText != ''): text = outText resApi = api resAry.append(text) resText += str(text) + ',' if (resText == '') and ((api == 'free') or (api == 'google')): a_googleAPI = a_google_api.SpeechAPI() a_res = a_googleAPI.authenticate( 'tra', a_google_key.getkey('tra'), ) if (a_res == True): resAry = [] resAry.append('[Translate] ' + trnLang + ' (' + api + ')') l = 0 for text in inpAry: l += 1 if (l > 1): outText, api = a_googleAPI.translate( inpText=text, inpLang=inpLang, outLang=trnLang, ) if (outText != ''): text = outText resApi = api resAry.append(text) resText += str(text) + ',' if (resText != ''): return resText, resApi, resAry return '', '', None
rt = codecs.open(inpFile, 'r', 'shift_jis') for t in rt: inpText = (inpText + ' ' + str(t)).strip() rt.close rt = None outText = '' if (inpText != ''): #try: print(' ' + inpText) googleAPI = google_api.SpeechAPI() res = googleAPI.authenticate( 'tra', google_key.getkey('tra'), ) if (res == True): outText, api = googleAPI.translate( inpText=inpText, inpLang=inpLang, outLang=outLang, ) #except: #print(' Error!', sys.exc_info()[0]) #sys.exit() print(' ' + outText) if (outText != ''):
def cv(self, inpImage, inpLang='ja', ): res_text = None res_api = '' if (self.cv_key == None): print('GOOGLE: Not Authenticate Error !') else: lang = inpLang if (True): try: rb = open(inpImage, 'rb') photo = rb.read() rb.close rb = None photo_b64 = base64.b64encode(photo).decode('utf-8') url = self.cv_url headers = { 'Content-Type': 'application/json', } params = { 'key': self.cv_key, } json_data = { 'requests': [ { 'image': { 'content': photo_b64, }, 'imageContext': { 'languageHints': lang, }, 'features': [ { 'type': 'LABEL_DETECTION', 'maxResults': 10, }, { 'type': 'LANDMARK_DETECTION', 'maxResults': 10, }, { 'type': 'TEXT_DETECTION', 'maxResults': 10, }, ], }, ], } res = requests.post(url, headers=headers, params=params, data=json.dumps(json_data), timeout=self.timeOut, ) #print(res) if (res.status_code == 200): res_json = json.loads(res.text) #print(res_json) detect_label = '' detect_landmark = '' for response in res_json['responses']: try: for label in response['labelAnnotations']: text = label['description'] a_googleAPI = a_google_api.SpeechAPI() a_res = a_googleAPI.authenticate('tra', a_google_key.getkey('tra'), ) if (a_res == True): outText, api = a_googleAPI.translate(inpText=text, inpLang='en', outLang=lang, ) if (outText != ''): text = outText detect_label += str(text) + ',' detect_label = detect_label.strip() except: pass try: for landmark in response['landmarkAnnotations']: text = landmark['description'] a_googleAPI = a_google_api.SpeechAPI() a_res = a_googleAPI.authenticate('tra', a_google_key.getkey('tra'), ) if (a_res == True): outText, api = a_googleAPI.translate(inpText=text, inpLang='en', outLang=lang, ) if (outText != ''): text = outText detect_landmark += str(text) + ',' detect_landmark = detect_landmark.strip() except: pass res_text = {} res_text['label'] = detect_label res_text['landmark'] = detect_landmark except: pass return res_text, 'google' return None, ''
if (os.path.exists(outFile)): rt = codecs.open(outFile, 'r', 'shift_jis') for t in rt: outText = (outText + ' ' + str(t)).strip() rt.close rt = None print(' ' + outText) if (outText != ''): #try: googleAPI = google_api.SpeechAPI() res = googleAPI.authenticate( 'tts', google_key.getkey('tts'), ) if (res == True): res, api = googleAPI.vocalize(outText=outText, outLang=outLang, outFile=tmpFile) if (res != ''): sox = subprocess.Popen(['sox', tmpFile, '-d', '-q']) sox.wait() sox.terminate() sox = None #except: #print(' Error!', sys.exc_info()[0])