idcardocr.py 文件源码

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

项目:idmatch 作者: maddevsio 项目源码 文件源码
def recognize_card(original_image, country='kg', preview=False):
    from processing.border_removal import resize
    from processing.crop import process_image
    result = []
    cropped_image = "croped-image.jpg"
    process_image(original_image, cropped_image)
    idcard = cv2.imread(cropped_image, cv2.COLOR_BGR2GRAY)
    idcard = resize(idcard, width=720)

    scale_down = (8 * 170 / detect_dpi(idcard))
    if scale_down <= 4:
        rows, cols = idcard.shape[:2]
        idcard = cv2.resize(idcard, (scale_down * cols / 8, scale_down * rows / 8))

    contours, hierarchy = recognize_text(idcard)
    for index, contour in enumerate(contours):
        [x, y, w, h] = cv2.boundingRect(contour)
        gray = cv2.cvtColor(idcard, cv2.COLOR_RGB2GRAY)
        roi = gray[y:y + h, x:x + w]
        if cv2.countNonZero(roi) / h * w > 0.55:
            if h > 16 and w > 16:
                filename = '%s.jpg' % index
                cv2.imwrite(filename, roi)
                text = pytesseract.image_to_string(
                    Image.open(filename), lang="kir+eng", config="-psm 7"
                )                
                item = {'x': x, 'y': y, 'w': w, 'h': h, 'text': text}
                result.append(item)
                cv2.rectangle(idcard, (x, y), (x + w, y + h), (255, 0, 255), 2)
    if preview:
        original_image = original_image.split('/')[-1]
        location = save_image('regions' + original_image, idcard)
        return location, regionskir(result)
    return regionskir(result)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号