图片文字识别

这是基于百度aip的图片文字识别,识别率还可以,自带gui界面。感觉很好玩。


# coding =utf-8
'''
    name:图片识别
    function:将图片里面的文字识别出来
    author:ww
    time:2019/1/8 13:30
    software:PyCharm

'''

from tkinter import *
from tkinter.filedialog import askdirectory
from os import path
import tkinter

from aip import AipOcr

# 定义常量
APP_ID = '15345466'
API_KEY = 'u4Uh1pg7rNBi0VQazfPSCQB7'
SECRET_KEY = 'ivmLcC3FWCUy6cwqHG2ViiObLNFbmEa5'
# 初始化AipFace对象
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)

global testd
testd = ''
global path_


def selectPath():
    global path_
    path_ = tkinter.filedialog.askopenfilename()

    path.set(path_)

def get():
    def get_file_content(path_):
        with open(path_, 'rb') as fp:
            return fp.read()
            # 定义参数变量

    options = {'detect_direction': 'true', 'language_type': 'CHN_ENG', }
    # 调用通用文字识别接口
    result = aipOcr.basicGeneral(get_file_content(path_), options)
    a = result['words_result']
    testd = ''

    for i in a:
        for k, v in i.items():
            print(k, v)
            testd += v
            testd += '\n'
    text_result.delete(0.0, END)
    text_result.insert(1.0, testd)

root = Tk()
path = StringVar()
root.title("图片文字识别")  # 设置窗口标题
root.geometry("500x400")  # 设置窗口大小 注意:是x 不是*
root.resizable(width=False, height=True)  # 设置窗口是否可以变化长/宽,False不可变,True可变,默认为True
# text = tkinter.Text(root).grid(row=2, column=1)
Label(root, text="").grid(row=0, column=0)
Button(root, text="路径选择", command=selectPath).grid(row=1, column=0)
Label(root, text="图片路径:").grid(row=1, column=0, rowspan=10, columnspan=10)
Entry(root, textvariable=path).grid(row=1, column=1, ipadx=1, ipady=1, columnspan=1, rowspan=1)
Button(root, text="转换文字", command=get).grid(row=2, column=0)

text_result = Text(root, width=68, height=20)
text_result.grid(row=4, column=0, columnspan=2, sticky=W, padx=10)
Label(root, text="-- by ww").grid(row=8, column=1, sticky=E, padx=10, pady=10)

root.mainloop()


运行效果

什么描述  ???我咋么看见!!!

点击路径选择选择图片
在这里插入图片描述

点击转换文字,运行效果。
在这里插入图片描述

是不是很好玩。

OpenCV是一个开源的计算机视觉和机器学习软件库,它提供了很多常用的图像处理功能。在OpenCV中实现图片文字识别,通常需要结合OCR(Optical Character Recognition,光学字符识别)技术。基本步骤包括图像预处理、文字检测和字符识别。OpenCV本身不直接提供OCR功能,但可以和Tesseract OCR等工具配合使用来实现文字识别。 1. 图像预处理:对输入的图像进行灰度化、二值化、去噪、旋转校正等操作,以便更好地检测文字区域。 2. 文字检测:使用OpenCV提供的图像处理功能,如霍夫变换、连通区域分析等,来定位图像中的文字区域。 3. 字符识别:通过Tesseract OCR等OCR工具对检测到的文字区域进行文字识别,将图像中的文字转换为可编辑的文本格式。 结合OpenCV和Tesseract OCR进行图片文字识别的流程大致如下: a. 安装OpenCV和Tesseract OCR库。 b. 使用OpenCV读取图像,并进行预处理,如灰度化、二值化、去噪等。 c. 应用OpenCV中的形态学操作和霍夫变换等算法来检测图像中的文字区域。 d. 对检测到的文字区域使用Tesseract OCR进行文字识别。 e. 输出识别结果。 示例代码(假设已安装好相关库): ```python import cv2 import pytesseract # 读取图像 image = cv2.imread('path_to_image.jpg') # 预处理图像(灰度化、二值化等) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) _, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV) # 检测图像中的文字区域(可能需要自定义更多参数和处理步骤) # 这里仅为示例,实际应用中需要根据情况调整参数 dilated = cv2.dilate(binary, None, iterations=2) contours, _ = cv2.findContours(dilated, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # 使用Tesseract OCR进行文字识别 custom_config = r'--oem 3 --psm 6' for cnt in contours: x, y, w, h = cv2.boundingRect(cnt) roi = binary[y:y+h, x:x+w] text = pytesseract.image_to_string(roi, config=custom_config) print(text) # 结果输出 # ... ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值