Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,75 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 3 |
-
from PIL import Image
|
| 4 |
import numpy as np
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Load model và processor
|
| 7 |
-
|
| 8 |
-
model = VisionEncoderDecoderModel.from_pretrained(
|
| 9 |
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-printed")
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def process_image(image):
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
generated_ids = model.generate(pixel_values)
|
| 28 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 29 |
|
| 30 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
)
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|
| 47 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 3 |
+
from PIL import Image, ImageOps
|
| 4 |
import numpy as np
|
| 5 |
+
import io
|
| 6 |
+
import base64
|
| 7 |
|
| 8 |
# Load model và processor
|
| 9 |
+
name = "chanelcolgate/trocr-base-printed_captcha_ocr"
|
| 10 |
+
model = VisionEncoderDecoderModel.from_pretrained(name)
|
| 11 |
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-printed")
|
| 12 |
|
| 13 |
+
|
| 14 |
+
def prepare_image(pil_image):
|
| 15 |
+
"""Xử lý nền trắng nếu ảnh có nền trong suốt"""
|
| 16 |
+
if pil_image.mode in ("RGBA", "LA"):
|
| 17 |
+
background = Image.new("RGB", pil_image.size, (255, 255, 255))
|
| 18 |
+
background.paste(pil_image, mask=pil_image.split()[-1])
|
| 19 |
+
return background
|
| 20 |
+
return pil_image.convert("RGB")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
def process_image(image):
|
| 24 |
+
pil_image = Image.fromarray(image)
|
| 25 |
+
image_clean = prepare_image(pil_image)
|
| 26 |
+
|
| 27 |
+
pixel_values = processor(image_clean, return_tensors="pt").pixel_values
|
| 28 |
+
generated_ids = model.generate(pixel_values)
|
| 29 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 30 |
+
|
| 31 |
+
return image_clean, generated_text
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def process_base64(base64_str):
|
| 35 |
+
# Tách phần prefix (data:image/png;base64,...) nếu có
|
| 36 |
+
if ',' in base64_str:
|
| 37 |
+
base64_str = base64_str.split(',')[1]
|
| 38 |
+
image_data = base64.b64decode(base64_str)
|
| 39 |
+
image = Image.open(io.BytesIO(image_data))
|
| 40 |
+
image_clean = prepare_image(image)
|
| 41 |
+
|
| 42 |
+
pixel_values = processor(image_clean, return_tensors="pt").pixel_values
|
| 43 |
generated_ids = model.generate(pixel_values)
|
| 44 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 45 |
|
| 46 |
+
return image_clean, generated_text
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("## Captcha OCR Demo")
|
| 51 |
|
| 52 |
+
with gr.Tab("Upload image"):
|
| 53 |
+
with gr.Row():
|
| 54 |
+
image_input = gr.Image(type="numpy", label="Upload Image")
|
| 55 |
+
image_output = gr.Image(type="pil", label="Processed Image")
|
| 56 |
+
text_output = gr.Textbox(label="OCR Output")
|
| 57 |
+
image_button = gr.Button("Submit")
|
| 58 |
+
image_button.click(fn=process_image, inputs=image_input, outputs=[image_output, text_output])
|
| 59 |
|
| 60 |
+
with gr.Tab("Paste base64"):
|
| 61 |
+
with gr.Row():
|
| 62 |
+
base64_input = gr.Textbox(label="Paste base64 here", lines=5, placeholder="data:image/png;base64,...")
|
| 63 |
+
with gr.Row():
|
| 64 |
+
base64_output_img = gr.Image(type="pil", label="Processed Image")
|
| 65 |
+
base64_output_txt = gr.Textbox(label="OCR Output")
|
| 66 |
+
base64_button = gr.Button("Submit")
|
| 67 |
+
base64_button.click(fn=process_base64, inputs=base64_input, outputs=[base64_output_img, base64_output_txt])
|
| 68 |
|
| 69 |
+
gr.Examples(
|
| 70 |
+
examples=[f"examples/captcha-{i}.png" for i in range(10)],
|
| 71 |
+
inputs=image_input
|
| 72 |
+
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
+
demo.launch()
|