One 、 Environment configuration
need pillow and pytesseract These two libraries ,pip install Just install it .
install pillow -i http://pypi.douban.com/simple --trusted-host pypi.douban.com pip install pytesseract -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
Install well Tesseract-OCR.exe
pytesseract Library Configuration : Search for pytesseract.py, Open the .py file , find tesseract_cmd, Change its value to just installed tesseract.exe The path of .
If you like to program, you can add some Q Group 768671345, You can get free learning materials when you join the group , There is also a big guy to answer your questions !
Two 、 Verification code recognition
Identification verification code , We need to preprocess the image first , Remove lines or noise that may affect the accuracy of recognition , Improve recognition accuracy .
example 1
import cv2 as cv import pytesseract from PIL import Image
def recognize_text(image):
Edge preserving filtering Denoise
dst = cv.pyrMeanShiftFiltering(image, sp=10, sr=150)
Grayscale image
gray = cv.cvtColor(dst, cv.COLOR_BGR2GRAY)
Two valued
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
Morphological operation corrosion inflation
erode = cv.erode(binary, None, iterations=2)
dilate = cv.dilate(erode, None, iterations=1)
cv.imshow(‘dilate’, dilate)
Logical operations Let the background be white The font is black Easy to identify
cv.bitwise_not(dilate, dilate)
cv.imshow(‘binary-image’, dilate)
distinguish
test_message = Image.fromarray(dilate)
text = pytesseract.image_to_string(test_message)
print(f’ Recognition result :{text}’)
src = cv.imread(r’./test/044.png’)
cv.imshow(‘input image’, src)
recognize_text(src)
cv.waitKey(0)
cv.destroyAllWindows()
The operation effect is as follows :
Recognition result :3n3D
Process finished with exit code 0
example 2
import cv2 as cv import pytesseract from PIL import Image
def recognize_text(image):
Edge preserving filtering Denoise
blur =cv.pyrMeanShiftFiltering(image, sp=8, sr=60)
cv.imshow(‘dst’, blur)
Grayscale image
gray = cv.cvtColor(blur, cv.COLOR_BGR2GRAY)
Two valued
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
print(f’ Binarization adaptive threshold :{ret}’)
cv.imshow(‘binary’, binary)
Morphological operation Get structure elements Open operation
kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 2))
bin1 = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)
cv.imshow(‘bin1’, bin1)
kernel = cv.getStructuringElement(cv.MORPH_OPEN, (2, 3))
bin2 = cv.morphologyEx(bin1, cv.MORPH_OPEN, kernel)
cv.imshow(‘bin2’, bin2)
Logical operations Let the background be white The font is black Easy to identify
cv.bitwise_not(bin2, bin2)
cv.imshow(‘binary-image’, bin2)
distinguish
test_message = Image.fromarray(bin2)
text = pytesseract.image_to_string(test_message)
print(f’ Recognition result :{text}’)
src = cv.imread(r’./test/045.png’)
cv.imshow(‘input image’, src)
recognize_text(src)
cv.waitKey(0)
cv.destroyAllWindows()
The operation effect is as follows :
Binarization adaptive threshold :181.0
Recognition result :8A62N1Process finished with exit code 0
example 3
import cv2 as cv import pytesseract from PIL import Image
def recognize_text(image):
Edge preserving filtering Denoise
blur = cv.pyrMeanShiftFiltering(image, sp=8, sr=60)
cv.imshow(‘dst’, blur)
Grayscale image
gray = cv.cvtColor(blur, cv.COLOR_BGR2GRAY)
Two valued Set the threshold Adaptive threshold Yellow 4 You can't extract it
ret, binary = cv.threshold(gray, 185, 255, cv.THRESH_BINARY_INV)
print(f’ Threshold set by binarization :{ret}’)
cv.imshow(‘binary’, binary)
Logical operations Let the background be white The font is black Easy to identify
cv.bitwise_not(binary, binary)
cv.imshow(‘bg_image’, binary)
distinguish
test_message = Image.fromarray(binary)
text = pytesseract.image_to_string(test_message)
print(f’ Recognition result :{text}’)
src = cv.imread(r’./test/045.jpg’)
cv.imshow(‘input image’, src)
recognize_text(src)
cv.waitKey(0)
cv.destroyAllWindows()
The operation effect is as follows :
Threshold set by binarization :185.0
Recognition result :7364Process finished with exit code 0
If you like to program, you can add some Q Group 768671345, You can get free learning materials when you join the group , There is also a big guy to answer your questions !