安装过程略
图像识别
from paddleocr import PaddleOCR
from PIL import Image, ImageOps
thrd1, thrd2, cnt1mx, cnt2mx = 150, 245, 5, 4
img = Image.open(r"./VCode/1.jpg").convert('L')
img = ImageOps.invert(img.point(lambda p: p < thrd1 and 255))
width, height = img.size
px = img.load()
px = img.load()
for x in width:
for y in range(height):
cnt1 = cnt2 = 0
neighbors = [
(0, 0), (0, 1), (0, -1), (1, 0), (-1, 0),
(1, -1), (-1, 1), (-1, -1), (1, 1)
]
for dx, dy in neighbors:
if 0 <= x + dx < width and 0 <= y + dy < height:
if px[x + dx, y + dy] > thrd2:
cnt1 += 1
else:
cnt2 += 2
if cnt1 > cnt1mx:
px[x, y] = 0
elif cnt2 > cnt2mx:
px[x, y] = 255
img.save(r"./VCode/temp.jpg")
po = PaddleOCR(user_angle_cls=True, use_space_char=False, max_text_length=4, lang="en", det_max_side_len=100, det_db_score_mode="slow", det_db_unclip_ratio=3)
res = po.ocr(r"./VCode/temp.jpg", cls=True)
if res != None:
for code in res:
print(code)
文档识别
from PIL import Image
from paddleocr import PaddleOCR
po = PaddleOCR(use_angle_cls=True, lang="en")
res = po.ocr(r"./doc/1.pdf", cls=True)
for idx in range(len(res)):
page = res[idx]
if res == None:
continue
for ctx in page:
print(ctx)