【发布时间】:2019-12-19 11:21:44
【问题描述】:
我想为我自己的新字体训练 tesseract,但我没有找到任何方法来做到这一点。我无法从图像创建盒子文件。我是编程语言的新手,有人告诉我 LabelImg 但它对 tesseract ocr 没有用。
请帮我推荐一个工具来标记图像中的文本,这对于 tesseract ocr 来说是新的。
【问题讨论】:
标签: python-3.x computer-vision tesseract text-recognition
我想为我自己的新字体训练 tesseract,但我没有找到任何方法来做到这一点。我无法从图像创建盒子文件。我是编程语言的新手,有人告诉我 LabelImg 但它对 tesseract ocr 没有用。
请帮我推荐一个工具来标记图像中的文本,这对于 tesseract ocr 来说是新的。
【问题讨论】:
标签: python-3.x computer-vision tesseract text-recognition
您可以创建自己的脚本来标记图像。 这里有一些示例代码可以让你这样做,你可以根据需要自定义它
import sys
import os
import cv2
def isImage(filepath) -> bool:
'''
checks if file is an image
'''
lowercasePath = filepath.lower()
# you can add more formats here
cases = [
lowercasePath.endswith('jpg'),
lowercasePath.endswith('png'),
lowercasePath.endswith('jpeg'),
]
return any(cases)
def getPaths(imgdir, condition=lambda x: True):
'''
given path to image folder will return you a list of full paths
to files which this folder contain
:param condition: is a function that will filter only those files
that satisfy condition
'''
files = map(lambda x: os.path.join(imgdir, x).strip(),
os.listdir(imgdir))
filtered = filter(condition, files)
return list(filtered)
def labelingProcess(imgdir):
print("Welcome to the labeling tool")
print("if you want to stop labeling just close the program or press ctrl+C")
WIDTH = 640
HEIGHT = 480
WINDOWNAME = "frame"
window = cv2.namedWindow(WINDOWNAME, cv2.WINDOW_NORMAL)
cv2.resizeWindow(WINDOWNAME, WIDTH, HEIGHT)
cv2.moveWindow(WINDOWNAME, 10, 10)
pathsToImages = getPaths(imgdir, isImage)
if not len(pathsToImages):
print("couldn't find any images")
return
for pathtoimage in pathsToImages:
imageName = os.path.basename(pathtoimage)
# label img has the same name as image only ends with .txt
labelName = ''.join(imageName.split('.')[:-1]) + '.gt.txt'
labelPath = os.path.join(imgdir, labelName)
# skip labeled images
if os.path.exists(labelPath):
continue
# read image
image = cv2.imread(pathtoimage)
if image is None:
print("couldn't open the image")
continue
h, w = image.shape[:2]
# resize to fixed size (only for visualization)
hnew = HEIGHT
wnew = int(w * hnew / h)
image = cv2.resize(image, (wnew, hnew))
cv2.imshow(WINDOWNAME, image)
cv2.waitKey(1)
print("enter what is written on the image or \
press enter to skip or")
label = input()
if not len(label):
continue
with open(labelPath, 'w') as labelfile:
labelfile.write(label)
cv2.destroyAllWindows()
if __name__ == '__main__':
imgdir = sys.argv[1]
labelingProcess(imgdir)
对于这个特殊的脚本要求是opencv
用法:
python3 labelingtool.py <path to your folder with images>
它将从您的文件夹中读取图像并创建相应的带有注释的 .gt.txt 文件。 在标记过程中,您可以在终端中输入注释。
进一步训练你自己的模型,你可以使用这个 repo https://github.com/thongvm/ocrd-train
需要数据集是格式图片和对应的注解
image1.tif
image1.gt.txt
image2.tif
image2.gt.txt
...
例如,要将图像转换为 .tif,您可以使用 mogrify
此代码会将所有 jpg 文件转换为 tif 文件
mogrify -format tif *.jpg
【讨论】: