如何使用OCR技术从票据中提取价格、品名等信息?

新手上路,请多包涵

需求,不同的票据识别出特定的信息,如价格、品名等?

ocr具体怎么用不清楚

阅读 639
2 个回答

推荐使用Python+PaddleOCR
1.path_to_your_invoice_image.jpg改成自己的图片路径
2.提取票据的信息根据正则表达式匹配

import cv2
import re
import paddleocr
from paddleocr import PaddleOCR

def preprocess_image(image_path):
    # 加载图像
    image = cv2.imread(image_path)
    # 转换为灰度图像
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # 二值化处理
    _, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    # 去噪
    denoised = cv2.fastNlMeansDenoising(binary, None, 30, 7, 21)
    return denoised

def extract_info(text):
    # 使用正则表达式提取票据中的关键信息
    prices = re.findall(r'\d+\.\d{2}', text)
    items = re.findall(r'品名[::]?\s*(\S+)', text)
    invoice_code = re.search(r'发票代码[::]?\s*(\d+)', text)
    invoice_number = re.search(r'发票号码[::]?\s*(\d+)', text)
    date = re.search(r'开票日期[::]?\s*([\d年\-月日]+)', text)
    return {
        '价格': prices,
        '品名': items,
        '发票代码': invoice_code.group(1) if invoice_code else None,
        '发票号码': invoice_number.group(1) if invoice_number else None,
        '开票日期': date.group(1) if date else None
    }

def ocr_image(image_path):
    # 初始化PaddleOCR
    ocr = PaddleOCR(use_angle_cls=True, lang='ch')
    # 预处理图像
    preprocessed_image = preprocess_image(image_path)
    # 进行OCR识别
    result = ocr.ocr(preprocessed_image, cls=True)
    # 提取识别结果
    text = '\n'.join([line[1][0] for line in result[0]])
    # 提取票据中的关键信息
    info = extract_info(text)
    return text, info

def process_invoice(image_path):
    recognized_text, info = ocr_image(image_path)
    return recognized_text, info

if __name__ == "__main__":
    image_path = 'path_to_your_invoice_image.jpg'
    recognized_text, invoice_info = process_invoice(image_path)
    print("识别的文本:\n", recognized_text)
    print("提取的票据信息:\n", invoice_info)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