Recognize images in Python

2019-03-11 16:16发布

I'm kinda new both to OCR recognition and Python.

What I'm trying to achieve is to run Tesseract from a Python script to 'recognize' some particular figures in a .tif.

I thought I could do some training for Tesseract but I didn't find any similar topic on Google and here at SO.

Basically I have some .tif that contains several images (like an 'arrow', a 'flower' and other icons), and I want the script to print as output the name of that icon. If it finds an arrow then print 'arrow'.

Is it feasible?

1条回答
趁早两清
2楼-- · 2019-03-11 17:14

This is by no means a complete answer, but if there are multiple images in the tif and if you know the size in advance, you can standardize the image samples prior to classifying them. You would cut up the image into all the possible rectangles in the tif.

So when you create a classifier (I don't mention the methods here), the end result would take a synthesis of classifying all of the smaller rectangles.

So if given a tif , the 'arrow' or 'flower' images are 16px by 16px , say, you can use Python PIL to create the samples.

from PIL import Image

image_samples = []

im = Image.open("input.tif")
sample_dimensions = (16,16)

for box in get_all_corner_combinations(im, sample_dimensions):

    image_samples.append(im.crop(box))


classifier = YourClassifier()

classifications = []

for sample in image_samples:
    classifications.append (classifier (sample))

label = fuse_classifications (classifications)

Again, I didn't talk about the learning step of actually writing YourClassifier. But hopefully this helps with laying out part of the problem.

There is a lot of research on the subject of learning to classify images as well as work in cleaning up noise in images before classifying them.

Consider browsing through this nice collection of existing Python machine learning libraries.

http://scipy-lectures.github.com/advanced/scikit-learn/index.html

There are many techniques that relate to images as well.

查看更多
登录 后发表回答