Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pprint import pprint
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
import torchvision.models as tv_models
|
| 5 |
+
import torchvision.transforms as transforms
|
| 6 |
+
import torchvision.utils as vutils
|
| 7 |
+
from IPython.display import Image as DisplayImage
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from torchvision.io import decode_image
|
| 10 |
+
|
| 11 |
+
import matplotlib.pyplot as plt
|
| 12 |
+
import numpy as np
|
| 13 |
+
|
| 14 |
+
from torchvision.transforms.functional import to_pil_image
|
| 15 |
+
from IPython.display import display
|
| 16 |
+
import gradio as gr
|
| 17 |
+
|
| 18 |
+
bb_model_weights = tv_models.detection.FasterRCNN_ResNet50_FPN_Weights.DEFAULT
|
| 19 |
+
model = tv_models.detection.fasterrcnn_resnet50_fpn(weights=bb_model_weights).eval()
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def detect_and_draw_bboxes(model, image_path, object_indices, labels, bbox_colors, threshold, bbox_width=5):
|
| 23 |
+
|
| 24 |
+
# Open and transform the image, and prepare the result tensor
|
| 25 |
+
pil_image = Image.open(image_path).convert("RGB")
|
| 26 |
+
transform_to_tensor = transforms.Compose([transforms.ToTensor()])
|
| 27 |
+
tensor_image_batch = transform_to_tensor(pil_image).unsqueeze(0)
|
| 28 |
+
result_image_tensor = (tensor_image_batch.squeeze(0) * 255).byte()
|
| 29 |
+
|
| 30 |
+
# Perform inference to get predictions for all possible objects
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
prediction = model(tensor_image_batch)[0]
|
| 33 |
+
|
| 34 |
+
# Initialize lists to collect all boxes, labels, and colors that meet the criteria
|
| 35 |
+
all_boxes_to_draw = []
|
| 36 |
+
all_labels_to_draw = []
|
| 37 |
+
all_colors_to_draw = []
|
| 38 |
+
|
| 39 |
+
# Loop through each target class to find its boxes
|
| 40 |
+
for index, label, color in zip(object_indices, labels, bbox_colors):
|
| 41 |
+
# Filter predictions for the current class index and confidence threshold
|
| 42 |
+
class_mask = (prediction['labels'] == index) & (prediction['scores'] > threshold)
|
| 43 |
+
|
| 44 |
+
# Get the boxes for the current class
|
| 45 |
+
boxes_for_this_class = prediction['boxes'][class_mask]
|
| 46 |
+
|
| 47 |
+
if boxes_for_this_class.nelement() > 0:
|
| 48 |
+
# Add the found boxes to our master list
|
| 49 |
+
all_boxes_to_draw.extend(boxes_for_this_class.tolist())
|
| 50 |
+
# Create and add corresponding labels and colors
|
| 51 |
+
all_labels_to_draw.extend([label] * len(boxes_for_this_class))
|
| 52 |
+
all_colors_to_draw.extend([color] * len(boxes_for_this_class))
|
| 53 |
+
|
| 54 |
+
# After checking all classes, draw all collected boxes at once if any were found
|
| 55 |
+
if all_boxes_to_draw:
|
| 56 |
+
result_image_tensor = vutils.draw_bounding_boxes(
|
| 57 |
+
result_image_tensor,
|
| 58 |
+
torch.tensor(all_boxes_to_draw),
|
| 59 |
+
labels=all_labels_to_draw,
|
| 60 |
+
colors=all_colors_to_draw,
|
| 61 |
+
width=bbox_width
|
| 62 |
+
)
|
| 63 |
+
else:
|
| 64 |
+
# If the list of boxes to draw is empty, print this information.
|
| 65 |
+
print(f"No objects from the list {labels} were found with a confidence score above {threshold}.\n")
|
| 66 |
+
|
| 67 |
+
return result_image_tensor
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def run_detection(image_path):
|
| 71 |
+
result_image_tensor = detect_and_draw_bboxes(
|
| 72 |
+
model=model,
|
| 73 |
+
image_path=image_path,
|
| 74 |
+
object_indices=[3, 10],
|
| 75 |
+
labels=['car', 'traffic light'],
|
| 76 |
+
bbox_colors=['red', 'blue'],
|
| 77 |
+
threshold=0.7,
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
return to_pil_image(result_image_tensor)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
demo = gr.Interface(
|
| 86 |
+
|
| 87 |
+
fn=run_detection,
|
| 88 |
+
inputs=gr.Image(type="filepath", label="Upload Image"),
|
| 89 |
+
outputs=gr.Image(type="pil",label="Output Image"),
|
| 90 |
+
|
| 91 |
+
title="FasterRCNN_ResNet50_FPN model detects 91 classes. For demo purpose, it's been adjusted to only detect car and traffic light"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
demo.launch()
|