<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: mohammad shareef</title>
    <description>The latest articles on DEV Community by mohammad shareef (@ritzdev).</description>
    <link>https://dev.to/ritzdev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2911133%2F0b720475-2326-4bba-ac26-5772b22ad50c.jpg</url>
      <title>DEV Community: mohammad shareef</title>
      <link>https://dev.to/ritzdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ritzdev"/>
    <language>en</language>
    <item>
      <title>Unlocking Text from Images: A Guide using React and Node.js</title>
      <dc:creator>mohammad shareef</dc:creator>
      <pubDate>Tue, 04 Mar 2025 16:51:40 +0000</pubDate>
      <link>https://dev.to/ritzdev/unlocking-text-from-images-a-guide-using-react-and-nodejs-p1k</link>
      <guid>https://dev.to/ritzdev/unlocking-text-from-images-a-guide-using-react-and-nodejs-p1k</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In our digital age, images are omnipresent. From scanned documents to social media photos, visual data is everywhere. But what if we could unlock the hidden textual information within these images? This article delves into how we can leverage React and Node.js to perform text extraction from images.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Text Extraction?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Text extraction from images has numerous applications:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Digitizing printed documents for archiving.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Extracting information from receipts or invoices.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enhancing accessibility by converting text in images to readable formats.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Setting Up the Environment
&lt;/h2&gt;

&lt;p&gt;First, let's set up our project environment. Ensure you have Node.js and npm installed (Stick to latest versions if possible).&lt;/p&gt;

&lt;p&gt;Step 1: Initialize the Project&lt;br&gt;
&lt;code&gt;mkdir text-extraction-app&lt;br&gt;
cd text-extraction-app&lt;br&gt;
npm init -y&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Step 2: Set Up the Frontend with React&lt;br&gt;
Create a React application using Vite (I prefer Vite choose what you are comfortable with).&lt;br&gt;
&lt;code&gt;npm create vite@latest frontend -- --template react&lt;br&gt;
cd frontend&lt;br&gt;
npm install&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Step 3: Set Up the Backend with Node.js&lt;br&gt;
Inside your project directory, create a server folder for the backend.&lt;br&gt;
Install the necessary packages, multer and tesseract are the must packages to be installed.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mkdir server&lt;br&gt;
cd server&lt;br&gt;
npm init -y&lt;br&gt;
npm install express multer tesseract.js&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
You can use the below basic sample code and tailor it the way you need as per your requirement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const multer = require('multer');
const Tesseract = require('tesseract.js');
const path = require('path');

const app = express();
const upload = multer({ dest: 'uploads/' });

app.use(express.static(path.join(__dirname, 'uploads')));

app.post('/upload', upload.single('image'), (req, res) =&amp;gt; {
    const imagePath = req.file.path;
      Tesseract.recognize(imagePath, "eng", {})
    .then((data) =&amp;gt; {
      const extractedText = data.data.text.replace(/(icon|image)/gi, "");
      //.replace(/[^\w\s]/gi, '').toLowerCase()
      res.json({ extractedText });
    })
    .catch((error) =&amp;gt; {
      console.error(error);
      res.status(500).json({ error: "Failed to extract text" });
    });
});

app.listen(5000, () =&amp;gt; console.log('Server started on http://localhost:5000'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Building the Frontend
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Setting Up Image Upload
In your React app, create a file App.js in the src folder.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';
import axios from 'axios';

const App = () =&amp;gt; {
    const [image, setImage] = useState(null);
    const [text, setText] = useState('');

    const handleImageChange = (e) =&amp;gt; {
        setImage(e.target.files[0]);
    };

    const handleSubmit = async (e) =&amp;gt; {
        e.preventDefault();
        const formData = new FormData();
        formData.append('image', image);

        try {
            const response = await axios.post('http://localhost:5000/upload', formData, {
                headers: {
                    'Content-Type': 'multipart/form-data',
                },
            });
            setText(response.data.extractedText);
        } catch (error) {
            console.error('Error uploading image:', error);
        }
    };

    return (
        &amp;lt;div&amp;gt;
            &amp;lt;h1&amp;gt;Text Extraction from Image&amp;lt;/h1&amp;gt;
            &amp;lt;form onSubmit={handleSubmit}&amp;gt;
                &amp;lt;input type="file" onChange={handleImageChange} accept="image/*" /&amp;gt;
                &amp;lt;button type="submit"&amp;gt;Extract Text&amp;lt;/button&amp;gt;
            &amp;lt;/form&amp;gt;
            &amp;lt;p&amp;gt;Extracted Text: {text}&amp;lt;/p&amp;gt;
        &amp;lt;/div&amp;gt;
    );
};
export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Congratulations! You've successfully built a text extraction app using React and Node.js. This basic implementation can be further extended to support multiple languages, different image formats, and improved accuracy. Text extraction opens up numerous possibilities for automating data entry, enhancing accessibility, and digitizing information.&lt;/p&gt;

&lt;p&gt;Happy learning 🌟. Cheers to new discoveries and continuous growth! 📚🚀&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>node</category>
    </item>
  </channel>
</rss>
