-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathImageToTextConverter.jsx
172 lines (159 loc) · 5.36 KB
/
ImageToTextConverter.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// import React, { useState, useEffect } from "react";
// import { Link } from "react-router-dom";
// import Tesseract from "tesseract.js";
// const Sixteen = () => {
// const [image, setImage] = useState(null);
// const [text, setText] = useState("");
// const [loading, setLoading] = useState(false);
// useEffect(() => {
// // Load the saved image from localStorage when the component mounts
// const savedImage = localStorage.getItem('selectedImage');
// if (savedImage) {
// setImage(savedImage);
// }
// }, []);
// const handleImageChange = event => {
// const file = event.target.files[0];
// if (file) {
// const imageUrl = URL.createObjectURL(file);
// setImage(imageUrl);
// // Save the new image URL to localStorage
// localStorage.setItem('selectedImage', imageUrl);
// }
// };
// const convertImageToText = () => {
// if (!image) return;
// setLoading(true);
// Tesseract.recognize(image, "eng", {
// logger: m => console.log("this is log :", m)
// })
// .then(({ data: { text } }) => {
// setText(text);
// setLoading(false);
// })
// .catch(error => {
// console.error(error);
// setLoading(false);
// });
// };
// return (
// <div className="container mx-auto p-4">
// <h1 className="text-2xl font-bold mb-4">Image to Text Converter</h1>
// <input
// type="file"
// onChange={handleImageChange}
// className="mb-4 p-2 border rounded"
// />
// <button
// onClick={convertImageToText}
// disabled={loading}
// className="bg-blue-500 text-white px-4 py-2 rounded mb-4 disabled:bg-gray-400"
// >
// {loading ? "Converting..." : "Convert Image to Text"}
// </button>
// <div className="sm:flex sm:mt-4 felx-col ">
// <div className="sm:w-1/2 h-96 border border-black p-3 mr-2 overflow-auto">
// {image &&
// <img src={image} alt="Selected" className="max-w-full h-auto" />}
// </div>
// <div className="sm:w-1/2 h-96 border border-black p-3 ml-2 overflow-auto">
// <h3 className="text-xl font-semibold mb-2">Extracted Text:</h3>
// <p>
// {text}
// </p>
// </div>
// </div>
// <Link to={`/`} className="underline hover:text-gray-600"> Return to Home</Link>
// </div>
// );
// };
// export default Sixteen;
import React, { useState, useEffect } from "react";
import { Link } from "react-router-dom";
import Tesseract from "tesseract.js";
import GoToHome from "../../Components/GoToHome";
const ImageToTextConverter = () => {
const [image, setImage] = useState(null);
const [text, setText] = useState("");
const [loading, setLoading] = useState(false);
useEffect(() => {
// Load the saved image and text from localStorage when the component mounts
const savedImage = localStorage.getItem("selectedImage");
const savedText = localStorage.getItem("extractedText");
console.log(
"Loaded image from localStorage:",
savedImage ? "Found" : "Not found"
);
console.log(
"Loaded text from localStorage:",
savedText ? "Found" : "Not found"
);
if (savedImage) {
setImage(savedImage);
}
if (savedText) {
setText(savedText);
}
}, []);
const handleImageChange = event => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = e => {
const imageDataUrl = e.target.result;
console.log("New image selected");
setImage(imageDataUrl);
setText(""); // Clear the text when a new image is selected
// Save the new image data URL to localStorage
localStorage.setItem("selectedImage", imageDataUrl);
localStorage.removeItem("extractedText"); // Remove the old text
};
reader.readAsDataURL(file);
}
};
const convertImageToText = () => {
if (!image) return;
setLoading(true);
Tesseract.recognize(image, "eng", {
logger: m => console.log("Tesseract log:", m)
})
.then(({ data: { text } }) => {
setText(text);
setLoading(false);
// Save the extracted text to localStorage
localStorage.setItem("extractedText", text);
})
.catch(error => {
console.error("Tesseract error:", error);
setLoading(false);
});
};
return (
<div className="container mx-auto p-4">
<h1 className="text-2xl font-bold mb-4">Image to Text Converter</h1>
<input
type="file"
onChange={handleImageChange}
className="mb-4 p-2 border rounded"
/>
<button
onClick={convertImageToText}
disabled={loading || !image}
className="bg-blue-500 text-white px-4 py-2 rounded mb-4 disabled:bg-gray-400"
>
{loading ? "Converting..." : "Convert Image to Text"}
</button>
<div className="flex flex-col sm:flex-row sm:mt-4">
<div className="sm:w-1/2 h-96 border border-black p-3 mr-2 overflow-auto">
{image && <img src={image} alt="Selected" className="max-w-full h-auto" />}
</div>
<div className="sm:w-1/2 h-96 border border-black p-3 ml-2 overflow-auto">
<h3 className="text-xl font-semibold mb-2">Extracted Text:</h3>
<p>{text}</p>
</div>
</div>
<GoToHome/>
</div>
);
};
export default ImageToTextConverter;