I've recently tried to develope a program that trasnlate the text within images. This without extracting the text since the program is meant for comics!
I tried using different sites like Yandex too, but settled on Google Trasnlate since it's the only service giving decent results. And I didn't really like the API option since it's not free and probably doesn't have the image feature.
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.implicitly_wait(15)
driver.get("https://translate.google.com/?sl=auto&tl=en&op=images")
button = driver.find_element('xpath', "//*[@id='yDmH0d']/c-wiz/div/div/div/div[2]/div[1]/div[3]/div[1]/div[1]/form[2]/div/div/button/span")
# Button is for accepting cookies
button.click()
driver.refresh()
driver.find_element( "id", "ucj-35").send_keys("/Users/marcojohanssontornblom/PycharmProjects/IV/Comic-Translater/1.jpg")
sleep(10)
But in trying to do so I've encountered a big problem; everytime I run my code, Google Trasnlate fails to recognize the text. Something it doesn't when using the manual browser. I have my suspissions on it being because of that I am using selelnium to autmoate the browser, but it can also be other causes of course.
How do I come around this problem and translate the Images?
A solution that worked for me was to connect selenium to my legitimate chrome browser, and it worked. (Selenium is probably detected by Google)
To do that, you have to start chrome with debugging port. In linux, you can do that by doing google-chrome --remote-debugging-port=3141
, where 3141
is the port number and you can change it (make sure it's above 1000).
Here's the modified code:
from selenium import webdriver
from time import sleep
options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", "127.0.0.1:3141")
driver = webdriver.Chrome(options)
driver.get("https://translate.google.com/?sl=auto&tl=en&op=images")
try:
button = driver.find_element('xpath', "//*[@id='yDmH0d']/c-wiz/div/div/div/div[2]/div[1]/div[3]/div[1]/div[1]/form[2]/div/div/button/span")
# Button is for accepting cookies
button.click()
driver.refresh()
sleep(1)
except Exception:
pass
driver.find_element( "id", "ucj-35").send_keys("path_to_your_image")
I just modified the code a bit so that if it doesn't find the cookies button it doesn't raise an error.
By the way, if you're on windows, you probably wanna know how to open google chrome with debugging port, I don't think you could do that with CMD alone. If you're not being able to do it at all, or the solution doesn't work, maybe try using Selenium Stealth.