Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,41 @@
|
|
| 1 |
|
| 2 |
from transformers import pipeline
|
| 3 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 4 |
# Load model directly
|
| 5 |
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
| 6 |
|
| 7 |
def model():
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def main():
|
| 15 |
st.title("Hugging Face Model Demo")
|
|
|
|
| 1 |
|
| 2 |
from transformers import pipeline
|
| 3 |
import streamlit as st
|
| 4 |
+
import torch
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import requests
|
| 7 |
# Load model directly
|
| 8 |
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
| 9 |
|
| 10 |
def model():
|
| 11 |
+
from transformers import AutoImageProcessor, DetrForObjectDetection
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
| 15 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
| 16 |
+
|
| 17 |
+
image_processor = AutoImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
| 18 |
+
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
| 19 |
+
|
| 20 |
+
inputs = image_processor(images=image, return_tensors="pt")
|
| 21 |
+
outputs = model(**inputs)
|
| 22 |
+
|
| 23 |
+
# convert outputs (bounding boxes and class logits) to Pascal VOC format (xmin, ymin, xmax, ymax)
|
| 24 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
| 25 |
+
results = image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[
|
| 26 |
+
0
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 30 |
+
box = [round(i, 2) for i in box.tolist()]
|
| 31 |
+
st.write("Prediction:", result[0]['label'], "| Score:", result[0]['score'])
|
| 32 |
+
print(
|
| 33 |
+
f"Detected {model.config.id2label[label.item()]} with confidence "
|
| 34 |
+
f"{round(score.item(), 3)} at location {box}"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
|
| 40 |
def main():
|
| 41 |
st.title("Hugging Face Model Demo")
|