Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,16 +1,21 @@
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile
|
| 2 |
-
from fastapi.responses import HTMLResponse
|
| 3 |
from io import BytesIO
|
| 4 |
import pytesseract
|
| 5 |
from PIL import Image
|
| 6 |
-
import
|
| 7 |
|
| 8 |
# Initialize FastAPI app
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Home route
|
| 16 |
@app.get("/", response_class=HTMLResponse)
|
|
@@ -23,23 +28,82 @@ async def home():
|
|
| 23 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 24 |
<title>Image to Text Converter</title>
|
| 25 |
<style>
|
| 26 |
-
body {
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
.
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
</style>
|
| 44 |
</head>
|
| 45 |
<body>
|
|
@@ -60,67 +124,89 @@ async def home():
|
|
| 60 |
return HTMLResponse(content=html_content)
|
| 61 |
|
| 62 |
# Upload image route (image is processed directly in memory)
|
| 63 |
-
@app.post("/upload_image/"
|
| 64 |
async def upload_image(image: UploadFile = File(...)):
|
| 65 |
-
#
|
| 66 |
image_bytes = await image.read()
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
with open(file_path, "wb") as f:
|
| 70 |
-
f.write(image_bytes)
|
| 71 |
-
|
| 72 |
# Open the image with PIL (Pillow)
|
| 73 |
-
img = Image.open(
|
| 74 |
|
| 75 |
# Process the image and extract text
|
| 76 |
extracted_text = pytesseract.image_to_string(img)
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
|
|
|
| 82 |
<!DOCTYPE html>
|
| 83 |
<html lang="en">
|
| 84 |
<head>
|
| 85 |
<meta charset="UTF-8">
|
| 86 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 87 |
-
<title>
|
| 88 |
<style>
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
</style>
|
| 91 |
</head>
|
| 92 |
<body>
|
| 93 |
<div class="container">
|
| 94 |
-
<h1>
|
| 95 |
-
<p>
|
| 96 |
-
<
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
<h2>Extracted Text:</h2>
|
| 102 |
-
<pre>{extracted_text}</pre>
|
| 103 |
-
</div>
|
| 104 |
-
<div class="actions">
|
| 105 |
-
<button onclick="copyToClipboard()">Copy to Clipboard</button>
|
| 106 |
-
<button onclick="window.location.href='/'">Upload Another Image</button>
|
| 107 |
-
</div>
|
| 108 |
</div>
|
| 109 |
-
<script>
|
| 110 |
-
function copyToClipboard() {
|
| 111 |
-
const text = document.querySelector('pre').textContent;
|
| 112 |
-
navigator.clipboard.writeText(text).then(() => {
|
| 113 |
-
alert('Text copied to clipboard!');
|
| 114 |
-
});
|
| 115 |
-
}
|
| 116 |
-
</script>
|
| 117 |
</body>
|
| 118 |
</html>
|
| 119 |
"""
|
| 120 |
-
return HTMLResponse(content=
|
| 121 |
-
|
| 122 |
-
# Serve the uploaded images statically
|
| 123 |
-
@app.get("/uploaded_images/{filename}")
|
| 124 |
-
async def serve_image(filename: str):
|
| 125 |
-
file_path = os.path.join(UPLOAD_DIR, filename)
|
| 126 |
-
return FileResponse(file_path)
|
|
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
from io import BytesIO
|
| 4 |
import pytesseract
|
| 5 |
from PIL import Image
|
| 6 |
+
import subprocess
|
| 7 |
|
| 8 |
# Initialize FastAPI app
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
def install_tesseract():
|
| 12 |
+
try:
|
| 13 |
+
subprocess.run(['apt-get', 'update'], check=True)
|
| 14 |
+
subprocess.run(['apt-get', 'install', '-y', 'tesseract-ocr'], check=True)
|
| 15 |
+
except subprocess.CalledProcessError as e:
|
| 16 |
+
print(f"Error installing Tesseract: {e}")
|
| 17 |
+
|
| 18 |
+
install_tesseract()
|
| 19 |
|
| 20 |
# Home route
|
| 21 |
@app.get("/", response_class=HTMLResponse)
|
|
|
|
| 28 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 29 |
<title>Image to Text Converter</title>
|
| 30 |
<style>
|
| 31 |
+
body {
|
| 32 |
+
font-family: Arial, sans-serif;
|
| 33 |
+
background-color: #000000;
|
| 34 |
+
color: #333;
|
| 35 |
+
margin: 0;
|
| 36 |
+
padding: 0;
|
| 37 |
+
display: flex;
|
| 38 |
+
justify-content: center;
|
| 39 |
+
align-items: center;
|
| 40 |
+
height: 100vh;
|
| 41 |
+
}
|
| 42 |
+
.container {
|
| 43 |
+
text-align: center;
|
| 44 |
+
background: #fff;
|
| 45 |
+
padding: 30px;
|
| 46 |
+
border-radius: 10px;
|
| 47 |
+
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
|
| 48 |
+
width: 90%;
|
| 49 |
+
max-width: 600px;
|
| 50 |
+
}
|
| 51 |
+
h1 {
|
| 52 |
+
font-size: 24px;
|
| 53 |
+
color: #4CAF50;
|
| 54 |
+
}
|
| 55 |
+
p {
|
| 56 |
+
color: #666;
|
| 57 |
+
}
|
| 58 |
+
.upload-box {
|
| 59 |
+
margin: 20px auto;
|
| 60 |
+
padding: 30px;
|
| 61 |
+
border: 2px dashed #ccc;
|
| 62 |
+
border-radius: 10px;
|
| 63 |
+
background-color: #f9f9f9;
|
| 64 |
+
cursor: pointer;
|
| 65 |
+
position: relative;
|
| 66 |
+
}
|
| 67 |
+
.upload-box:hover {
|
| 68 |
+
background-color: #f4f4f4;
|
| 69 |
+
}
|
| 70 |
+
.upload-box span {
|
| 71 |
+
color: #888;
|
| 72 |
+
font-size: 14px;
|
| 73 |
+
display: block;
|
| 74 |
+
}
|
| 75 |
+
.upload-box input[type="file"] {
|
| 76 |
+
position: absolute;
|
| 77 |
+
width: 100%;
|
| 78 |
+
height: 100%;
|
| 79 |
+
top: 0;
|
| 80 |
+
left: 0;
|
| 81 |
+
opacity: 0;
|
| 82 |
+
cursor: pointer;
|
| 83 |
+
}
|
| 84 |
+
.process-button {
|
| 85 |
+
background-color: #4CAF50;
|
| 86 |
+
color: white;
|
| 87 |
+
padding: 10px 20px;
|
| 88 |
+
border: none;
|
| 89 |
+
border-radius: 5px;
|
| 90 |
+
cursor: pointer;
|
| 91 |
+
font-size: 16px;
|
| 92 |
+
}
|
| 93 |
+
.process-button:hover {
|
| 94 |
+
background-color: #45a049;
|
| 95 |
+
}
|
| 96 |
+
.result-box {
|
| 97 |
+
margin-top: 20px;
|
| 98 |
+
text-align: left;
|
| 99 |
+
}
|
| 100 |
+
pre {
|
| 101 |
+
background-color: #f4f4f4;
|
| 102 |
+
padding: 10px;
|
| 103 |
+
border-radius: 5px;
|
| 104 |
+
white-space: pre-wrap;
|
| 105 |
+
word-wrap: break-word;
|
| 106 |
+
}
|
| 107 |
</style>
|
| 108 |
</head>
|
| 109 |
<body>
|
|
|
|
| 124 |
return HTMLResponse(content=html_content)
|
| 125 |
|
| 126 |
# Upload image route (image is processed directly in memory)
|
| 127 |
+
@app.post("/upload_image/")
|
| 128 |
async def upload_image(image: UploadFile = File(...)):
|
| 129 |
+
# Read the image file directly into memory
|
| 130 |
image_bytes = await image.read()
|
| 131 |
+
image_stream = BytesIO(image_bytes)
|
| 132 |
+
|
|
|
|
|
|
|
|
|
|
| 133 |
# Open the image with PIL (Pillow)
|
| 134 |
+
img = Image.open(image_stream)
|
| 135 |
|
| 136 |
# Process the image and extract text
|
| 137 |
extracted_text = pytesseract.image_to_string(img)
|
| 138 |
|
| 139 |
+
# Convert image to base64 for displaying in HTML
|
| 140 |
+
import base64
|
| 141 |
+
buffered = BytesIO()
|
| 142 |
+
img.save(buffered, format="PNG")
|
| 143 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 144 |
|
| 145 |
+
# HTML response with image and extracted text
|
| 146 |
+
html_response = f"""
|
| 147 |
<!DOCTYPE html>
|
| 148 |
<html lang="en">
|
| 149 |
<head>
|
| 150 |
<meta charset="UTF-8">
|
| 151 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 152 |
+
<title>Extracted Text</title>
|
| 153 |
<style>
|
| 154 |
+
body {{
|
| 155 |
+
font-family: Arial, sans-serif;
|
| 156 |
+
background-color: #000000;
|
| 157 |
+
color: #333;
|
| 158 |
+
margin: 0;
|
| 159 |
+
padding: 0;
|
| 160 |
+
display: flex;
|
| 161 |
+
justify-content: center;
|
| 162 |
+
align-items: center;
|
| 163 |
+
height: 100vh;
|
| 164 |
+
}}
|
| 165 |
+
.container {{
|
| 166 |
+
text-align: center;
|
| 167 |
+
background: #fff;
|
| 168 |
+
padding: 30px;
|
| 169 |
+
border-radius: 10px;
|
| 170 |
+
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
|
| 171 |
+
width: 90%;
|
| 172 |
+
max-width: 600px;
|
| 173 |
+
}}
|
| 174 |
+
h1 {{
|
| 175 |
+
font-size: 24px;
|
| 176 |
+
color: #4CAF50;
|
| 177 |
+
}}
|
| 178 |
+
pre {{
|
| 179 |
+
background-color: #f4f4f4;
|
| 180 |
+
padding: 10px;
|
| 181 |
+
border-radius: 5px;
|
| 182 |
+
white-space: pre-wrap;
|
| 183 |
+
word-wrap: break-word;
|
| 184 |
+
}}
|
| 185 |
+
.process-button {{
|
| 186 |
+
background-color: #4CAF50;
|
| 187 |
+
color: white;
|
| 188 |
+
padding: 10px 20px;
|
| 189 |
+
border: none;
|
| 190 |
+
border-radius: 5px;
|
| 191 |
+
cursor: pointer;
|
| 192 |
+
font-size: 16px;
|
| 193 |
+
}}
|
| 194 |
+
.process-button:hover {{
|
| 195 |
+
background-color: #45a049;
|
| 196 |
+
}}
|
| 197 |
</style>
|
| 198 |
</head>
|
| 199 |
<body>
|
| 200 |
<div class="container">
|
| 201 |
+
<h1>Extracted Text</h1>
|
| 202 |
+
<p>Here is the text extracted from the image you uploaded:</p>
|
| 203 |
+
<img src="data:image/png;base64,{img_str}" alt="Uploaded Image" style="max-width: 100%; margin-bottom: 20px;">
|
| 204 |
+
<pre>{extracted_text}</pre>
|
| 205 |
+
<form action="/" method="get">
|
| 206 |
+
<button class="process-button" type="submit">Upload Another Image</button>
|
| 207 |
+
</form>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
</body>
|
| 210 |
</html>
|
| 211 |
"""
|
| 212 |
+
return HTMLResponse(content=html_response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|