将列添加到空列表中,使用numpy进行基本OCR?

时间:2014-06-12 23:49:05

标签: python numpy ocr text-recognition

我正在尝试创建一个程序,该程序通过一个模拟一行文本的图像并从中抓取每个字母。考虑到2D像素阵列的图像,如果连续列中存在黑色像素,则将这些列写入缓冲区。一旦到达没有黑色像素的列(即字母之间的空格),缓冲区将被转入已检测到的字母的图像。但是,我收到一个编译错误,我不明白,我希望你们可以帮助我。希望你也能抓住我没有认识到的任何逻辑错误。

无论如何,代码:

from PIL import Image
import numpy as np

class ShapeDetect:
    def __init__(self):
        self.img = Image.open("test3.jpg")
        self.pixels = self.img.load()
        self.l = np.empty([0, 0])
        self.valid_pixels = ['(0, 0, 0)', '(1, 1, 1)', '(2, 2, 2)', '(3, 3, 3)', '(4, 4, 4)', '(5, 5, 5)']

def printPixels(self):
    for i in range(self.img.size[0]):
        for j in range(self.img.size[1]):
            print(self.pixels[i, j])

def processPixels(self):
    n = 1

    # If a black pixel is in the current column, add it to l
    for i in range (self.img.size[0]):
        for j in range(self.img.size[1]):
            if str(self.pixels[i, j]) in self.valid_pixels:
                self.writeColumn(i)
                break

            # Once a whole shape has been written to l, make l into an image
            else:
                 if self.l.size > 0 and j == self.img.size[1] - 1:
                    new_img = Image.new(self.img.mode, (self.l.size, 100))
                    new_img.putdata(self.l)
                    new_img.save(str(n) + ".jpg")

                    n = n + 1
                    self.l = np.empty([0], [0])

def writeColumn(self, n):
    # put column in pixels in temp, then append temp to l
    temp = np.zeros((1, self.img.size[1]), dtype=np.int64)
    for i in range(self.img.size[1]):
        temp[0, i], = (self.pixels[n, i])

    np.append(self.l, temp, axis = 1)

if __name__ == "__main__":
    shapeDetect = ShapeDetect()
    ShapeDetect.processPixels(shapeDetect)

我得到的错误是:

Traceback (most recent call last):
  File "SD.py", line 46, in <module>
    ShapeDetect.processPixels(shapeDetect)
  File "SD.py", line 23, in processPixels
    self.writeColumn(i)
  File "SD.py", line 40, in writeColumn
    temp[0, i], = (self.pixels[n, i])
ValueError: too many values to unpack

1 个答案:

答案 0 :(得分:0)

发生错误是因为self.pixels[n, i]返回了一个有3个值的像素。 看起来你实际上想要所有3个值,但是你错误地在temp[0,i]之后放了一个逗号。删除逗号可以解决问题。

但是,有一种更快的方法来提取列。你可以替换

temp = np.zeros((1, self.img.size[1]), dtype=np.int64)
for i in range(self.img.size[1]):
    temp[0, i], = (self.pixels[n, i])

temp = self.pixels[n, :]

用于numpy数组