Nikhil0987 commited on
Commit
0cf3363
·
verified ·
1 Parent(s): 79f29a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -18
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
- file_path = hf_hub_download(repo_id="nielsr/example-pdf", repo_type="dataset", filename="example_pdf.png")
8
- image = Image.open(file_path).convert("RGB")
 
 
 
 
 
9
 
10
- image_processor = AutoImageProcessor.from_pretrained("microsoft/table-transformer-detection")
11
- model = TableTransformerForObjectDetection.from_pretrained("microsoft/table-transformer-detection")
12
 
13
- inputs = image_processor(images=image, return_tensors="pt")
14
- outputs = model(**inputs)
15
 
16
- # convert outputs (bounding boxes and class logits) to Pascal VOC format (xmin, ymin, xmax, ymax)
17
- target_sizes = torch.tensor([image.size[::-1]])
18
- results = image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[
19
- 0
20
- ]
21
 
22
- for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
23
- box = [round(i, 2) for i in box.tolist()]
24
- print(
25
- f"Detected {model.config.id2label[label.item()]} with confidence "
26
- f"{round(score.item(), 3)} at location {box}"
27
- )
28
- Detected table with confidence 1.0 at location [202.1, 210.59, 1119.22, 385.09]
 
 
 
 
 
 
 
 
 
 
 
 
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
+ )