Exemplo n.º 1
0
def recogniseOne(svm, img, path0, paths, charIndex):
    print("---recogniseOne---")
    height, width = img.shape
    start = Constants.SPILTLINE_RANGES[charIndex][0]
    end = Constants.SPILTLINE_RANGES[charIndex][1]
    char1 = []
    ret = []
    choosedPaths = []
    for path in paths:
        if path[0][0] >= start and path[0][0] <= end:
            #这里要再进行一次搜索的,因为之前的path是不完全的
            if path[len(path) - 1][1] == 0:
                SpiltUtil.searchRecursionFromTop(img, path, path[0][0],
                                                 path[0][1], charIndex)
            else:
                SpiltUtil.searchRecursionFromBottom(img, path, path[0][0],
                                                    path[0][1], charIndex)
            path.sort(key=lambda y: y[1])
            path1 = Util.fillPath(path, height)
            oneChar = ReadImg.getOneChar(img, path0, path1)
            print("NonZero in oneChar:" + str(np.count_nonzero(oneChar)))
            #如果这个分割出来的字符中黑色像素点太少,那直接不要了,因为肯定是错误的
            if np.count_nonzero(oneChar) < 30:
                continue
            c1 = Util.conventToTrainChar(oneChar)
            choosedPaths.append(path1)
            #Util.printCV2Image(c1)
            for angle in range(Constants.LEFT_ANGLE, Constants.RIGHT_ANGLE):
                c2 = Util.rotate(c1, angle)
                #Util.printCV2Image(c2)
                #cv2.imshow('img' + str(angle), c2)
                char1.append(c2)
    #最后一个字符,特殊处理
    if end == width - 1:
        path1 = Util.getRightBoder(width, height)
        choosedPaths.append(path1)
        c1 = Util.conventToTrainChar(ReadImg.getOneChar(img, path0, path1))
        for angle in range(Constants.LEFT_ANGLE, Constants.RIGHT_ANGLE):
            c2 = Util.rotate(c1, angle)
            #Util.printCV2Image(c2)
            #cv2.imshow('img' + str(angle), c2)
            char1.append(c2)
    '''for p1 in range(start, end):
        path1 = Util.fillPath(SpiltUseCV2.spiltOne(img, p1), height)
        c1 = Util.conventToTrainChar(ReadImg.getOneChar(img, path0, path1))
        for angle in range(Constants.LEFT_ANGLE, Constants.RIGHT_ANGLE):
            c2 = Util.rotate(c1, angle)
            #Util.printCV2Image(c2)
            #cv2.imshow('img' + str(angle), c2)
            char1.append(c2)'''
    #要训练的是(height, width, 20, 20)这样的四维数组
    ret.append(char1)
    result = predictInMyWay(svm, ret)
    #print("result:" + str(result))
    maxValue = getMaxNumChar(result)
    bestSplitPath = findBestSplitPath(result, maxValue, choosedPaths)
    print("bses Path:" + str(bestSplitPath))
    return bestSplitPath, maxValue
Exemplo n.º 2
0
def searchAllPossible(img, path0, path1, char1, choosedPaths, charIndex):
    height, width = img.shape
    #在这里想加上对这个开始的分割点左右5个点分别进行搜索,以尽量获得一个最大概率的
    #新加的左右五个点search,先把旋转去掉了
    #从上搜索
    searchRange = Constants.SEARCH_ALL_POSSIBLE_RANGE
    for start in range(max(path1[0][0] - searchRange, 0), path1[0][0]):
        newPath = Util.fillPath(
            SpiltUtil.spiltOne(img, start, Constants.SEARCH_FROM_TOP,
                               charIndex), height)
        char1.append(
            Util.conventToTrainChar(
                ReadImg.getOneChar(img, path0, newPath)[0]))
        choosedPaths.append(newPath)
    #从上搜索
    for start in range(path1[0][0] + 1,
                       min(path1[0][0] + searchRange, height - 1)):
        newPath = Util.fillPath(
            SpiltUtil.spiltOne(img, start, Constants.SEARCH_FROM_TOP,
                               charIndex), height)
        char1.append(
            Util.conventToTrainChar(
                ReadImg.getOneChar(img, path0, newPath)[0]))
        choosedPaths.append(newPath)
    #从下搜索
    for start in range(max(path1[height - 1][0] - searchRange, 0),
                       path1[height - 1][0]):
        newPath = Util.fillPath(
            SpiltUtil.spiltOne(img, start, Constants.SEARCH_FROM_BOTTOM,
                               charIndex), height)
        char1.append(
            Util.conventToTrainChar(
                ReadImg.getOneChar(img, path0, newPath)[0]))
        choosedPaths.append(newPath)

    #从下搜索
    for start in range(path1[height - 1][0] + 1,
                       min(path1[height - 1][0] + searchRange, height - 1)):
        newPath = Util.fillPath(
            SpiltUtil.spiltOne(img, start, Constants.SEARCH_FROM_BOTTOM,
                               charIndex), height)
        char1.append(
            Util.conventToTrainChar(
                ReadImg.getOneChar(img, path0, newPath)[0]))
        choosedPaths.append(newPath)
