Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import torchvision.transforms as T
|
| 4 |
+
from torchvision.models.detection import fasterrcnn_resnet50_fpn
|
| 5 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
# Load a pre-trained Faster R-CNN model
|
| 9 |
+
@st.cache_resource
|
| 10 |
+
def load_model():
|
| 11 |
+
model = fasterrcnn_resnet50_fpn(pretrained=True)
|
| 12 |
+
model.eval()
|
| 13 |
+
return model
|
| 14 |
+
|
| 15 |
+
model = load_model()
|
| 16 |
+
|
| 17 |
+
# Replace this in your code
|
| 18 |
+
OFFICE_INSTANCE_CATEGORY_NAMES = [
|
| 19 |
+
'__background__', # Always include the background class
|
| 20 |
+
'person',
|
| 21 |
+
'chair',
|
| 22 |
+
'couch',
|
| 23 |
+
'potted plant',
|
| 24 |
+
'dining table',
|
| 25 |
+
'tv',
|
| 26 |
+
'laptop',
|
| 27 |
+
'mouse',
|
| 28 |
+
'remote',
|
| 29 |
+
'keyboard',
|
| 30 |
+
'cell phone',
|
| 31 |
+
'book',
|
| 32 |
+
'clock',
|
| 33 |
+
'vase',
|
| 34 |
+
'scissors',
|
| 35 |
+
'teddy bear',
|
| 36 |
+
'hair drier',
|
| 37 |
+
'toothbrush',
|
| 38 |
+
'bottle',
|
| 39 |
+
'cup',
|
| 40 |
+
'fork',
|
| 41 |
+
'knife',
|
| 42 |
+
'spoon',
|
| 43 |
+
'bowl',
|
| 44 |
+
'backpack',
|
| 45 |
+
'handbag',
|
| 46 |
+
'tie',
|
| 47 |
+
'suitcase',
|
| 48 |
+
'frisbee',
|
| 49 |
+
'sports ball',
|
| 50 |
+
'surfboard',
|
| 51 |
+
'tennis racket',
|
| 52 |
+
'wine glass',
|
| 53 |
+
'sandwich',
|
| 54 |
+
'orange',
|
| 55 |
+
'pizza',
|
| 56 |
+
'donut',
|
| 57 |
+
'cake',
|
| 58 |
+
'microwave',
|
| 59 |
+
'oven',
|
| 60 |
+
'toaster',
|
| 61 |
+
'sink',
|
| 62 |
+
'refrigerator',
|
| 63 |
+
'apple',
|
| 64 |
+
'clock',
|
| 65 |
+
'book',
|
| 66 |
+
'vase',
|
| 67 |
+
'bench',
|
| 68 |
+
'umbrella'
|
| 69 |
+
]
|
| 70 |
+
|
| 71 |
+
# Define a transformation to convert the image to tensor
|
| 72 |
+
transform = T.Compose([
|
| 73 |
+
T.ToTensor(),
|
| 74 |
+
])
|
| 75 |
+
|
| 76 |
+
# Function to draw bounding boxes on the image
|
| 77 |
+
def draw_boxes(image, predictions, threshold=0.5):
|
| 78 |
+
draw = ImageDraw.Draw(image)
|
| 79 |
+
font = ImageFont.load_default()
|
| 80 |
+
|
| 81 |
+
for idx, box in enumerate(predictions[0]['boxes']):
|
| 82 |
+
score = predictions[0]['scores'][idx].item()
|
| 83 |
+
label = COCO_INSTANCE_CATEGORY_NAMES[predictions[0]['labels'][idx]]
|
| 84 |
+
|
| 85 |
+
if score >= threshold:
|
| 86 |
+
box = box.tolist()
|
| 87 |
+
draw.rectangle(box, outline="red", width=3)
|
| 88 |
+
text = f"{label}: {int(score * 100)}%"
|
| 89 |
+
text_size = font.getsize(text)
|
| 90 |
+
draw.rectangle([box[0], box[1] - text_size[1], box[0] + text_size[0], box[1]], fill="red")
|
| 91 |
+
draw.text((box[0], box[1] - text_size[1]), text, fill="white", font=font)
|
| 92 |
+
|
| 93 |
+
# Streamlit interface
|
| 94 |
+
def main():
|
| 95 |
+
st.title("Object Detection with Faster R-CNN")
|
| 96 |
+
st.write("Upload an image and detect objects using Faster R-CNN.")
|
| 97 |
+
|
| 98 |
+
# File uploader
|
| 99 |
+
uploaded_image = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
|
| 100 |
+
|
| 101 |
+
if uploaded_image is not None:
|
| 102 |
+
image = Image.open(uploaded_image).convert("RGB")
|
| 103 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 104 |
+
|
| 105 |
+
if st.button("Analyze"):
|
| 106 |
+
with st.spinner("Analyzing..."):
|
| 107 |
+
image_tensor = transform(image).unsqueeze(0) # Add batch dimension
|
| 108 |
+
|
| 109 |
+
# Perform object detection
|
| 110 |
+
with torch.no_grad():
|
| 111 |
+
predictions = model(image_tensor)
|
| 112 |
+
|
| 113 |
+
# Draw bounding boxes
|
| 114 |
+
image_with_boxes = image.copy()
|
| 115 |
+
draw_boxes(image_with_boxes, predictions)
|
| 116 |
+
|
| 117 |
+
st.image(image_with_boxes, caption="Detected Objects", use_container_width=True)
|
| 118 |
+
|
| 119 |
+
if __name__ == "__main__":
|
| 120 |
+
main()
|