Centos安装tesserocr实现验证码识别

一、 安装依赖

注意Cython不是Cpython,这个库要在tesserocr库安装前装好

yum install -y gcc-c++ epel-release autoconf automake libtool libjpeg-devel libpng-devel libtiff-devel zlib-devel tesseract tesseract-devel
wget http://www.leptonica.org/source/leptonica-1.78.0.tar.gz
tar -xzvf leptonica-1.78.0.tar.gz
cd leptonica-1.78.0
./configure
make && make install
rm -r -f leptonica-1.78.0 leptonica-1.78.0.tar.gz
pip install Cython==0.29.24 opencv-python==4.2.0.32 Pillow==6.2.2 -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
pip install tesserocr==2.5.2 -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

 二、python代码示例

python2代码,python3自行修改吧。

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import re
import cv2
import tesserocr
import numpy as np
from PIL import Image
from io import BytesIO


# 噪点处理
def interference_point(img):
    h, w = img.shape[:2]
    # 遍历像素点进行处理
    for y in range(0, w):
        for x in range(0, h):
            # 去掉边框上的点
            if y == 0 or y == w - 1 or x == 0 or x == h - 1:
                img[x, y] = 255
                continue
            count = 0
            if img[x, y - 1] == 255:
                count += 1
            if img[x, y + 1] == 255:
                count += 1
            if img[x - 1, y] == 255:
                count += 1
            if img[x + 1, y] == 255:
                count += 1
            if count > 2:
                img[x, y] = 255
    return img


def identify_captcha(image_bytes):
    try:
        with BytesIO(image_bytes) as image_file:
            with Image.open(image_file) as image:
                # image.save('captcha.png')
                with BytesIO() as new_image_file:
                    with image.convert('L').point(lambda x: 0 if x < 100 else 255) as new_image:  # 灰度化,二值化
                        new_image.save(new_image_file, 'PNG')
                        image_bytes = new_image_file.getvalue()
        image_array = np.frombuffer(image_bytes, np.uint8)
        image_array = cv2.cvtColor(cv2.imdecode(image_array, cv2.IMREAD_COLOR), cv2.COLOR_BGR2GRAY)  # 灰度
        image_array = cv2.adaptiveThreshold(image_array, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 21, 1)  # 降噪
        image_array = interference_point(image_array)
        success, image_array = cv2.imencode(".png", image_array)
        with BytesIO(image_array.tobytes()) as image_file:
            with Image.open(image_file) as image:
                return re.sub(r'\s', '', tesserocr.image_to_text(image))
    except:
        return ''

image_bytes = '\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x02\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c\x14\r\x0c\x0b\x0b\x0c\x19\x12\x13\x0f\x14\x1d\x1a\x1f\x1e\x1d\x1a\x1c\x1c $.\' ",#\x1c\x1c(7),01444\x1f\'9=82<.342\xff\xdb\x00C\x01\t\t\t\x0c\x0b\x0c\x18\r\r\x182!\x1c!22222222222222222222222222222222222222222222222222\xff\xc0\x00\x11\x08\x00(\x00d\x03\x01"\x00\x02\x11\x01\x03\x11\x01\xff\xc4\x00\x1f\x00\x00\x01\x05\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\xff\xc4\x00\xb5\x10\x00\x02\x01\x03\x03\x02\x04\x03\x05\x05\x04\x04\x00\x00\x01}\x01\x02\x03\x00\x04\x11\x05\x12!1A\x06\x13Qa\x07"q\x142\x81\x91\xa1\x08#B\xb1\xc1\x15R\xd1\xf0$3br\x82\t\n\x16\x17\x18\x19\x1a%&\'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\x83\x84\x85\x86\x87\x88\x89\x8a\x92\x93\x94\x95\x96\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xff\xc4\x00\x1f\x01\x00\x03\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\xff\xc4\x00\xb5\x11\x00\x02\x01\x02\x04\x04\x03\x04\x07\x05\x04\x04\x00\x01\x02w\x00\x01\x02\x03\x11\x04\x05!1\x06\x12AQ\x07aq\x13"2\x81\x08\x14B\x91\xa1\xb1\xc1\t#3R\xf0\x15br\xd1\n\x16$4\xe1%\xf1\x17\x18\x19\x1a&\'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x92\x93\x94\x95\x96\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xff\xda\x00\x0c\x03\x01\x00\x02\x11\x03\x11\x00?\x00\xef|\x1f\xe0/\t_x#\xc3\xf7w^\x1b\xd2\xe5\xb8\x9bL\xb6y$ke\xcb\xb1\x89I\'\x8eI<\x93\xde\xb5[\xe1\xf7\x83\xe1\x97\xe7\xd1t\x95yH\x005\x85\xb7\'\xd8\x14\xfeU\x1f\x84.&\x97\xc3\xda\x13=\xed\xc2Gsa\x04\x80\xca\xfek\x9d\xd1)P\xce\xdcn\'w8\x19 \xf1\xc8\x03f\xf6{8\xac\xae&\xbe\xd5~\xcb\r\xbbbI.\x8ch\xb1\x92p\t$\x0e\x0eq\xd7\x9c\xe3\xbd\x00f\\x\x1f\xc1\xc8\x90%\xc7\x85\xb4Bd\x94&\xe8\xec#NH8\xe83\xed\xd6\xa4\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\x99\xe2/\x0b\xc9\x08\xb2P\xce\xf6kv\xcc\xdb\xdc\x92]a\x90\x03\x828\xdd\xef\x81\x9a(\xa2\x80?\xff\xd9'
with BytesIO(image_bytes) as image_file:
    with Image.open(image_file) as image:
        image.save('captcha.png')
print(identify_captcha(image_bytes))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值