我正在使用python中的计算机视觉来检测手写符号。我在单个字符的数据集上训练了cnn,但是现在我希望能够从图像中提取字符,以便对单个字符进行预测。做这个的最好方式是什么?我将使用的手写文本不会草书,而且字符之间会有明显的分隔。
答案 0 :(得分:1)
在下面的代码段中,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)
答案 1 :(得分:0)
您可以使用查找轮廓并将其与框绑定。
image = cv2.imread("filename")
image = cv2.fastNlMeansDenoisingColored(image,None,10,10,7,21)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
res,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV) #threshold
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
dilated = cv2.dilate(thresh,kernel,iterations = 5)
val,contours, hierarchy =
cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
coord = []
for contour in contours:
[x,y,w,h] = cv2.boundingRect(contour)
if h>300 and w>300:
continue
if h<40 or w<40:
continue
coord.append((x,y,w,h))
coord.sort(key=lambda tup:tup[0]) # if the image has only one sentence sort in one axis
count = 0
for cor in coord:
[x,y,w,h] = cor
t = image[y:y+h,x:x+w,:]
cv2.imwrite(str(count)+".png",t)
print("number of char in image:", count)