Ejemplo n.º 1
0
def mubanDetectInvoiceDate(filepath, setKey='invoiceDate'):
    midProcessResult = [None, None]
    midProcessResult[0] = filepath
    midProcessResult[1] = 11
    # vat发票专票
    VATInvoiceTemplet = {
    }

    dic = xmlToDict.XmlTodict('/home/huangzheng/ocr/VATInvoiceSimpleMuban.xml')

    # tplt = [dic['QRCode'][0], dic['QRCode'][1], dic['figureX'][0] + dic['figureX'][2] / 2, dic['figureX'][1] + dic['figureX'][3] / 2]
    tplt = [dic['figureX'][0] + dic['figureX'][2] / 2, dic['figureX'][1] + dic['figureX'][3] / 2]
    # print(tplt)
    '''
    for c in tplt:
        if c == None:
            print('Templet VATInvoice error')
    '''
    TemType = {}
    if midProcessResult[1] == 11:  # 增值税专用
        VATInvoiceTemplet[setKey] = [int(dic.get(setKey)[0]), int(dic.get(setKey)[1]), int(dic.get(setKey)[2]),
                                     int(dic.get(setKey)[3])]
        TemType = VATInvoiceTemplet

    fcv = cv2.imread(filepath, 1)
    # print(fcv)
    try:
        w1 = fcv.shape
    except:
        print("picture is None")

    if w1[0] + w1[1] > 1500:
        rate = 0.5
        # print("rate : 0.5")

    if midProcessResult[1] == 11:
        # box = Detect.detect(cv2.imread(midProcessResult[0]), rate)
        figureP = FindCircle.findSymbol(filepath)
        # StBox = sortBox(box)
        # print(box)
        # print(figureP)
        # print(StBox)
        if figureP == None:
            return None
        Templet = simplyAdjust(TemType, [figureP[0], figureP[1]], tplt, w1)  # 增值税专票

        attributeLine = lineToAttribute.getAtbt.compute(textline(midProcessResult[0]), Templet)

    return attributeLine
Ejemplo n.º 2
0
def straighten_direction(blob_id, dir, port_h=False, history=120):
    """ Function straighten_direction(port_h): Runs the optimization phase for droplet walking.
    This function runs almost all of it's code in a continuous loop.
    Arguments:
    port_h - The serial port to use when communicating with God Droplet
    history - Optional argument to control the number of data points to take before running the optimization routine.
    """
    settings_history = {}
    settings_history[dir] = Counter()
    settings_history[(dir + 3) % 6] = Counter()
    if not port_h:
        port_h = default_port
    init_dir = dir
    while (True):
        try:
            pos_list = carefully_get_position_data_batch(
                dir, blob_id, history, port_h)
        except MyException:
            return None
        (center, radius, residual, sign) = fc.lsq(pos_list)
        print("center: (%f,%f), rad: %f, residu: %f >> " %
              (center[0], center[1], radius, residual),
              end='')
        prop = get_prop_control_value(radius)
        settings_history[dir]["%d %d %d" % tuple(
            map(get_raw_motor, current_motor_settings[dir]))] += 1
        if (sign > 0):
            print("Left")
            change_values = map(lambda x: x * prop,
                                change_motor_signs_LEFT[dir])
            change_motor(dir, change_values, port_h)
            sys.stdout.flush()
        else:
            print("Right")
            change_values = map(lambda x: x * prop,
                                change_motor_signs_RIGHT[dir])
            change_motor(dir, change_values, port_h)
            sys.stdout.flush()
        dir = (dir + 3) % 6
        if dir is init_dir:
            if settings_history[dir].most_common(
                    1)[0][1] > 4 and settings_history[
                        (dir + 3) % 6].most_common(1)[0][1] > 4:
                return settings_history  #successful completion
Ejemplo n.º 3
0
def straighten_direction(blob_id, dir, port_h=False, history = 120):
    """ Function straighten_direction(port_h): Runs the optimization phase for droplet walking.
    This function runs almost all of it's code in a continuous loop.
    Arguments:
    port_h - The serial port to use when communicating with God Droplet
    history - Optional argument to control the number of data points to take before running the optimization routine.
    """
    settings_history = {}
    settings_history[dir] = Counter()
    settings_history[(dir+3)%6] = Counter()
    if not port_h:
        port_h = default_port
    init_dir = dir
    while(True):
        try:
            pos_list = carefully_get_position_data_batch(dir, blob_id, history, port_h)
        except MyException:
            return None
        (center, radius, residual, sign) = fc.lsq(pos_list)
        print("center: (%f,%f), rad: %f, residu: %f >> "%(center[0], center[1], radius,residual), end='')
        prop=get_prop_control_value(radius)
        settings_history[dir]["%d %d %d"%tuple(map(get_raw_motor,current_motor_settings[dir]))]+=1
        if(sign>0):
            print("Left")
            change_values = map(lambda x: x*prop, change_motor_signs_LEFT[dir])
            change_motor(dir, change_values, port_h)                    
            sys.stdout.flush()
        else:
            print("Right")
            change_values = map(lambda x: x*prop, change_motor_signs_RIGHT[dir])
            change_motor(dir, change_values, port_h)  
            sys.stdout.flush()
        dir = (dir+3)%6
        if dir is init_dir:
            if settings_history[dir].most_common(1)[0][1]>4 and settings_history[(dir+3)%6].most_common(1)[0][1]>4:
                return settings_history #successful completion
