Esempio n. 1
0
def recogChars(seg_block, mid):
    name = ""
    confidence = 0.00
    refined = refineCrop(seg_block, mid - 1)
    for i, one in enumerate(refined):

        res_pre = cRP.SimplePredict(one, i)
        confidence += res_pre[0]

        name += res_pre[1]

    return seg_block, name, confidence
def slidingWindowsEval(image):
    windows_size = 16
    stride = 1
    height = image.shape[0]
    t0 = time.time()
    data_sets = []

    for i in range(0, image.shape[1] - windows_size + 1, stride):
        data = image[0:height, i:i + windows_size]
        data = cv2.resize(data, (23, 23))
        # cv2.imshow("image",data)
        data = cv2.equalizeHist(data)
        data = data.astype(np.float) / 255
        data = np.expand_dims(data, 3)
        data_sets.append(data)

    res = model.predict(np.array(data_sets))
    # print "分割",time.time() - t0

    pin = res
    p = 1 - (res.T)[1]
    p = f.gaussian_filter1d(np.array(p, dtype=np.float), 3)
    lmin = l.argrelmax(np.array(p), order=3)[0]
    interval = []
    for i in xrange(len(lmin) - 1):
        interval.append(lmin[i + 1] - lmin[i])

    if (len(interval) > 3):
        mid = get_median(interval)
    else:
        return []
    pin = np.array(pin)
    res = searchOptimalCuttingPoint(image, pin, 0, mid, 3)

    cutting_pts = res[1]
    last = cutting_pts[-1] + mid
    if last < image.shape[1]:
        cutting_pts.append(last)
    else:
        cutting_pts.append(image.shape[1] - 1)
    name = ""
    confidence = 0.00
    seg_block = []
    for x in xrange(1, len(cutting_pts)):
        if x != len(cutting_pts) - 1 and x != 1:
            section = image[0:36, cutting_pts[x - 1] - 2:cutting_pts[x] + 2]
        elif x == 1:
            c_head = cutting_pts[x - 1] - 2
            if c_head < 0:
                c_head = 0
            c_tail = cutting_pts[x] + 2
            section = image[0:36, c_head:c_tail]
        elif x == len(cutting_pts) - 1:
            end = cutting_pts[x]
            diff = image.shape[1] - end
            c_head = cutting_pts[x - 1]
            c_tail = cutting_pts[x]
            if diff < 7:
                section = image[0:36, c_head - 5:c_tail + 5]
            else:
                diff -= 1
                section = image[0:36, c_head - diff:c_tail + diff]
        elif x == 2:
            section = image[0:36,
                            cutting_pts[x - 1] - 3:cutting_pts[x - 1] + mid]
        else:
            section = image[0:36, cutting_pts[x - 1]:cutting_pts[x]]
        seg_block.append(section)
    refined = refineCrop(seg_block, mid - 1)

    t0 = time.time()
    for i, one in enumerate(refined):
        res_pre = cRP.SimplePredict(one, i)
        # cv2.imshow(str(i),one)
        # cv2.waitKey(0)
        confidence += res_pre[0]
        name += res_pre[1]
    # print "字符识别",time.time() - t0

    return refined, name, confidence
Esempio n. 3
0
def slidingWindowsEval(image):
    windows_size = 16;
    stride = 1
    height= image.shape[0]
    # print image.shape[1]
    p = []
    ch_p = []

    gain = []
    pin=[]
    for i in range(0,image.shape[1]-windows_size+1,stride):
        data = image[0:height,i:i+windows_size]
        data = cv2.resize(data,(23,23))
        # cv2.imshow("image",data)
        data = cv2.equalizeHist(data)
        data = data.astype(np.float)/255
        data=  np.expand_dims(data,3)
        res = model.predict(np.array([data]))
        pin.append(res[0])

        p.append(res[0][0]+res[0][2])
        ch_p.append(res[0][2])

        gain.append(res.argmax())

    p =  np.insert(p,0,0);
    p = np.insert(p,len(p),0);
    p = f.gaussian_filter1d(np.array(p,dtype=np.float),3)
    # print p
    sum = image.sum(axis=0)


    lmin = l.argrelmax(np.array(p),order = 3)[0]
    interval = []
    for i in xrange(len(lmin)-1):
        interval.append(lmin[i+1]-lmin[i])

    if(len(interval)>3):
        mid  = get_median(interval)
    else:
        return []

    ch_p = np.array(ch_p)
    pin = np.array(pin)
    res =  searchOptimalCuttingPoint(image,pin,0,mid,3)

    cutting_pts = res[1]
    last =  cutting_pts[-1] + mid
    if last < image.shape[1]:
        cutting_pts.append(last)
    else:
        cutting_pts.append(image.shape[1]-1)


    name = ""
    confidence =0.00;
    seg_block = []
    for x in xrange(1,len(cutting_pts)):
        if x != len(cutting_pts)-1 and x!=1:
            section = image[0:36,cutting_pts[x-1]-2:cutting_pts[x]+2]
        elif  x==1:

            c_head = cutting_pts[x - 1]- 2
            if c_head<0:
                c_head=0
            c_tail = cutting_pts[x] + 2

            section = image[0:36, c_head:c_tail]
        elif x==len(cutting_pts)-1:
            end = cutting_pts[x]
            diff = image.shape[1]-end

            c_head = cutting_pts[x - 1]
            c_tail = cutting_pts[x]

            if diff<7 :
                section = image[0:36, c_head-5:c_tail+5]

            else:
                diff-=1

                section = image[0:36, c_head - diff:c_tail + diff]



        elif  x==2:
            section = image[0:36, cutting_pts[x - 1] - 3:cutting_pts[x-1]+ mid]
        else:
            section = image[0:36,cutting_pts[x-1]:cutting_pts[x]]
        seg_block.append(section)
    refined = refineCrop(seg_block,mid-1)
    for i,one in enumerate(refined):


        res_pre = cRP.SimplePredict(one, i )
        confidence+=res_pre[0]

        name+= res_pre[1]

    return seg_block,name,confidence