Update app.py
Browse files
app.py
CHANGED
|
@@ -8,15 +8,13 @@ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-printed")
|
|
| 8 |
|
| 9 |
|
| 10 |
def process_image(image):
|
| 11 |
-
# Chuyển
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
#
|
| 17 |
pixel_values = processor(image, return_tensors="pt").pixel_values
|
| 18 |
-
|
| 19 |
-
# Sinh kết quả
|
| 20 |
generated_ids = model.generate(pixel_values)
|
| 21 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 22 |
return generated_text
|
|
@@ -32,6 +30,4 @@ interface = gr.Interface(
|
|
| 32 |
outputs="text",
|
| 33 |
title=title,
|
| 34 |
description=description
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
interface.launch()
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
def process_image(image):
|
| 11 |
+
# Chuyển nền trong suốt thành trắng (nếu có alpha channel)
|
| 12 |
+
if image.mode in ("RGBA", "LA"):
|
| 13 |
+
background = Image.new("RGB", image.size, (255, 255, 255))
|
| 14 |
+
image = Image.alpha_composite(background, image.convert("RGBA"))
|
| 15 |
|
| 16 |
+
# Xử lý ảnh
|
| 17 |
pixel_values = processor(image, return_tensors="pt").pixel_values
|
|
|
|
|
|
|
| 18 |
generated_ids = model.generate(pixel_values)
|
| 19 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 20 |
return generated_text
|
|
|
|
| 30 |
outputs="text",
|
| 31 |
title=title,
|
| 32 |
description=description
|
| 33 |
+
).launch()
|
|
|
|
|
|