Обнаружение арифметических операторов в изображенииPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Обнаружение арифметических операторов в изображении

Сообщение Anonymous »

Я создал сценарий OCR, используя keras_ocr. Входные данные представляют собой блок-схему (оттенки серого). Я хочу извлечь тексты и координаты фигур изображения блок-схемы. Однако он не извлекает арифметические операторы, такие как «+,-,*,/». Иногда он также не обнаруживает числовые значения. Вот мой полный сценарий.

Код: Выделить всё

# Import necessary libraries
import os
import matplotlib.pyplot as plt
import keras_ocr
import cv2
import numpy as np
from google.colab import drive
from symspellpy.symspellpy import SymSpell, Verbosity
import pkg_resources

class OCRProcessor:
def __init__(self):
# Create a pipeline for OCR processing
self.pipeline = keras_ocr.pipeline.Pipeline()

def __get_bbox(self, image_path):
try:
# Read the image using OpenCV
image = cv2.imread(image_path)
if image is None:
raise ValueError(f"Image at path {image_path} could not be read.")

# Convert the image to RGB (keras-ocr expects RGB images)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Use the OCR pipeline to detect text
images = keras_ocr.tools.read(image_path)
self.image = images
prediction_groups = self.pipeline.recognize([images])

if not prediction_groups or not prediction_groups[0]:
return [], []

# Extract the bounding boxes and text
texts = []
results = []
for text, box in prediction_groups[0]:
texts.append(text)
xs, ys = set(), set()
for x in box:
xs.add(x[0])
ys.add(x[1])
results.append(list(map(int, [min(xs), min(ys), max(xs), max(ys)])))  # ymin, xmin, ymax, xmax

return texts, results
except Exception as e:
print(f"An error occurred in __get_bbox: {e}")
return [], []

def process_image(self, image_path):
return self.__get_bbox(image_path)

# Define the function to correct text
def correct_text(text_array):
# Initialize SymSpell object
sym_spell = SymSpell(max_dictionary_edit_distance=2, prefix_length=7)

# Load the dictionary
dictionary_path = pkg_resources.resource_filename(
"symspellpy", "frequency_dictionary_en_82_765.txt")
sym_spell.load_dictionary(dictionary_path, term_index=0, count_index=1)

corrected_text_array = []
for text in text_array:
suggestions = sym_spell.lookup(text, Verbosity.CLOSEST, max_edit_distance=2)
if suggestions:
corrected_text_array.append(suggestions[0].term)
else:
corrected_text_array.append(text)
return corrected_text_array

# Main function
def main(image_path):
ocr_processor = OCRProcessor()
ex_text, ex_co = ocr_processor.process_image(image_path)
if not ex_text:
print(f"No text detected in the image at path {image_path}.")
return [], [], []

# Correct the extracted text
cr_text = correct_text(ex_text)

# Print the results
print("Extracted Texts:", ex_text)
print("Corrected Texts:", cr_text)
print("Extracted Coordinates:", ex_co)

return ex_text, cr_text, ex_co

# Example usage (you can update the image path as needed)
image_path = '/content/Test2.jpg'
ex_text, cr_text, ex_co = main(image_path)

ex_shape, ex_coor = detect_shapes(image_path)

# Print or use the results
print("Detected Shapes:", ex_shape)
print("Coordinates for Shapes:", ex_coor)
Есть ли какое-либо решение этой проблемы?

Подробнее здесь: https://stackoverflow.com/questions/787 ... n-an-image
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»