def donusturme(img): img = Image.open(img) pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract' result = pytesseract.image_to_string(img) tr = Translater() tr.set_key( 'KEY') # Api key found on https://translate.yandex.com/developers/keys tr.set_from_lang('en') tr.set_to_lang('tr') tr.translate() tr.set_text(result) with open("magic.txt", mode="w") as file: file.write(tr.translate()) file.close() print(">ceviri tamamlandı...") return
def main(): from yandex import Translater tr=Translater() tr.set_key('trnsl.1.1.20180513T075008Z.0977a7606fc0921a.5fa98626b8990514e07e29aa73211e45f42e934f') tr.set_from_lang('fa') tr.set_to_lang('en') #tr.set_text('درود') tr.set_text(input("enter your word!\n")) print(tr.translate())
def translator(string, words): tr = Translater() for i in range(0, len(words)): if (words[i] == 'перевод') or (words[i] == 'переведи') or (words[i] == 'translate') or (words[i] == 'translator'): string = string.replace(words[i], '') tr.set_key(data.TRANSLATOR_KEY) tr.set_text(string) tr.set_from_lang('ru') tr.set_to_lang('en') return tr.translate()
def transl(file): tr = Translater() tr.set_key( 'trnsl.1.1.20181027T064233Z.3eb896622966c6cb.3685096a854ebdc502d7d01bdecdf7d490e88f9d' ) # Api key found on https://translate.yandex.com/developers/keys tr.set_text(file) tr.set_from_lang('en') tr.set_to_lang('hi') l = tr.translate() print(l) talkToMe(l)
def translate(self, text, toLang, fromLang=None): tr = Translater() tr.set_key(self.api_key) tr.set_text(text) if fromLang is None: fromLang = tr.detect_lang() if fromLang is None: fromLang = 'de' tr.set_from_lang(fromLang) tr.set_to_lang(toLang) return tr.translate()
def translate(values): # TODO: исправить перевод. Сейчас переводит только с русского на англ # message = getTranslatedText(values.message[values.message.find(' '):]) # fixme: временный переводчик tr = Translater() tr.set_key(os.environ['YANDEX_TRNSL_KEY']) tr.set_text(values.message[values.message.find(' '):]) fromLang = tr.detect_lang() if fromLang == 'ru': tr.set_from_lang('ru') tr.set_to_lang('en') else: tr.set_from_lang('en') tr.set_to_lang('ru') message = tr.translate() return message, None, None
class Translator(Activity): def __init__(self): locale.setlocale(locale.LC_ALL, '') self.translator = Translater() self.translator.set_key(YANDEX_API_KEY) self.mode = 'EN-->RU' self.default_markup = rkm([['Exit']]) def first_query(self, bot, update): self.__init__() self.mode = rkm([['EN-->RU'], ['RU-->EN']], one_time_keyboard=True) bot.sendMessage( chat_id=update.message.chat.id, text="Select languages:", reply_markup=self.mode ) def process(self, query, bot, update): result = "" if query == 'RU-->EN': self.translator.set_from_lang('ru') self.translator.set_to_lang('en') self.locale = "ru-RU" elif query == 'EN-->RU': self.translator.set_from_lang('en') self.translator.set_to_lang('ru') self.locale = "en-US" else: ans = update2text(update, self.locale) if ans != None: self.translator.set_text(ans) result = self.translator.translate() bot.sendMessage( chat_id=update.message.chat.id, text=result, reply_markup=self.default_markup ) if len(result) == 0: result = "What? try again, keep calm speak slowly and clearly."
def translate(): """ translate Russian okpd category descriptions to English """ f = codecs.open("okpd2.csv", "r", "cp1251") reader = csv.reader(f, delimiter=";") okpd_dict = {l[3]: l[1] for l in reader} names = {okpd_dict[k] for k in okpd_dict} names_eng = dict() names_eng_yandex = dict() try: names_eng = pickle.load(open("okpd_eng.pcl", 'rb')) except Exception as ex: print(ex) try: names_eng_yandex = pickle.load(open("okpd_eng_yandex.pcl", 'rb')) except Exception as ex: print(ex) translator = Translator() yandex_translator = Translater() yandex_translator.set_from_lang('ru') yandex_translator.set_to_lang('en') yandex_translator.set_key('trnsl.1.1.20170307T175252Z.243ee3f0dfd2721a.' '7af76b883f12f9f4fc2d861bab7d733112aeb66d') texts = [] total_len = 0 for n_i, n in enumerate(names): if n in names_eng or n in names_eng_yandex: continue else: yandex_translator.set_text(n) translation = yandex_translator.translate() names_eng_yandex[n] = translation print(n, translation) # time.sleep(1) for n_i, n in enumerate(names): if n in names_eng: continue else: if total_len > 14000: try: translations = translator.translate(texts) time.sleep(2) for t_i, t in texts: names_eng[t] = translations[t_i] print(len(names_eng), len(names)) except Exception as ex: pickle.dump(names_eng, open("okpd_eng.pcl", 'wb')) translator = Translator() time.sleep(1) texts = [n] total_len = len(n) else: texts.append(n) total_len += len(n) pickle.dump(names_eng, open("okpd_eng.pcl", 'wb')) pickle.dump(names_eng_yandex, open("okpd_eng_yandex.pcl", 'wb'))
tr.set_default_ui('ru') tr.set_key( 'trnsl.1.1.20180blah-BLAH-blah' ) # get your own free key here https://translate.yandex.com/developers/keys (press Create key) tr.set_from_lang('en') tr.set_to_lang('ru') files = "/path/to/folder/with/texts/to/be/translated/" # provide a real path to the input texts fh = [f for f in os.listdir(files) if f.endswith('.split')] for f in fh: print(f) lines_in = open(files + f.strip(), 'r').readlines() file_out = open( files + f + '.yt', 'w' ) # the output texts with a different extension are stored to the input folder for line in lines_in: # try: tr.set_text(line) trans = tr.translate() print(trans) file_out.write(trans) file_out.close() print("DONE") # tr.set_text('text to translate') # print(tr.translate())
def test(): os.chmod("/home/ubuntu/flaskapp/Img1.png", 0777) uid = pwd.getpwnam("nobody").pw_uid gid = grp.getgrnam("nogroup").gr_gid #os.chown("/home/ubuntu/flaskapp/static/Img1.png", uid, gid) im = Image.open("/home/ubuntu/flaskapp/Img1.png") im.save("/home/ubuntu/flaskapp/Img1.png", dpi=(300, 300)) ref = cv2.imread("/home/ubuntu/flaskapp/Img1.png") height, width, channels = ref.shape max_height = 1000 max_width = 700 scaling_factor = 1 print(height, width) # only shrink if img is bigger than required if max_height < height or max_width < width: # get scaling factor scaling_factor = max_height / float(height) if max_width / float(width) < scaling_factor: scaling_factor = max_width / float(width) # resize image ref = cv2.resize(ref, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) ref = cv2.resize(ref, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_AREA) morph = ref.copy() kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1)) morph = cv2.morphologyEx(morph, cv2.MORPH_CLOSE, kernel) morph = cv2.morphologyEx(morph, cv2.MORPH_OPEN, kernel) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2)) gradient_image = cv2.morphologyEx(morph, cv2.MORPH_GRADIENT, kernel) image_channels = np.split(np.asarray(gradient_image), 3, axis=2) channel_height, channel_width, _ = image_channels[0].shape for i in range(0, 3): _, image_channels[i] = cv2.threshold( ~image_channels[i], 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY) image_channels[i] = np.reshape(image_channels[i], newshape=(channel_height, channel_width, 1)) image_channels = np.concatenate( (image_channels[0], image_channels[1], image_channels[2]), axis=2) ref = image_channels ref = cv2.cvtColor(ref, cv2.COLOR_BGR2GRAY) ref = cv2.equalizeHist(ref) # create a CLAHE object (Arguments are optional). clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) ref = clahe.apply(ref) #cv2.imshow("Image", ref) os.environ[ "TESSDATA_PREFIX"] = "/usr/share/tesseract-ocr/tessdata/tesseract-ocr/tessdata" ref = Image.fromarray(ref) text = pytesseract.image_to_string(ref) print('Orginal Text Extracted from Image:', text) if not text: text = "No Text Available" tr = Translater() tr.set_key( 'trnsl.1.1.20180215T092213Z.aa1fc0e95288ebd7.dab36b649144ed25babf6247bcabd9885b5bb63f' ) tr.set_from_lang('en') tr.set_to_lang('hi') tr.set_text(text) TransText = tr.translate() #TransText="Not Available" print('Translated Text', TransText) return text, TransText