{"id":1089843,"date":"2025-01-08T13:57:07","date_gmt":"2025-01-08T05:57:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1089843.html"},"modified":"2025-01-08T13:57:10","modified_gmt":"2025-01-08T05:57:10","slug":"python%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e9%aa%8c%e8%af%81%e7%a0%81%e9%97%ae%e9%a2%98-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1089843.html","title":{"rendered":"python\u5982\u4f55\u5904\u7406\u9a8c\u8bc1\u7801\u95ee\u9898"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202549\/92106ac7-87c5-4ac5-ace0-e872a9311778.webp\" alt=\"python\u5982\u4f55\u5904\u7406\u9a8c\u8bc1\u7801\u95ee\u9898\" \/><\/p>\n<p><p> <strong>Python\u5904\u7406\u9a8c\u8bc1\u7801\u95ee\u9898\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528OCR\u6280\u672f\u8bc6\u522b\u9a8c\u8bc1\u7801\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u9a8c\u8bc1\u7801\u8bc6\u522b\u670d\u52a1\u3001\u901a\u8fc7\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u5de5\u5177\u7ed5\u8fc7\u9a8c\u8bc1\u7801\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528OCR\u6280\u672f\u8bc6\u522b\u9a8c\u8bc1\u7801<\/strong> \u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7Python\u5e93\u5982Pillow\u548cTesseract\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1a<strong>\u4f7f\u7528OCR\u6280\u672f\u8bc6\u522b\u9a8c\u8bc1\u7801<\/strong> \u53ef\u4ee5\u5b9e\u73b0\u81ea\u52a8\u5316\u5904\u7406\u9a8c\u8bc1\u7801\u3002\u9996\u5148\uff0c\u4f7f\u7528Pillow\u5e93\u52a0\u8f7d\u548c\u9884\u5904\u7406\u9a8c\u8bc1\u7801\u56fe\u50cf\uff08\u5982\u7070\u5ea6\u5316\u3001\u4e8c\u503c\u5316\u7b49\uff09\uff0c\u7136\u540e\u4f7f\u7528Tesseract OCR\u5f15\u64ce\u8bc6\u522b\u56fe\u50cf\u4e2d\u7684\u6587\u5b57\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u9a8c\u8bc1\u7801\uff0c\u4f46\u5bf9\u4e8e\u590d\u6742\u7684\u9a8c\u8bc1\u7801\u53ef\u80fd\u9700\u8981\u66f4\u591a\u7684\u9884\u5904\u7406\u6b65\u9aa4\u548c\u8bad\u7ec3\u6570\u636e\uff0c\u4ee5\u63d0\u9ad8\u8bc6\u522b\u51c6\u786e\u7387\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528OCR\u6280\u672f\u8bc6\u522b\u9a8c\u8bc1\u7801<\/h3>\n<\/p>\n<p><p>OCR\uff08\u5149\u5b66\u5b57\u7b26\u8bc6\u522b\uff09\u6280\u672f\u662f\u5904\u7406\u9a8c\u8bc1\u7801\u7684\u4e00\u79cd\u5e38\u89c1\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5c06\u56fe\u50cf\u4e2d\u7684\u6587\u5b57\u8f6c\u6362\u4e3a\u53ef\u7f16\u8f91\u7684\u6587\u672c\u3002Python\u4e2d\u5e38\u7528\u7684OCR\u5e93\u662fPillow\u548cTesseract\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u6240\u9700\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Pillow\u548cTesseract OCR\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow pytesseract<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u5b89\u88c5Tesseract OCR\u5f15\u64ce\uff0c\u53ef\u4ee5\u4ece\u4ee5\u4e0b\u7f51\u5740\u4e0b\u8f7d\u5e76\u5b89\u88c5\uff1a<\/p>\n<p><a href=\"https:\/\/github.com\/tesseract-ocr\/tesseract\">Tesseract OCR<\/a><\/p>\n<\/p>\n<p><h4>2. \u56fe\u50cf\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528OCR\u8bc6\u522b\u9a8c\u8bc1\u7801\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u9a8c\u8bc1\u7801\u56fe\u50cf\u8fdb\u884c\u9884\u5904\u7406\u3002\u5e38\u89c1\u7684\u9884\u5904\u7406\u6b65\u9aa4\u5305\u62ec\u7070\u5ea6\u5316\u3001\u4e8c\u503c\u5316\u3001\u53bb\u566a\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import pytesseract<\/p>\n<h2><strong>\u52a0\u8f7d\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;captcha.png&#39;)<\/p>\n<h2><strong>\u7070\u5ea6\u5316<\/strong><\/h2>\n<p>gray_image = image.convert(&#39;L&#39;)<\/p>\n<h2><strong>\u4e8c\u503c\u5316<\/strong><\/h2>\n<p>binary_image = gray_image.point(lambda x: 0 if x &lt; 128 else 255, &#39;1&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u9884\u5904\u7406\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>binary_image.save(&#39;processed_captcha.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528Tesseract\u8bc6\u522b\u9a8c\u8bc1\u7801<\/h4>\n<\/p>\n<p><p>\u9884\u5904\u7406\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Tesseract OCR\u5f15\u64ce\u6765\u8bc6\u522b\u56fe\u50cf\u4e2d\u7684\u6587\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528Tesseract\u8bc6\u522b\u56fe\u50cf\u4e2d\u7684\u6587\u5b57<\/p>\n<p>captcha_text = pytesseract.image_to_string(binary_image)<\/p>\n<p>print(&quot;\u8bc6\u522b\u7684\u9a8c\u8bc1\u7801\u4e3a\uff1a&quot;, captcha_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5bf9\u4e8e\u7b80\u5355\u7684\u9a8c\u8bc1\u7801\u6709\u8f83\u9ad8\u7684\u8bc6\u522b\u7387\uff0c\u4f46\u5bf9\u4e8e\u590d\u6742\u7684\u9a8c\u8bc1\u7801\uff08\u5982\u626d\u66f2\u3001\u5e72\u6270\u7ebf\u7b49\uff09\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u4e00\u6b65\u7684\u9884\u5904\u7406\u548c\u8bad\u7ec3\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u9a8c\u8bc1\u7801\u8bc6\u522b\u670d\u52a1<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528OCR\u6280\u672f\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u9a8c\u8bc1\u7801\u8bc6\u522b\u670d\u52a1\uff0c\u4f8b\u5982\u8d85\u7ea7\u9e70\u3001\u6253\u7801\u5154\u7b49\u3002\u8fd9\u4e9b\u670d\u52a1\u901a\u5e38\u5177\u6709\u8f83\u9ad8\u7684\u8bc6\u522b\u51c6\u786e\u7387\uff0c\u4f46\u9700\u8981\u4ed8\u8d39\u3002<\/p>\n<\/p>\n<p><h4>1. \u6ce8\u518c\u5e76\u83b7\u53d6API\u5bc6\u94a5<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5728\u7b2c\u4e09\u65b9\u9a8c\u8bc1\u7801\u8bc6\u522b\u670d\u52a1\u5e73\u53f0\u6ce8\u518c\u8d26\u53f7\uff0c\u5e76\u83b7\u53d6API\u5bc6\u94a5\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528API\u8bc6\u522b\u9a8c\u8bc1\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528\u8d85\u7ea7\u9e70\u8bc6\u522b\u9a8c\u8bc1\u7801\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u8d85\u7ea7\u9e70API\u63a5\u53e3<\/strong><\/h2>\n<p>url = &quot;http:\/\/upload.chaojiying.net\/Upload\/Processing.php&quot;<\/p>\n<h2><strong>\u8d85\u7ea7\u9e70\u8d26\u53f7\u4fe1\u606f<\/strong><\/h2>\n<p>username = &quot;your_username&quot;<\/p>\n<p>password = &quot;your_password&quot;<\/p>\n<p>soft_id = &quot;your_soft_id&quot;<\/p>\n<h2><strong>\u52a0\u8f7d\u9a8c\u8bc1\u7801\u56fe\u50cf<\/strong><\/h2>\n<p>with open(&#39;captcha.png&#39;, &#39;rb&#39;) as f:<\/p>\n<p>    image_data = f.read()<\/p>\n<h2><strong>\u6784\u5efa\u8bf7\u6c42\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;user&#39;: username,<\/p>\n<p>    &#39;pass2&#39;: password,<\/p>\n<p>    &#39;softid&#39;: soft_id,<\/p>\n<p>    &#39;codetype&#39;: &#39;1902&#39;,<\/p>\n<p>}<\/p>\n<p>files = {&#39;userfile&#39;: (&#39;captcha.png&#39;, image_data)}<\/p>\n<h2><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.post(url, data=data, files=files)<\/p>\n<p>result = response.json()<\/p>\n<p>if result[&#39;err_no&#39;] == 0:<\/p>\n<p>    print(&quot;\u8bc6\u522b\u7684\u9a8c\u8bc1\u7801\u4e3a\uff1a&quot;, result[&#39;pic_str&#39;])<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8bc6\u522b\u5931\u8d25\uff0c\u9519\u8bef\u7801\uff1a&quot;, result[&#39;err_no&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u7b2c\u4e09\u65b9\u9a8c\u8bc1\u7801\u8bc6\u522b\u670d\u52a1\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u8bc6\u522b\u51c6\u786e\u7387\uff0c\u4f46\u9700\u8981\u4e00\u5b9a\u7684\u8d39\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u5de5\u5177\u7ed5\u8fc7\u9a8c\u8bc1\u7801<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5904\u7406\u9a8c\u8bc1\u7801\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u5de5\u5177\uff0c\u4f8b\u5982Selenium\u3002\u901a\u8fc7\u6a21\u62df\u7528\u6237\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7ed5\u8fc7\u4e00\u4e9b\u9a8c\u8bc1\u7801\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Selenium<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Selenium\u5e93\u548c\u6d4f\u89c8\u5668\u9a71\u52a8\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Selenium\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u540c\u65f6\uff0c\u9700\u8981\u4e0b\u8f7d\u5e76\u5b89\u88c5\u5bf9\u5e94\u6d4f\u89c8\u5668\u7684\u9a71\u52a8\u7a0b\u5e8f\uff0c\u4f8b\u5982ChromeDriver\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528Selenium\u6a21\u62df\u7528\u6237\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Selenium\u6a21\u62df\u7528\u6237\u64cd\u4f5c\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u76ee\u6807\u7f51\u7ad9<\/strong><\/h2>\n<p>driver.get(&#39;https:\/\/example.com&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u9a8c\u8bc1\u7801\u8f93\u5165\u6846\u548c\u63d0\u4ea4\u6309\u94ae<\/strong><\/h2>\n<p>captcha_input = driver.find_element_by_name(&#39;captcha&#39;)<\/p>\n<p>submit_button = driver.find_element_by_name(&#39;submit&#39;)<\/p>\n<h2><strong>\u8f93\u5165\u9a8c\u8bc1\u7801\uff08\u5047\u8bbe\u5df2\u7ecf\u901a\u8fc7\u5176\u4ed6\u65b9\u6cd5\u83b7\u53d6\u9a8c\u8bc1\u7801\uff09<\/strong><\/h2>\n<p>captcha_text = &#39;1234&#39;<\/p>\n<p>captcha_input.send_keys(captcha_text)<\/p>\n<h2><strong>\u63d0\u4ea4\u8868\u5355<\/strong><\/h2>\n<p>submit_button.click()<\/p>\n<h2><strong>\u83b7\u53d6\u63d0\u4ea4\u540e\u7684\u9875\u9762\u5185\u5bb9<\/strong><\/h2>\n<p>page_content = driver.page_source<\/p>\n<p>print(page_content)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7Selenium\u53ef\u4ee5\u81ea\u52a8\u5316\u5904\u7406\u9a8c\u8bc1\u7801\uff0c\u4f46\u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u7684\u9a8c\u8bc1\u7801\uff08\u5982\u6ed1\u52a8\u9a8c\u8bc1\u3001\u70b9\u51fb\u9a8c\u8bc1\uff09\uff0c\u53ef\u80fd\u9700\u8981\u66f4\u591a\u7684\u64cd\u4f5c\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u63d0\u9ad8\u8bc6\u522b\u51c6\u786e\u7387<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u590d\u6742\u7684\u9a8c\u8bc1\u7801\uff0c\u5355\u4e00\u7684\u65b9\u6cd5\u53ef\u80fd\u65e0\u6cd5\u8fbe\u5230\u7406\u60f3\u7684\u6548\u679c\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u63d0\u9ad8\u8bc6\u522b\u51c6\u786e\u7387\u3002\u4f8b\u5982\uff0c\u5148\u4f7f\u7528OCR\u6280\u672f\u8fdb\u884c\u521d\u6b65\u8bc6\u522b\uff0c\u5982\u679c\u8bc6\u522b\u5931\u8d25\uff0c\u518d\u4f7f\u7528\u7b2c\u4e09\u65b9\u9a8c\u8bc1\u7801\u8bc6\u522b\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1. \u521d\u6b65\u8bc6\u522b\u9a8c\u8bc1\u7801<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528OCR\u6280\u672f\u8fdb\u884c\u521d\u6b65\u8bc6\u522b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">captcha_text = pytesseract.image_to_string(binary_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u68c0\u67e5\u8bc6\u522b\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u68c0\u67e5OCR\u8bc6\u522b\u7684\u7ed3\u679c\uff0c\u5982\u679c\u8bc6\u522b\u7ed3\u679c\u4e3a\u7a7a\u6216\u4e0d\u7b26\u5408\u9884\u671f\uff0c\u5219\u4f7f\u7528\u7b2c\u4e09\u65b9\u9a8c\u8bc1\u7801\u8bc6\u522b\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if not captcha_text or len(captcha_text) != 4:<\/p>\n<p>    response = requests.post(url, data=data, files=files)<\/p>\n<p>    result = response.json()<\/p>\n<p>    if result[&#39;err_no&#39;] == 0:<\/p>\n<p>        captcha_text = result[&#39;pic_str&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u4fdd\u8bc1\u8bc6\u522b\u51c6\u786e\u7387\u7684\u540c\u65f6\uff0c\u51cf\u5c11\u4f7f\u7528\u7b2c\u4e09\u65b9\u9a8c\u8bc1\u7801\u8bc6\u522b\u670d\u52a1\u7684\u6b21\u6570\uff0c\u4ece\u800c\u964d\u4f4e\u6210\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u590d\u6742\u9a8c\u8bc1\u7801\u7684\u9ad8\u7ea7\u6280\u5de7<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u7684\u9a8c\u8bc1\u7801\uff0c\u4f8b\u5982\u5305\u542b\u626d\u66f2\u3001\u5e72\u6270\u7ebf\u3001\u80cc\u666f\u566a\u58f0\u7b49\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u9ad8\u7ea7\u7684\u56fe\u50cf\u5904\u7406\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6280\u672f\u6765\u63d0\u9ad8\u8bc6\u522b\u51c6\u786e\u7387\u3002<\/p>\n<\/p>\n<p><h4>1. \u56fe\u50cf\u5206\u5272<\/h4>\n<\/p>\n<p><p>\u56fe\u50cf\u5206\u5272\u662f\u5c06\u9a8c\u8bc1\u7801\u56fe\u50cf\u5206\u5272\u6210\u5355\u4e2a\u5b57\u7b26\u7684\u8fc7\u7a0b\u3002\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u56fe\u50cf\u5904\u7406\u6280\u672f\uff0c\u4f8b\u5982\u5f62\u6001\u5b66\u64cd\u4f5c\u3001\u8f6e\u5ed3\u68c0\u6d4b\u7b49\uff0c\u6765\u5b9e\u73b0\u56fe\u50cf\u5206\u5272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u52a0\u8f7d\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;captcha.png&#39;, cv2.IMREAD_GRAYSCALE)<\/p>\n<h2><strong>\u4e8c\u503c\u5316<\/strong><\/h2>\n<p>_, binary_image = cv2.threshold(image, 128, 255, cv2.THRESH_BINARY_INV)<\/p>\n<h2><strong>\u5f62\u6001\u5b66\u64cd\u4f5c<\/strong><\/h2>\n<p>kernel = np.ones((3, 3), np.uint8)<\/p>\n<p>morph_image = cv2.morphologyEx(binary_image, cv2.MORPH_CLOSE, kernel)<\/p>\n<h2><strong>\u8f6e\u5ed3\u68c0\u6d4b<\/strong><\/h2>\n<p>contours, _ = cv2.findContours(morph_image, cv2.RETR_EXTERNAL, cv2.CH<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>N_APPROX_SIMPLE)<\/p>\n<h2><strong>\u5206\u5272\u56fe\u50cf<\/strong><\/h2>\n<p>for i, contour in enumerate(contours):<\/p>\n<p>    x, y, w, h = cv2.boundingRect(contour)<\/p>\n<p>    char_image = binary_image[y:y+h, x:x+w]<\/p>\n<p>    cv2.imwrite(f&#39;char_{i}.png&#39;, char_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bad\u7ec3\u81ea\u5b9a\u4e49OCR\u6a21\u578b<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u9a8c\u8bc1\u7801\uff0c\u53ef\u4ee5\u8bad\u7ec3\u81ea\u5b9a\u4e49OCR\u6a21\u578b\u3002\u9996\u5148\uff0c\u9700\u8981\u6536\u96c6\u5e76\u6807\u6ce8\u5927\u91cf\u9a8c\u8bc1\u7801\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528\u673a\u5668\u5b66\u4e60\u6216\u6df1\u5ea6\u5b66\u4e60\u6280\u672f\u8bad\u7ec3\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528TensorFlow\u6216PyTorch\u7b49\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u6765\u8bad\u7ec3\u81ea\u5b9a\u4e49OCR\u6a21\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<h2><strong>\u5b9a\u4e49\u6a21\u578b<\/strong><\/h2>\n<p>model = tf.keras.Sequential([<\/p>\n<p>    tf.keras.layers.Conv2D(32, (3, 3), activation=&#39;relu&#39;, input_shape=(28, 28, 1)),<\/p>\n<p>    tf.keras.layers.MaxPooling2D((2, 2)),<\/p>\n<p>    tf.keras.layers.Conv2D(64, (3, 3), activation=&#39;relu&#39;),<\/p>\n<p>    tf.keras.layers.MaxPooling2D((2, 2)),<\/p>\n<p>    tf.keras.layers.Flatten(),<\/p>\n<p>    tf.keras.layers.Dense(128, activation=&#39;relu&#39;),<\/p>\n<p>    tf.keras.layers.Dense(10, activation=&#39;softmax&#39;)<\/p>\n<p>])<\/p>\n<h2><strong>\u7f16\u8bd1\u6a21\u578b<\/strong><\/h2>\n<p>model.compile(optimizer=&#39;adam&#39;, loss=&#39;sparse_categorical_crossentropy&#39;, metrics=[&#39;accuracy&#39;])<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))<\/p>\n<h2><strong>\u4fdd\u5b58\u6a21\u578b<\/strong><\/h2>\n<p>model.save(&#39;captcha_model.h5&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u53ef\u4ee5\u7528\u4e8e\u8bc6\u522b\u590d\u6742\u9a8c\u8bc1\u7801\uff0c\u5e76\u4e14\u5177\u6709\u8f83\u9ad8\u7684\u51c6\u786e\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u9a8c\u8bc1\u7801\u5904\u7406\u4e2d\u7684\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u9a8c\u8bc1\u7801\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\uff0c\u4f8b\u5982\u8bc6\u522b\u51c6\u786e\u7387\u4f4e\u3001\u5904\u7406\u65f6\u95f4\u957f\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<\/p>\n<p><h4>1. \u8bc6\u522b\u51c6\u786e\u7387\u4f4e<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u8bc6\u522b\u51c6\u786e\u7387\u4f4e\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e0b\u65b9\u6cd5\u6765\u63d0\u9ad8\u51c6\u786e\u7387\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6539\u8fdb\u56fe\u50cf\u9884\u5904\u7406<\/strong>\uff1a\u91c7\u7528\u66f4\u9ad8\u7ea7\u7684\u56fe\u50cf\u9884\u5904\u7406\u6280\u672f\uff0c\u4f8b\u5982\u53bb\u566a\u3001\u56fe\u50cf\u589e\u5f3a\u7b49\u3002<\/li>\n<li><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49OCR\u6a21\u578b<\/strong>\uff1a\u8bad\u7ec3\u81ea\u5b9a\u4e49OCR\u6a21\u578b\uff0c\u9488\u5bf9\u7279\u5b9a\u7c7b\u578b\u7684\u9a8c\u8bc1\u7801\u8fdb\u884c\u4f18\u5316\u3002<\/li>\n<li><strong>\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5<\/strong>\uff1a\u7ed3\u5408OCR\u6280\u672f\u548c\u7b2c\u4e09\u65b9\u9a8c\u8bc1\u7801\u8bc6\u522b\u670d\u52a1\uff0c\u63d0\u9ad8\u8bc6\u522b\u51c6\u786e\u7387\u3002<\/li>\n<\/ul>\n<p><h4>2. \u5904\u7406\u65f6\u95f4\u957f<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5904\u7406\u65f6\u95f4\u957f\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e0b\u65b9\u6cd5\u6765\u4f18\u5316\u5904\u7406\u6548\u7387\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f18\u5316\u56fe\u50cf\u9884\u5904\u7406<\/strong>\uff1a\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u56fe\u50cf\u9884\u5904\u7406\u6b65\u9aa4\uff0c\u91c7\u7528\u66f4\u9ad8\u6548\u7684\u56fe\u50cf\u5904\u7406\u7b97\u6cd5\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5e76\u884c\u5904\u7406<\/strong>\uff1a\u5bf9\u4e8e\u5927\u91cf\u9a8c\u8bc1\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u5e76\u884c\u5904\u7406\uff0c\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002<\/li>\n<li><strong>\u7f13\u5b58\u8bc6\u522b\u7ed3\u679c<\/strong>\uff1a\u5bf9\u4e8e\u91cd\u590d\u51fa\u73b0\u7684\u9a8c\u8bc1\u7801\uff0c\u53ef\u4ee5\u7f13\u5b58\u8bc6\u522b\u7ed3\u679c\uff0c\u907f\u514d\u91cd\u590d\u8bc6\u522b\u3002<\/li>\n<\/ul>\n<p><h3>\u4e03\u3001\u9a8c\u8bc1\u7801\u5904\u7406\u7684\u5b89\u5168\u6027\u4e0e\u5408\u89c4\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u9a8c\u8bc1\u7801\u65f6\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u5b89\u5168\u6027\u4e0e\u5408\u89c4\u6027\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u7b2c\u4e09\u65b9\u9a8c\u8bc1\u7801\u8bc6\u522b\u670d\u52a1\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u8d26\u53f7\u4fe1\u606f\u548c\u9a8c\u8bc1\u7801\u6570\u636e\u7684\u5b89\u5168\u3002\u540c\u65f6\uff0c\u9700\u8981\u9075\u5b88\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\uff0c\u907f\u514d\u975e\u6cd5\u7ed5\u8fc7\u9a8c\u8bc1\u7801\u3002<\/p>\n<\/p>\n<p><h4>1. \u786e\u4fdd\u6570\u636e\u5b89\u5168<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u7b2c\u4e09\u65b9\u9a8c\u8bc1\u7801\u8bc6\u522b\u670d\u52a1\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u8d26\u53f7\u4fe1\u606f\u548c\u9a8c\u8bc1\u7801\u6570\u636e\u7684\u5b89\u5168\u3002\u53ef\u4ee5\u91c7\u7528\u52a0\u5bc6\u4f20\u8f93\u3001\u8bbf\u95ee\u63a7\u5236\u7b49\u63aa\u65bd\uff0c\u4fdd\u62a4\u6570\u636e\u5b89\u5168\u3002<\/p>\n<\/p>\n<p><h4>2. \u9075\u5b88\u6cd5\u5f8b\u6cd5\u89c4<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u9a8c\u8bc1\u7801\u65f6\uff0c\u9700\u8981\u9075\u5b88\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\u3002\u4f8b\u5982\uff0c\u907f\u514d\u975e\u6cd5\u7ed5\u8fc7\u9a8c\u8bc1\u7801\uff0c\u5c0a\u91cd\u7f51\u7ad9\u7684\u8bbf\u95ee\u89c4\u5219\u548c\u7528\u6237\u534f\u8bae\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5904\u7406\u9a8c\u8bc1\u7801\u95ee\u9898\uff0c\u63d0\u9ad8\u81ea\u52a8\u5316\u5904\u7406\u6548\u7387\u3002\u540c\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u5b89\u5168\u6027\u4e0e\u5408\u89c4\u6027\u95ee\u9898\uff0c\u786e\u4fdd\u6570\u636e\u5b89\u5168\u548c\u5408\u6cd5\u5408\u89c4\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u5904\u7406\u9a8c\u8bc1\u7801\u95ee\u9898\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5305\u62ec\u4f7f\u7528OCR\u6280\u672f\u8bc6\u522b\u9a8c\u8bc1\u7801\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u9a8c\u8bc1\u7801\u8bc6\u522b\u670d\u52a1\u3001\u901a\u8fc7\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u5de5\u5177\u7ed5\u8fc7\u9a8c\u8bc1\u7801\u7b49\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u9a8c\u8bc1\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528OCR\u6280\u672f\u8fdb\u884c\u5904\u7406\uff1b\u5bf9\u4e8e\u590d\u6742\u7684\u9a8c\u8bc1\u7801\uff0c\u53ef\u4ee5\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\uff0c\u63d0\u9ad8\u8bc6\u522b\u51c6\u786e\u7387\u3002\u5904\u7406\u9a8c\u8bc1\u7801\u65f6\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u56fe\u50cf\u9884\u5904\u7406\u3001\u6a21\u578b\u8bad\u7ec3\u3001\u5e76\u884c\u5904\u7406\u7b49\u6280\u672f\uff0c\u4f18\u5316\u5904\u7406\u6548\u7387\u3002\u540c\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u6570\u636e\u5b89\u5168\u548c\u5408\u6cd5\u5408\u89c4\uff0c\u907f\u514d\u975e\u6cd5\u7ed5\u8fc7\u9a8c\u8bc1\u7801\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u5e0c\u671b\u8bfb\u8005\u80fd\u591f\u638c\u63e1Python\u5904\u7406\u9a8c\u8bc1\u7801\u7684\u57fa\u672c\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u5e94\u7528\uff0c\u63d0\u9ad8\u81ea\u52a8\u5316\u5904\u7406\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u901a\u8fc7Python\u8bc6\u522b\u548c\u5904\u7406\u9a8c\u8bc1\u7801\uff1f<\/strong><br \/>\u5904\u7406\u9a8c\u8bc1\u7801\u901a\u5e38\u9700\u8981\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u548c\u673a\u5668\u5b66\u4e60\u6280\u672f\u3002\u5e38\u89c1\u7684\u5e93\u6709Pillow\uff08\u56fe\u50cf\u5904\u7406\uff09\u3001Tesseract\uff08OCR\u8bc6\u522b\uff09\u548cOpenCV\uff08\u8ba1\u7b97\u673a\u89c6\u89c9\uff09\u3002\u9996\u5148\u9700\u8981\u5bf9\u9a8c\u8bc1\u7801\u56fe\u50cf\u8fdb\u884c\u9884\u5904\u7406\uff0c\u4f8b\u5982\u53bb\u566a\u3001\u4e8c\u503c\u5316\u548c\u5b57\u7b26\u5206\u5272\uff0c\u7136\u540e\u4f7f\u7528OCR\u5e93\u8fdb\u884c\u5b57\u7b26\u8bc6\u522b\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6709\u4e9b\u9a8c\u8bc1\u7801\u91c7\u7528\u4e86\u590d\u6742\u7684\u5e72\u6270\u5143\u7d20\uff0c\u53ef\u80fd\u9700\u8981\u66f4\u9ad8\u7ea7\u7684\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u8fdb\u884c\u8bc6\u522b\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u89e3\u51b3\u9a8c\u8bc1\u7801\u95ee\u9898\uff1f<\/strong><br \/>Python\u4e2d\u6709\u591a\u79cd\u5e93\u53ef\u4f9b\u9009\u62e9\uff0c\u6bd4\u5982Pillow\u7528\u4e8e\u56fe\u50cf\u5904\u7406\uff0cOpenCV\u7528\u4e8e\u56fe\u50cf\u5206\u6790\u548c\u5904\u7406\uff0cTesseract\u53ef\u4ee5\u8fdb\u884c\u5149\u5b66\u5b57\u7b26\u8bc6\u522b\u3002\u6b64\u5916\uff0cKeras\u548cTensorFlow\u7b49\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u4e5f\u53ef\u7528\u4e8e\u8bad\u7ec3\u81ea\u5b9a\u4e49\u6a21\u578b\uff0c\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u9a8c\u8bc1\u7801\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53d6\u51b3\u4e8e\u9a8c\u8bc1\u7801\u7684\u590d\u6742\u6027\u548c\u5177\u4f53\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u9ad8\u9a8c\u8bc1\u7801\u8bc6\u522b\u7684\u51c6\u786e\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u9ad8\u8bc6\u522b\u7684\u51c6\u786e\u6027\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u591a\u79cd\u9884\u5904\u7406\u6280\u672f\uff0c\u5982\u56fe\u50cf\u7f29\u653e\u3001\u53bb\u566a\u548c\u4e8c\u503c\u5316\u3002\u540c\u65f6\uff0c\u4f7f\u7528\u66f4\u5f3a\u5927\u7684OCR\u6a21\u578b\u548c\u6df1\u5ea6\u5b66\u4e60\u7b97\u6cd5\u4e5f\u80fd\u663e\u8457\u63d0\u5347\u8bc6\u522b\u6548\u679c\u3002\u6b64\u5916\uff0c\u8bad\u7ec3\u81ea\u5b9a\u4e49\u6a21\u578b\u65f6\uff0c\u589e\u52a0\u6837\u672c\u6570\u636e\u96c6\u7684\u591a\u6837\u6027\u548c\u6570\u91cf\uff0c\u5bf9\u63d0\u5347\u6a21\u578b\u7684\u6cdb\u5316\u80fd\u529b\u4e5f\u81f3\u5173\u91cd\u8981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5904\u7406\u9a8c\u8bc1\u7801\u95ee\u9898\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528OCR\u6280\u672f\u8bc6\u522b\u9a8c\u8bc1\u7801\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u9a8c\u8bc1\u7801\u8bc6\u522b\u670d\u52a1\u3001\u901a\u8fc7\u6d4f\u89c8\u5668\u81ea\u52a8\u5316 [&hellip;]","protected":false},"author":3,"featured_media":1089851,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089843"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1089843"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089843\/revisions"}],"predecessor-version":[{"id":1089853,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089843\/revisions\/1089853"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1089851"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1089843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1089843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1089843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}