Ejemplo n.º 4
0
def pick_sign(blob_id, dir, port_h=False, history = 60):
    """ Function straighten_direction(port_h): Runs the optimization phase for droplet walking.
    This function runs almost all of it's code in a continuous loop.
    Arguments:
    port_h - The serial port to use when communicating with God Droplet
    history - Optional argument to control the number of data points to take before running the optimization routine.
    """
    if not port_h:
        port_h = default_port
    raw_settings = map(get_raw_motor,current_motor_settings[dir])
    possible_signs = [copy.deepcopy(current_motor_settings[dir]) for i in range(4)]
    possible_signs[0][motors_involved[dir][0]]*=-1
    possible_signs[1][motors_involved[dir][1]]*=-1
    possible_signs[2][motors_involved[dir][0]]*=-1
    possible_signs[2][motors_involved[dir][1]]*=-1

    distances = []
    radii = []
    for setting in possible_signs:
        set_motor(dir, *map(get_raw_motor, setting))
        try:
            pos_list = get_position_data_batch(dir, blob_id, history, port_h)
        except MyException:
            return -1
        (center, radius, residual, sign) = fc.lsq(pos_list)
        distance = np.linalg.norm(pos_list[0]-pos_list[-1])
        radii.append(radius)
        distances.append(distance)
        print("rad: %f, dist: %f"%(radius,distance))

    radiiA = (radii[0]+radii[1])/2.0
    radiiB = (radii[2]+radii[3])/2.0
    if radiiA>radiiB:
        forward=possible_signs[0]
        backward=possible_signs[1]
    else:
        forward=possible_signs[2]
        backward=possible_signs[3]

    '''At this point, we have the signs right, except we need to figure out if it's going forward or backward.'''
    current_motor_settings[dir]=copy.deepcopy(forward)
    set_motor(dir, *map(get_raw_motor, forward))
    try:
        pos_listA = get_position_data_batch(dir, blob_id, history, port_h)
    except MyException:
        return -1
    change_values = map(lambda x: x*0.333, change_motor_signs_LEFT[dir])
    change_motor(dir, change_values, port_h) 
    try:
        pos_listB = carefully_get_position_data_batch(dir, blob_id, history, port_h)
    except MyException:
        return -1

    (centerA, radiusA, residualA, signA) = fc.lsq(pos_listA)
    (centerB, radiusB, residualB, signB) = fc.lsq(pos_listB)
    
    print("radA: %f, signA: %f, radB: %f, signB: %f"%(radiusA, signA, radiusB, signB))
    if signA>0:
        if radiusA>radiusB and signB>0:
            print("Center on left, but radius got smaller. Switching.")
            (forward, backward) = (backward, forward)
        else:
            print("Center on left, and radius got bigger. Should be good.")
    elif signA<0:
        if radiusA<radiusB or signB>0:
            print("Center on right, but radius got bigger. Switching.")
            (forward, backward) = (backward, forward)
        else:
            print("Center on right, and radius got smaller. Should be good.")

    current_motor_settings[dir]=forward
    current_motor_settings[(dir+3)%6]=backward
    set_motor(dir, *map(get_raw_motor, forward))
    set_motor((dir+3)%6, *map(get_raw_motor, backward))
    return 1 #successful completion
