Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,41 @@
|
|
1 |
# Object Detection
|
|
|
2 |
from huggingface_hub import hf_hub_download
|
3 |
from transformers import AutoImageProcessor, TableTransformerForObjectDetection
|
4 |
import torch
|
5 |
from PIL import Image
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
image_processor =
|
11 |
-
model = TableTransformerForObjectDetection.from_pretrained("microsoft/table-transformer-detection")
|
12 |
|
13 |
-
|
14 |
-
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
results = image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[
|
19 |
-
0
|
20 |
-
]
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
)
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# Object Detection
|
2 |
+
import streamlit as st
|
3 |
from huggingface_hub import hf_hub_download
|
4 |
from transformers import AutoImageProcessor, TableTransformerForObjectDetection
|
5 |
import torch
|
6 |
from PIL import Image
|
7 |
|
8 |
+
# Model and Image Processor Loading (ideally at the app start)
|
9 |
+
@st.cache_resource
|
10 |
+
def load_assets():
|
11 |
+
file_path = hf_hub_download(repo_id="nielsr/example-pdf", repo_type="dataset", filename="example_pdf.png")
|
12 |
+
image_processor = AutoImageProcessor.from_pretrained("microsoft/table-transformer-detection")
|
13 |
+
model = TableTransformerForObjectDetection.from_pretrained("microsoft/table-transformer-detection")
|
14 |
+
return file_path, image_processor, model
|
15 |
|
16 |
+
file_path, image_processor, model = load_assets()
|
|
|
17 |
|
18 |
+
# App Title
|
19 |
+
st.title("Table Detection in Images")
|
20 |
|
21 |
+
# Image Upload
|
22 |
+
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
|
|
|
|
|
|
23 |
|
24 |
+
# Process Image and Display Results
|
25 |
+
if uploaded_file:
|
26 |
+
image = Image.open(uploaded_file).convert("RGB")
|
27 |
+
|
28 |
+
inputs = image_processor(images=image, return_tensors="pt")
|
29 |
+
outputs = model(**inputs)
|
30 |
+
|
31 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
32 |
+
results = image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[0]
|
33 |
+
|
34 |
+
st.image(image) # Display the uploaded image
|
35 |
+
|
36 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
37 |
+
box = [round(i, 2) for i in box.tolist()]
|
38 |
+
st.write(
|
39 |
+
f"Detected {model.config.id2label[label.item()]} with confidence "
|
40 |
+
f"{round(score.item(), 3)} at location {box}"
|
41 |
+
)
|