OpenCV/text2.py

57 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import cv2
import pytesseract
from matplotlib import pyplot as plt
# Укажите путь к исполняемому файлу tesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Users\ASUS\Desktop\fishrungame\treadBibliotek\tesseract.exe'
def draw_boxes(image, boxes, texts):
for i, box in enumerate(boxes):
x, y, x_w, y_h = box
cv2.rectangle(image, (x, y), (x + x_w, y + y_h), (0, 255, 0), 2)
cv2.putText(image, texts[i], (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
def process_image(image_path, coordinates_list):
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Применение OCR
custom_config = r'--oem 3 --psm 6 -l kir+eng+rus'
text = pytesseract.image_to_string(gray, config=custom_config)
# Получение координат и размеров квадратов
h, w, _ = image.shape
boxes = [(int(x), int(h - y_h), int(x_w - x), int(h - y)) for x, y, x_w, y_h in coordinates_list]
# Фильтрация квадратов по проценту (если нужно)
percentage_threshold = 1
filtered_boxes = [box for box in boxes if (box[2] * box[3]) / (w * h) * 100 >= percentage_threshold]
# Извлечение текста из области каждого квадрата
texts = []
for box in filtered_boxes:
x, y, x_w, y_h = box
crop_img = gray[y:y + y_h, x:x + x_w]
cropped_text = pytesseract.image_to_string(crop_img, config=custom_config)
texts.append(cropped_text)
print(f"Text inside the box at ({x}, {y}): {cropped_text}")
# Рисование квадратов и вывод текста
draw_boxes(image, filtered_boxes, texts)
# Отображение изображения с квадратами с использованием matplotlib
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title('Image with Boxes and Text')
plt.show()
if __name__ == "__main__":
image_path = 'img/img.png'
# Задайте координаты для каждого квадрата в формате (x, y, x_w, y_h)
coordinates_list = [(248, 399, 480, 330), (248, 399, 480, 282), (248, 399, 480, 230), (282, 399, 480, 190), (248, 399, 600, 160), (248, 399, 430, 120), (452, 399, 600, 103), (450, 395, 600, 60)]
process_image(image_path, coordinates_list)