28行python代码做出截图文字识别OCR小工具方便做网课笔记

最近上网课,很多老师都不共享PPT,自己打字的速度又跟不上老师说话的速度,所以导致记笔记记不下来。于是想到了用python做一个辅助工具。做这个工具主要需要以下预备工作:
1.安装截图软件snipaste
2.通过keyboard模块调用snipaste的快捷键:f1截图和Ctrl+C保存;
3.通过百度AipOCR调用百度OCR接口(需要在百度官网申请并且获得APP_ID/API_KEY/SECRET_KEY三个个人参数);
4.通过pillow库保存剪贴板中的截图到当前目录中;
5.with open对文件进行操作;
6.字符串和列表之间的转换;
7.通过PySimpleGUI库进行操作提示;
8.时间模块time的使用。

import keyboard				#pip install keyboard
import time					#built-in module
from aip import AipOcr #pip install baidu-aip
from PIL import ImageGrab 	#pip install pillow (PIL is short for pillow)
keyboard.wait(hotkey='f1')
keyboard.wait(hotkey='ctrl+c')
time.sleep(0.1)
image = ImageGrab.grabclipboard()
image.save('screen.png')
with open('ocr_content.txt','r',encoding='utf-8') as f:
	r = f.readlines()
	content_list = r
content = ' '.join(content_list)
APP_ID 	   = 'XXXXXXXXX'
API_KEY	   = 'YYYYYYYYY'
SECRET_KEY = 'ZZZZZZZZZ'
client = AipOcr(APP_ID,API_KEY,SECRET_KEY)
with open('screen.png','rb') as f:
	image = f.read()
	text  = client.basicAccurate(image)
	result= text["words_result"]
	for i in result:
		content = content + i["words"]
	content = content
content = content + '\n'
with open('ocr_content.txt','w',encoding = 'utf-8') as f:
	f.write(content)

运行之后,我们可以截图视频中的ppt,如下图:
在这里插入图片描述
我们对其中的第二段截图,就可以在指定的txt文档中获得这段内容的文字版了:
在这里插入图片描述
当然,我们也可以使用pyinstall打包成可执行文件,这样就可以把写好的程序传给其他用户使用了。参考博文《在虚拟环境使用pyinstaller命令打包.py程序成为exe可执行文件发布给其他用户》

发布了273 篇原创文章 · 获赞 40 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_41855010/article/details/104880975