I need help with something, I have a python script that recognizes what is said in an audio and returns the text, it works very well when I run it without defining it as a function, but I can’t figure out how to organize my code to that it remains as a function and be able to execute it from UiPath
This is the script (I leave it in case someone needs it)
import speech_recognition as sr
import time
from pydub import AudioSegment
# files
src = 'C:\\Users\\TTssRpaConba\\Desktop\\audio.mp3'
dst = 'C:\\Users\\TTssRpaConba\\Desktop\\audio.wav'
# convert wav to mp3
audSeg = AudioSegment.from_mp3('C:\\Users\\TTssRpaConba\\Desktop\\audio.mp3')
audSeg.export(dst, format="wav")
r = sr.Recognizer()
with sr.AudioFile('C:\\Users\\TTssRpaConba\\Desktop\\audio.wav') as source:
# print("Say Something...")
audio = r.listen(source)
try:
print("Reading audio file. Please, wait a moment...")
text = r.recognize_google(audio, language='en-US')
time.sleep(1.5)
print(text)
except:
print("I am sorry! I can not understand!")
Excuse me if what I did is very clumsy but it’s my first time with python
def test();
import speech_recognition as sr
import time
from pydub import AudioSegment
# files
src = 'C:\\Users\\TTssRpaConba\\Desktop\\audio.mp3'
dst = 'C:\\Users\\TTssRpaConba\\Desktop\\audio.wav'
# convert wav to mp3
audSeg = AudioSegment.from_mp3('C:\\Users\\TTssRpaConba\\Desktop\\audio.mp3')
audSeg.export(dst, format="wav")
r = sr.Recognizer()
with sr.AudioFile('C:\\Users\\TTssRpaConba\\Desktop\\audio.wav') as source:
# print("Say Something...")
audio = r.listen(source)
try:
print("Reading audio file. Please, wait a moment...")
text = r.recognize_google(audio, language='en-US')
time.sleep(1.5)
print(text)
except:
print("I am sorry! I can not understand!")
return text
No worries. We all start from the same place
With python, indentation is critical. It uses indentation to distinguish blocks of code. I made minor changes to you code.
imported Path to make it easier to handle file paths.
set dst from src to make it more dynamic.
Try this (I haven’t tested this though)
import speech_recognition as sr
import time
from pydub import AudioSegment
from pathlib import Path
def convert_audio_to_text(src):
dst = Path(src).with_suffix('.wav')
# convert wav to mp3
audSeg = AudioSegment.from_mp3(src)
audSeg.export(dst, format="wav")
r = sr.Recognizer()
with sr.AudioFile(dst) as source:
# print("Say Something...")
audio = r.listen(source)
try:
print("Reading audio file. Please, wait a moment...")
text = r.recognize_google(audio, language='en-US')
time.sleep(1.5)
print(text)
except:
print("I am sorry! I can not understand!")
convert_audio_to_text('C:\\Users\\TTssRpaConba\\Desktop\\audio.mp3')
Perfect, it works, thank you very much, before closing the post, I wanted to consult something else, suddenly you know why the following error is generated
Get Python Object: Error converting Python object
I am sure that the return variable is type String and that is how I have it in uipat to be displayed
you are on the right track. The function doesn’t return anything which is why you are probably seeing that error.
try this modified code. added a return statement instead of print:
If it doesn’t work, try debugging in your IDE and let me know which line no is it erroring on.
import speech_recognition as sr
import time
from pydub import AudioSegment
from pathlib import Path
def convert_audio_to_text(src):
dst = Path(src).with_suffix('.wav')
# convert wav to mp3
audSeg = AudioSegment.from_mp3(src)
audSeg.export(dst, format="wav")
r = sr.Recognizer()
with sr.AudioFile(dst) as source:
audio = r.listen(source)
try:
print("Reading audio file. Please, wait a moment...")
text = r.recognize_google(audio, language='en-US')
time.sleep(1.5)
# print(text)
return text
except:
return "I am sorry! I can not understand!"
x = convert_audio_to_text('C:\\Users\\TTssRpaConba\\Desktop\\audio.mp3')
print(x)
Everything works fine running it from the IDE, but the problem I see is in UiPath, when executing the Get Python Object activity
The variable I get when executing the Invoke Python Method activity is ObpyText of type PythonObject, I pass this variable as Input to the Get Python Object activity and I have TypeArgument = String and the output is named Result of type String
Try passing the audio file path in Invoke Python Method activity as list.
Eg. {“audio.mp3”}
try replacing audio.mp3 with the full file path or relative path based on the python folder in python scope activity.
Due to a software limitation, this activity uses, by default, the installation location of the Python activities pack as the run directory for the script. To change this, and to be able to use relative paths inside the script, you can add the following code to the loaded script as a workaround:
Python
import sys
import os
sys.path.append(os.path.dirname(os.path.realpath(_file_)))
import <your module here>