Exemplo n.º 3
0
def readOneImg(path):
    oriImg = cv2.imread(path, 0)
    img = Util.binaryzation(oriImg)
    img = Util.erasePaddingInOneChar(img)
    height, width = img.shape
    #获取指定的三个分割点, 再根据分割点做分割线
    points, directions = getSpiltPoint(path[path.rindex('/'):])
    
    path1 = Util.fillPath(SpiltUtil.spiltOne(img, points[0], directions[0], 0), height)
    path2 = Util.fillPath(SpiltUtil.spiltOne(img, points[1], directions[1], 1), height)
    path3 = Util.fillPath(SpiltUtil.spiltOne(img, points[2], directions[2], 2), height)
    path0 = Util.getLeftBorder(height)
    path4 = Util.getRightBoder(width, height)
    ret = []
    
    ret.extend(Util.conventToTrainCharFromAllWrap(getOneChar(img, path0, path1)))
    ret.extend(Util.conventToTrainCharFromAllWrap(getOneChar(img, path1, path2)))
    ret.extend(Util.conventToTrainCharFromAllWrap(getOneChar(img, path2, path3)))
    ret.extend(Util.conventToTrainCharFromAllWrap(getOneChar(img, path3, path4)))
    return ret
Exemplo n.º 4
0
def getAllPaths(img):
    #四个字符,三条分割线
    startPoint = {0: [20, 21], 1: [45, 46], 2: [68, 69]}
    height, width = img.shape
    for i in range(2):
        paths = []
        for x in startPoint[i]:
            paths.append(Util.fillPath(spiltOne_old(img, x), height))
        allPaths[i] = paths
    #printAllPath(allPaths)
    return allPaths
Exemplo n.º 5
0
def recogniseOne(svm, img, path0, paths, charIndex):
    print("---recogniseOne---")
    height, width = img.shape
    start = Constants.SPILTLINE_RANGES[charIndex][0]
    end = Constants.SPILTLINE_RANGES[charIndex][1]
    char1 = []
    ret = []
    choosedPaths = []
    for path in paths:
        if path[0][0] >= start and path[0][0] <= end:
            #这里要再进行一次搜索的,因为之前的path是不完全的
            if path[len(path) - 1][1] == 0:
                SpiltUtil.searchRecursionFromTop(img, path, path[0][0],
                                                 path[0][1], charIndex)
            else:
                SpiltUtil.searchRecursionFromBottom(img, path, path[0][0],
                                                    path[0][1], charIndex)
            path.sort(key=lambda y: y[1])
            path1 = Util.fillPath(path, height)
            oneChar = ReadImg.getOneChar(img, path0, path1)
            #因为在getOneChar里加了wrapAffine,导致了getOneChar是返回的一个字符数组
            #但我们识别字符时现在还不想作wrapaffine,所以就默认去第一个原始字符了
            oneChar = oneChar[0]
            print("NonZero in oneChar:" + str(np.count_nonzero(oneChar)))
            #如果这个分割出来的字符中黑色像素点太少,那直接不要了,因为肯定是错误的
            if np.count_nonzero(oneChar) < 40:
                continue
            c1 = Util.conventToTrainChar(oneChar)
            choosedPaths.append(path1)
            char1.append(c1)
            searchAllPossible(img, path0, path1, char1, choosedPaths,
                              charIndex)

    #即在上面的paths中并没有找到合适的分割线
    if len(char1) == 0:
        '''
                            因为上面并没有合适的分割线,所以这里手动选取两个合适的分割点
        1.path0.x + 20
        2.path0.x + 2 * Constants.SEARCH_ALL_POSSIBLE_RANGE
        '''
        startX1 = path0[0][0] + 20
        path1 = Util.fillPath(
            SpiltUtil.spiltOne(img, startX1, Constants.SEARCH_FROM_TOP,
                               charIndex), height)
        searchAllPossible(img, path0, path1, char1, choosedPaths, charIndex)
        startX1 = startX1 + (2 * Constants.SEARCH_ALL_POSSIBLE_RANGE)
        path1 = Util.fillPath(
            SpiltUtil.spiltOne(img, startX1, Constants.SEARCH_FROM_TOP,
                               charIndex), height)
        searchAllPossible(img, path0, path1, char1, choosedPaths, charIndex)

    #最后一个字符,特殊处理
    if end == width - 1:
        path1 = Util.getRightBoder(width, height)
        choosedPaths.append(path1)
        #因为在getOneChar里加了wrapAffine,导致了getOneChar是返回的一个字符数组
        #但我们识别字符时现在还不想作wrapaffine,所以就默认去第一个原始字符了
        c1 = Util.conventToTrainChar(ReadImg.getOneChar(img, path0, path1)[0])
        searchAllPossible(img, path0, path1, char1, choosedPaths, charIndex)

    #要训练的是(height, width, 20, 20)这样的四维数组
    ret.append(char1)
    indexs, probs = predictAndCalc(svm, ret)
    print("indexs:" + str(indexs))
    print("probs:" + str(probs))
    maxIndex, spiltIndex = getMaxNumChar_New(indexs, probs)
    print("maxIndex:" + str(maxIndex))
    print("spiltIndex:" + str(spiltIndex))
    bestSplitPath = findBestSplitPath_New(spiltIndex, choosedPaths)
    print("best Path:" + str(bestSplitPath))
    return bestSplitPath, maxIndex