{"id":5846,"date":"2025-01-10T20:02:00","date_gmt":"2025-01-10T12:02:00","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=5846"},"modified":"2025-01-10T15:42:18","modified_gmt":"2025-01-10T07:42:18","slug":"%e5%9c%a8macos%e4%b8%8a%e7%94%a8%e5%91%bd%e4%bb%a4-%e8%84%9a%e6%9c%ac%e8%bf%9b%e8%a1%8cocr%e6%8f%90%e5%8f%96%e6%96%87%e5%ad%97%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/5846.html","title":{"rendered":"\u5728macOS\u4e0a\u7528\u547d\u4ee4\/\u811a\u672c\u8fdb\u884cOCR\u63d0\u53d6\u6587\u5b57\u5185\u5bb9"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u7b80\u5355\u6574\u7406\u4e00\u4e0b\uff0c\u65b9\u4fbf\u540e\u9762\u6709\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\u3002\u53e6\u5916\u63d0\u4e00\u4e0b\uff0cocrmac\u7b49\u6a21\u5757\u662f\u5bf9macOS\u7cfb\u7edf\u80fd\u529b\u505a\u7684\u5c01\u88c5\uff0c\u6240\u4ee5\u4ec5\u4ec5\u5728\u8f83\u9ad8\u7248\u672cmacOS(10.15+)\u7cfb\u7edf\u4e0a\u624d\u53ef\u751f\u6548\u3002<\/p>\n\n\n\n<p>\u7b80\u5355\u6765\u8bf4\u5c31\u662f\uff0cmacOS\u7cfb\u7edf\u4e0a\u7528\u5b83\u81ea\u5e26\u7684OCR\u80fd\u529b\uff0c\u901f\u5ea6\u66f4\u5feb\u3001\u6548\u679c\u66f4\u597d\uff0c\u66f4\u503c\u5f97\u63a8\u8350\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u73af\u5883\u51c6\u5907<\/li>\n<\/ol>\n\n\n\n<p>Tesseract<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>brew install tesseract\nbrew install tesseract-lang\n\ntesseract --help\ntesseract --help-extra\n\ntesseract test.png - --oem 1 #\u5c06\u8bc6\u522b\u7ed3\u679c\u76f4\u63a5\u8f93\u51fa\u5230\u7ec8\u7aef\n\ntesseract test.png ocr_result_tesseract --oem 1 -l chi_sim<\/code><\/pre>\n\n\n\n<p>ocrmac<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4f7f\u7528 Python3 \u81ea\u5e26\u7684 venv \u6a21\u5757\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u9694\u79bb\u73af\u5883\npython3 -m venv venv1\nsource venv1\/bin\/activate\n\n# \u5b89\u88c5\u4f9d\u8d56\npip install ocrmac\npip install ipython<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u7b80\u5355\u7684\u7f16\u7a0b\u5b9e\u73b0<\/li>\n<\/ol>\n\n\n\n<p>\u76f4\u63a5\u8c03\u7528\u547d\u4ee4\u6765\u8bc6\u522b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# \u622a\u5c4f\u5c06\u5b58\u50a8\u4e3a .png \u6587\u4ef6\uff0c\u800c\u5f55\u5c4f\u5c06\u5b58\u50a8\u4e3a .mov \u6587\u4ef6\uff0c\u9ed8\u8ba4\u5b58\u50a8\u4f4d\u7f6e\u662f`~\/Desktop`\u3002\u6587\u4ef6\u540d\u5c06\u4ee5\u201c\u622a\u5c4f\u201d\u6216\u201c\u5f55\u5c4f\u201d\u5f00\u5934\uff0c\u5e76\u5305\u62ec\u65e5\u671f\u548c\u65f6\u95f4\u3002\u6bd4\u5982\uff1a\n# ~\/Desktop\/\u622a\u5c4f2025-01-08 20.32.16.png\n# \u4e0a\u9762\u7684\u8fd9\u4e2a\u81ea\u52a8\u4fdd\u5b58\u7684\u6587\u4ef6\u540d\u4e2d\u5c31\u5305\u542b\u7a7a\u683c\u3002\n\n# ls -1 ~\/Desktop\/*.png | while IFS= read -r file; do\nls ~\/Desktop\/*.png | while IFS= read -r file; do\n    # echo \"$file\"\n    file \"$file\"\n    tesseract \"$file\" - --oem 1 -l chi_sim &gt;\"$file\".txt\ndone\n\n# ls \u7684 -1 \u9009\u9879\uff0c\u5f3a\u5236\u8f93\u51fa\u6bcf\u884c\u4e00\u4e2a\u6761\u76ee\u3002\u5f53\u8f93\u51fa\u4e0d\u6307\u5411\u7ec8\u7aef\u65f6\uff0c\u8fd9\u662f\u9ed8\u8ba4\u503c\u3002 Force output to be one entry per line.  This is the default when output is not to a terminal.\n# \u76f4\u63a5 ls \u547d\u4ee4\u4e0d\u6307\u5b9a\u4efb\u4f55\u9009\u9879\u4e5f\u662f\u53ef\u4ee5\u7684\u3002\n\n# \u56fe\u7247\u7684\u5904\u7406\u987a\u5e8f\u4e0d\u53ef\u63a7\nfind ~\/Desktop -iname \\*.png -mindepth 1 -type f -print0 | sort | while IFS= read -r -d $'\\0' fp; do\n    #statements\n    echo \"$fp\"\n    # \/opt\/homebrew\/Cellar\/tesseract\/5.5.0\/bin\/tesseract \"$fp\" - --oem 1 -l chi_sim &gt;&gt; \"$fp\".txt\ndone<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7Python\u7f16\u7a0b\u6765\u8bc6\u522b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from ocrmac import ocrmac\n\nannotations = ocrmac.OCR('test.png').recognize() #\u5bf9\u4e8e\u56fe\u7247\u4e2d\u7684\u4e2d\u6587\u5185\u5bb9\u8bc6\u522b\u4e0d\u597d\uff0c\u9700\u8981\u901a\u8fc7 language_preference \u6307\u5b9a\u8bed\u8a00\n\nannotations = ocrmac.OCR('test.png', language_preference=&#91;'zh-Hans']).recognize() #ok\n'''\n&#91;('\u4f5c\u65b0\u5e74\u91cc\u7684\u7b2c\u4e00\u7bc7\uff0c\u6211\u4eec\u8bb2\u4ec0\u4e48\u8bdd\u9898\u5462\uff1f', 0.5, &#91;0.02004453812264858, 0.9707568809405439, 0.4632516871018234, 0.021975676947777467]), ('\u8bb2\u7834\u5c40\u3002', 1.0, &#91;0.02004454434757439, 0.9243119263511643, 0.09131403086684081, 0.020642201834862317]), ('\u6211\u7ecf\u5e38\u542c\u5230\u4e00\u7c7b\u58f0\u97f3\uff1a', 0.5, &#91;0.020044542850228938, 0.8793604652597939, 0.23608017638194745, 0.017441860032737733]), ('\u65f6\u95f4\u4e0d\u7ecf\u6d6a\uff0c\u4eba\u751f\u4e0d\u7ecf\u6643\uff0c\u53ea\u6709\u5e74\u7eaa\u564c\u564c\u5f80\u4e0a\u6da8\uff1b', 0.5, &#91;0.02004454478891597, 0.8255813958466146, 0.550111365024989, 0.02476264030561537]), ('\u5bb6\u91cc\u6ca1\u6709\u77ff\uff0c\u5168\u9760\u81ea\u5df1\u625b\uff0c\u8f9b\u82e6\u5230\u5934\u6765\u4e00\u573a\u7a7a\u5fd9\uff1b', 0.5, &#91;0.02004454570576815, 0.7790697679033006, 0.550111365024989, 0.02482931438936009]), ..., ('\u8fd9\u4ef6\u4e8b\uff0c\u8ddf\u7740\u6211\u4eec\u9605\u8bfb\u8d85\u8fc71\u5e74\u7684\u8bfb\u8005\uff0c\u90fd\u8bb0\u5f97\uff0c\u4e0d\u8bb0\u5f97\u4f60\u4e5f\u53ef\u4ee5\u70b9\u8fdb\u53bb\u590d\u4e60\u590d\u4e60\u3002', 1.0, &#91;0.020044559825702565, 0.002866972200416651, 0.8930957438866158, 0.021842329873951294])]\n'''\n\nannotations = ocrmac.OCR('test.png', language_preference=&#91;'zh-Hans'], framework=\"livetext\").recognize() #ok \u4e0d\u8fc7\u8fd9\u91cc\u7684\u8bc6\u522b\u7ed3\u679c\u5355\u4f4d\u53d8\u6210\u4e86\u4e00\u4e2a\u5b57\u7b26\uff0c\u4f46\u6bcf\u4e2a\u8bc6\u522b\u7ed3\u679c\u7684\u7f6e\u4fe1\u5ea6\u90fd\u5f88\u9ad8\n'''\n&#91;('\u4f5c', 1.0, &#91;0.02004454736451367, 0.970756880733945, 0.027846489542340463, 0.020642201834862386]), ('\u4e3a', 1.0, &#91;0.04789103690685413, 0.970756880733945, 0.023725834797891032, 0.020642201834862386]), ..., ('\u3002', 1.0, &#91;0.9009226713532513, 0.0028669724770642446, 0.012217638223586125, 0.021842329848517084])]\n'''\nprint(annotations)\n\n# annotations \u662f\u4e00\u4e2a list \uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a\u5143\u7d20\u7684\u7ed3\u6784\u662f (Text, Confidence, BoundingBox) \u5373\uff08\u63d0\u53d6\u6587\u672c\uff0c\u7f6e\u4fe1\u5ea6\uff0c\u8fb9\u754c\u6846\uff09\n\nfor x in annotations:\n    print(x&#91;0])<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>from ocrmac import ocrmac\n\nannotations = ocrmac.OCR('test.png', language_preference=&#91;'zh-Hans']).recognize() #\u8bc6\u522b\u7ed3\u679c\u5355\u4f4d\u662f\u4e00\u884c\u6587\u5b57\n\nannotations = ocrmac.OCR('test.png', language_preference=&#91;'zh-Hans'], framework=\"vision\", recognition_level=\"fast\").recognize() #\u6839\u636e\u4f5c\u8005\u7684\u6d4b\u8bd5\u7edf\u8ba1\u6765\u770b\uff0c\u8fd9\u4e2a\u4e0d\u5982\u4e0b\u9762\u8fd9\u4e2a livetext \u65b9\u5f0f\u5747\u8861\n\n# \u63a8\u8350\u65b9\u5f0f\nannotations = ocrmac.OCR('test.png', language_preference=&#91;'zh-Hans'], framework=\"livetext\").recognize() #\u8bc6\u522b\u7ed3\u679c\u5355\u4f4d\u662f\u4e00\u4e2a\u5b57\u7b26\uff0c\u4f46\u6bcf\u4e2a\u8bc6\u522b\u7ed3\u679c\u7684\u7f6e\u4fe1\u5ea6\u90fd\u5f88\u9ad8\n\n'''\n# language_preference\nen-US\nzh-Hans\nde-DE\n...\n\n# framework\nvision\nlivetext\n\n# recognition_level\nfast\naccurate\n'''\n\n\n\n# annotations \u662f\u4e00\u4e2a list \uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a\u5143\u7d20\u7684\u7ed3\u6784\u662f (Text, Confidence, BoundingBox) \u5373\uff08\u63d0\u53d6\u6587\u672c\uff0c\u7f6e\u4fe1\u5ea6\uff0c\u8fb9\u754c\u6846\uff09\nprint(annotations)\n\n# \u53ea\u6253\u5370\u8bc6\u522b\u7ed3\u679c\nfor x in annotations:\n    print(x&#91;0])<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>ocrmac<br><a href=\"https:\/\/github.com\/straussmaximilian\/ocrmac\">https:\/\/github.com\/straussmaximilian\/ocrmac<\/a><\/p>\n\n\n\n<p>A Python wrapper for Google Tesseract<br><a href=\"https:\/\/github.com\/madmaze\/pytesseract\">https:\/\/github.com\/madmaze\/pytesseract<\/a><\/p>\n\n\n\n<p>EasyOCR<br><a href=\"https:\/\/github.com\/JaidedAI\/EasyOCR\">https:\/\/github.com\/JaidedAI\/EasyOCR<\/a><\/p>\n\n\n\n<p>A powerful OCR tool for macOS &#8211; from terminal to API serve<br><a href=\"https:\/\/github.com\/dielect\/mac-ocr-cli\">https:\/\/github.com\/dielect\/mac-ocr-cli<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u7b80\u5355\u6574\u7406\u4e00\u4e0b\uff0c\u65b9\u4fbf\u540e\u9762\u6709\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\u3002\u53e6\u5916\u63d0\u4e00\u4e0b\uff0cocrmac\u7b49\u6a21\u5757\u662f\u5bf9macOS\u7cfb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,560,12],"tags":[1780,1900,2420,8,1899,19,71],"class_list":["post-5846","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-mac","category-tools","tag-macos","tag-ocr","tag-ocrmac","tag-python","tag-tesseract","tag-tips","tag-tools"],"views":1406,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=5846"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5846\/revisions"}],"predecessor-version":[{"id":5847,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5846\/revisions\/5847"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=5846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=5846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=5846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}