{"id":4251,"date":"2024-03-20T15:38:30","date_gmt":"2024-03-20T07:38:30","guid":{"rendered":"http:\/\/cnliutz.ipyingshe.net\/?p=4251"},"modified":"2024-03-22T23:48:52","modified_gmt":"2024-03-22T15:48:52","slug":"python%e5%ad%97%e7%ac%a6%e8%af%86%e5%88%abocr-tessreact%e5%ba%93","status":"publish","type":"post","link":"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=4251","title":{"rendered":"python\u5b57\u7b26\u8bc6\u522bOCR Tessreact\u5e93"},"content":{"rendered":"\n<p>Tesseract \u662f\u4e00\u6b3e\u5f3a\u5927\u7684\u5f00\u6e90 OCR\uff08\u5149\u5b66\u5b57\u7b26\u8bc6\u522b\uff09\u5f15\u64ce\uff0c\u53ef\u4ee5\u7528\u4e8e\u4ece\u56fe\u50cf\u4e2d\u63d0\u53d6\u6587\u672c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 Python \u548c Tesseract \u8fdb\u884c\u6587\u5b57\u8bc6\u522b\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86 Tesseract\u3002\u4f60\u53ef\u4ee5\u6309\u7167\u5b98\u65b9\u7f51\u7ad9\u4e0a\u7684\u6307\u5357\u8fdb\u884c\u5b89\u88c5\uff0c\u5e76\u8bbe\u7f6e\u597d\u73af\u5883\u53d8\u91cf\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 Python \u4e2d\u7684 <code>pytesseract<\/code> \u5e93\u6765\u8c03\u7528 Tesseract\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5b83\u4f1a\u52a0\u8f7d\u4e00\u5f20\u56fe\u7247\uff0c\u8fdb\u884c\u7b80\u5355\u7684\u56fe\u50cf\u5904\u7406\uff0c\u7136\u540e\u4f7f\u7528 Tesseract \u8fdb\u884c\u6587\u5b57\u8bc6\u522b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># -*- coding: utf-8 -*-\r\n\"\"\"\r\nCreated on Fri Mar 22 15:06:57 2024\r\nOCR\u5b57\u7b26\u8bc6\u522b Tesseract\r\n@author: cnliutz\r\n\"\"\"\n# 1.\u5b89\u88c5 Tesseract OCR \u5f15\u64ce\r\n#tesseract \u5b89\u88c5\u4e0b\u8f7d\u5730\u5740 https:\/\/github.com\/UB-Mannheim\/tesseract\/wiki\r\n#tesseract \u8bed\u8a00\u5305\u4e0b\u8f7d\u5730\u5740 https:\/\/github.com\/tesseract-ocr\/tessdata  Global Language\r\n#\u4e2d\u6587\u5305 https:\/\/github.com\/tesseract-ocr\/tessdata\/blob\/main\/chi_sim.traineddata \r\n#\u8bf7\u628a\u6587\u4ef6chi_sim.traineddata\u653e\u5230 C:\\Program Files\\Tesseract-OCR\\tessdata\\chi_sim.traineddata\r\n# 2.\u6253\u5f00DOS\u547d\u4ee4\u7a97\u53e3\uff0c\u5b89\u88c5\u9700\u8981\u7684\u5e93\r\n#pip install pytesseract\r\n#pip install pillow\r\n#pip install opencv-python\r\n#pip install numpy\r\n\n\n# \u5bfc\u5165\u6240\u9700\u7684\u5e93\nfrom PIL import Image   #pip install pillow\nimport pytesseract\nimport argparse\nimport cv2              #pip install  opencv-python\nimport os\n\n# Set the path to the Tesseract OCR engine\npytesseract.pytesseract.tesseract_cmd = r'C:\\Program Files\\Tesseract-OCR\/tesseract.exe'\n\n# \u6784\u5efa\u53c2\u6570\u89e3\u6790\u5668\u5e76\u89e3\u6790\u53c2\u6570\nap = argparse.ArgumentParser()\nap.add_argument(\"-i\", \"--image\", required=True, help=\"\u8f93\u5165\u8981\u8fdb\u884cOCR\u7684\u56fe\u50cf\u8def\u5f84\")\nap.add_argument(\"-p\", \"--preprocess\", type=str, default=\"thresh\", help=\"\u9884\u5904\u7406\u7c7b\u578bPREPROCESS: 'thresh' \u6216\u8005 'blur'\")\nargs = vars(ap.parse_args())\n\n# \u52a0\u8f7d\u793a\u4f8b\u56fe\u50cf\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\nimage = cv2.imread(args&#91;\"image\"])\ngray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n\n# \u6839\u636e\u9884\u5904\u7406\u7c7b\u578b\u5bf9\u56fe\u50cf\u8fdb\u884c\u5904\u7406\nif args&#91;\"preprocess\"] == \"thresh\":\n    gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)&#91;1]\nelif args&#91;\"preprocess\"] == \"blur\":\n    gray = cv2.medianBlur(gray, 3)\n\n# \u5c06\u7070\u5ea6\u56fe\u50cf\u4fdd\u5b58\u4e3a\u4e34\u65f6\u6587\u4ef6\uff0c\u4ee5\u4fbf\u8fdb\u884c OCR\nfilename = \"{}.png\".format(os.getpid())\ncv2.imwrite(filename, gray)\n\n# \u4f7f\u7528 Tesseract \u8fdb\u884c\u6587\u5b57\u8bc6\u522b\ntext = pytesseract.image_to_string(Image.open(filename))\nos.remove(filename)  # \u5220\u9664\u4e34\u65f6\u6587\u4ef6\n\n# \u6253\u5370\u8bc6\u522b\u7ed3\u679c\nprint(text)\n<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528 Pillow \u5e93\u6253\u5f00\u56fe\u50cf\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002\u7136\u540e\uff0c\u6839\u636e\u9884\u5904\u7406\u7c7b\u578b\uff08\u9608\u503c\u5206\u5272\u6216\u6a21\u7cca\u5904\u7406\uff09\uff0c\u5bf9\u56fe\u50cf\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>pytesseract.image_to_string()<\/code> \u51fd\u6570\u5bf9\u5904\u7406\u540e\u7684\u56fe\u50cf\u8fdb\u884c\u6587\u5b57\u8bc6\u522b\u3002<br>\u4f60\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u4ee3\u7801\u4fdd\u5b58\u4e3a <code>ocr.py<\/code> \u6587\u4ef6\uff0c\u7136\u540e\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python ocr.py --image images\/example.png -p blur\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u5c06\u5bf9\u793a\u4f8b\u56fe\u50cf\u8fdb\u884c\u6587\u5b57\u8bc6\u522b\u3002\u8bf7\u786e\u4fdd\u66ff\u6362 <code>images\/example.png<\/code> \u4e3a\u4f60\u8981\u8bc6\u522b\u7684\u5b9e\u9645\u56fe\u50cf\u8def\u5f84\u3002<\/p>\n\n\n\n<p>tesseract \u7535\u8111\u73af\u5883\u5b89\u88c5\uff0c\u5904\u7406\u4e2d\u6587 <a href=\"https:\/\/blog.csdn.net\/jclian91\/article\/details\/80628188\" data-type=\"link\" data-id=\"https:\/\/blog.csdn.net\/jclian91\/article\/details\/80628188\" target=\"_blank\" rel=\"noreferrer noopener\">\u53c2\u8003\u7f51\u5740<\/a><\/p>\n\n\n\n<p>\u53c2\u8003\u7f51\u5740\uff1a <a href=\"https:\/\/blog.csdn.net\/weixin_41013322\/category_8760189.html\" data-type=\"link\" data-id=\"https:\/\/blog.csdn.net\/weixin_41013322\/category_8760189.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/blog.csdn.net\/weixin_41013322\/category_8760189.html<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u7b80\u5316\u7248OCR\u4ee3\u7801!^^!\nimport pytesseract\nfrom PIL import Image\n\n# Set the path to the Tesseract OCR engine\npytesseract.pytesseract.tesseract_cmd = r'C:\\Program Files\\Tesseract-OCR\/tesseract.exe'\n\n\n# Open an image file\nimage_path = r'C:\\Users\\czliu\\Documents\\python\/wz.png'\nimage = Image.open(image_path)\n\n# Perform OCR on the image\ntext = pytesseract.image_to_string(image,lang='chi_sim')\n\n# Print the extracted text\nprint(text)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Tesseract \u662f\u4e00\u6b3e\u5f3a\u5927\u7684\u5f00\u6e90 OCR\uff08\u5149\u5b66\u5b57\u7b26\u8bc6\u522b\uff09\u5f15\u64ce\uff0c\u53ef\u4ee5\u7528\u4e8e\u4ece\u56fe\u50cf <span class=\"readmore\"><a href=\"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=4251\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,10],"tags":[],"class_list":["post-4251","post","type-post","status-publish","format-standard","hentry","category-2","category-python"],"_links":{"self":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/4251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4251"}],"version-history":[{"count":7,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/4251\/revisions"}],"predecessor-version":[{"id":4274,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/4251\/revisions\/4274"}],"wp:attachment":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4251"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}