Spaces:
Running
Running
File size: 2,790 Bytes
26ab692 cd068ab 98a2ff2 a95b973 26ab692 a95b973 98a2ff2 a95b973 98a2ff2 cd068ab 98a2ff2 cd068ab 98a2ff2 cd068ab 98a2ff2 cd068ab 1f36a7b cd068ab 98a2ff2 26ab692 98a2ff2 26ab692 98a2ff2 cd068ab 98a2ff2 26ab692 98a2ff2 26ab692 98a2ff2 26ab692 |
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 |
import logging
import streamlit as st
import pandas as pd
import io
from PIL import Image, ImageDraw, ImageFont
from myocr.pipelines import CommonOCRPipeline
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s"
)
logger = logging.getLogger(__name__)
st.set_page_config(layout="wide")
st.title("MyOCR Demo")
# init pipeline
@st.cache_resource
def load_pipeline():
return CommonOCRPipeline("cpu")
pipeline = load_pipeline()
font = ImageFont.truetype("src/NotoSans.ttf", 12)
def process_image(image: Image.Image, format):
buffer = io.BytesIO()
image.save(buffer, format=format)
results = pipeline(buffer.getvalue())
logger.info(f"ocr results: {results}")
if not results or not results.regions:
return None, []
image_with_boxes = image.copy()
draw = ImageDraw.Draw(image_with_boxes)
table_data = []
for item in results.regions:
if item.confidence <= 0.5:
continue
shape = item.bounding_shape
points = shape.points
if shape.type == "rectangle":
draw.rectangle([(points[0].x, points[0].y), (points[2].x, points[2].y)], outline="red", width=2)
text_pos_y = max(points[0].y - 18, 0)
draw.text((points[0].x, text_pos_y), item.text, font=font, fill="green")
table_data.append((item.text, item.confidence))
return image_with_boxes, table_data
def main():
left_col, right_col = st.columns([2, 1.5])
with left_col:
uploaded_file = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg"])
if not uploaded_file:
return
try:
image = Image.open(uploaded_file).convert("RGB")
except Exception:
st.error("⚠️ Invalid image file. Please upload a valid PNG or JPG.")
logger.warning("Invalid file uploaded.")
return
spinner_container = st.empty()
image_slot = st.empty()
image_slot.image(image, use_container_width=True)
with spinner_container:
with st.spinner("Recognizing text..."):
mime_type = uploaded_file.type
processed_image, table_data = process_image(image, mime_type.split("/")[-1])
if not table_data:
right_col.warning("No text detected.")
return
image_slot.image(processed_image, use_container_width=True)
# --- Show results ---
with right_col:
st.subheader("Recognized Text")
df = pd.DataFrame(table_data, columns=["Text", "Confidence"])
st.table(df)
if __name__ == "__main__":
try:
main()
except Exception as e:
logger.exception("Unhandled exception occurred.")
st.error(f"Internal Error!") |