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()

你可能感兴趣的:(机器学习,python,opencv,计算机视觉)