Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,21 +6,6 @@ import streamlit as st
|
|
| 6 |
import torch
|
| 7 |
import requests
|
| 8 |
|
| 9 |
-
#def prettier(results):
|
| 10 |
-
# for item in results:
|
| 11 |
-
# score = round(item['score'], 3)
|
| 12 |
-
# label = item['label'] # Use square brackets to access the 'label' key
|
| 13 |
-
# location = [round(value, 2) for value in item['box'].values()]
|
| 14 |
-
# print(f'Detected {label} with confidence {score} at location {location}')
|
| 15 |
-
|
| 16 |
-
#def prettify_results(results):
|
| 17 |
-
# for item in results:
|
| 18 |
-
# score = round(item['score'].item(), 3)
|
| 19 |
-
# label = model.config.id2label[item['label']] # Get label from id2label mapping in model config
|
| 20 |
-
# box = [round(coord, 2) for coord in item['box']]
|
| 21 |
-
# st.write(f'Detected {label} with confidence {score} at location {box}')
|
| 22 |
-
# Function to process uploaded image and prepare input for model
|
| 23 |
-
|
| 24 |
def input_image_setup(uploaded_file):
|
| 25 |
if uploaded_file is not None:
|
| 26 |
bytes_data = uploaded_file.getvalue()
|
|
@@ -29,7 +14,6 @@ def input_image_setup(uploaded_file):
|
|
| 29 |
else:
|
| 30 |
raise FileNotFoundError("No file uploaded")
|
| 31 |
|
| 32 |
-
|
| 33 |
#Streamlit App
|
| 34 |
st.set_page_config(page_title="Image Detection")
|
| 35 |
st.header("Object Detection Application")
|
|
@@ -45,27 +29,6 @@ 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
|
| 52 |
-
inputs = processor(images=image, return_tensors="pt")
|
| 53 |
-
outputs = model(**inputs)
|
| 54 |
-
|
| 55 |
-
# model predicts bounding boxes and corresponding COCO classes
|
| 56 |
-
logits = outputs.logits
|
| 57 |
-
bboxes = outputs.pred_boxes
|
| 58 |
-
# print results
|
| 59 |
-
target_sizes = torch.tensor([image.size[::-1]])
|
| 60 |
-
results = processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[0]
|
| 61 |
-
# prettify_results(results)
|
| 62 |
-
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 63 |
-
box = [round(i, 2) for i in box.tolist()]
|
| 64 |
-
st.write(
|
| 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..")
|
|
@@ -87,4 +50,10 @@ if submit:
|
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import torch
|
| 7 |
import requests
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def input_image_setup(uploaded_file):
|
| 10 |
if uploaded_file is not None:
|
| 11 |
bytes_data = uploaded_file.getvalue()
|
|
|
|
| 14 |
else:
|
| 15 |
raise FileNotFoundError("No file uploaded")
|
| 16 |
|
|
|
|
| 17 |
#Streamlit App
|
| 18 |
st.set_page_config(page_title="Image Detection")
|
| 19 |
st.header("Object Detection Application")
|
|
|
|
| 29 |
image = Image.open(uploaded_file)
|
| 30 |
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
| 31 |
submit = st.button("Detect Objects ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
if submit:
|
| 33 |
image_data = input_image_setup(uploaded_file)
|
| 34 |
st.subheader("The response is..")
|
|
|
|
| 50 |
label_text = f"{model.config.id2label[label.item()]} ({round(score.item(), 2)})"
|
| 51 |
draw.text((box[0], box[1]), label_text, fill="red")
|
| 52 |
|
| 53 |
+
st.image(drawn_image, caption="Detected Objects", use_column_width=True)
|
| 54 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 55 |
+
box = [round(i, 2) for i in box.tolist()]
|
| 56 |
+
st.write(
|
| 57 |
+
f"Detected {model.config.id2label[label.item()]} with confidence "
|
| 58 |
+
f"{round(score.item(), 3)} at location {box}"
|
| 59 |
+
)
|