首页 > 解决方案 > python中的字符分割

问题描述

我正在使用 python 中的计算机视觉检测手写符号。我在单个字符的数据集上训练了一个 cnn,但现在我希望能够从图像中提取字符以便对单个字符进行预测。做这个的最好方式是什么?我将使用的手写文本不会是草书,字符之间会有明显的分隔。

标签: machine-learningcomputer-visionocr

解决方案


在下面的代码片段中,boxes 变量具有图像中每个字符的尺寸。

import cv2
import pytesseract

file = '/content/Captchas/image22.jpg'

img = cv2.imread(file)
h, w, _ = img.shape

boxes = pytesseract.image_to_boxes(img)

for b in boxes.splitlines():
    b = b.split(' ')
    img = cv2.rectangle(img, (int(b[1]), h - int(b[2])), (int(b[3]), h - int(b[4])), (0, 255, 0), 2)

cv2_imshow(img)
print(boxes)

推荐阅读