JobPlus知识库 IT 工业智能4.0 文章
【Python】Python简单验证码识别

1.通过二值化处理去掉干扰线

2.对黑白图片进行降噪,去掉那些单独的黑色像素点

3.消除边框上附着的黑色像素点

4.识别图像中的文字,去掉空格与’.’

[python] 

  1. from PIL import Image  
  2. from aip import AipOcr  
  3.   
  4. file='1-1-7'  
  5.   
  6. # 二值化处理,转化为黑白图片  
  7. def two_value():  
  8.     for i in range(1, 5):  
  9.         # 打开文件夹中的图片    
  10.         image = Image.open(file+'.jpg')  
  11.         # 灰度图    
  12.         lim = image.convert('L')  
  13.         # 灰度阈值设为165,低于这个值的点全部填白色    
  14.         threshold = 165  
  15.         table = []  
  16.   
  17.         for j in range(256):  
  18.             if j < threshold:  
  19.                 table.append(0)  
  20.             else:  
  21.                 table.append(1)  
  22.   
  23.         bim = lim.point(table, '1')  
  24.         bim.save(file+'.1.jpg')  
  25.   
  26. two_value()  
  27.   
  28. # 去除干扰线  
  29. im = Image.open(file+'.1.jpg')  
  30. # 图像二值化  
  31. data = im.getdata()  
  32. w, h = im.size  
  33. black_point = 0  
  34.   
  35. for x in range(1, w - 1):  
  36.     for y in range(1, h - 1):  
  37.         mid_pixel = data[w * y + x]  # 中央像素点像素值  
  38.         if mid_pixel < 50:  # 找出上下左右四个方向像素点像素值  
  39.             top_pixel = data[w * (y - 1) + x]  
  40.             left_pixel = data[w * y + (x - 1)]  
  41.             down_pixel = data[w * (y + 1) + x]  
  42.             right_pixel = data[w * y + (x + 1)]  
  43.   
  44.             # 判断上下左右的黑色像素点总个数  
  45.             if top_pixel < 5:   #小于5比小于10更精确  
  46.                 black_point += 1  
  47.             if left_pixel < 5:  
  48.                 black_point += 1  
  49.             if down_pixel < 5:  
  50.                 black_point += 1  
  51.             if right_pixel < 5:  
  52.                 black_point += 1  
  53.             if black_point < 1:  
  54.                 im.putpixel((x, y), 255)  
  55.                 # print(black_point)  
  56.             black_point = 0  
  57.   
  58. im.save(file+'.2.jpg')  
  59.   
  60. # 去除干扰线  
  61. im = Image.open(file+'.2.jpg')  
  62. # 图像二值化  
  63. data = im.getdata()  
  64. w, h = im.size  
  65. black_point = 0  
  66.   
  67. for x in range(1, w - 1):  
  68.     for y in range(1, h - 1):  
  69.         if x < 2 or y < 2:  
  70.             im.putpixel((x - 1, y - 1), 255)  
  71.         if x > w - 3 or y > h - 3:  
  72.             im.putpixel((x + 1, y + 1), 255)  
  73.   
  74. im.save(file+'.3.jpg')  
  75.   
  76. # 定义常量  
  77. APP_ID = '11352343'  
  78. API_KEY = 'Nd5Z1NkGoLDvHwBnD2bFLpCE'  
  79. SECRET_KEY = 'A9FsnnPj1Ys2Gof70SNgYo23hKOIK8Os'  
  80.   
  81. # 初始化AipFace对象  
  82. aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)  
  83.   
  84. # 读取图片  
  85. filePath=file+'.3.jpg'  
  86. def get_file_content(filePath):  
  87.     with open(filePath, 'rb') as fp:  
  88.         return fp.read()  
  89.   
  90. # 定义参数变量  
  91. options = {  
  92.     'detect_direction': 'true',  
  93.     'language_type': 'CHN_ENG',  
  94. }  
  95.   
  96. # 调用通用文字识别接口  
  97. result = aipOcr.basicGeneral(get_file_content(filePath), options)  
  98. print(result)  
  99. words_result=result['words_result']  
  100. for i in range(len(words_result)):  
  101.     print(words_result[i]['words'].replace(' ','').replace('.',''))  #去掉可能被识别的空格与.  


如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

¥ 打赏支持
298人赞 举报
分享到
用户评价(0)

暂无评价,你也可以发布评价哦:)

扫码APP

扫描使用APP

扫码使用

扫描使用小程序