Python 2.7 如何获取pytesseract中的字符位置
我正在尝试使用pytesseract库获取图像文件的字符位置Python 2.7 如何获取pytesseract中的字符位置,python-2.7,image-processing,ocr,python-tesseract,pytesser,Python 2.7,Image Processing,Ocr,Python Tesseract,Pytesser,我正在尝试使用pytesseract库获取图像文件的字符位置 import pytesseract from PIL import Image print pytesseract.image_to_string(Image.open('5.png')) 是否有任何库可用于使用PyteSeract获取角色的每个位置?拥有该位置似乎不是最好的主意,但您可以这样做: from pytesseract import pytesseract pytesseract.run_tesseract('imag
import pytesseract
from PIL import Image
print pytesseract.image_to_string(Image.open('5.png'))
是否有任何库可用于使用PyteSeract获取角色的每个位置?拥有该位置似乎不是最好的主意,但您可以这样做:
from pytesseract import pytesseract
pytesseract.run_tesseract('image.png', 'output', lang=None, boxes=False, config="hocr")
角色的位置可以如下所示
import csv
import cv2
from pytesseract import pytesseract as pt
pt.run_tesseract('bw.png', 'output', lang=None, boxes=True, config="hocr")
# To read the coordinates
boxes = []
with open('output.box', 'rb') as f:
reader = csv.reader(f, delimiter = ' ')
for row in reader:
if(len(row)==6):
boxes.append(row)
# Draw the bounding box
img = cv2.imread('bw.png')
h, w, _ = img.shape
for b in boxes:
img = cv2.rectangle(img,(int(b[1]),h-int(b[2])),(int(b[3]),h-int(b[4])),(255,0,0),2)
cv2.imshow('output',img)
使用此方法时,可能会遗漏一些文本。这需要对图像进行一些预处理(即背景减法)才能获得更好的结果。是否尝试使用pytesseract.image\u to\u data()
您可以使用下面的脚本将每个字符保存为图像,这是针对python 3.6的+
import cv2
import pytesseract
import numpy
import time
from PIL import ImageGrab
path_to_save = "img_out/"
img = cv2.imread("image_INAUTXQ_www.facebook.com.png")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
hImg, wImg,_ = img.shape
boxes = pytesseract.image_to_boxes(img)
ROI_number=0
for b in boxes.splitlines():
b = b.split(' ')
x, y, w, h = int(b[1]), int(b[2]), int(b[3]), int(b[4])
# cv2.rectangle(img, (x,hImg- y), (w,hImg- h), (50, 50, 255), 1)
x1,y1=hImg-h,hImg-y
x2,y2=x,w
roi=img[x1:y1,x2:y2]
output = cv2.resize(roi,(300,300))
# cv2.imshow('roi', roi)
# cv2.imshow('img',img)
# cv2.waitKey(0)
cv2.imwrite("test"+str(ROI_number)+".jpeg",output)
ROI_number+=1
它不起作用。对于最新版本,请使用此
import cv2
import pytesseract
import numpy
import time
from PIL import ImageGrab
path_to_save = "img_out/"
img = cv2.imread("image_INAUTXQ_www.facebook.com.png")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
hImg, wImg,_ = img.shape
boxes = pytesseract.image_to_boxes(img)
ROI_number=0
for b in boxes.splitlines():
b = b.split(' ')
x, y, w, h = int(b[1]), int(b[2]), int(b[3]), int(b[4])
# cv2.rectangle(img, (x,hImg- y), (w,hImg- h), (50, 50, 255), 1)
x1,y1=hImg-h,hImg-y
x2,y2=x,w
roi=img[x1:y1,x2:y2]
output = cv2.resize(roi,(300,300))
# cv2.imshow('roi', roi)
# cv2.imshow('img',img)
# cv2.waitKey(0)
cv2.imwrite("test"+str(ROI_number)+".jpeg",output)
ROI_number+=1