Ejemplo n.º 5
0
def init(filepath, type):
    # filepath = 'Image_065.jpg'
    '''if len(sys.argv) > 1:
        filePathg = sys.argv[1]
        filepath = filePathg
    '''
    # print(sys.argv)
    # print(filepath)
    if type == None:
        midProcessResult = detectType.detectType(filepath)  # tangpeng 预处理
        # 未分类
    else:
        if type == 'blue' or type == 'excess':
            midProcessResult = PipeInvoice.getPipe(filepath, type, False)
        else:
            ##type == 'red'
            midProcessResult = [None, None, None]
            midProcessResult[0] = filepath
            midProcessResult[1] = 2  # 专票
            midProcessResult[2] = textline(midProcessResult[0])
    # midProcessResult = [None, None]
    # midProcessResult[0] = midPR[0]
    # midProcessResult[1] = midPR[1]
    # midProcessResult[0] = filepath
    # 行提取
    blueTemplet = {
        'departCity': [48, 62, 222, 56],
        'arriveCity': [412, 61, 228, 55],
        'trainNumber': [264, 62, 170, 57],
        'invoiceDate': [24, 139, 369, 42],
        'seatNum': [408, 138, 160, 40],
        'idNum': [22, 276, 306, 38],
        'passenger': [328, 276, 150, 38],
        'totalAmount': [33, 177, 151, 39],
        'ticketsNum': [21, 10, 195, 66]
    }
    '''        'departCity': [29, 74, 218, 54],
            'arriveCity': [425, 68, 224, 64],
            'trainNumber': [230, 65, 203, 62],
            'invoiceDate': [0, 163, 357, 41],
            'seatNum': [392, 164, 203, 46],
            'idNum': [0, 343, 350, 45],
            'totalAmount': [3, 206, 212, 52],
            'ticketsNum': [34, 40, 202, 47]
        }'''
    redTemplet = {
        'idNum': [66, 242, 357, 38],
        'departCity': [66, 66, 222, 45],
        'arriveCity': [388, 66, 225, 47],
        'trainNumber': [288, 53, 103, 41],
        'invoiceDate': [66, 114, 237, 43],
        'seatNum': [400, 115, 210, 46],
        'totalAmount': [66, 163, 188, 34],
        'ticketsNum': [21, 23, 218, 47]
    }
    ''''''
    excessTemplet = {
        'departCity': [26, 40, 151, 33],
        'arriveCity': [271, 40, 169, 33],
        'trainNumber': [178, 35, 92, 32],
        'invoiceDate': [12, 82, 203, 29],
        'seatNum': [315, 79, 136, 33],
        'totalAmount': [12, 118, 167, 28],
        'ticketsNum': [23, 12, 147, 37]
    }

    # vatinvoice
    VATInvoiceTemplet = {}

    dic = xmlToDict.XmlTodict('VATInvoiceMuban.xml')

    tplt = [
        dic['QRCode'][0], dic['QRCode'][1],
        dic['figureX'][0] + dic['figureX'][2] / 2,
        dic['figureX'][1] + dic['figureX'][3] / 2
    ]
    # print(tplt)
    for c in tplt:
        if c == None:
            print('Templet VATInvoice error')

    # print(VATInvoiceTemplet)
    # midProcessResult[1] = 2#专票 for test
    # TemType = redTemplet #默认蓝票
    if midProcessResult[1] == 1:
        TemType = blueTemplet

    if midProcessResult[1] == 2:
        TemType = redTemplet

    if midProcessResult[1] == 3:
        TemType = excessTemplet

    if midProcessResult[1] == 11:  # 增值税专用             预留
        for item in dic:
            if item != 'QRCode' and item != 'figureX':
                # print(item)
                # tmp = MakeFileInV([[int(dic.get(item)[0]), int(dic.get(item)[1])], [int(dic.get(item)[2]), int(dic.get(item)[3])]], box, symbol, filePath, item, tplt)
                VATInvoiceTemplet[item] = [
                    int(dic.get(item)[0]),
                    int(dic.get(item)[1]),
                    int(dic.get(item)[2]),
                    int(dic.get(item)[3])
                ]
        TemType = VATInvoiceTemplet

    rate = 1
    # print(filepath)
    fcv = cv2.imread(filepath, 1)
    # print(fcv)
    w1 = fcv.shape
    if w1[0] + w1[1] > 1500:
        rate = 0.5
        print("rate : 0.5")

    if midProcessResult[1] == 1:
        box = Detect.detect(cv2.imread(midProcessResult[0]), rate)
        Templet = adjustToTextLine(TemType, box, midProcessResult[1],
                                   None)  # 蓝火车票
    if midProcessResult[1] == 2:
        rate = 2.0
        print("rate : 2.0")
        box = Detect.detect(cv2.imread(midProcessResult[0]), rate)
        # print( box.tolist())
        Templet = adjustToTextLine(TemType, box, midProcessResult[1],
                                   None)  # 红火车票
    if midProcessResult[1] == 3:
        rate = 1.0
        print("rate : 1.0")
        box = Detect.detect(cv2.imread(midProcessResult[0]), rate)
        # print( box.tolist())
        Templet = adjustToTextLine(TemType, box, midProcessResult[1],
                                   None)  # 红(补票)车票
    if midProcessResult[1] == 11:
        box = Detect.detect(cv2.imread(midProcessResult[0]), rate)
        figureP = FindCircle.findSymbol(filepath)
        StBox = sortBox(box)
        # print(box)
        # print(figureP)
        # print(StBox)
        Templet = adjustToTextLine(
            TemType, [StBox[0], StBox[1], figureP[0], figureP[1]],
            midProcessResult[1], tplt)  # 增值税专票

    im = cv2.imread(filepath, 0)
    rec = []
    for c in TemType:
        rec.append(TemType[c])
    vis_textline0 = fp.util.visualize.rects(im, rec)
    # vis_textline1 = fp.util.visualize.rects(im, rects, types)
    # 显示
    pl.figure(figsize=(15, 10))
    pl.subplot(2, 2, 1)
    pl.imshow(im, 'gray')

    pl.subplot(2, 2, 2)
    pl.imshow(vis_textline0)
    pl.show()

    # print(textline(midProcessResult[0]))
    attributeLine = lineToAttribute.getAtbt.compute(midProcessResult[2],
                                                    Templet)

    # print(attributeLine)
    # print(type(attributeLine))
    # print(attributeLine['departCity'])
    jsonResult = flow.cropToOcr(midProcessResult[0], attributeLine,
                                midProcessResult[1])  # ocr和分词
    print(jsonResult)
    return jsonResult
