Spaces:
Runtime error
Runtime error
FirstInterfacePush
Browse files- app.py +33 -1
- mine2.jpeg +0 -0
app.py
CHANGED
|
@@ -1,3 +1,35 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
| 6 |
|
| 7 |
+
# Load the processor and model for table structure recognition
|
| 8 |
+
processor = AutoImageProcessor.from_pretrained("microsoft/table-transformer-structure-recognition")
|
| 9 |
+
model = AutoModelForObjectDetection.from_pretrained("microsoft/table-transformer-structure-recognition")
|
| 10 |
+
|
| 11 |
+
# Define the inference function
|
| 12 |
+
def predict(image):
|
| 13 |
+
# Preprocess the input image
|
| 14 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 15 |
+
|
| 16 |
+
# Perform object detection using the model
|
| 17 |
+
with torch.no_grad():
|
| 18 |
+
outputs = model(**inputs)
|
| 19 |
+
|
| 20 |
+
# Extract bounding boxes and class labels
|
| 21 |
+
predicted_boxes = outputs.pred_boxes[0].cpu().numpy() # First image
|
| 22 |
+
predicted_classes = outputs.logits.argmax(-1).cpu().numpy() # Class predictions
|
| 23 |
+
|
| 24 |
+
# Return the bounding boxes for display
|
| 25 |
+
return {"boxes": predicted_boxes.tolist(), "classes": predicted_classes.tolist()}
|
| 26 |
+
|
| 27 |
+
# Set up the Gradio interface
|
| 28 |
+
interface = gr.Interface(
|
| 29 |
+
fn=predict, # The function that gets called when an image is uploaded
|
| 30 |
+
inputs=gr.Image(type="pil"), # Image input (as PIL image)
|
| 31 |
+
outputs="json", # Outputting a JSON with the boxes and classes
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Launch the Gradio app
|
| 35 |
+
interface.launch()
|
mine2.jpeg
ADDED
|