zoukankan      html  css  js  c++  java
  • 【识别码】使用(ImageMagick+tesseractocr)实现图像验证码识别实例

    最近在搞一个无人值守系统时,需要能自动登录,在登录时需要输入验证码,所以研究了验证码识别技术,否则我这个无人值守系统的作用就没有了。目前只测试了字母和数字的识别,准确率还是可以的,呵呵,已经够我自已用了~~,至于中文的识别可以参考我上篇文章:利用开源程序(ImageMagick+tesseract-ocr)实现图像验证码识别

    验证码识别率如下图:(准确率还可以吧)

    好吧,切入正题,赶快上架源码吧~~,不足之处请多多包涵

    注意:在使用验证码识别类之前,一、请先安装好ImageMagick,二、需要下载tesseract-2.04.exe.tar.gz和 tesseract-2.00.eng.tar.gz,并解压内容到tesseract目录中,之后拷备tesseract目录至应用程序下(debug 目录下)

    一、图片识别,验证识别类 

    1. /// <summary>  
    2. /// 图片识别,验证识别  
    3. /// </summary>  
    4. public class Identify  
    5. {  
    6.     /// <summary>  
    7.     /// 功能描述:静态成员 识别数字、字母,验证码等图片上的内容  
    8.      /// </summary>  
    9.     /// <param name="img_path">待识别内容的图片</param>  
    10.     /// <param name="save_dir">识别内容保存目录</param>  
    11.     /// <param name="scale">放大处理比率(建议值范围:100~1000)-(参数:提高识别成功率)</param>  
    12.     /// <returns></returns>  
    13.     public static string StartIdentifyingCaptcha(string img_path, string save_dir,int scale)  
    14.     {  
    15.         return StartIdentifyingCaptcha(img_path, save_dir, 4, 0, scale);  
    16.     }  
    17.     /// <summary>  
    18.     /// 功能描述:静态成员 识别数字、字母,验证码等图片上的内容  
    19.      /// </summary>  
    20.     /// <param name="img_path">待识别内容的图片</param>  
    21.     /// <param name="save_dir">识别内容保存目录</param>  
    22.     /// <param name="content_type">内容分类(0:纯数字 1:纯字母 2:数字与字母混合 3:纯汉字,4、中英文混合)-(参数:提高识别成功率)</param>  
    23.     /// <param name="content_length">内容长度(参数:提高识别成功率)</param>  
    24.     /// <param name="scale">放大处理比率(建议值范围:100~1000)-(参数:提高识别成功率)</param>  
    25.     /// <returns></returns>  
    26.     public static string StartIdentifyingCaptcha(string img_path, string save_dir, byte content_type, int content_length, int scale)  
    27.     {  
    28.         string captcha = "";            // 识别的验证码  
    29.   
    30.         //int captcha_length = 6;         // 验证码长度(参数:提高识别成功率)  
    31.   
    32.         //int scale = 730;                // 放大处理比率(参数:提高识别成功率)  
    33.   
    34.         do  
    35.         {  
    36.             // 识别验证码(一、安装ImageMagick,二、拷备tesseract目录至应用程序下)  
    37.             Common.StartProcess("cmd.exe", new string[] { "cd tesseract" ,  
    38.   
    39.                                                                 // 输换图片  
    40.                                                                 String.Format(@"convert.exe -compress none -depth 8 -alpha off -scale {0}% -colorspace gray {1} {2}\captcha.tif",scale,img_path,save_dir),  
    41.                               
    42.                                                                 // 识别图片  
    43.                                                                 String.Format(@"tesseract.exe {0}\captcha.tif {0}\result",save_dir),  
    44.   
    45.                                                                 "exit"});  
    46.   
    47.             // 读取识别的验证码  
    48.             StreamReader reader = new StreamReader(String.Format(@"{0}\result.txt", save_dir));  
    49.   
    50.             captcha = reader.ReadLine().Trim();  
    51.   
    52.             reader.Close();  
    53.               
    54.             // 匹配规则  
    55.             string pattern = "";  
    56.   
    57.             // 纯数字  
    58.             if (content_type == 0)  
    59.             {  
    60.                 pattern = (content_length > 0) ? "^[0-9]{" + content_length + "}$" : "^[0-9]+$";  
    61.             }  
    62.             // 纯字母  
    63.             else if (content_type == 1)  
    64.             {  
    65.                 pattern = (content_length > 0) ? "^[a-zA-Z]{" + content_length + "}$" : "^[a-zA-Z]+$";  
    66.             }  
    67.             // 数字与字母混合  
    68.             else if (content_type == 2)  
    69.             {  
    70.                 pattern = (content_length > 0) ? "^[a-zA-Z0-9]{" + content_length + "}$" : "^[a-zA-Z0-9]+$";  
    71.             }  
    72.             // 纯汉字  
    73.             else if (content_type == 3)  
    74.             {  
    75.                 pattern = (content_length > 0) ? "^[\u4e00-\u9fa5]{" + content_length + "}$" : "^[\u4e00-\u9fa5]+$";  
    76.             }  
    77.             // 中英文混合  
    78.             else  
    79.             {  
    80.                 pattern = (content_length > 0) ? "^[A-Za-z0-9\u4e00-\u9fa5]{" + content_length + "}$" : "^[A-Za-z0-9\u4e00-\u9fa5]+$";  
    81.             }  
    82.   
    83.   
    84.             // 识别失败,调整放大比率重新识别  
    85.             if (pattern != "" && !Regex.IsMatch(captcha, pattern))  
    86.             {  
    87.                 captcha = "";  
    88.   
    89.                 scale++;  
    90.             }  
    91.   
    92.             // 放大比率为9倍时,仍无法识别,认为识别失败,退出识别  
    93.             if (scale > 900)  
    94.             {  
    95.                 captcha = "";  
    96.   
    97.                 break;  
    98.             }  
    99.   
    100.         } while (captcha == "");  
    101.   
    102.         return captcha;  
    103.     }  
    104. }  


    二、C#中执行命令行命令的函数

      1. public class Common  
      2.     {  
      3.  
      4.         #region 功能描述:静态成员 C#里运行命令行里的命令  
      5.         /// <summary>  
      6.         /// 功能描述:静态成员 C#里运行命令行里的命令  
      7.         /// </summary>  
      8.         /// <param name="startFileName">要启动的应用程序</param>  
      9.         /// <param name="commands">命令行数组</param>  
      10.         /// <returns></returns>  
      11.         public static string StartProcess(string startFileName, string[] commands)  
      12.         {  
      13.             //实例一个Process类,启动一个独立进程  
      14.             Process p = new Process();  
      15.   
      16.             //Process类有一个StartInfo属性,这个是ProcessStartInfo类,包括了一些属性和方法,  
      17.   
      18.             //下面我们用到了他的几个属性:  
      19.   
      20.             //设定程序名  
      21.             p.StartInfo.FileName = startFileName;  
      22.   
      23.             //关闭Shell的使用  
      24.             p.StartInfo.UseShellExecute = false;  
      25.   
      26.             //重定向标准输入  
      27.             p.StartInfo.RedirectStandardInput = true;  
      28.   
      29.             //重定向标准输出  
      30.             p.StartInfo.RedirectStandardOutput = true;  
      31.   
      32.             //重定向错误输出  
      33.             p.StartInfo.RedirectStandardError = true;  
      34.   
      35.             //设置不显示窗口  
      36.             p.StartInfo.CreateNoWindow = true;  
      37.   
      38.             //上面几个属性的设置是比较关键的一步。  
      39.   
      40.             //既然都设置好了那就启动进程吧  
      41.             p.Start();  
      42.   
      43.             //输入要执行的命令  
      44.             foreach (string command in commands)  
      45.             {  
      46.                 p.StandardInput.WriteLine(command);  
      47.             }  
      48.   
      49.             //从输出流获取命令执行结果  
      50.             return p.StandardOutput.ReadToEnd();  
      51.         }  
      52.         #endregion  
      53.     } 
  • 相关阅读:
    Mvc5+Ef 6.0入门开发随笔(2.MVC的简单创建与使用图文详解)
    转载:CSDN mvc ef 的简单增删改查操作
    插件
    Jmeter-后置处理器--json提取器
    jmeter的几种参数化方式
    Python--变量命名规范
    Python--对list、tuple、dict的操作
    Python--列表中字符串按照某种规则排序的方法
    Python--遍历文件夹下所有文件和目录的方法(os.walk(rootdir)函数返回一个三元素元祖)
    Python--递归函数实现:多维嵌套字典数据无限遍历
  • 原文地址:https://www.cnblogs.com/teacher/p/3916547.html
Copyright © 2011-2022 走看看