Skip to content

Instantly share code, notes, and snippets.

@KaushikShresth07
Last active September 12, 2023 05:05
Show Gist options
  • Select an option

  • Save KaushikShresth07/26d5dbe37eac5d2190b29c335e5dd24e to your computer and use it in GitHub Desktop.

Select an option

Save KaushikShresth07/26d5dbe37eac5d2190b29c335e5dd24e to your computer and use it in GitHub Desktop.
# Import necessary packages
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import warnings
from selenium.webdriver.chrome.service import Service
# Ignore unnecessary warnings
warnings.simplefilter("ignore")
try:
# Define the URL
url = "https://dictation.io/speech"
# Set up Chrome options
chrome_driver_path = 'Brain\\chromedriver.exe'
chrome_options = Options()
chrome_options.headless = False
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
chrome_options.add_argument('--log-level=3')
service = Service(chrome_driver_path)
chrome_options.add_argument("--use-fake-ui-for-media-stream") # Disable UI pop-ups for media access
chrome_options.add_argument("--use-fake-device-for-media-stream")
# Initialize the Chrome driver
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.maximize_window()
driver.get(url)
try:
driver.find_element(by=By.XPATH,value="/html/body/div[1]/div").click()
except:
pass
# Wait for the page to load
sleep(15)
# Execute JavaScript to enable microphone access
driver.execute_script('navigator.mediaDevices.getUserMedia({ audio: true })')
sleep(1)
# Click the "Clear" button to reset
clear_button_xpath = '/html/body/div[3]/section/div/div/div[2]/div/div[3]/div[2]/a[8]'
driver.find_element(by=By.XPATH, value=clear_button_xpath).click()
sleep(1)
# Click the start button
start_button_xpath = "/html/body/div[3]/section/div/div/div[2]/div/div[3]/div[1]/a"
driver.find_element(by=By.XPATH, value=start_button_xpath).click()
print("Microphone is turned on")
except Exception as e:
print("Error: Unable to configure the ChromeDriver properly.")
print("To resolve this error, make sure to set up the ChromeDriver correctly.")
print(e)
# Continuous loop for capturing and writing text
while True:
# Get the text from the dictation interface
text_element_xpath = '/html/body/div[3]/section/div/div/div[2]/div/div[2]'
text = driver.find_element(by=By.XPATH, value=text_element_xpath).text
if len(text) == 0:
pass
else:
# Click the "Clear" button to reset
driver.find_element(by=By.XPATH, value=clear_button_xpath).click()
text = text.strip()
# Write the text to a file
output_file_path = "Body\\SpeechRecognition.txt"
with open(output_file_path, "w") as file_write:
file_write.write(text)
@thekbbohara

thekbbohara commented Sep 11, 2023

Copy link
Copy Markdown

driver.execute_script("localStorage.setItem('language','en-in')") # To set lang t0 en-in
driver.execute_script("localStorage.setItem('dictation','< p >< br >< /p >')") # < remove space >
driver.refresh()
driver.execute_script("dictation('clear')") #to clean
this saturday i will upload code to my github you use this if you don't want to wait for 15 second

@thekbbohara

Copy link
Copy Markdown

if kaushik will tell me i will create a website just like this one and you can use it directly offline

@TonyMaxo

TonyMaxo commented Sep 12, 2023 via email

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment