PaddleOCR的使用方法及开发搜题工具怎样实现
Admin 2022-11-02 群英技术资讯 1182 次浏览
这篇主要是介绍“PaddleOCR的使用方法及开发搜题工具怎样实现”的内容了,下文有实例供大家参考,对大家了解操作过程或相关知识有一定的帮助,而且实用性强,希望这篇文章能帮助大家解决PaddleOCR的使用方法及开发搜题工具怎样实现的问题,下面我们一起来了解看看吧。PaddleOCR 是一个基于百度飞桨的OCR工具库,包含总模型仅8.6M的超轻量级中文OCR,单模型支持中英文数字组合识别、竖排文本识别、长文本识别。同时支持多种文本检测、文本识别的训练算法。
本教程将介绍PaddleOCR的基本使用方法以及如何使用它开发一个自动搜题的小工具。
项目地址
OR
虽然PaddleOCR支持服务端部署并提供识别API,但根据我们的需求,搭建一个本地离线的OCR识别环境,所以此次我们只介绍如何在本地安装并使用的做法。
一、环境准备
1.1 目前飞桨支持的环境
Windows 7/8/10 专业版/企业版 (64bit)
GPU版本支持CUDA 10.1/10.2/11.0/11.2,且仅支持单卡
Python 版本 3.6+/3.7+/3.8+/3.9+ (64 bit)
pip 版本 20.2.2或更高版本 (64 bit)
二、安装命令
pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple
(注意此版本为CPU版本,如需GPU版本请查看PaddlePaddle文档)
安装完成后您可以使用 python 进入python解释器,输入import paddle ,再输入 paddle.utils.run_check()
如果出现PaddlePaddle is installed successfully!,说明您已成功安装。
pip install "paddleocr>=2.0.1" # 推荐使用2.0.1+版本
安装完成后你可以使用以下代码来进行简单的功能测试
from paddleocr import PaddleOCR, draw_ocr
# Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改lang参数进行切换
# 参数依次为`ch`, `en`, `french`, `german`, `korean`, `japan`。
ocr = PaddleOCR(use_angle_cls=True, lang="ch") # need to run only once to download and load model into memory
# 选择你要识别的图片路径
img_path = '11.jpg'
result = ocr.ocr(img_path, cls=True)
for line in result:
print(line)
# 显示结果
from PIL import Image
image = Image.open(img_path).convert('RGB')
boxes = [line[0] for line in result]
txts = [line[1][0] for line in result]
scores = [line[1][1] for line in result]
im_show = draw_ocr(image, boxes, txts, scores, font_path='/path/to/PaddleOCR/doc/fonts/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')
结果是一个list,每个item包含了文本框,文字和识别置信度
[[[24.0, 36.0], [304.0, 34.0], [304.0, 72.0], [24.0, 74.0]], ['纯臻营养护发素', 0.964739]]
[[[24.0, 80.0], [172.0, 80.0], [172.0, 104.0], [24.0, 104.0]], ['产品信息/参数', 0.98069626]]
[[[24.0, 109.0], [333.0, 109.0], [333.0, 136.0], [24.0, 136.0]], ['(45元/每公斤,100公斤起订)', 0.9676722]]
......
可视化效果

