0

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?

2 Answers 2

0

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.

2
  • I tried this and it worked perfectly! But can I somehow automate this process since I'm now having to type this command every time before running my code: /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222. I tried doing so by having os.system do this for me. But then the solution doesn't work anymore. Any clues? @eternal_white Commented Jan 14 at 16:23
  • Nvm. Just use package "keyboard" and "os.system" to open terminal via its path and write the command with keyboard. Then use pyautogui to press enter. Then it works. Link for keyboard Commented Jan 14 at 18:28
0

Maybe you can implement AI to read the image then convert that to text then run that through a built in translator then give the result, but that's just my solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.