OCR with python

Hi, I am trying to use OCR for this example of numbers https://drive.google.com/folderview?id=0B68PDhV5SW8BTjd0T0FqTG94cG8&usp=sharing

I am making a database of numbers, I am taking a screenshot from the numbers 1, 2, 3 .......

Later, for number recognition, I take a screenshot and compare it with my database snapshots.

The code works fine, but I have one lazy problem, the numbers can be from 0.00 to 999.99, so I need to take all these screenshots, and I can’t create the numbers, so I think I need to find another solution.

I think if I can break the screenshots between them. (100.99 = 100 and 99) I need only 999 samples in my database.

So, do you think this might be a good solution?

News!!! I keep searching and finally found a solution with pytesseract

Few things I need to resize images to min. 25 pixels for 100% good results.

If I save the image in png format, it does not work, but with the jpg work prefix. If I open a png image with paint and save it unchanged, nothing will improve the work of the code with png image. I do not understand this.

I really need work with png because I need to work quickly with code. Any idea to solve this problem using the png format?

import pytesseract
from PIL import Image
x = pytesseract.image_to_string(Image.open('101.jpg'))
y = float(x)

print y

I searched for code on image segmentation, found outlines and connected components. I found this code to search for the area of ​​numbers and points.

samplenumbers

samplenumbersregion

Found 1 region in numbers 0,1,6,8 and a point, in others 2 regions found.

I can’t change the code to work with my image (the number of the white background is black), so I change the color of my image and I see an impossible editing code to fix the problem with the regions.

, , , , - .

i=0
while i < len(regionfound)
   if height(region[i] = 13 #(max height)
     compare region with dabatabe image of numbers 0,1,6 and 8

   if height = 2
     region are dot

   if height = .....  

   i+=1

import sys
from PIL import Image, ImageDraw

class Region():
    def __init__(self, x, y):
        self._pixels = [(x, y)]
        self._min_x = x
        self._max_x = x
        self._min_y = y
        self._max_y = y

    def add(self, x, y):
        self._pixels.append((x, y))
        self._min_x = min(self._min_x, x)
        self._max_x = max(self._max_x, x)
        self._min_y = min(self._min_y, y)
        self._max_y = max(self._max_y, y)

    def box(self):
        return [(self._min_x, self._min_y), (self._max_x, self._max_y)]

def find_regions(im):
    width, height  = im.size
    regions = {}
    pixel_region = [[0 for y in range(height)] for x in range(width)]
    equivalences = {}
    n_regions = 0
    #first pass. find regions.
    for x in xrange(width):
        for y in xrange(height):
            #look for a black pixel
            if im.getpixel((x, y)) == (0, 0, 0, 255): #BLACK NUMBERS FOR WHITE NUMBER USE (255, 255, 255, 255)
                # get the region number from north or west
                # or create new region
                region_n = pixel_region[x-1][y] if x > 0 else 0
                region_w = pixel_region[x][y-1] if y > 0 else 0
                max_region = max(region_n, region_w)

                if max_region > 0:
                    #a neighbour already has a region
                    #new region is the smallest > 0
                    new_region = min(filter(lambda i: i > 0, (region_n, region_w)))
                    #update equivalences
                    if max_region > new_region:
                        if max_region in equivalences:
                            equivalences[max_region].add(new_region)
                        else:
                            equivalences[max_region] = set((new_region, ))
                else:
                    n_regions += 1
                    new_region = n_regions

                pixel_region[x][y] = new_region

    #Scan image again, assigning all equivalent regions the same region value.
    for x in xrange(width):
        for y in xrange(height):
                r = pixel_region[x][y]
                if r > 0:
                    while r in equivalences:
                        r = min(equivalences[r])

                    if not r in regions:
                        regions[r] = Region(x, y)
                    else:
                        regions[r].add(x, y)

    return list(regions.itervalues())

def main():
    im = Image.open(r"0.png")
    regions = find_regions(im)
    draw = ImageDraw.Draw(im)
    for r in regions:
        draw.rectangle(r.box(), outline=(255, 0, 0))
    del draw 
    #im.show()
    output = file("output.png", "wb")
    im.save(output)
    output.close()

if __name__ == "__main__":
    main()
+4

Source: https://habr.com/ru/post/1599186/


All Articles