我想从这张图片中的表中提取数据,我使用 cv2 和 pytesseract 但我没有得到可靠的结果。这是我的代码和我的图像。
import cv2
import pytesseract
from PIL import Image
def preprocess_image(image_path):
# Load the image using OpenCV
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# Apply thresholding (binarization)
_, thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# Invert the image colors
inverted_img = cv2.bitwise_not(thresh)
# Convert the thresholded and inverted image back to PIL image format
pil_img = Image.fromarray(inverted_img)
return pil_img
def image_to_text(path):
# Path to your Tesseract executable
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
# Preprocess the image
img = preprocess_image(path)
# Perform OCR using Tesseract
text = pytesseract.image_to_string(img, config='--psm 4 --oem 2 ', lang='rus')
return text
print(image_to_text('concatenated_image3.jpg'))
要使用 Tesseract OCR 检测并打印图像中的文本,您可以使用 Python 和 OpenCV 进行图像预处理,并使用 Pytesseract 进行 OCR。
import cv2
import pytesseract
import re
class TextDetector:
def __init__(self, image_path, language='rus'):
self.image_path = image_path
self.language = language
self.text_results = None
def preprocess_image(self):
img = cv2.imread(self.image_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
return thresh
def detect_text_boxes(self):
img = self.preprocess_image()
results = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT, lang=self.language)
img_height, img_width = img.shape[:2]
text_data = []
for i in range(len(results['text'])):
x, y, w, h = results['left'][i], results['top'][i], results['width'][i], results['height'][i]
text = results['text'][i]
# Filter out non-alphanumeric characters using regex
filtered_text = re.sub(r'[^A-Za-z0-9А-Яа-я]', '', text)
# Only consider alphanumeric text
if filtered_text.isalnum():
text_data.append({'Text': filtered_text, 'Bounding Box': (x, y, w, h)})
self.text_results = text_data
def get_text_results(self):
if self.text_results is None:
self.detect_text_boxes()
return self.text_results
if __name__ == "__main__":
detector = TextDetector('KnPrd3fG.png')
text_results = detector.get_text_results()
# Print detected text
for result in text_results:
print(f"Detected Text: {result['Text']}")
以下是如何使用 Pandas 将检测到的文本保存到 CSV 文件中:
import pandas as pd
class TextProcessor:
def __init__(self, text_detector):
self.text_detector = text_detector
self.text_data = None
def process_text(self):
text_results = self.text_detector.get_text_results()
if text_results:
df = pd.DataFrame(text_results)
self.text_data = df
def save_to_csv(self, file_path):
if self.text_data is None:
self.process_text()
if self.text_data is not None:
self.text_data.to_csv(file_path, index=False)
print(f"Data saved to '{file_path}' successfully.")
else:
print("No text data to save. Please run 'process_text' first.")
if __name__ == "__main__":
detector = TextDetector('KnPrd3fG.png')
processor = TextProcessor(detector)
processor.save_to_csv('text_data.csv')