示例#1
0
def tester_char(uri):
    img = cv2.imread(uri)

    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    dataString_middle=""
    dataString_lower=""
    dataString_upper=""
    lowerZoneLabels = fileIO.read_label_file('learner/lowerZoneLabels')
    middleZoneLabels = fileIO.read_label_file('learner/middleZoneLabels')
    upperZoneLabels = fileIO.read_label_file('learner/upperZoneLabels')


    character = gray[0:64, 0:128]
    character = normalize.get_mask(character, 5)
    xMin, xMax, yMin, yMax= locate_character.char_location(character)
    # print xMin, xMax, yMin, yMax

    classifier_middle = pickle.load(open('learner/ANN_middle'))
    classifier_lower = pickle.load(open('learner/ANN_lower'))
    classifier_upper = pickle.load(open('learner/ANN_upper'))



    dataArray_middle = feature_mapper.middle(character, 21, 43, xMin, xMax)
    dataArray_middle.append('m1')
    # print dataArray_middle

    dataArray_lower = feature_mapper.lower(character, 42, 64, xMin, xMax)
    dataArray_lower.append('l0')
    # print dataArray_lower

    dataArray_upper = feature_mapper.upper(character, 0, 22, xMin, xMax)
    dataArray_upper.append('u0')
 
    # MiddleZoneClasss = classifier_middle(dataArray_middle, Orange.classification.Classifier.GetBoth)
    MiddleZoneProb = classifier_middle(dataArray_middle, Orange.classification.Classifier.GetProbabilities)
    UpperZoneProb = classifier_upper(dataArray_upper, Orange.classification.Classifier.GetProbabilities)
    LowerZoneProb = classifier_lower(dataArray_lower, Orange.classification.Classifier.GetProbabilities)

    MiddleZoneClass=(classifier_middle(dataArray_middle, Orange.classification.Classifier.GetBoth))
    UpperZoneClass=(classifier_upper(dataArray_upper, Orange.classification.Classifier.GetBoth))
    LowerZoneClass=(classifier_lower(dataArray_lower, Orange.classification.Classifier.GetBoth))


    char, lower, middle, upper = prob_match.probability_match(lower=[LowerZoneProb,lowerZoneLabels],
                                        middle=[MiddleZoneProb, middleZoneLabels],
                                        upper=[UpperZoneProb,upperZoneLabels])

    # char_mapper.char_map()

    print char+"   "+lower+"  "+middle+"  "+upper
