Python错误的多输出

时间:2017-11-08 13:03:56

标签: python python-imaging-library tesseract

我有一个从程序中检索信息的函数。它指向鼠标抓取图像,调整大小,然后使用tesseract识别并添加到ValueIndex。

  from PIL import Image
    import pytesseract
    import pyscreenshot as ImageGrab
    from pynput.mouse import Button, Controller
    import re

    def GetValues(): #Read matrix of digit from 
        pytesseract.pytesseract.tesseract_cmd ='C:\\Program Files (x86)\\Tesseract-OCR\\tesseract'
        mouse = Controller()
        Mul=2
        ValueIndex=[0 for i in range(0,170)] # Results
        y1=163
        if __name__ == "__main__":
            for j in range(0,1):# get 1 line for example
                y1=y1+67
                x1=1350
                for i in range(0,13):
                    x1=x1+67
                    mouse.position = (x1, y1)      
                    mouse.press(Button.left)
                    mouse.release(Button.left)
                    im=ImageGrab.grab(bbox=(1485, 1112,1680, 1177)) 
                    x,y = im.size
                    x=x*Mul
                    y=y*Mul
                    size=x,y
                    im_resized = im.resize(size, Image.ANTIALIAS) # resize for tesseract
                    im_resized.save('test%s.png' % i)
                    data = pytesseract.image_to_string(im_resized)
                    q=''.join(re.findall('\d',data)) # gt digits from str
                    if (len(q)==4):
                        q=indexq[:2]+'.'+q[2:]
                    if len(q)==3:
                        q=q[:1]+'.'+q[1:]
                    GetInd=j*13+i+1
                    ValueIndex[GetInd]=float(q)
                    print(i,'  ',data,' ',ValueIndex[GetInd]) # print to see the progress
        return(ValueIndex)

GetValues()   #When i comment it, print only 1 time


print('Why so many times?:)')

它工作正常,但是当我在prorgam的主体中添加打印时,它会输出多次。它不是在func循环中,什么是错的?

Why so many times?:)
0    +9.76%   9.76
Why so many times?:)
1    +619%   6.19
Why so many times?:)
2    +5.36%   5.36
 and so on

0 个答案:

没有答案