Anonymous
Как я могу удалить горизонтальную линию из изображения капчи кадрирования в Python
Сообщение
Anonymous » 09 мар 2026, 22:47
Я пытаюсь получить кадрированное изображение без горизонтальной линии и не могу получить ошибку в выводе, но он работает не так, как я хочу. Как я могу удалить горизонтальную линию из изображения капчи кадрирования в Python
Есть ли кто-нибудь, как я могу это сделать в Python?
вот мой код:
Код: Выделить всё
import cv2
import numpy as np
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
def solve_final_step(path):
img = cv2.imread(path)
if img is None: return
h_orig, w_orig = img.shape[:2]
roi = img[h_orig//2-35:h_orig//2+35, w_orig//2-110:w_orig//2+110]
lower_black = np.array([0, 0, 0])
upper_black = np.array([80, 80, 80])
mask = cv2.inRange(roi, lower_black, upper_black)
resized = cv2.resize(mask, None, fx=6, fy=6, interpolation=cv2.INTER_NEAREST)
v_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 5))
cleaned = cv2.morphologyEx(resized, cv2.MORPH_OPEN, v_kernel)
ero_kernel = np.ones((2, 2), np.uint8)
eroded = cv2.erode(cleaned, ero_kernel, iterations=1)
dil_kernel = np.ones((2, 2), np.uint8)
repaired = cv2.dilate(eroded, dil_kernel, iterations=1)
cv2.imwrite("debug_clean.png", repaired)
h_f, w_f = repaired.shape
part_w = w_f
captcha = ""
whitelist = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
for i in range(4):
x_s = i * part_w
x_e = (i + 1) * part_w
char = repaired[:, x_s:x_e]
char_clean = cv2.copyMakeBorder(char[5:-5, 5:-5], 5, 5, 5, 5, cv2.BORDER_CONSTANT, value=0)
char_inv = cv2.bitwise_not(char_clean)
cv2.imwrite(f"char_{i}.png", char_inv)
config = f'--oem 3 --psm 10 -c tessedit_char_whitelist={whitelist}'
text = pytesseract.image_to_string(char_inv, config=config).strip()
if text:
captcha += text[0]
else:
config_alt = f'--oem 3 --psm 8 -c tessedit_char_whitelist={whitelist}'
text_alt = pytesseract.image_to_string(char_inv, config=config_alt).strip()
captcha += text_alt[0] if text_alt else "?"
print("\n" + "="*20)
print(f"solve: {captcha}")
print("="*20)
solve_final_step("3.jpg")
оригинал
вывод
Подробнее здесь:
https://stackoverflow.com/questions/799 ... -in-python
1773085625
Anonymous
Я пытаюсь получить кадрированное изображение без горизонтальной линии и не могу получить ошибку в выводе, но он работает не так, как я хочу. Как я могу удалить горизонтальную линию из изображения капчи кадрирования в Python Есть ли кто-нибудь, как я могу это сделать в Python? вот мой код: [code]import cv2 import numpy as np import pytesseract pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe" def solve_final_step(path): img = cv2.imread(path) if img is None: return h_orig, w_orig = img.shape[:2] roi = img[h_orig//2-35:h_orig//2+35, w_orig//2-110:w_orig//2+110] lower_black = np.array([0, 0, 0]) upper_black = np.array([80, 80, 80]) mask = cv2.inRange(roi, lower_black, upper_black) resized = cv2.resize(mask, None, fx=6, fy=6, interpolation=cv2.INTER_NEAREST) v_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 5)) cleaned = cv2.morphologyEx(resized, cv2.MORPH_OPEN, v_kernel) ero_kernel = np.ones((2, 2), np.uint8) eroded = cv2.erode(cleaned, ero_kernel, iterations=1) dil_kernel = np.ones((2, 2), np.uint8) repaired = cv2.dilate(eroded, dil_kernel, iterations=1) cv2.imwrite("debug_clean.png", repaired) h_f, w_f = repaired.shape part_w = w_f captcha = "" whitelist = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" for i in range(4): x_s = i * part_w x_e = (i + 1) * part_w char = repaired[:, x_s:x_e] char_clean = cv2.copyMakeBorder(char[5:-5, 5:-5], 5, 5, 5, 5, cv2.BORDER_CONSTANT, value=0) char_inv = cv2.bitwise_not(char_clean) cv2.imwrite(f"char_{i}.png", char_inv) config = f'--oem 3 --psm 10 -c tessedit_char_whitelist={whitelist}' text = pytesseract.image_to_string(char_inv, config=config).strip() if text: captcha += text[0] else: config_alt = f'--oem 3 --psm 8 -c tessedit_char_whitelist={whitelist}' text_alt = pytesseract.image_to_string(char_inv, config=config_alt).strip() captcha += text_alt[0] if text_alt else "?" print("\n" + "="*20) print(f"solve: {captcha}") print("="*20) solve_final_step("3.jpg") [/code] оригинал [img]https://i.sstatic.net/4H7OtDLj.png[/img] вывод [img]https://i.sstatic.net/oT4gmTYA.png[/img] Подробнее здесь: [url]https://stackoverflow.com/questions/79904103/how-can-i-horizontal-line-removal-from-crop-captcha-picture-in-python[/url]