示例#2
0
def validator_char(start, end):
    # lowerZoneLabels, middleZoneLabels, upperZoneLabels, classifier_middle, classifier_lower, classifier_upper=loadfile()
    img = cv2.imread(os.path.join(package_directory, 'data/chars - Copy.jpg'))
    iMax = 109
    # iMax=5
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    index = 0
    dataString_middle = ""
    dataString_lower = ""
    dataString_upper = ""
    char_matrix = {}
    char_time_matrix = {}
    lowerZoneLabels = fileIO.read_label_file('learner/lowerZoneLabels')
    middleZoneLabels = fileIO.read_label_file('learner/middleZoneLabels')
    upperZoneLabels = fileIO.read_label_file('learner/upperZoneLabels')
    classifier_middle = pickle.load(
        open(os.path.join(package_directory, 'learner/ANN_middle')))
    classifier_lower = pickle.load(
        open(os.path.join(package_directory, 'learner/ANN_lower')))
    classifier_upper = pickle.load(
        open(os.path.join(package_directory, 'learner/ANN_upper')))

    for j in range(start, end):
        for i in range(1, iMax):
            time_start = time.time()
            character = gray[64 * (j - 1):64 * j, 128 * (i - 1):128 * i]
            character = normalize.get_mask(character, 5)
            xMin, xMax, yMin, yMax = locate_character.char_location(character)
            # cv2.imshow("sd",character)
            # cv2.waitKey(0)

            dataArray_middle = feature_mapper.middle(character, 21, 43, xMin,
                                                     xMax)
            dataArray_middle.append('m1')
            # print dataArray_middle

            dataArray_lower = feature_mapper.lower(character, 42, 64, xMin,
                                                   xMax)
            dataArray_lower.append('l0')

            dataArray_upper = feature_mapper.upper(character, 0, 22, xMin,
                                                   xMax)
            dataArray_upper.append('u0')

            # MiddleZoneClasss = classifier_middle(dataArray_middle, Orange.classification.Classifier.GetBoth)
            MiddleZoneProb = classifier_middle(
                dataArray_middle,
                Orange.classification.Classifier.GetProbabilities)
            UpperZoneProb = classifier_upper(
                dataArray_upper,
                Orange.classification.Classifier.GetProbabilities)
            LowerZoneProb = classifier_lower(
                dataArray_lower,
                Orange.classification.Classifier.GetProbabilities)

            MiddleZoneClass = (classifier_middle(
                dataArray_middle, Orange.classification.Classifier.GetBoth))
            UpperZoneClass = (classifier_upper(
                dataArray_upper, Orange.classification.Classifier.GetBoth))
            LowerZoneClass = (classifier_lower(
                dataArray_lower, Orange.classification.Classifier.GetBoth))
            # print MiddleZoneClass
            # char1=char_mapper.char_map(LowerZoneClass, MiddleZoneClass, UpperZoneClass)
            # print char1

            predicted_char, lower, middle, upper = prob_match.probability_match(
                lower=[LowerZoneProb, lowerZoneLabels],
                middle=[MiddleZoneProb, middleZoneLabels],
                upper=[UpperZoneProb, upperZoneLabels])
            # char_mapper.char_map()
            input_char = validate_mapper.char_map(i)
            time_end = time.time()
            if input_char is not None:
                print str(i) + "####################"
                print str(
                    input_char
                ) + " : " + predicted_char + "   " + lower + "  " + middle + "  " + upper + "     time:" + str(
                    time_end - time_start)

            # char_matrix[input_char]=predicted_cha
            # #fdfdr
            if input_char is not None:
                char_matrix.setdefault(input_char, []).append(predicted_char)
                char_time_matrix.setdefault(input_char,
                                            []).append(time_end - time_start)

    performance(char_matrix, char_time_matrix)
def extractor(zone, start, end, phase):
    img = cv2.imread("C:/Users/Naleen/PycharmProjects/CharReco2/data/chars - Copy.jpg")
    iMax = 109
    jMax = 41
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    dataString_middle = ""
    dataString_lower = ""
    dataString_upper = ""
    for j in range(start, end):
        # for j in range (1,jMax):
        for i in range(1, iMax):
            ####tab file####
            character = gray[64 * (j - 1) : 64 * j, 128 * (i - 1) : 128 * i]
            character = normalize.get_mask(character, 5)
            xMin, xMax, yMin, yMax = locate_character.char_location(character)

            if zone == "middle":
                imapper, ilabel = mapper_middle.middlezone_mapper(i)
                if imapper is True:
                    dataArray_middle = feature_mapper.middle(character, 21, 43, xMin, xMax)
                    dataString_middle = (
                        dataString_middle + (ilabel + "\t") + ("\t".join(map(str, dataArray_middle))) + ("\n")
                    )
                else:
                    continue

            if zone == "lower":
                imapper, ilabel = mapper_lower.lowerzone_mapper(i)
                if imapper is True:
                    # print i
                    dataArray_lower = feature_mapper.lower(character, 42, 64, xMin, xMax)
                    dataString_lower = (
                        dataString_lower + (ilabel + "\t") + ("\t".join(map(str, dataArray_lower))) + ("\n")
                    )
                else:
                    continue

            if zone == "upper":
                imapper, ilabel = mapper_upper.upperzone_mapper(i)
                if imapper is True:
                    dataArray_upper = feature_mapper.upper(character, 0, 22, xMin, xMax)
                    dataString_upper = (
                        dataString_upper + (ilabel + "\t") + ("\t".join(map(str, dataArray_upper))) + ("\n")
                    )
                else:
                    continue

    # write the features to tab file
    if zone == "middle":
        fileIO.write_tl_file(dataArray_middle, dataString_middle, "learner/data_middle.tab")
        tl_data = Orange.data.Table("learner/data_middle")
        if phase == "validate":
            fileIO.write_tl_file(dataArray_middle, dataString_middle, "learner/data_middle_validate.tab")
            tl_data = Orange.data.Table("learner/data_middle_validate")
    if zone == "lower":
        fileIO.write_tl_file(dataArray_lower, dataString_lower, "learner/data_lower.tab")
        tl_data = Orange.data.Table("learner/data_lower")
        if phase == "validate":
            fileIO.write_tl_file(dataArray_lower, dataString_lower, "learner/data_lower_validate.tab")
            tl_data = Orange.data.Table("learner/data_lower_validate")
    if zone == "upper":
        fileIO.write_tl_file(dataArray_upper, dataString_upper, "learner/data_upper.tab")
        tl_data = Orange.data.Table("learner/data_upper")
        if phase == "validate":
            fileIO.write_tl_file(dataArray_upper, dataString_upper, "learner/data_upper_validate.tab")
            tl_data = Orange.data.Table("learner/data_upper_validate")

    return tl_data
