{"id":3481,"date":"2023-09-10T11:29:46","date_gmt":"2023-09-10T02:29:46","guid":{"rendered":"https:\/\/blue-bear.jp\/kb\/?p=3481"},"modified":"2023-09-10T11:29:50","modified_gmt":"2023-09-10T02:29:50","slug":"php-tesseract_ocr%e3%81%a7%e7%94%bb%e5%83%8f%e8%aa%8d%e8%ad%98%e3%81%95%e3%81%9b%e3%82%8b%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/blue-bear.jp\/kb\/php-tesseract_ocr%e3%81%a7%e7%94%bb%e5%83%8f%e8%aa%8d%e8%ad%98%e3%81%95%e3%81%9b%e3%82%8b%e6%96%b9%e6%b3%95\/","title":{"rendered":"[PHP] tesseract_ocr\u3067\u753b\u50cf\u8a8d\u8b58\u3055\u305b\u308b\u65b9\u6cd5"},"content":{"rendered":"\n<p>Composer\u3067tesseract_ocr\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>composer require thiagoalessio\/tesseract_ocr<\/code><\/pre>\n\n\n\n<p>apt\u3067tesseract-ocr\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\uff0b\u30d0\u30fc\u30b8\u30e7\u30f3\u78ba\u8a8d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\nsudo apt install tesseract-ocr\ntesseract --version<\/code><\/pre>\n\n\n\n<p>\u65e5\u672c\u8a9e\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wget https:\/\/github.com\/tesseract-ocr\/tessdata\/raw\/main\/jpn.traineddata\nsudo mv jpn.traineddata \/usr\/share\/tesseract-ocr\/4.00\/tessdata\/<\/code><\/pre>\n\n\n\n<p>PHP\u3067\u30b3\u30fc\u30c9\u5b9f\u88c5\u3001\u3053\u306e\u4f8b\u3067\u306f\u753b\u50cfURL\u3092\u6307\u5b9a\u3057\u3066\u753b\u50cf\u8a8d\u8b58\u3055\u305b\u3066\u3044\u308b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nrequire 'vendor\/autoload.php';\n\nuse thiagoalessio\\TesseractOCR\\TesseractOCR;\n\n\/\/ Replace with the path to your Tesseract executable\n$tesseractExecutable = '\/usr\/bin\/tesseract';\n\n\/\/ Replace with the URL of the image you want to process\n$imageUrl = 'https:\/\/example.com\/path\/to\/your\/image.png';\n\ntry {\n    \/\/ Set the TESSDATA_PREFIX environment variable\n    putenv('TESSDATA_PREFIX=\/usr\/share\/tesseract-ocr\/4.00\/tessdata');\n\n    \/\/ Download the image from the URL\n    $imageContents = file_get_contents($imageUrl);\n\n    \/\/ Create a temporary file to store the downloaded image\n    $tempImagePath = tempnam(sys_get_temp_dir(), 'image_');\n    file_put_contents($tempImagePath, $imageContents);\n\n    \/\/ Create an instance of TesseractOCR and specify the language\n    $tesseract = new TesseractOCR($tempImagePath);\n\n    \/\/ Set the Tesseract executable path\n    $tesseract->executable($tesseractExecutable);\n\n    \/\/ Specify the language (Japanese in this example)\n    $tesseract->lang('jpn');\n\n    \/\/ Run OCR on the image\n    $text = $tesseract->run();\n\n    \/\/ Output the detected text\n    echo \"Detected Text:\\n\";\n    echo $text;\n\n    \/\/ Clean up by deleting the temporary image file\n    unlink($tempImagePath);\n} catch (Exception $e) {\n    echo 'Error: ' . $e->getMessage();\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Composer\u3067tesseract_o<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,6],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/3481"}],"collection":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/comments?post=3481"}],"version-history":[{"count":3,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/3481\/revisions"}],"predecessor-version":[{"id":3484,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/3481\/revisions\/3484"}],"wp:attachment":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/media?parent=3481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/categories?post=3481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/tags?post=3481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}