检测图像中的文本位置并将其裁剪为 Python

Detect text position in an image and crop it in Python

我有这张照片 Text in an image

我想检测该文本位置,并裁剪仅聚焦于该文本的图像。

这是我的代码:

from PIL import Image 
# Opens a image in RGB mode 
im = Image.open(r"image.jpg") 
# Size of the image in pixels (size of orginal image) 
# (This is not mandatory) 
width, height = im.size 
print(im.size)
# Setting the points for cropped image 
left = 5
top = height / 4
right = 164
bottom = 3 * height / 4
# Cropped image of above dimension 
# (It will not change orginal image) 
im1 = im.crop((left, top, right, bottom)) 
# Shows the image in image viewer 
im1.save("new.jpg")

此代码工作正常,但文本在图像中的位置不是静态的。 我希望代码自动检测文本的位置然后裁剪它。

您可以使用基于深度学习的模型 "EAST" 来检测图像上的文本。 OpenCV 的 EAST 文本检测器基于一种新颖的架构和训练模式。它能够

(1) 运行 在 720p 图像上以 13 FPS 接近实时并且

(2)获得state-of-the-art文本检测accuracy.See这个link供参考:https://www.pyimagesearch.com/2018/08/20/opencv-text-detection-east-text-detector/.

您可以使用getbbox()获取边界框:

image=Image.open('text.jpg') 
x1,y1,x2,y2=image.getbbox() 
print(x1,y1,x2,y2)   

输出

16 192 208 216