Python利用PaddleOCR制作個(gè)搜題小工具
介紹
PaddleOCR 是一個(gè)基于百度飛槳的OCR工具庫,包含總模型僅8.6M的超輕量級(jí)中文OCR,單模型支持中英文數(shù)字組合識(shí)別、豎排文本識(shí)別、長(zhǎng)文本識(shí)別。同時(shí)支持多種文本檢測(cè)、文本識(shí)別的訓(xùn)練算法。
本教程將介紹PaddleOCR的基本使用方法以及如何使用它開發(fā)一個(gè)自動(dòng)搜題的小工具。
安裝
雖然PaddleOCR支持服務(wù)端部署并提供識(shí)別API,但根據(jù)我們的需求,搭建一個(gè)本地離線的OCR識(shí)別環(huán)境,所以此次我們只介紹如何在本地安裝并使用的做法。
安裝PaddlePaddle飛槳框架
一、環(huán)境準(zhǔn)備
1.1 目前飛槳支持的環(huán)境
Windows 7/8/10 專業(yè)版/企業(yè)版 (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版本請(qǐng)查看PaddlePaddle文檔)
安裝完成后您可以使用 python
進(jìn)入python解釋器,輸入import paddle
,再輸入 paddle.utils.run_check()
如果出現(xiàn)PaddlePaddle is installed successfully!
,說明您已成功安裝。
安裝PaddleOCR
pip install "paddleocr>=2.0.1" # 推薦使用2.0.1+版本
代碼使用
安裝完成后你可以使用以下代碼來進(jìn)行簡(jiǎn)單的功能測(cè)試
from paddleocr import PaddleOCR, draw_ocr # Paddleocr目前支持中英文、英文、法語、德語、韓語、日語,可以通過修改lang參數(shù)進(jìn)行切換 # 參數(shù)依次為`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 # 選擇你要識(shí)別的圖片路徑 img_path = '11.jpg' result = ocr.ocr(img_path, cls=True) for line in result: print(line) # 顯示結(jié)果 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')
結(jié)果是一個(gè)list,每個(gè)item包含了文本框,文字和識(shí)別置信度
[[[24.0, 36.0], [304.0, 34.0], [304.0, 72.0], [24.0, 74.0]], ['純臻營(yíng)養(yǎng)護(hù)發(fā)素', 0.964739]]
[[[24.0, 80.0], [172.0, 80.0], [172.0, 104.0], [24.0, 104.0]], ['產(chǎn)品信息/參數(shù)', 0.98069626]]
[[[24.0, 109.0], [333.0, 109.0], [333.0, 136.0], [24.0, 136.0]], ['(45元/每公斤,100公斤起訂)', 0.9676722]]
......
可視化效果
至此我們就掌握了 PaddleOCR 的基本使用,基于這個(gè)我們就能開發(fā)出一個(gè)OCR的搜題小工具了。
更多使用方法請(qǐng)參考
搜題小工具
現(xiàn)在有很多那種答題競(jìng)賽的小游戲,在限定時(shí)間內(nèi)看誰答題正確率更高?;蛘攥F(xiàn)在一些單位會(huì)搞一些大練兵什么的競(jìng)賽,需要在網(wǎng)上答題,這個(gè)時(shí)候手動(dòng)輸入題目去搜索就很慢,效率也不會(huì)太高,所以我們就可以來寫一個(gè)腳本,幫助我們完成搜題的過程。
基本思路就是通過ADB截取當(dāng)前屏幕,然后剪切出題目所在位置,然后通過PaddleOCR來獲取題目文字,之后打開搜索引擎搜索或者打開題庫搜索。
安裝ADB
你可以到這里下載安裝ADB之后配置環(huán)境變量。
配置完環(huán)境變量后在終端輸入adb,如果出現(xiàn)以下字符則證明adb安裝完成。
Android Debug Bridge version 1.0.41 Version 31.0.3-7562133
截圖并保存題目區(qū)域圖片
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") # 切割問題區(qū)域 # (起始點(diǎn)的橫坐標(biāo),起始點(diǎn)的縱坐標(biāo),寬度,高度) question = img.crop((10, 400, 1060, 1000)) # 保存問題區(qū)域 question.save("./question.png")
OCR識(shí)別,獲取題目
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 = "" # 將數(shù)組轉(zhuǎn)換為字符串 for str in questionList : text += str print(text)
打開瀏覽器搜索
import webbrowser webbrowser.open('https://baidu.com/s?wd=' + urllib.parse.quote(question))
之后你就可以查看搜索結(jié)果了
如果有題庫,你還可以使用pyautogui
來模擬鼠標(biāo)鍵盤操作,去操作Word等軟件在題庫中進(jìn)行搜索。
完整代碼
# -*- 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 # 鼠標(biāo)位置 currentMouseX, currentMouseY = 60, 282 # 截圖獲取當(dāng)前題目 def pull_screenshot(): os.system('adb shell screencap -p /sdcard/screenshot.png') os.system('adb pull /sdcard/screenshot.png .') # 移動(dòng)鼠標(biāo)到搜索框搜索 def MoveMouseToSearch(): # duration 參數(shù),移動(dòng)時(shí)間,即用時(shí)0.1秒移動(dòng)到對(duì)應(yīng)位置 pyautogui.moveTo(currentMouseX, currentMouseY, duration=0.1) # 左鍵點(diǎn)擊 pyautogui.click() pyautogui.click() # 模擬組合鍵,粘貼 pyautogui.hotkey('ctrl', 'v') # 擴(kuò)充問題 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)) # 顯示所識(shí)別的題目 def ShowAllQuestionText(list): text = "" for str in list: text += str print(text) if __name__ == "__main__": while True: print("\n\n請(qǐng)將鼠標(biāo)放在Word的搜索框上,三秒后腳本將自動(dòng)獲取Word搜索框位置!\n\n") # 延時(shí)三秒輸出鼠標(biāo)位置 time.sleep(3) # 獲取當(dāng)前鼠標(biāo)位置 currentMouseX, currentMouseY = pyautogui.position() print('當(dāng)前鼠標(biāo)位置為: {0} , {1}'.format(currentMouseX, currentMouseY)) start = input("按y鍵程序開始運(yùn)行,按其他鍵重新獲取搜索框位置:") if start == 'y': break while True: t = time.perf_counter() pull_screenshot() img = Image.open("./screenshot.png") # 切割問題區(qū)域 # (起始點(diǎn)的橫坐標(biāo),起始點(diǎn)的縱坐標(biāo),寬度,高度) question = img.crop((10, 400, 1060, 1000)) # 保存問題區(qū)域 question.save("./question.png") # 加載 PaddleOCR # Paddleocr目前支持中英文、英文、法語、德語、韓語、日語,可以通過修改lang參數(shù)進(jìn)行切換 # 參數(shù)依次為`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) # 將結(jié)果寫入剪切板 pyperclip.copy(text) # 點(diǎn)擊搜索 MoveMouseToSearch() # 計(jì)算時(shí)間 print('\n\n') end_time3 = time.perf_counter() print('用時(shí): {0}'.format(end_time3 - t)) go = input('輸入回車?yán)^續(xù)運(yùn)行,輸入 e 打開瀏覽器搜索,輸入 a 增加題目長(zhǎng)度,輸入 n 結(jié)束程序運(yùn)行: ') if go == 'n': break if go == 'a': text = AddText(questionList, length, text) pyperclip.copy(text) # 點(diǎn)擊搜索 MoveMouseToSearch() stop = input("輸入回車?yán)^續(xù)") elif go == 'e': # 打開瀏覽器 open_webbrowser(text) stop = input("輸入回車?yán)^續(xù)") print('\n------------------------\n\n')
到此這篇關(guān)于Python利用PaddleOCR制作個(gè)搜題小工具 的文章就介紹到這了,更多相關(guān)Python PaddleOCR搜題工具 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入理解Python虛擬機(jī)中魔術(shù)方法的使用
這篇文章主要給大家介紹在?cpython?當(dāng)中一些比較花里胡哨的魔術(shù)方法,以幫助我們自己實(shí)現(xiàn)比較花哨的功能,當(dāng)然這其中也包含一些也非常實(shí)用的魔術(shù)方法,需要的可以參考下2023-05-05用Python進(jìn)行行為驅(qū)動(dòng)開發(fā)的入門教程
這篇文章主要介紹了用Python進(jìn)行行為驅(qū)動(dòng)開發(fā)的入門教程,本文也對(duì)BDD的概念做了詳細(xì)的解釋,需要的朋友可以參考下2015-04-04python實(shí)現(xiàn)三階魔方還原的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)三階魔方還原的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04