I have shown how you can convert image to text or do Optical Character Recognition OCR using PHP as front end. You can upload the file you want to do the OCR and the result will show on the page itself.
First you have to download the tesseract application and install. You can download it from below link.
You can download that in the below link. Download tesseract-ocr-setup files here
After installing the OCR Application in any location, In my PC its installed in “C:\Program Files (x86)\Tesseract-OCR”.
index.php
<html>
<body>
<center>
<h3>PHP OCR Test</h3>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</center>
</body>
the form in index.php will submit the image to upload.php and will upload the file into the images folder in root directory.
<?php
if(isset($_FILES['image'])){
$file_name = $_FILES['image']['name'];
$file_tmp =$_FILES['image']['tmp_name'];
move_uploaded_file($file_tmp,"images/".$file_name);
echo "<h3>Image Upload Success</h3>";
echo '<img src="images/'.$file_name.'" style="width:100%">';
shell_exec('"C:\\Program Files (x86)\\Tesseract-OCR\\tesseract" "C:\\xampp\\htdocs\\images\\'.$file_name.'" out');
echo "<br><h3>OCR after reading</h3><br><pre>";
$myfile = fopen("out.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("out.txt"));
fclose($myfile);
echo "</pre>";
}
?>
After the image is saved in images folder, The command prompt code in
upload.php will process the CMD commands to do OCR on the page and
create the output txt file in the root folder.
Later the out.txt is read and displayed in the page itself.
No comments:
Post a Comment