html - How to convert from image to text using Javascript - Stack Overflow

I am trying to convert image to text. When anyone upload image then press "Submit" image text

I am trying to convert image to text. When anyone upload image then press "Submit" image text should be show into the textarea. My following code is not working, please help!

Code:

 <html>
    <body>
    
    <input type="file" id="myFile" name="filename">  
    <br><br>
<button onclick="myFunction()">Submit</button>
<br><br>
    
    <label><b>Your Converted Text:</b></label><br><br>
    
    <textarea cols="30" name="original" rows="10" style="width: 100%;" id="convertedText">
    </textarea>

    <script src='.js/1.0.10/dist/tesseract.js'></script>
    
    <script>  
     function myFunction() {
           var myImage= document.getElementById('myFile');
          
    
           Tesseract.recognize(myImage).then(function(result){
    
            console.log(result.text);
    
           document.getElementById("convertedText").value = result.text;
            
    
            });
    }
            </script>
            
    </body>
    </html>

I am trying to convert image to text. When anyone upload image then press "Submit" image text should be show into the textarea. My following code is not working, please help!

Code:

 <html>
    <body>
    
    <input type="file" id="myFile" name="filename">  
    <br><br>
<button onclick="myFunction()">Submit</button>
<br><br>
    
    <label><b>Your Converted Text:</b></label><br><br>
    
    <textarea cols="30" name="original" rows="10" style="width: 100%;" id="convertedText">
    </textarea>

    <script src='https://cdn.rawgit./naptha/tesseract.js/1.0.10/dist/tesseract.js'></script>
    
    <script>  
     function myFunction() {
           var myImage= document.getElementById('myFile');
          
    
           Tesseract.recognize(myImage).then(function(result){
    
            console.log(result.text);
    
           document.getElementById("convertedText").value = result.text;
            
    
            });
    }
            </script>
            
    </body>
    </html>
Share Improve this question edited Aug 5, 2020 at 11:26 santosh asked Aug 5, 2020 at 11:16 santosh santosh 8921 gold badge11 silver badges22 bronze badges 3
  • 1 You need to pass an actual image to the recognize method, not just the reference to some file upload field. github./naptha/tesseract.js/blob/master/docs/image-format.md – C3roe Commented Aug 5, 2020 at 11:20
  • Show the image as a text in the textarea or as image? – Edson Magombe Commented Aug 5, 2020 at 11:22
  • @Edson Magombe I want to show the text from the image in TextArea – santosh Commented Aug 5, 2020 at 11:27
Add a ment  | 

2 Answers 2

Reset to default 2

If you attach an event listener to the file input, you can then recognize text once the file has been loaded successfully, like so:

<html>
<body>

<input type="file" id="myFile" name="filename">  
<br><br>

<label><b>Your Converted Text:</b></label><br><br>

<textarea cols="30" name="original" rows="10" style="width: 100%;" id="convertedText">
</textarea>

<script src='https://cdn.rawgit./naptha/tesseract.js/1.0.10/dist/tesseract.js'></script>

<script>  

    var myFile = document.getElementById('myFile');
    myFile.addEventListener('change', recognizeText);

    async function recognizeText({ target: { files }  }) {
        Tesseract.recognize(files[0]).then(function(result) {
            console.log("recognizeText: result.text:", result.text);
            document.getElementById("convertedText").value = result.text;
        });
    }

</script>
        
</body>
</html>

Below code will convert any image to text!

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Image to Text Converter</title>
  <script src="https://cdn.jsdelivr/npm/tesseract.js/dist/tesseract.min.js"></script>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      min-height: 100vh;
      background-color: #f3f4f6;
    }
    .container {
      max-width: 500px;
      width: 100%;
      padding: 20px;
      background: white;
      border-radius: 10px;
      box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
      text-align: center;
    }
    .container h1 {
      font-size: 1.5rem;
      margin-bottom: 20px;
    }
    input[type="file"] {
      margin-bottom: 20px;
    }
    #output {
      margin-top: 20px;
      padding: 10px;
      background: #f9fafb;
      border: 1px solid #e5e7eb;
      border-radius: 5px;
      overflow: auto;
      max-height: 200px;
    }
    button {
      padding: 10px 20px;
      background-color: #2563eb;
      color: white;
      border: none;
      border-radius: 5px;
      cursor: pointer;
      font-size: 1rem;
    }
    button:hover {
      background-color: #1d4ed8;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>Image to Text Converter</h1>
    <input type="file" id="imageInput" accept="image/*">
    <button id="convertButton">Convert to Text</button>
    <div id="output" hidden>
      <h3>Extracted Text:</h3>
      <p id="text"></p>
    </div>
  </div>

  <script>
    const imageInput = document.getElementById('imageInput');
    const convertButton = document.getElementById('convertButton');
    const outputDiv = document.getElementById('output');
    const textOutput = document.getElementById('text');

    convertButton.addEventListener('click', () => {
      if (imageInput.files.length === 0) {
        alert('Please upload an image.');
        return;
      }

      const imageFile = imageInput.files[0];
      const reader = new FileReader();

      reader.onload = () => {
        Tesseract.recognize(
          reader.result, // Image data
          'eng', // Language
          {
            logger: (info) => console.log(info) // Log progress
          }
        ).then(({ data: { text } }) => {
          textOutput.textContent = text.trim();
          outputDiv.hidden = false;
        }).catch((error) => {
          console.error(error);
          alert('Error processing the image.');
        });
      };

      reader.readAsDataURL(imageFile);
    });
  </script>
</body>
</html>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745272279a4619820.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信