CoolFace
Apppublic

drmurataltun/objectDetection2

sourceHugging Facemitupdated 2y agoView on Hugging Face
1likes
app.py39 linesDownload Raw Back to root
1import streamlit as st2from transformers import AutoImageProcessor, DetaForObjectDetection3from PIL import Image4import requests5 6st.title("Object Detection")7 8# Sidebar instructions9st.sidebar.header("Instructions")10st.sidebar.write("1. Enter an image URL in the text input below.")11st.sidebar.write("2. Click the 'Detect Objects' button to process the image.")12 13# Image URL input14image_url = st.text_input("Enter image URL:", "http://images.cocodataset.org/val2017/000000039769.jpg")15 16if st.button("Detect Objects"):17    try:18        # Load the image19        image = Image.open(requests.get(image_url, stream=True).raw)20 21        # Initialize the image processor and model22        image_processor = AutoImageProcessor.from_pretrained("jozhang97/deta-swin-large")23        model = DetaForObjectDetection.from_pretrained("jozhang97/deta-swin-large")24 25        # Process the image26        inputs = image_processor(images=image, return_tensors="pt")27        outputs = model(**inputs)28 29        # Convert outputs to Pascal VOC format30        target_sizes = torch.tensor([image.size[::-1]])31        results = image_processor.post_process_object_detection(outputs, threshold=0.5, target_sizes=target_sizes)[0]32 33        # Display the image and detected objects34        st.image(image, use_column_width=True)35        for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):36            box = [round(i, 2) for i in box.tolist()]37            st.write(f"Detected {model.config.id2label[label.item()]} with confidence {round(score.item(), 3)} at location {box}")38    except:39        st.write("Error: Unable to process the image. Please check the URL and try again.")