Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import io
|
| 3 |
-
from PIL import Image
|
| 4 |
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
| 5 |
import streamlit as st
|
| 6 |
import torch
|
|
@@ -45,7 +45,7 @@ if uploaded_file is not None:
|
|
| 45 |
image = Image.open(uploaded_file)
|
| 46 |
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
| 47 |
submit = st.button("Detect Objects ")
|
| 48 |
-
if submit:
|
| 49 |
image_data=input_image_setup(uploaded_file)
|
| 50 |
st.subheader("The response is..")
|
| 51 |
#process with model
|
|
@@ -65,3 +65,26 @@ if submit:
|
|
| 65 |
f"Detected {model.config.id2label[label.item()]} with confidence "
|
| 66 |
f"{round(score.item(), 3)} at location {box}"
|
| 67 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import io
|
| 3 |
+
from PIL import Image,ImageDraw
|
| 4 |
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
| 5 |
import streamlit as st
|
| 6 |
import torch
|
|
|
|
| 45 |
image = Image.open(uploaded_file)
|
| 46 |
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
| 47 |
submit = st.button("Detect Objects ")
|
| 48 |
+
"""if submit:
|
| 49 |
image_data=input_image_setup(uploaded_file)
|
| 50 |
st.subheader("The response is..")
|
| 51 |
#process with model
|
|
|
|
| 65 |
f"Detected {model.config.id2label[label.item()]} with confidence "
|
| 66 |
f"{round(score.item(), 3)} at location {box}"
|
| 67 |
)
|
| 68 |
+
"""
|
| 69 |
+
if submit:
|
| 70 |
+
image_data = input_image_setup(uploaded_file)
|
| 71 |
+
st.subheader("The response is..")
|
| 72 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 73 |
+
outputs = model(**inputs)
|
| 74 |
+
|
| 75 |
+
logits = outputs.logits
|
| 76 |
+
bboxes = outputs.pred_boxes
|
| 77 |
+
|
| 78 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
| 79 |
+
results = processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[0]
|
| 80 |
+
|
| 81 |
+
# Draw bounding boxes on the image
|
| 82 |
+
drawn_image = image.copy()
|
| 83 |
+
draw = ImageDraw.Draw(drawn_image)
|
| 84 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 85 |
+
box = [int(i) for i in box.tolist()]
|
| 86 |
+
draw.rectangle(box, outline="red", width=2)
|
| 87 |
+
label_text = f"{model.config.id2label[label.item()]} ({round(score.item(), 2)})"
|
| 88 |
+
draw.text((box[0], box[1]), label_text, fill="red")
|
| 89 |
+
|
| 90 |
+
st.image(drawn_image, caption="Detected Objects", use_column_width=True)
|