至此我们就掌握了 PaddleOCR 的基本使用,基于这个我们就能开发出一个OCR的搜题小工具了。
更多使用方法请参考
现在有很多那种答题竞赛的小游戏,在限定时间内看谁答题正确率更高。或者现在一些单位会搞一些大练兵什么的竞赛,需要在网上答题,这个时候手动输入题目去搜索就很慢,效率也不会太高,所以我们就可以来写一个脚本,帮助我们完成搜题的过程。
基本思路就是通过ADB截取当前屏幕,然后剪切出题目所在位置,然后通过PaddleOCR来获取题目文字,之后打开搜索引擎搜索或者打开题库搜索。
你可以到这里下载安装ADB之后配置环境变量。
配置完环境变量后在终端输入adb,如果出现以下字符则证明adb安装完成。
Android Debug Bridge version 1.0.41 Version 31.0.3-7562133
import os
from PIL import Image
# 截图
def pull_screenshot():
os.system('adb shell screencap -p /sdcard/screenshot.png')
os.system('adb pull /sdcard/screenshot.png .')
img = Image.open("./screenshot.png")
# 切割问题区域
# (起始点的横坐标,起始点的纵坐标,宽度,高度)
question = img.crop((10, 400, 1060, 1000))
# 保存问题区域
question.save("./question.png")
ocr = PaddleOCR(use_angle_cls=False,
lang="ch",
show_log=False
) # need to run only once to download and load model into memory
img_path = 'question.png'
result = ocr.ocr(img_path, cls=False)
# 获取题目文本
questionList = [line[1][0] for line in result]
text = ""
# 将数组转换为字符串
for str in questionList :
text += str
print(text)
import webbrowser
webbrowser.open('https://baidu.com/s?wd=' + urllib.parse.quote(question))
之后你就可以查看搜索结果了
如果有题库,你还可以使用pyautogui来模拟鼠标键盘操作,去操作Word等软件在题库中进行搜索。
# -*- coding: utf-8 -*-
# @Author : Pu Zhiwei
# @Time : 2021-09-02 20:29
from PIL import Image
import os
import matplotlib.pyplot as plt
from paddleocr import PaddleOCR, draw_ocr
import pyperclip
import pyautogui
import time
import webbrowser
import urllib.parse
# 鼠标位置
currentMouseX, currentMouseY = 60, 282
# 截图获取当前题目
def pull_screenshot():
os.system('adb shell screencap -p /sdcard/screenshot.png')
os.system('adb pull /sdcard/screenshot.png .')
# 移动鼠标到搜索框搜索
def MoveMouseToSearch():
# duration 参数,移动时间,即用时0.1秒移动到对应位置
pyautogui.moveTo(currentMouseX, currentMouseY, duration=0.1)
# 左键点击
pyautogui.click()
pyautogui.click()
# 模拟组合键,粘贴
pyautogui.hotkey('ctrl', 'v')
# 扩充问题
def AddText(list, length, text):
if length > 3:
return text + list[3]
else:
return text
# 打开浏览器
def open_webbrowser(question):
webbrowser.open('https://baidu.com/s?wd=' + urllib.parse.quote(question))
# 显示所识别的题目
def ShowAllQuestionText(list):
text = ""
for str in list:
text += str
print(text)
if __name__ == "__main__":
while True:
print("\n\n请将鼠标放在Word的搜索框上,三秒后脚本将自动获取Word搜索框位置!\n\n")
# 延时三秒输出鼠标位置
time.sleep(3)
# 获取当前鼠标位置
currentMouseX, currentMouseY = pyautogui.position()
print('当前鼠标位置为: {0} , {1}'.format(currentMouseX, currentMouseY))
start = input("按y键程序开始运行,按其他键重新获取搜索框位置:")
if start == 'y':
break
while True:
t = time.perf_counter()
pull_screenshot()
img = Image.open("./screenshot.png")
# 切割问题区域
# (起始点的横坐标,起始点的纵坐标,宽度,高度)
question = img.crop((10, 400, 1060, 1000))
# 保存问题区域
question.save("./question.png")
# 加载 PaddleOCR
# Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改lang参数进行切换
# 参数依次为`ch`, `en`, `french`, `german`, `korean`, `japan`。
# 自定义模型地址
# det_model_dir='./inference/ch_ppocr_server_v2.0_det_train',
# rec_model_dir='./inference/ch_ppocr_server_v2.0_rec_pre',
# cls_model_dir='./inference/ch_ppocr_mobile_v2.0_cls_train',
ocr = PaddleOCR(use_angle_cls=False,
lang="ch",
show_log=False
) # need to run only once to download and load model into memory
img_path = 'question.png'
result = ocr.ocr(img_path, cls=False)
questionList = [line[1][0] for line in result]
length = len(questionList)
text = ""
if length < 1:
text = questionList[0]
elif length == 2:
text = questionList[1]
else:
text = questionList[1] + questionList[2]
print('\n\n')
ShowAllQuestionText(questionList)
# 将结果写入剪切板
pyperclip.copy(text)
# 点击搜索
MoveMouseToSearch()
# 计算时间
print('\n\n')
end_time3 = time.perf_counter()
print('用时: {0}'.format(end_time3 - t))
go = input('输入回车继续运行,输入 e 打开浏览器搜索,输入 a 增加题目长度,输入 n 结束程序运行: ')
if go == 'n':
break
if go == 'a':
text = AddText(questionList, length, text)
pyperclip.copy(text)
# 点击搜索
MoveMouseToSearch()
stop = input("输入回车继续")
elif go == 'e':
# 打开浏览器
open_webbrowser(text)
stop = input("输入回车继续")
print('\n------------------------\n\n')
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
路径字符串磁盘将数据写入各个扇区中,使数据分散在各个未使用的块上来储存数据。并用一套文件系统对数据进行管理,微观上,文件系统使用INode结构体来记录这些块区的地址以及数据的先后顺序,实现对这些乱序储存的数据管理。宏观上,为了方便用户的管理,使用路径字符串的方式来“描述”文件的位置,但这只是一种逻辑位置,并不存在此种数据上的层级关系,一个文件字符串在将会映射到一个的INode结构体中,再使用iN
这篇文章主要介绍了python 如何求N的阶乘,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
这篇文章主要介绍了python数据结构之递归讲解,递归是解决问题的一种方法,它将问题不断地分成更小的子问题,直到子问题可以用普通的方法解决。通常情况下,递归会使用一个不停调用自己的函数,下面来看看文章对此的详细介绍吧
这篇文章主要为大家介绍了python上下文管理器使用场景及异常处理示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
本文主要介绍了TensorFlow训练网络两种方式,一种是基于tensor(array),另外一种是迭代器,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008