есть ли ошибки в коде: from threading import Thread from time import sleep from PIL…
- User
есть ли ошибки в коде: from threading import Thread from time import sleep from PIL import ImageGrab from googletrans import Translator import pytesseract from colorama import Fore import cv2 import numpy as np import re import keyboard import pyautogui
custom_conf = "--psm 11 --oem 1" translator = Translator() sharpening_kernel = np.array([ [-1, -1, -1], [-1, 9, -1], [-1, -1, -1] ], dtype=np.float32) preset = 1 last_result = None last_phone = None
def tr(image): global last_result pytesseract.pytesseract.tesseract_cmd = r"D:\Tesseract\tesseract.exe" result = pytesseract.image_to_string(image, config=custom_conf, output_type='string') if result != last_result: try: text = re.sub("\n", " - ", result, count=1) text = re.sub("\n", " ", text) text = text.replace('|', 'I') print(Fore.RED + '--en--') print(Fore.RED + text) translate = translator.translate(text, dest='ru') print(Fore.GREEN + '--ru--') print(Fore.GREEN + translate.text) last_result = result except: pass
def tr_phone(image, image_phone): global last_result global last_phone pytesseract.pytesseract.tesseract_cmd = r"D:\Tesseract\tesseract.exe" phone = pytesseract.image_to_string(image_phone, config=custom_conf, output_type='string') if phone != last_phone: try: ptext = re.sub("\n", " - ", phone, count=1) ptext = re.sub("\n", " ", ptext) ptext = ptext.replace('|', 'I') print(Fore.CYAN + 'Phone') print(Fore.RED + '--en--') print(Fore.RED + ptext) translate = translator.translate(ptext, dest='ru') print(Fore.GREEN + '--ru--') print(Fore.GREEN + translate.text) last_phone = phone except: pass result = pytesseract.image_to_string(image, config=custom_conf, output_type='string') if result != last_result: try: text = re.sub("\n", " - ", result, count=1) text = re.sub("\n", " ", text) text = text.replace('|', 'I') print(Fore.CYAN + 'Kiruy') print(Fore.RED + '--en--') print(Fore.RED + text) translate = translator.translate(text, dest='ru') print(Fore.GREEN + '--ru--') print(Fore.GREEN + translate.text) last_result = result except: pass
def tr_cut_mess(image): global last_result pytesseract.pytesseract.tesseract_cmd = r"D:\Tesseract\tesseract.exe" result = pytesseract.image_to_string(image, config=custom_conf, output_type='string') if result != last_result: try: text = re.sub("\n", " ", result) text = text.replace('|', 'I') print(Fore.RED + '--en--') print(Fore.RED + text) translate = translator.translate(text, dest='ru') print(Fore.GREEN + '--ru--') print(Fore.GREEN + translate.text) last_result = result except: pass
def crop(image): match preset: case 1: crop_sub = image[765:1000, 450:1480] preprocessing(crop_sub) case 2: crop_phone = image[100:260, 500:1500] crop_sub = image[765:1000, 450:1480] preprocessing_phone(crop_sub, crop_phone) case 3: crop_cut = image[880:1050, 440:1480] preprocessing_cutscene(crop_cut) case 4: mess_crop = image[400:875, 630:1320] preprocessing_message(mess_crop)
def preprocessing(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) _, image = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) image = cv2.medianBlur(image, 3) image = cv2.filter2D(image, -1, sharpening_kernel) tr(image)
- Assistant
There don't appear to be any syntax errors in the code. However, without knowing the specific requirements or intended functionality, it is not possible to determine whether there are logical or functional errors in the code.
- User
можно ли его как-то оптимизировать?
- Assistant
- User
а на русском можешь ответить?
- Assistant
Да, конечно! Вот оптимизированная версия кода:
import cv2 import numpy as np import pytesseract from googletrans import Translator from PIL import ImageGrab from threading import Thread from time import sleep import re import keyboard from colorama import Fore import pyautogui custom_conf = "--psm 11 --oem 1" translator = Translator() sharpening_kernel = np.array([ [-1, -1, -1], [-1, 9, -1], [-1, -1, -1] ], dtype=np.float32) pytesseract.pytesseract.tesseract_cmd = r"D:\Tesseract\tesseract.exe" class OCRTranslator: def __init__(self): self.last_result = None self.last_phone = None self.preset = 1 def translate(self, text): text = re.sub("\n", " - ", text) text = text.replace('|', 'I') print(Fore.GREEN + '--ru--') try: translate = translator.translate(text, dest='ru') print(Fore.GREEN + translate.text) except: print(Fore.GREEN + text) def preprocess_image(self, image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) _, image = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) image = cv2.medianBlur(image, 3) image = cv2.filter2D(image, -1, sharpening_kernel) return image def process_dialog(self, image): image = self.preprocess_image(image) result = pytesseract.image_to_string(image, config=custom_conf, output_type='string') if result != self.last_result: print(Fore.RED + '--en--') print(Fore.RED + result) self.translate(result) self.last_result = result def process_phone_dialog(self, image, image_phone): image = self.preprocess_image(image) gray = cv2.cvtColor(image_phone, cv2.COLOR_BGR2GRAY) _, image_phone = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) image_phone = cv2.medianBlur(image_phone, 3) image_phone = cv2.filter2D(image_phone, -1, sharpening_kernel) phone = pytesseract.image_to_string(image_phone, config=custom_conf, output_type='string') if phone != self.last_phone: print(Fore.CYAN + 'Телефон') print(Fore.RED + '--en--') print(Fore.RED +
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.