文档ocr

主要对图片进行预处理,然后调用ocr接口

1,灰度图

复制代码
gray=cv.cvtColor(image,cv.COLOR_BGR2GRAY)

2,高斯去噪

复制代码
gray=cv.GaussianBlur(gray,(3,3),0)

3,边缘检测

复制代码
edged=cv.Canny(gray,60,260)

4,轮廓检测

复制代码
cnts=cv.findContours(edged.copy(),cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)[0]

5,查找轮廓面积最大的五个

复制代码
cnts=sorted(cnts,key=cv.contourArea,reverse=True)[:5]

6,近似轮廓,调整其形状

复制代码
screenCnts=[]
#遍历轮廓
for c in cnts:
    peri=cv.arcLength(c,True)
    approx=cv.approxPolyDP(c,0.02*peri,True)
    # cv.drawContours(image,[approx],-1,(0,255,0),1)
    # cv_show(image)
    if len(approx)==4:
        screenCnts.append(approx)
复制代码

7,透视变换

先对应好四个角点的坐标,然后按四边形的最长宽和长将多边形拉成矩形,最后利用变换矩阵进行透视变换

复制代码
#透视变换
#对应每个坐标
def order_points(pts):
    #一共4个坐标点
    rect=np.zeros((4,2),dtype="float32")

    #j计算左上,右下
    #x+y
    s=pts.sum(axis=1)
    rect[0]=pts[np.argmin(s)]
    rect[2]=pts[np.argmax(s)]

    #计算右上和左下
    #x-y
    diff=np.diff(pts,axis=1)
    rect[1]=pts[np.argmin(diff)]
    rect[3]=pts[np.argmax(diff)]

    return rect

def four_point_transform(img,pts):
    #获取坐标点
    rect=order_points(pts)
    (tl,tr,br,bl)=rect

    #计算输入的w和h值,选择四边形中的最长宽和长作为矫正后矩形的宽和长
    widthA=np.sqrt(((br[0]-bl[0])**2)+((br[1]-bl[1])**2))
    widthB=np.sqrt(((tr[0]-tl[0])**2)+((tr[1]-tl[1])**2))
    maxwidth=max(int(widthA),int(widthB))

    heightA=np.sqrt(((bl[0]-tl[0])**2)+((bl[1]-tl[1])**2))
    heightB=np.sqrt(((br[0]-tr[0])**2)+((br[1]-tr[1])**2))
    maxheight=max(int(heightA),int(heightB))

    #变换后对应坐标坐标
    dst=np.array([
        [0,0],
        [maxwidth-1,0],
        [maxwidth-1,maxheight-1],
        [0,maxheight-1]],dtype="float32"
    )

    #计算变换矩阵
    M=cv.getPerspectiveTransform(rect,dst)
    warped=cv.warpPerspective(img,M,(maxwidth,maxheight))

    return warped


for (i,sc) in enumerate(screenCnts):
    warped = four_point_transform(orig, sc.reshape(4,2)*ratio)
    cv_show(warped)
    warped=cv.cvtColor(warped,cv.COLOR_BGR2GRAY)
    # cv_show(warped)
    ref=cv.threshold(warped,190,255,cv.THRESH_BINARY)[1]
    cv_show(ref)
    cv.imwrite(f'./data/reciept/scan{i+1}.jpg',ref)

8,调用ocr

复制代码
import cv2
import pytesseract
from PIL import Image

# 基本识别
text = pytesseract.image_to_string(Image.open('./data/reciept/scan1.jpg'))
print(text)

img=cv2.imread('./data/reciept/scan1.jpg')
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
相关推荐
Albart5752 天前
保姆级教程|Python+DeepSeek-V4-Pro实现AI客服Demo 支持OCR扫描PDF+全格式RAG知识库(附完整源码)
人工智能·python·ocr·ai客服·大模型api·私有知识库·deepseek
SamChan902 天前
对比 4 种主流 PDF 文档解析方案:PyMuPDF vs pdfplumber vs Apache PDFBox vs 大模型 OCR
python·ai·pdf·ocr·apache·机器翻译
zhonyu鱼2 天前
Pot 翻译:开源免费的跨平台划词翻译与 OCR 工具
windows·macos·开源·ocr·开源软件
AI人工智能+3 天前
医疗器械生产备案凭证识别技术,以AI为核心驱动力,为构建透明、高效、安全的医疗器械流通秩序提供了坚实的技术支撑。
人脸识别·ocr·医疗器械生产备案凭证识别
Zguigo3 天前
OCR 核心原理 + 模型 + 检测与识别
数据库·ocr
楚识科技3 天前
从感知到执行:OCR RPA深度融合构建端到端智能流程自动化新范式
自动化·ocr·rpa
楚识科技3 天前
破除通用瓶颈——企业级OCR定制化开发的架构思维与实战范式
架构·ocr
程序员-李俞3 天前
Mistral OCR 4真正改变的不是“识字”:文档AI正在变成Agent的数据入口
人工智能·windows·ai作画·aigc·ocr·ai编程·ai写作
楚识科技4 天前
不动产证OCR赋能金融房产:从字段提取到核验闭环的落地逻辑
金融·数据挖掘·ocr
苏苏susuus4 天前
从像素到汉字:OCR 模型是如何依靠 CNN “学会“识字的?
人工智能·cnn·ocr