File size: 1,586 Bytes
79f29a1
0cf3363
44608bc
 
 
 
b88cd09
0cf3363
 
 
 
 
 
 
b88cd09
0cf3363
b88cd09
0cf3363
 
b88cd09
0cf3363
 
b88cd09
0cf3363
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Object Detection
import streamlit as st
from huggingface_hub import hf_hub_download
from transformers import AutoImageProcessor, TableTransformerForObjectDetection
import torch
from PIL import Image

# Model and Image Processor Loading (ideally at the app start)
@st.cache_resource
def load_assets():
    file_path = hf_hub_download(repo_id="nielsr/example-pdf", repo_type="dataset", filename="example_pdf.png")
    image_processor = AutoImageProcessor.from_pretrained("microsoft/table-transformer-detection")
    model = TableTransformerForObjectDetection.from_pretrained("microsoft/table-transformer-detection")
    return file_path, image_processor, model

file_path, image_processor, model = load_assets()

# App Title
st.title("Table Detection in Images")

# Image Upload
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])

# Process Image and Display Results
if uploaded_file:
    image = Image.open(uploaded_file).convert("RGB")

    inputs = image_processor(images=image, return_tensors="pt")
    outputs = model(**inputs)

    target_sizes = torch.tensor([image.size[::-1]])
    results = image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[0]

    st.image(image)  # Display the uploaded image

    for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
        box = [round(i, 2) for i in box.tolist()]
        st.write(
            f"Detected {model.config.id2label[label.item()]} with confidence "
            f"{round(score.item(), 3)} at location {box}"
        )