Python-OpenCV-- 台式机外接摄像头pyTesseract文本框实时检测
生活随笔
收集整理的這篇文章主要介紹了
Python-OpenCV-- 台式机外接摄像头pyTesseract文本框实时检测
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、正常讀取磁盤圖片使用 teesseract 進行簡單的文字識別
首先要安裝?pytesseract
之后要安裝?TesseractOCR
非攝像頭版本 Python + tesseractocr 的文本識別方案參考:https://blog.csdn.net/u014361280/article/details/105110506
?
二、外接USB攝像頭文本識別 TesseractOCR?
import cv2 as cv from PIL import Image import pytesseract as tesscap = cv.VideoCapture(0) cap.set(cv.CAP_PROP_FPS, 20)def recoginse_text(img):# 步驟:# 1、灰度,二值化處理# 2、形態學操作去噪# 3、識別# :param image:# :return:# 灰度 二值化gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)# 如果是白底黑字 建議 _INVret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)# 形態學操作 (根據需要設置參數(1,2))kernel = cv.getStructuringElement(cv.MORPH_RECT, (1, 2)) # 去除橫向細線morph1 = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)kernel = cv.getStructuringElement(cv.MORPH_RECT, (2, 1)) # 去除縱向細線morph2 = cv.morphologyEx(morph1, cv.MORPH_OPEN, kernel)cv.imshow("Morph", morph2)# 黑底白字取非,變為白底黑字(便于pytesseract 識別)cv.bitwise_not(morph2, morph2)textImage = Image.fromarray(morph2)# 圖片轉文字text = tess.image_to_string(textImage)print("識別結果:%s" % text)while True:ret, img = cap.read()# 識別recoginse_text(img)if cv.waitKey(10) == ord("q"):break cv.waitKey(0) cv.destroyAllWindows()簡化版本,直接在原圖像上進行識別,識別率很低,但是速度快了不少。
import cv2 as cv from PIL import Image import pytesseract as tesscap = cv.VideoCapture(0) cap.set(cv.CAP_PROP_FPS, 20)while True:ret, img = cap.read()# 識別# recoginse_text(img)cv.imshow("Morph", img)text = tess.image_to_string(img)print("識別結果:%s" % text)if cv.waitKey(10) == ord("q"):break cv.waitKey(0) cv.destroyAllWindows()?
三、改進版
Windows臺式機下 CPU跑傳統方法,識別率較低。速度較慢。
待研究
總結
以上是生活随笔為你收集整理的Python-OpenCV-- 台式机外接摄像头pyTesseract文本框实时检测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python-OpenCV学习--外接摄
- 下一篇: Python-OpenCV-- 台式机外