示例#4
0
def classify(img):
    lowerZoneLabels = fileIO.read_label_file('learner/lowerZoneLabels')
    middleZoneLabels = fileIO.read_label_file('learner/middleZoneLabels')
    upperZoneLabels = fileIO.read_label_file('learner/upperZoneLabels')

    index = 0
    #print img
    # for i in range(1, 3)
    if img is None:
        # print "None"
        return ""

    if img.any() == np.array([0]).all():
        # print "space"
        return " "

    else:
        #im = np.array(img * 255, dtype = np.uint8)
        #gray = np.array(img * 255, dtype = np.uint8)

        dataString_middle = ""
        dataString_lower = ""
        dataString_upper = ""

        character = img
        # print character

        character = normalize.get_mask(character, 5)
        xMin, xMax, yMin, yMax = locate_character.char_location(character)
        # print xMin, xMax, yMin, yMax

        dataArray_middle = feature_mapper.middle(character, 21, 43, xMin, xMax)
        dataArray_middle.append('m1')
        # print dataArray_middle

        dataArray_lower = feature_mapper.lower(character, 42, 64, xMin, xMax)
        dataArray_lower.append('l0')
        # print dataArray_lower

        dataArray_upper = feature_mapper.upper(character, 0, 22, xMin, xMax)
        dataArray_upper.append('u0')

        # for i in range(1, 3):
        # char1=""
        # char2=""
        # char3=""
        MiddleZoneProb = classifier_middle(
            dataArray_middle,
            Orange.classification.Classifier.GetProbabilities)
        UpperZoneProb = classifier_upper(
            dataArray_upper, Orange.classification.Classifier.GetProbabilities)
        LowerZoneProb = classifier_lower(
            dataArray_lower, Orange.classification.Classifier.GetProbabilities)

        # MiddleZoneClass=(classifier_middle(dataArray_middle, Orange.classification.Classifier.GetBoth))
        # UpperZoneClass=(classifier_upper(dataArray_upper, Orange.classification.Classifier.GetBoth))
        # LowerZoneClass=(classifier_lower(dataArray_lower, Orange.classification.Classifier.GetBoth))

        ###############################################
        if classifier_lower(dataArray_lower,
                            Orange.classification.Classifier.GetValue) == 'l0':

            for i in range(0, len(lowerZoneLabels)):
                lowerZoneLabels[i] = 'l0'

            for i in xrange(0, len(LowerZoneProb)):
                LowerZoneProb[i] = 1

        if classifier_upper(dataArray_upper,
                            Orange.classification.Classifier.GetValue) == 'u0':

            for i in range(0, len(upperZoneLabels)):
                upperZoneLabels[i] = 'u0'

            for i in xrange(0, len(UpperZoneProb)):
                UpperZoneProb[i] = 1

        predicted_char, lower, middle, upper = prob_match.probability_match(
            lower=[LowerZoneProb, lowerZoneLabels],
            middle=[MiddleZoneProb, middleZoneLabels],
            upper=[UpperZoneProb, upperZoneLabels])
        # char_mapper.char_map()

        # print char+"   "+lower+"  "+middle+"  "+upper
        return predicted_char
