验证码图像识别-(PIL|tesserocr)和百度AI开放识图

1,PIL|tesserocr

#安装会议很多坑,主要tesserocr pip安装同时需要你下载安装Tesseract软件,最后把Tesseract解压里的tessdata文件夹复制一份放在python根目录下即可成功运用
在这里插入图片描述
在这里插入图片描述


import tesserocr

from PIL import Image

image=Image.open(r'C:\Users\Administrator\Desktop\20180621094823314.png')
image=image.convert("L") #转灰度
threshold=200 #阈值可以调整测试
table=[]
for i in range(256):
    if i <threshold:
        table.append(0)
    else:
        table.append(1)
image=image.point(table,'1') #转二值化
# image.show()
res = tesserocr.image_to_text(image)
print(res)

2.百度AI开放识图文字(实测效果挺不错,文字也能识别)

有需求可以去看下了解下百度ai开放平台
在这里插入图片描述
在这里插入图片描述

from aip import AipOcr

""" 你的 APPID AK SK """
APP_ID = '14560927'
API_KEY = '90kb90Xo93kkXEreOhGKQomn'
SECRET_KEY = '********'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

image = get_file_content(r'C:\Users\Administrator\Desktop\优惠券\timg (1).jpg')

""" 调用通用文字识别, 图片参数为本地图片 """
# client.basicGeneral(image)

""" 如果有可选参数 """
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"

""" 带参数调用通用文字识别, 图片参数为本地图片 """
a=client.basicGeneral(image, options)

print(a)
print(type(a))
print(a['words_result'][0]['words'])

3.百度AI通用识图
结合百度接口和tkinter制作识图软件


from aip import AipImageClassify
import tkinter as tk
from tkinter import filedialog

window=tk.Tk()
window.title("图片识别")
window.geometry("500x350+800+300")


# b1=tk.Button(window,text="点击查询",width=10,font="微软雅黑 12")
# b1.grid(row=4,column=6)
def click():
    file_path = filedialog.askopenfilename()  # w文件路径
    """ 你的 APPID AK SK """
    APP_ID = '14608086'
    API_KEY = 'G3MdoZoCwc8fEXGehGDGZAhk'
    SECRET_KEY = '~~~~~'

    client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)
    def get_file_content(filePath):
        with open(filePath, 'rb') as fp:
            return fp.read()

    image = get_file_content(file_path)

    """ 调用通用物体识别 """
    client.advancedGeneral(image)

    """ 如果有可选参数 """
    options = {}
    options["baike_num"] = 5

    """ 带参数调用通用物体识别 """
    a=client.advancedGeneral(image, options)
    e1.insert("end", a)
b=tk.Button(window,text="上传图片",command=click,width=10,font="微软雅黑 12")
b.grid(row=1,column=1)
e1=tk.Text(window,height=20)
e1.grid(row=3,column=1)
window.mainloop()

在这里插入图片描述

然后可以在cmd下通过 pyinstaller -F -w (为要打包的py文件)打包为电脑exe可执行的软件

猜你喜欢

转载自blog.csdn.net/weixin_42357472/article/details/83385422