idcard.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:dust_repos 作者: taozhijiang 项目源码 文件源码
def img_tesseract_detect(c_rect, im):
    # ????minAreaRect??????-90~0??????????????????
    # ???????????????????????????????????????
    pts = c_rect.reshape(4, 2)
    rect = np.zeros((4, 2), dtype = "float32")

    # the top-left point has the smallest sum whereas the
    # bottom-right has the largest sum
    s = pts.sum(axis = 1)
    rect[0] = pts[np.argmin(s)]
    rect[3] = pts[np.argmax(s)]

    # compute the difference between the points -- the top-right
    # will have the minumum difference and the bottom-left will
    # have the maximum difference
    diff = np.diff(pts, axis = 1)
    rect[2] = pts[np.argmin(diff)]
    rect[1] = pts[np.argmax(diff)]    

    width = rect[3][0] - rect[0][0]
    height = rect[3][1] - rect[0][1]

    width = (int)((50.0 / height) * width)
    height = 50

    dst = np.float32([[0,0],[0,height],[width,0],[width,height]])

    M = cv2.getPerspectiveTransform(rect, dst)
    warp = cv2.warpPerspective(im, M, (width, height))

    img_show_hook("??????", warp) 

    warp = np.array(warp, dtype=np.uint8)
    radius = 13
    selem = disk(radius)

    #????????OTSU????
    local_otsu = rank.otsu(warp, selem)
    l_otsu = np.uint8(warp >= local_otsu)
    l_otsu *= 255

    kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(2,2))
    l_otsu = cv2.morphologyEx(l_otsu, cv2.MORPH_CLOSE, kernel)    

    img_show_hook("?????OTSU??", l_otsu) 

    print("?????")
    print(pytesseract.image_to_string(Image.fromarray(l_otsu), lang="chi-sim"))

    cv2.waitKey(0)
    return
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号