博客
关于我
python 图片转文字、语音转文字、文字转语音保存音频并朗读
阅读量:796 次
发布时间:2023-03-07

本文共 2616 字,大约阅读时间需要 8 分钟。

Python OCR工具使用指南

一、Python图片转文字

1、引言

PyTesseract是基于Python的OCR工具,依托Google的Tesseract-OCR引擎,支持多种图片格式(如JPG、PNG、GIF等)转文字识别。

2、环境配置

  • Python版本:3.6及以上
  • PIL库(Python图像处理库)
  • 安装Google Tesseract OCR

3、安装PyTesseract

通过pip安装(建议使用pip3):

pip install pytesseract==0.3.10 pillow==10.4.0

4、安装Google Tesseract OCR

Tesseract是开源OCR引擎,支持多语言文字识别,安装方法如下:

Tesseract OCR GitHub地址:https://github.com/tesseract-ocr/tesseract
Windows安装地址:https://digi.bib.uni-mannheim.de/tesseract
Mac/Linux安装方法:https://tesseract-ocr.github.io/tessdoc/Installation.html

安装完成后,建议设置PATH环境变量和TESSDATA_PREFIX变量:

PATH="D:\Development\Tesseract-OCR;...";
TESSDATA_PREFIX="D:\Development\Tesseract-OCR\tessdata";

5、使用PyTesseract转换图片

import pytesseractfrom PIL import Image

def image_to_string(image_path, language='chi_sim'):image = Image.open(image_path)enhancer = ImageEnhance.Contrast(image)img_contrast = enhancer.enhance(2.0)text = pytesseract.image_to_string(img_contrast, lang=language)return text

二、语音转文字

1、注册百度AI开放平台

登录百度AI开放平台,进入“控制台—人工智能—语音技术”界面,获取APPID、API KEY、SECRET KEY。

2、安装百度AI

pip install baidu-aip==4.16.13

3、使用百度AI语音识别

from aip import AipSpeech

class SpeechRecognition(APIView):def post(self, request):try:APP_ID = '' # 替换为你的APPIDAPI_KEY = '' # 替换为你的API KEYSECRET_KEY = '' # 替换为你的SECRET KEY

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)        audio_file = 'media\\audio\\16k.wav'  # 语音文件路径        with open(audio_file, 'rb') as fp:            audio_data = fp.read()        res = client.asr(audio_data, 'wav', 16000, {            'dev_pid': 1536,        })        if res['err_no'] == 0:            ret['data'] = res['result'][0]        else:            ret['code'] = 101            ret['msg'] = res['err_msg']    except Exception as e:        ret['code'] = 102        ret['msg'] = e    return Response(ret)

三、语音播放

1、安装Speech模块

pip install speech

2、语音播放注意事项

由于Speech模块最初开发于Python 2.x,在Python 3.x中需手动调整源文件,建议参考官方文档进行配置。

3、使用Speech播放语音

import speech

def speak(text):speech.say(text)speech.runAndWait()speech.stop()

四、文字转音频并保存

1、安装PyTTSx

pip install pyttsx3

2、文字转音频代码示例

import pyttsx3

def text_to_audio(text, language, rate, volume, filename):engine = pyttsx3.init()engine.setProperty('rate', rate)engine.setProperty('volume', volume)voices = engine.getProperty('voices')

if language == 0:    engine.setProperty('voice', voices[0].id)elif language == 1:    engine.setProperty('voice', voices[1].id)engine.say(text)engine.save_to_file(text, filename)engine.runAndWait()engine.stop()

3、使用示例

def main(): text = """从前,有一座美丽的大森林...""" pyttsx3(text=text, language=0, rate=200, volume=0.9, filename="media\\audio\\ptttsx3中文测试.mp3", sayit=1)

if name == 'main':main()

转载地址:http://mjofk.baihongyu.com/

你可能感兴趣的文章
python | xlsxwriter,一个实用的 Python 库!
查看>>
python | xlwings,一个非常实用的 Excel 相关的 Python 库!
查看>>
python | xmltodict,一个非常厉害的 关于XML数据 Python 库!
查看>>
python | xonsh,一个超酷的 Python 库!
查看>>
python | yagmail,一个实用的 Python 库!
查看>>
python | 一文掌握Python的上下文管理器和with语句
查看>>
python | 一文看懂Python闭包机制与变量作用域规则
查看>>
python读取含中文的json
查看>>
python | 如何用Python锁避免并发错误?
查看>>
python | 提升代码迭代速度的Python重载方法
查看>>
python | 深入理解Python并发编程中的GIL限制与解决方案
查看>>
Python | 爬虫实战——亚马逊搜索页监控(附详细源码)
查看>>
python | 高效使用Python工具自动生成模块文档的秘诀
查看>>
python 一个list去除另一个list中的值
查看>>
python 三大框架的 介绍。
查看>>
Python 下载的 11 种姿势,一种比一种高级!
查看>>
python读取一个文件夹下所有图片_初学Python-找出文件夹下的所有图片
查看>>
Python 中 3 个不可思议的返回功能
查看>>
python 中 dict 的另一种用法
查看>>
Python 中 PIL 读取图片出现异常旋转的解决方法
查看>>