File size: 6,701 Bytes
316e596
fc312cf
316e596
665ac31
316e596
fc312cf
665ac31
 
 
 
fc312cf
 
 
 
 
 
 
 
665ac31
 
 
 
 
 
 
 
 
 
 
 
fc312cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
665ac31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316e596
fc312cf
665ac31
fc312cf
316e596
fc312cf
 
316e596
fc312cf
665ac31
316e596
665ac31
 
fc312cf
 
 
 
 
9b7c2e6
fc312cf
 
9600354
 
 
 
 
fc312cf
9600354
fc312cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9600354
 
 
 
fc312cf
 
 
 
 
 
 
9600354
 
 
 
fc312cf
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import HTMLResponse
from io import BytesIO
import pytesseract
from PIL import Image
import subprocess

# Initialize FastAPI app
app = FastAPI()

def install_tesseract():
    try:
        subprocess.run(['apt-get', 'update'], check=True)
        subprocess.run(['apt-get', 'install', '-y', 'tesseract-ocr'], check=True)
    except subprocess.CalledProcessError as e:
        print(f"Error installing Tesseract: {e}")

install_tesseract()

# Home route
@app.get("/", response_class=HTMLResponse)
async def home():
    html_content = """
    <!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>
        <style>
            body {
                font-family: Arial, sans-serif;
                background-color: #000000;
                color: #333;
                margin: 0;
                padding: 0;
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
            }
            .container {
                text-align: center;
                background: #fff;
                padding: 30px;
                border-radius: 10px;
                box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
                width: 90%;
                max-width: 600px;
            }
            h1 {
                font-size: 24px;
                color: #4CAF50;
            }
            p {
                color: #666;
            }
            .upload-box {
                margin: 20px auto;
                padding: 30px;
                border: 2px dashed #ccc;
                border-radius: 10px;
                background-color: #f9f9f9;
                cursor: pointer;
                position: relative;
            }
            .upload-box:hover {
                background-color: #f4f4f4;
            }
            .upload-box span {
                color: #888;
                font-size: 14px;
                display: block;
            }
            .upload-box input[type="file"] {
                position: absolute;
                width: 100%;
                height: 100%;
                top: 0;
                left: 0;
                opacity: 0;
                cursor: pointer;
            }
            .process-button {
                background-color: #4CAF50;
                color: white;
                padding: 10px 20px;
                border: none;
                border-radius: 5px;
                cursor: pointer;
                font-size: 16px;
            }
            .process-button:hover {
                background-color: #45a049;
            }
            .result-box {
                margin-top: 20px;
                text-align: left;
            }
            pre {
                background-color: #f4f4f4;
                padding: 10px;
                border-radius: 5px;
                white-space: pre-wrap;
                word-wrap: break-word;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>Image to Text Converter</h1>
            <p>Quickly extract text from your uploaded images!</p>
            <form action="/upload_image/" method="POST" enctype="multipart/form-data">
                <div class="upload-box">
                    <span>Drag & Drop the Images<br>Or Click to Browse</span>
                    <input type="file" name="image" accept="image/*" id="image-file">
                </div>
                <button class="process-button" type="submit">Upload Image</button>
            </form>
        </div>
    </body>
    </html>
    """
    return HTMLResponse(content=html_content)

# Upload image route (image is processed directly in memory)
@app.post("/upload_image/")
async def upload_image(image: UploadFile = File(...)):
    # Read the image file directly into memory
    image_bytes = await image.read()
    image_stream = BytesIO(image_bytes)

    # Open the image with PIL (Pillow)
    img = Image.open(image_stream)

    # Process the image and extract text
    extracted_text = pytesseract.image_to_string(img)

    # Convert image to base64 for displaying in HTML
    import base64
    buffered = BytesIO()
    img.save(buffered, format="PNG")
    img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")

    # HTML response with image and extracted text
    html_response = f"""
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Extracted Text</title>
        <style>
            body {{
                font-family: Arial, sans-serif;
                background-color: #000000;
                color: #333;
                margin: 0;
                padding: 0;
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
            }}
            .container {{
                text-align: center;
                background: #fff;
                padding: 30px;
                border-radius: 10px;
                box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
                width: 90%;
                max-width: 600px;
            }}
            h1 {{
                font-size: 24px;
                color: #4CAF50;
            }}
            pre {{
                background-color: #f4f4f4;
                padding: 10px;
                border-radius: 5px;
                white-space: pre-wrap;
                word-wrap: break-word;
            }}
            .process-button {{
                background-color: #4CAF50;
                color: white;
                padding: 10px 20px;
                border: none;
                border-radius: 5px;
                cursor: pointer;
                font-size: 16px;
            }}
            .process-button:hover {{
                background-color: #45a049;
            }}
        </style>
    </head>
    <body>
        <div class="container">
            <h1>Extracted Text</h1>
            <p>Here is the text extracted from the image you uploaded:</p>
            <img src="data:image/png;base64,{img_str}" alt="Uploaded Image" style="max-width: 100%; margin-bottom: 20px;">
            <pre>{extracted_text}</pre>
            <form action="/" method="get">
                <button class="process-button" type="submit">Upload Another Image</button>
            </form>
        </div>
    </body>
    </html>
    """
    return HTMLResponse(content=html_response)