Skip to content

Commit

Permalink
added Recognazer option
Browse files Browse the repository at this point in the history
  • Loading branch information
Shoker2 committed Jan 12, 2023
1 parent c74ecc1 commit a0a7032
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 34 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ TS2_ScreenTranslator.exe - саморазархивирующийся архив

Нажмите правой кнопкой мыши и в появившимся окно выберите с какого язывка вы хотите переводить текст "From" и на какой "To". Дальше нажмите "Apply"

![image](https://user-images.githubusercontent.com/66993983/209827732-da5fe956-fa73-46e9-962a-802bd7432091.png)
![image](https://user-images.githubusercontent.com/66993983/212059973-b173f60d-9abc-4ae9-a83e-4366c98d6fc3.png)

Теперь выделите область с текстом для перевода с помощью левой кнопкой мыши. После обработки, создастся окно на месте выделения с переведённым текстом.

Expand All @@ -84,11 +84,12 @@ TS2_ScreenTranslator.exe - саморазархивирующийся архив

### General

![image](https://user-images.githubusercontent.com/66993983/209829002-4afa3717-13c2-40f8-a68a-f8e1a8f984dd.png)
![image](https://user-images.githubusercontent.com/66993983/212059973-b173f60d-9abc-4ae9-a83e-4366c98d6fc3.png)

- From - С какого языка переводить текст
- To - На какой язык переводить текст
- Translator - Переводчик
- Recognizer - Выбор распознователя текста (Чтобы работал tesseract, нужно [скачать его отдельно и установить в папку приложения](https://github.com/UB-Mannheim/tesseract/wiki))
- Apply - Кнопка для применения настроек

### Font
Expand Down Expand Up @@ -123,7 +124,7 @@ TS2_ScreenTranslator.exe - саморазархивирующийся архив

![image](https://user-images.githubusercontent.com/66993983/209830709-3def4d19-c3fd-4714-9df2-eb7e447088cd.png)

Тут находится таблица для выполнения замен в переведённом тексте (Полезно, если вы стример и хотите не переводить некоторые слова)
Тут находится таблица для выполнения замен в тексте (Полезно, если вы стример и хотите не переводить некоторые слова)

- Колонка "Source" - Что должно быть заменено
- Колонка "Changed" - На что должно быть заменено
Expand Down
Binary file removed __pycache__/ScreenOutput.cpython-310.pyc
Binary file not shown.
Binary file removed __pycache__/ScreenShot.cpython-310.pyc
Binary file not shown.
14 changes: 12 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

from moduls.Snip import SnippingWidget as Snipper
from moduls.ScreenOutput import Ui_Output_Ui
from moduls.TextRecognitor import text_recognition
from moduls.TextRecognitor import text_recognition_easyocr
from moduls.TextRecognitor import text_recognition_tesseract
from moduls.Translator import Translator
from moduls.Settings import Ui_Settings
from moduls.Configure import Configure
Expand All @@ -37,6 +38,7 @@ def apply(self, Settings):
config.update('General', 'from', returned_settings['from']) # Сохраняю все выбранные значения в конфиг файл
config.update('General', 'to', returned_settings['to'])
config.update('General', 'translator', returned_settings['translator'])
config.update('General', 'recognitor', returned_settings['recognitor'])

config.update('Font', 'font', returned_settings['font'])
config.update('Font', 'font_size', str(returned_settings['font_size']))
Expand Down Expand Up @@ -83,6 +85,7 @@ def settings_win_setup(self, event):
Settings_UI.fromComboBox.setCurrentText(config.read('General', 'from')) # Устанавливаю значения из конфиг файла
Settings_UI.toComboBox.setCurrentText(config.read('General', 'to'))
Settings_UI.translatorComboBox.setCurrentText(config.read('General', 'translator'))
Settings_UI.recognizerComboBox.setCurrentText(config.read('General', 'recognitor'))

Settings_UI.fontComboBox.setCurrentText(config.read('Font', 'font'))
Settings_UI.fontSpinBox.setValue(int(config.read('Font', 'font_size')))
Expand Down Expand Up @@ -111,8 +114,15 @@ def end_screen_shot():
os.system('cls')

langs_easyocr = str(Settings_UI.langs_easyocr[config.read('General', 'from')]) # Получаю язык для распознования текста с картинки
langs_tesseract = str(Settings_UI.langs_tesseract[config.read('General', 'from')]) # Получаю язык для распознования текста с картинки
langs_translate = str(Settings_UI.langs_translate[config.read('General', 'from')]) # Получаю язык для распознования текста с картинки
text = ' '.join(text_recognition(image_path, [langs_easyocr])) # Получаю текст с изображения

if config.read('General', 'recognitor') == 'easyocr':
text = ' '.join(text_recognition_easyocr(image_path, [langs_easyocr])) # Получаю текст с изображения
elif config.read('General', 'recognitor') == 'tesseract':
text = ' '.join(text_recognition_tesseract(image_path, [langs_tesseract])) # Получаю текст с изображения

text = replace_from_list(text, config.read_dictionary('Change_list', 'json'))

if config.read('Output', 'original') == '1':
print(f'\n{text}')
Expand Down
3 changes: 2 additions & 1 deletion moduls/Configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def __init__(self, config_path):
self.config['General'] = {
'from': 'English',
'to': 'Russian',
'translator': 'Google'
'translator': 'Google',
'recognitor': 'easyocr'
}
self.config['Font'] = {
'font': 'Calibri',
Expand Down
152 changes: 129 additions & 23 deletions moduls/Settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class Ui_Settings(object):
'Azerbaijani':'az',
'Belarusian':'be',
'Bulgarian':'bg',
'Bhojpuri':'bho',
'Bengali':'bn',
'Bosnian':'bs',
'Chinese (simplified)':'zh-CN',
Expand All @@ -33,7 +32,6 @@ class Ui_Settings(object):
'Persian':'fa',
'French':'fr',
'Irish':'ga',
'Konkani':'gom',
'Hindi':'hi',
'Croatian':'hr',
'Hungarian':'hu',
Expand All @@ -46,7 +44,6 @@ class Ui_Settings(object):
'Latin':'la',
'Lithuanian':'lt',
'Latvian':'lv',
'Maithili':'mai',
'Maori':'mi',
'Mongolian':'mn',
'Marathi':'mr',
Expand Down Expand Up @@ -83,7 +80,6 @@ class Ui_Settings(object):
'Azerbaijani':'az',
'Belarusian':'be',
'Bulgarian':'bg',
'Bhojpuri':'bho',
'Bengali':'bn',
'Bosnian':'bs',
'Chinese (simplified)':'ch_sim',
Expand All @@ -98,7 +94,6 @@ class Ui_Settings(object):
'Persian':'fa',
'French':'fr',
'Irish':'ga',
'Konkani':'gom',
'Hindi':'hi',
'Croatian':'hr',
'Hungarian':'hu',
Expand All @@ -111,7 +106,6 @@ class Ui_Settings(object):
'Latin':'la',
'Lithuanian':'lt',
'Latvian':'lv',
'Maithili':'mai',
'Maori':'mi',
'Mongolian':'mn',
'Marathi':'mr',
Expand Down Expand Up @@ -141,6 +135,70 @@ class Ui_Settings(object):
'Vietnamese':'vi'
}

langs_tesseract = {
'Afrikaans':'afr',
'Arabic':'amh',
'Assamese':'asm',
'Azerbaijani':'aze',
'Belarusian':'bel',
'Bulgarian':'bul',
'Bengali':'ben',
'Bosnian':'bos',
'Chinese (simplified)':'chi_sim',
'Chinese (traditional)':'chi_tra',
'Czech':'ces',
'Welsh':'cym',
'Danish':'dan',
'German':'deu',
'English':'eng',
'Spanish':'spa',
'Estonian':'est',
'Persian':'fas',
'French':'fra',
'Irish':'gle',
'Hindi':'hin',
'Croatian':'hrv',
'Hungarian':'hun',
'Indonesian':'ind',
'Icelandic':'isl',
'Italian':'ita',
'Japanese':'jpn',
'Kannada':'kan',
'Korean':'kor',
'Latin':'lat',
'Lithuanian':'lit',
'Latvian':'lav',
'Maori':'mri',
'Mongolian':'mon',
'Marathi':'mar',
'Malay':'msa',
'Maltese':'mlt',
'Nepali':'nep',
'Dutch':'nld',
'Norwegian':'nor',
'Polish':'pol',
'Portuguese':'por',
'Romanian':'ron',
'Russian':'rus',
'Serbian':'srp',
'Slovak':'slk',
'Slovenian':'slv',
'Albanian':'sqi',
'Swedish':'swe',
'Swahili':'swa',
'Tamil':'tam',
'Telugu':'tel',
'Thai':'tha',
'Tajik':'tgk',
'Turkish':'tur',
'Uyghur':'uig',
'Urdu':'urd',
'Uzbek':'uzb',
'Vietnamese':'vie'
}

recognitors = ['easyocr', 'tesseract']

def setupUi(self, Settings, icon_path=''):
if icon_path != '':
Settings.setWindowIcon(QtGui.QIcon(icon_path))
Expand All @@ -159,39 +217,83 @@ def setupUi(self, Settings, icon_path=''):
self.tabWidget.setObjectName("tabWidget")
self.general_page = QtWidgets.QWidget()
self.general_page.setObjectName("general_page")
self.Font_label_2 = QtWidgets.QLabel(self.general_page)
self.Font_label_2.setGeometry(QtCore.QRect(10, 10, 41, 31))
self.scrollArea_General = QtWidgets.QScrollArea(self.general_page)
self.scrollArea_General.setGeometry(QtCore.QRect(0, 0, Settings.size().width() - 5, Settings.size().height() - 24))
self.scrollArea_General.setWidgetResizable(True)
self.scrollArea_General.setObjectName("scrollArea_General")
self.scrollAreaWidgetContents_2 = QtWidgets.QWidget()
self.scrollAreaWidgetContents_2.setGeometry(QtCore.QRect(0, 0, 352, 180))
self.scrollAreaWidgetContents_2.setObjectName("scrollAreaWidgetContents_2")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents_2)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setContentsMargins(0, -1, -1, -1)
self.horizontalLayout.setObjectName("horizontalLayout")
self.Font_label_2 = QtWidgets.QLabel(self.scrollAreaWidgetContents_2)
font = QtGui.QFont()
font.setPointSize(10)
self.Font_label_2.setFont(font)
self.Font_label_2.setObjectName("Font_label_2")
self.fromComboBox = QtWidgets.QComboBox(self.general_page)
self.fromComboBox.setGeometry(QtCore.QRect(60, 16, 111, 22))
self.horizontalLayout.addWidget(self.Font_label_2)
self.fromComboBox = QtWidgets.QComboBox(self.scrollAreaWidgetContents_2)
self.fromComboBox.setObjectName("fromComboBox")
self.Font_label_3 = QtWidgets.QLabel(self.general_page)
self.Font_label_3.setGeometry(QtCore.QRect(220, 10, 31, 31))
self.horizontalLayout.addWidget(self.fromComboBox)
self.verticalLayout_2.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setContentsMargins(0, -1, -1, -1)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.Font_label_3 = QtWidgets.QLabel(self.scrollAreaWidgetContents_2)
font = QtGui.QFont()
font.setPointSize(10)
self.Font_label_3.setFont(font)
self.Font_label_3.setObjectName("Font_label_3")
self.toComboBox = QtWidgets.QComboBox(self.general_page)
self.toComboBox.setGeometry(QtCore.QRect(255, 16, 110, 22))
self.horizontalLayout_2.addWidget(self.Font_label_3)
self.toComboBox = QtWidgets.QComboBox(self.scrollAreaWidgetContents_2)
self.toComboBox.setObjectName("toComboBox")
self.Font_label_4 = QtWidgets.QLabel(self.general_page)
self.Font_label_4.setGeometry(QtCore.QRect(10, 55, 91, 31))
self.horizontalLayout_2.addWidget(self.toComboBox)
self.verticalLayout_2.addLayout(self.horizontalLayout_2)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setContentsMargins(0, -1, -1, -1)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.Font_label_4 = QtWidgets.QLabel(self.scrollAreaWidgetContents_2)
font = QtGui.QFont()
font.setPointSize(10)
self.Font_label_4.setFont(font)
self.Font_label_4.setObjectName("Font_label_4")
self.translatorComboBox = QtWidgets.QComboBox(self.general_page)
self.translatorComboBox.setGeometry(QtCore.QRect(90, 58, 151, 26))
self.horizontalLayout_4.addWidget(self.Font_label_4)
self.translatorComboBox = QtWidgets.QComboBox(self.scrollAreaWidgetContents_2)
font = QtGui.QFont()
font.setPointSize(9)
self.translatorComboBox.setFont(font)
self.translatorComboBox.setObjectName("translatorComboBox")
self.pushButton = QtWidgets.QPushButton(self.general_page)
self.pushButton.setGeometry(QtCore.QRect(275, 57, 93, 28))
self.horizontalLayout_4.addWidget(self.translatorComboBox)
self.verticalLayout_2.addLayout(self.horizontalLayout_4)
self.horizontalLayout_6 = QtWidgets.QHBoxLayout()
self.horizontalLayout_6.setContentsMargins(0, -1, -1, -1)
self.horizontalLayout_6.setObjectName("horizontalLayout_6")
self.Font_label_8 = QtWidgets.QLabel(self.scrollAreaWidgetContents_2)
font = QtGui.QFont()
font.setPointSize(10)
self.Font_label_8.setFont(font)
self.Font_label_8.setObjectName("Font_label_8")
self.horizontalLayout_6.addWidget(self.Font_label_8)
self.recognizerComboBox = QtWidgets.QComboBox(self.scrollAreaWidgetContents_2)
font = QtGui.QFont()
font.setPointSize(9)
self.recognizerComboBox.setFont(font)
self.recognizerComboBox.setObjectName("recognizerComboBox")
self.horizontalLayout_6.addWidget(self.recognizerComboBox)
self.verticalLayout_2.addLayout(self.horizontalLayout_6)
self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
self.horizontalLayout_5.setContentsMargins(0, -1, -1, -1)
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_5.addItem(spacerItem)
self.pushButton = QtWidgets.QPushButton(self.scrollAreaWidgetContents_2)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout_5.addWidget(self.pushButton)
self.verticalLayout_2.addLayout(self.horizontalLayout_5)
self.scrollArea_General.setWidget(self.scrollAreaWidgetContents_2)
self.tabWidget.addTab(self.general_page, "")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
Expand Down Expand Up @@ -287,7 +389,7 @@ def setupUi(self, Settings, icon_path=''):
self.change_list_page = QtWidgets.QWidget()
self.change_list_page.setObjectName("change_list_page")
self.tableWidget = QtWidgets.QTableWidget(self.change_list_page)
self.tableWidget.setGeometry(QtCore.QRect(0, 0, 346, 91))
self.tableWidget.setGeometry(QtCore.QRect(0, 0, 365, 91))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(2)
self.tableWidget.setRowCount(1)
Expand All @@ -304,7 +406,7 @@ def setupUi(self, Settings, icon_path=''):
self.pushButton.clicked.connect(lambda: self.apply(Settings))
self.tableWidget.cellChanged.connect(lambda: self.table_changed())

cell_width = int((Settings.size().width() - 40)/2)
cell_width = int((Settings.size().width() - 60)/2)
self.tableWidget.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.tableWidget.setColumnWidth(0, cell_width)
self.tableWidget.setColumnWidth(1, cell_width)
Expand All @@ -320,6 +422,7 @@ def retranslateUi(self, Settings):
self.Font_label_2.setText("From")
self.Font_label_3.setText("To")
self.Font_label_4.setText("Translator")
self.Font_label_8.setText("Recognizer")
self.pushButton.setText("Apply")
self.tabWidget.setTabText(self.tabWidget.indexOf(self.general_page), "General")
self.Font_label.setText("Font")
Expand Down Expand Up @@ -363,7 +466,6 @@ def table_changed(self):
def set_table_from_dictionary(self, dic: dict):
row_count = len(dic)


if row_count > 0:
self.tableWidget.setRowCount(row_count)

Expand Down Expand Up @@ -398,6 +500,7 @@ def get_all(self):
get['from'] = self.fromComboBox.currentText()
get['to'] = self.toComboBox.currentText()
get['translator'] = self.translatorComboBox.currentText()
get['recognitor'] = self.recognizerComboBox.currentText()
get['font'] = self.fontComboBox.currentText()
get['font_size'] = self.fontSpinBox.value()

Expand Down Expand Up @@ -462,6 +565,9 @@ def set_comboboxs(self):
for lang in self.langs_easyocr.keys():
self.toComboBox.addItem(lang)
self.fromComboBox.addItem(lang)

for recognizer in self.recognitors:
self.recognizerComboBox.addItem(recognizer)

def apply(self, Settings):
Settings.hide()
Expand Down
14 changes: 11 additions & 3 deletions moduls/TextRecognitor.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import easyocr
import pytesseract
from PIL import Image

def text_recognition(file_path:str, langs:list = ["en", "ru"], detail=0):
def text_recognition_easyocr(file_path:str, langs:list = ["en", "ru"]):
reader = easyocr.Reader(langs)
result = reader.readtext(file_path, detail=detail)
result = reader.readtext(file_path, detail=0)

return result

def text_recognition_tesseract(file_path:str, langs:list = ["en"]):
img = Image.open(file_path)
result = pytesseract.image_to_string(img, lang = langs[0])

return result

if __name__ == '__main__':
text = text_recognition('./img/Image.png')
text = text_recognition_tesseract('./img/Image.png')
print(text)
Binary file modified requirements.txt
Binary file not shown.
Loading

0 comments on commit a0a7032

Please sign in to comment.