Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,21 @@
|
|
| 1 |
import os
|
| 2 |
from PIL import Image
|
|
|
|
| 3 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
def input_image_setup(uploaded_file):
|
| 5 |
if uploaded_file is not None:
|
| 6 |
-
#read
|
| 7 |
bytes_data = uploaded_file.getvalue()
|
| 8 |
image_parts=[
|
| 9 |
{
|
|
@@ -18,14 +30,34 @@ def input_image_setup(uploaded_file):
|
|
| 18 |
#Streamlit App
|
| 19 |
st.set_page_config(page_title="Image Detection")
|
| 20 |
st.header("Object Detection Application")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
uploaded_file = st.file_uploader("choose an image...", type=["jpg","jpeg","png"])
|
| 22 |
image=""
|
| 23 |
if uploaded_file is not None:
|
| 24 |
image = Image.open(uploaded_file)
|
| 25 |
-
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
| 26 |
submit = st.button("Detect Objects ")
|
| 27 |
-
|
| 28 |
if submit:
|
| 29 |
image_data=input_image_setup(uploaded_file)
|
| 30 |
st.subheader("The response is..")
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from PIL import Image
|
| 3 |
+
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
| 4 |
import streamlit as st
|
| 5 |
+
import torch
|
| 6 |
+
import requests
|
| 7 |
+
|
| 8 |
+
def prettier(results):
|
| 9 |
+
for item in results:
|
| 10 |
+
score = round(item['score'], 3)
|
| 11 |
+
label = item['label'] # Use square brackets to access the 'label' key
|
| 12 |
+
location = [round(value, 2) for value in item['box'].values()]
|
| 13 |
+
print(f'Detected {label} with confidence {score} at location {location}')
|
| 14 |
+
|
| 15 |
+
|
| 16 |
def input_image_setup(uploaded_file):
|
| 17 |
if uploaded_file is not None:
|
| 18 |
+
#read the file into byte
|
| 19 |
bytes_data = uploaded_file.getvalue()
|
| 20 |
image_parts=[
|
| 21 |
{
|
|
|
|
| 30 |
#Streamlit App
|
| 31 |
st.set_page_config(page_title="Image Detection")
|
| 32 |
st.header("Object Detection Application")
|
| 33 |
+
#Select your model
|
| 34 |
+
models = ["facebook/detr-resnet-50", "ciasimbaya/ObjectDetection", "hustvl/yolos-tiny"] # List of supported models
|
| 35 |
+
model_name = st.selectbox("Select model", models)
|
| 36 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 37 |
+
model = AutoModelForObjectDetection.from_pretrained(model_name)
|
| 38 |
+
#Upload an image
|
| 39 |
uploaded_file = st.file_uploader("choose an image...", type=["jpg","jpeg","png"])
|
| 40 |
image=""
|
| 41 |
if uploaded_file is not None:
|
| 42 |
image = Image.open(uploaded_file)
|
| 43 |
+
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
| 44 |
submit = st.button("Detect Objects ")
|
|
|
|
| 45 |
if submit:
|
| 46 |
image_data=input_image_setup(uploaded_file)
|
| 47 |
st.subheader("The response is..")
|
| 48 |
+
#process with model
|
| 49 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 50 |
+
outputs = model(**inputs)
|
| 51 |
+
|
| 52 |
+
# model predicts bounding boxes and corresponding COCO classes
|
| 53 |
+
logits = outputs.logits
|
| 54 |
+
bboxes = outputs.pred_boxes
|
| 55 |
+
# print results
|
| 56 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
| 57 |
+
results = image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[0]
|
| 58 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 59 |
+
box = [round(i, 2) for i in box.tolist()]
|
| 60 |
+
print(
|
| 61 |
+
f"Detected {model.config.id2label[label.item()]} with confidence "
|
| 62 |
+
f"{round(score.item(), 3)} at location {box}"
|
| 63 |
+
)
|