python的easyocr图像文字识别

一、基本流程

        1.OpenCV图像基础操作,如读取,灰度转换等

        2.阈值操作,如二值化

        3.canny边缘检测以及boundingBox构建

        4.卷积核构建

        5.膨胀操作

        6.形态学操作如close操作

        7.用pyplot查看图片,便于debug

        8.高斯滤波

        9.easyocr库函数调用

        10.仿射变换

二、代码如下

import easyocr
import re
import cv2
import numpy as np
import math

#一、利用opencv处理图片
def simpledeal(img):
    img_str = cv2.imread(img)
    # 对照片进行灰度处理
    img_gray = cv2.imread(img,0)
    #进行高斯滤波去掉环境中的噪点
    img_blur = cv2.GaussianBlur(img_gray,(5,5),1)
    cv2.imwrite('result.jpg', img_blur)
    # #边缘检测
    # edges = cv2.Canny(img_blur, 10, 400)
    # kernel = np.ones((2, 2), np.uint8)
    # rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 15))
    # edges_close = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, rectKernel)
    # edges_dilate = cv2.dilate(edges_close, kernel, iterations=3)
    #
    # contours, hierarchy = cv2.findContours(edges_dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    # contours = sorted(contours, key=lambda cnts: cv2.arcLength(cnts, True), reverse=True)
    #
    # img_copy = img_str.copy()
    # res = cv2.drawContours(img_copy, contours, 0, (0, 0, 255), 2)
    # img_copy = img_str.copy()
    # cnt = contours[0]
    # epsilon = 0.03 * cv2.arcLength(cnt, True)  # epsilon占周长的比例
    # approx = cv2.approxPolyDP(cnt, epsilon, True)
    # res2 = cv2.drawContours(img_copy, [approx], -1, (0, 0, 255), 5)
    # [[lt], [lb], [rb], [rt]] = approx
    # [ltx, lty] = lt
    # [lbx, lby] = lb
    # [rbx, rby] = rb
    # [rtx, rty] = rt


    #
    # # 仿射变换
    # width = max(math.sqrt((rtx - ltx) ** 2 + (rty - lty) ** 2), math.sqrt((rbx - lbx) ** 2 + (rby - lby) ** 2))
    # height = max(math.sqrt((ltx - lbx) ** 2 + (lty - lby) ** 2), math.sqrt((rtx - rbx) ** 2 + (rty - rby) ** 2))
    # pts1 = np.float32([[ltx, lty], [rtx, rty], [lbx, lby], [rbx, rby]])
    # pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])
    # M = cv2.getPerspectiveTransform(pts1, pts2)
    # width = int(width)
    # height = int(height)
    # dst = cv2.warpPerspective(img_str, M, (width, height))
    # resu = cv2.threshold(dst, 120, 255, cv2.THRESH_BINARY)[1]
    # cv2.imwrite('result.jpg', resu)

#二、通过easyocr实现对文字的识别
def ocrsb():
    color=(0,0,255)
    thick=3
    reader = easyocr.Reader(['ch_sim','en'],gpu=False)
    result = reader.readtext('result.jpg',detail=0)
    for res in result:
        print(res)

def main():
    img = 'jkb.jpg'
    simpledeal(img)
    ocrsb()
if __name__ == '__main__':
    main()

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值