示例#5
0
def validator_char(start, end):
    # lowerZoneLabels, middleZoneLabels, upperZoneLabels, classifier_middle, classifier_lower, classifier_upper=loadfile()
    img = cv2.imread('C:/Users/Naleen/PycharmProjects/CharReco2/data/chars - Copy.jpg')
    iMax = 109
    # iMax=5
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    dataString_middle=""
    dataString_lower=""
    dataString_upper=""
    char_matrix = {}
    char_time_matrix = {}
    lowerZoneLabels = fileIO.read_label_file('learner/lowerZoneLabels')
    middleZoneLabels = fileIO.read_label_file('learner/middleZoneLabels')
    upperZoneLabels = fileIO.read_label_file('learner/upperZoneLabels')
    # classifier_middle = pickle.load(open('learner/ANN_middle'))
    # classifier_lower = pickle.load(open('learner/ANN_lower'))
    # classifier_upper = pickle.load(open('learner/ANN_upper'))
    classifier_middle = pickle.load(open(os.path.join(package_directory, 'learner/ANN_middle')))
    classifier_lower = pickle.load(open(os.path.join(package_directory, 'learner/ANN_lower')))
    classifier_upper = pickle.load(open(os.path.join(package_directory, 'learner/ANN_upper')))



    for j in range (start, end):
        for i in range (1, iMax):
            time_start=time.time()
            character = gray[64*(j-1):64*j, 128*(i-1):128*i]
            character = normalize.get_mask(character, 5)
            xMin, xMax, yMin, yMax= locate_character.char_location(character)
            # cv2.imshow("sd",character)
            # cv2.waitKey(0)





            dataArray_middle = feature_mapper.middle(character, 21, 43, xMin, xMax)
            dataArray_middle.append('m1')
            # print dataArray_middle

            dataArray_lower = feature_mapper.lower(character, 42, 64, xMin, xMax)
            dataArray_lower.append('l0')

            dataArray_upper = feature_mapper.upper(character, 0, 22, xMin, xMax)
            dataArray_upper.append('u0')


            # MiddleZoneClasss = classifier_middle(dataArray_middle, Orange.classification.Classifier.GetBoth)
            MiddleZoneProb = classifier_middle(dataArray_middle, Orange.classification.Classifier.GetProbabilities)
            UpperZoneProb = classifier_upper(dataArray_upper, Orange.classification.Classifier.GetProbabilities)
            LowerZoneProb = classifier_lower(dataArray_lower, Orange.classification.Classifier.GetProbabilities)

            MiddleZoneClass=(classifier_middle(dataArray_middle, Orange.classification.Classifier.GetBoth))
            UpperZoneClass=(classifier_upper(dataArray_upper, Orange.classification.Classifier.GetBoth))
            LowerZoneClass=(classifier_lower(dataArray_lower, Orange.classification.Classifier.GetBoth))
            # print MiddleZoneClass
            # char1=char_mapper.char_map(LowerZoneClass, MiddleZoneClass, UpperZoneClass)
            # print char1


            predicted_char, lower, middle, upper = prob_match.probability_match(lower=[LowerZoneProb,lowerZoneLabels],
                                                middle=[MiddleZoneProb, middleZoneLabels],
                                                upper=[UpperZoneProb,upperZoneLabels])

            # char_mapper.char_map()
            input_char= validate_mapper.char_map(i)
            time_end=time.time()
            if input_char is not None:
                print str(i)+ "####################"
                print str(input_char)+" : "+predicted_char+"   "+lower+"  "+middle+"  "+upper+"     time:"+str(time_end-time_start)

            # char_matrix[input_char]=predicted_cha
            # #fdfdr
            if input_char is not None:
                char_matrix.setdefault(input_char, []).append(predicted_char)
                char_time_matrix.setdefault(input_char, []).append(time_end-time_start)


    performance(char_matrix, char_time_matrix)