Ejemplo n.º 6
0
def pick_sign(blob_id, dir, port_h=False, history=60):
    """ Function straighten_direction(port_h): Runs the optimization phase for droplet walking.
    This function runs almost all of it's code in a continuous loop.
    Arguments:
    port_h - The serial port to use when communicating with God Droplet
    history - Optional argument to control the number of data points to take before running the optimization routine.
    """
    if not port_h:
        port_h = default_port
    raw_settings = map(get_raw_motor, current_motor_settings[dir])
    possible_signs = [
        copy.deepcopy(current_motor_settings[dir]) for i in range(4)
    ]
    possible_signs[0][motors_involved[dir][0]] *= -1
    possible_signs[1][motors_involved[dir][1]] *= -1
    possible_signs[2][motors_involved[dir][0]] *= -1
    possible_signs[2][motors_involved[dir][1]] *= -1

    distances = []
    radii = []
    for setting in possible_signs:
        set_motor(dir, *map(get_raw_motor, setting))
        try:
            pos_list = get_position_data_batch(dir, blob_id, history, port_h)
        except MyException:
            return -1
        (center, radius, residual, sign) = fc.lsq(pos_list)
        distance = np.linalg.norm(pos_list[0] - pos_list[-1])
        radii.append(radius)
        distances.append(distance)
        print("rad: %f, dist: %f" % (radius, distance))

    radiiA = (radii[0] + radii[1]) / 2.0
    radiiB = (radii[2] + radii[3]) / 2.0
    if radiiA > radiiB:
        forward = possible_signs[0]
        backward = possible_signs[1]
    else:
        forward = possible_signs[2]
        backward = possible_signs[3]
    '''At this point, we have the signs right, except we need to figure out if it's going forward or backward.'''
    current_motor_settings[dir] = copy.deepcopy(forward)
    set_motor(dir, *map(get_raw_motor, forward))
    try:
        pos_listA = get_position_data_batch(dir, blob_id, history, port_h)
    except MyException:
        return -1
    change_values = map(lambda x: x * 0.333, change_motor_signs_LEFT[dir])
    change_motor(dir, change_values, port_h)
    try:
        pos_listB = carefully_get_position_data_batch(dir, blob_id, history,
                                                      port_h)
    except MyException:
        return -1

    (centerA, radiusA, residualA, signA) = fc.lsq(pos_listA)
    (centerB, radiusB, residualB, signB) = fc.lsq(pos_listB)

    print("radA: %f, signA: %f, radB: %f, signB: %f" %
          (radiusA, signA, radiusB, signB))
    if signA > 0:
        if radiusA > radiusB and signB > 0:
            print("Center on left, but radius got smaller. Switching.")
            (forward, backward) = (backward, forward)
        else:
            print("Center on left, and radius got bigger. Should be good.")
    elif signA < 0:
        if radiusA < radiusB or signB > 0:
            print("Center on right, but radius got bigger. Switching.")
            (forward, backward) = (backward, forward)
        else:
            print("Center on right, and radius got smaller. Should be good.")

    current_motor_settings[dir] = forward
    current_motor_settings[(dir + 3) % 6] = backward
    set_motor(dir, *map(get_raw_motor, forward))
    set_motor((dir + 3) % 6, *map(get_raw_motor, backward))
    return 1  #successful completion