Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,7 +30,6 @@ print(f"Using device: {device}")
|
|
| 30 |
# Initialize the PaddleOCR detection model
|
| 31 |
print("Initializing PaddleOCR text detection model...")
|
| 32 |
try:
|
| 33 |
-
# Use the PaddleOCR class with a specific model for detection only
|
| 34 |
det_model = PaddleOCR(use_angle_cls=False, lang='en', use_gpu=torch.cuda.is_available(), show_log=False)
|
| 35 |
except Exception as e:
|
| 36 |
print(f"Error initializing PaddleOCR: {e}")
|
|
@@ -204,4 +203,47 @@ def process_file_and_create_pdf(file):
|
|
| 204 |
|
| 205 |
temp_img_path = os.path.join(temp_output_dir, "original_image.png")
|
| 206 |
original_image.save(temp_img_path)
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# Initialize the PaddleOCR detection model
|
| 31 |
print("Initializing PaddleOCR text detection model...")
|
| 32 |
try:
|
|
|
|
| 33 |
det_model = PaddleOCR(use_angle_cls=False, lang='en', use_gpu=torch.cuda.is_available(), show_log=False)
|
| 34 |
except Exception as e:
|
| 35 |
print(f"Error initializing PaddleOCR: {e}")
|
|
|
|
| 203 |
|
| 204 |
temp_img_path = os.path.join(temp_output_dir, "original_image.png")
|
| 205 |
original_image.save(temp_img_path)
|
| 206 |
+
# Corrected the typo: `T` changed to `True`
|
| 207 |
+
c.drawImage(temp_img_path, 50, height - 300, width=200, preserveAspectRatio=True)
|
| 208 |
+
os.remove(temp_img_path)
|
| 209 |
+
|
| 210 |
+
y = height - 350
|
| 211 |
+
c.setFont("Helvetica", 12)
|
| 212 |
+
for _, text in results:
|
| 213 |
+
c.drawString(50, y, text)
|
| 214 |
+
y -= 15
|
| 215 |
+
if y < 50:
|
| 216 |
+
c.showPage()
|
| 217 |
+
c.setFont("Helvetica", 12)
|
| 218 |
+
y = height - 50
|
| 219 |
+
c.save()
|
| 220 |
+
|
| 221 |
+
return output_pdf_path, input_image_for_display
|
| 222 |
+
|
| 223 |
+
except Exception as e:
|
| 224 |
+
print(f"An error occurred: {e}")
|
| 225 |
+
if os.path.exists(temp_output_dir):
|
| 226 |
+
shutil.rmtree(temp_output_dir)
|
| 227 |
+
return None, None
|
| 228 |
+
|
| 229 |
+
# Gradio Interface
|
| 230 |
+
@GPU
|
| 231 |
+
def process_file_for_gradio(file):
|
| 232 |
+
output_path, input_image = process_file_and_create_pdf(file)
|
| 233 |
+
if output_path is None:
|
| 234 |
+
return None, None
|
| 235 |
+
return output_path, input_image
|
| 236 |
+
|
| 237 |
+
demo = gr.Interface(
|
| 238 |
+
fn=process_file_for_gradio,
|
| 239 |
+
inputs=gr.File(label="Upload an Image (PNG, JPG) or a PDF", file_types=['.png', '.jpg', '.jpeg', '.pdf']),
|
| 240 |
+
outputs=[
|
| 241 |
+
gr.File(label="Download OCR Results PDF", interactive=False, visible=True),
|
| 242 |
+
gr.Image(label="Uploaded Image Preview", interactive=False)
|
| 243 |
+
],
|
| 244 |
+
title="OCR App with PaddleOCR and TrOCR",
|
| 245 |
+
description="Upload an image or a multi-page PDF to get an output PDF with the recognized text from each page. The output PDF will be downloaded automatically.",
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
if __name__ == "__main__":
|
| 249 |
+
demo.launch()
|