python easyocr和cv2实现名片识别及裁剪摆正

公司希望能做一个名片裁剪和识别功能。我来开发小程序,在寻找合适的api的途中,因嫌弃乙方弄得太慢,自己花了半天百度搞了个python版本的。很久没用python了好多基础方法都忘记了,实现的很草率。

效果图,识别的准确度还得微调,资源包应该还得训练啥的。

命令:

pip install opencv-python==4.1.2.30 -i https://pypi.tuna.tsinghua.edu.cn/simple

python代码(与原作者代码有差异):

# -*- coding:utf-8 -*-
import easyocr
import time

import cv2
import numpy as np
from PIL import Image

# 可信度最小阈值
BASE_MIN = 0.1 
BASE_ROTA = 0
time1 = time.time()
url = './1.jpg'
reader = easyocr.Reader(['ch_sim','en'],gpu=True)

result = reader.readtext(url,batch_size =100,rotation_info=[90,180,270],low_text=0.4,slope_ths=0.3,beamWidth =1,min_size=50)

numLis = [0,0,0,0]
for i in result:
    numLis[i[3]] = numLis[i[3]]+1
for i in range(len(numLis)):
    if(numLis[i]>numLis[BASE_ROTA]):
        BASE_ROTA = i
for i in result:
    if(float(i[2])>BASE_MIN and i[3] == BASE_ROTA):
        print(i[1])


image = cv2.imread(url)

# 灰度图
img=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 二值化
ret , thresh = cv2.threshold(img, 230, 255, cv2.THRESH_BINARY_INV)

contours, hierarchy = cv2.findContours(cv2.Canny(img,80,100),cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)


dot=[]  # 用来保存所有轮廓返回的坐标点。
for c in contours:
    # 找到边界坐标
    min_list=[] # 保存单个轮廓的信息,x,y,w,h,area。 x,y 为起始点坐标
    x, y, w, h = cv2.boundingRect(c)  # 计算点集最外面的矩形边界
    min_list.append(x)
    min_list.append(y)
    min_list.append(w)
    min_list.append(h)
    min_list.append(w*h) # 把轮廓面积也添加到 dot 中
    dot.append(min_list)

max_area=dot[0][4]
for inlist in dot:
    area=inlist[4]
    if area >= max_area:
        x=inlist[0]
        y=inlist[1]
        w=inlist[2]
        h=inlist[3]
        max_area=area
# 在原图上画出最大的矩形
cv2.rectangle(image, (x, y), (x + w , y + h ), (0, 255, 0), 1)

cv2.imwrite('big.jpg',image)

img = Image.open('big.jpg')
region = img.crop((x, y, x + w, y + h))

if(BASE_ROTA == 1):
    region = region.transpose(Image.ROTATE_90)
elif(BASE_ROTA == 2):
    region = region.transpose(Image.ROTATE_180)
elif(BASE_ROTA == 3):
    region = region.transpose(Image.ROTATE_270)
region.save('result.jpg')
time2 = time.time()

print("绘制结束,用时:",time2-time1)

需要修改一下easyocr的包返回一个识别的方向供图片裁剪摆正:

 

 



参考链接:
使用cv2报错:https://blog.csdn.net/iLOVEJohnny/article/details/126466515;

下载cv2:https://blog.csdn.net/songyuc/article/details/102855144

识别文字:https://blog.csdn.net/weixin_47046791/article/details/124149260

最大矩形以及裁剪:https://blog.csdn.net/aqqwvfbukn/article/details/120787480

posted @ 2023-02-09 23:49  xiaochuchun  阅读(328)  评论(0编辑  收藏  举报