Spaces:
Running
on
Zero
Running
on
Zero
Uploading Trashify Box Detector Demo from the videos!
Browse files- .gitattributes +2 -0
- README.md +30 -6
- app.py +178 -0
- requirements.txt +4 -0
- trashify_examples/trashify_example_1.jpeg +0 -0
- trashify_examples/trashify_example_2.jpeg +3 -0
- trashify_examples/trashify_example_3.jpeg +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
trashify_examples/trashify_example_2.jpeg filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
trashify_examples/trashify_example_3.jpeg filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,12 +1,36 @@
|
|
| 1 |
---
|
| 2 |
-
title: Trashify Demo V4
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Trashify Demo V4 🚮
|
| 3 |
+
emoji: 🗑️
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.37.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# 🚮 Trashify Object Detector V4
|
| 14 |
+
|
| 15 |
+
Object detection demo to detect `trash`, `bin`, `hand`, `trash_arm`, `not_trash`, `not_bin`, `not_hand`.
|
| 16 |
+
|
| 17 |
+
Used as example for encouraging people to cleanup their local area.
|
| 18 |
+
|
| 19 |
+
If `trash`, `hand`, `bin` all detected = +1 point.
|
| 20 |
+
|
| 21 |
+
## Dataset
|
| 22 |
+
|
| 23 |
+
All Trashify models are trained on a custom hand-labelled dataset of people picking up trash and placing it in a bin.
|
| 24 |
+
|
| 25 |
+
The dataset can be found on Hugging Face as [`mrdbourke/trashify_manual_labelled_images`](https://huggingface.co/datasets/mrdbourke/trashify_manual_labelled_images).
|
| 26 |
+
|
| 27 |
+
## Demos
|
| 28 |
+
|
| 29 |
+
* [V1](https://huggingface.co/spaces/mrdbourke/trashify_demo_v1) = Fine-tuned [Conditional DETR](https://huggingface.co/docs/transformers/en/model_doc/conditional_detr) model trained *without* data augmentation.
|
| 30 |
+
* [V2](https://huggingface.co/spaces/mrdbourke/trashify_demo_v2) = Fine-tuned Conditional DETR model trained *with* data augmentation.
|
| 31 |
+
* [V3](https://huggingface.co/spaces/mrdbourke/trashify_demo_v3) = Fine-tuned Conditional DETR model trained *with* data augmentation (same as V2) with an NMS (Non Maximum Suppression) post-processing step.
|
| 32 |
+
* [V4](https://huggingface.co/spaces/mrdbourke/trashify_demo_v4) = Fine-tuned [RT-DETRv2](https://huggingface.co/docs/transformers/main/en/model_doc/rt_detr_v2) model trained *without* data augmentation or NMS post-processing (current best mAP).
|
| 33 |
+
|
| 34 |
+
## Learn more
|
| 35 |
+
|
| 36 |
+
See the full end-to-end code of how this demo was built at [learnhuggingface.com](https://www.learnhuggingface.com/notebooks/hugging_face_object_detection_tutorial).
|
app.py
ADDED
|
@@ -0,0 +1,178 @@
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# 1. Import the required dependencies
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
+
# import spaces # for GPU usage
|
| 6 |
+
|
| 7 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 8 |
+
|
| 9 |
+
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
| 10 |
+
|
| 11 |
+
# Model path = mrdbourke/rt_detrv2_finetuned_trashify_box_detector_v1
|
| 12 |
+
|
| 13 |
+
# 2. Setup preprocessing and model functions - mrdbourke/rt_detrv2_finetuned_trashify_box_detector_v1
|
| 14 |
+
model_save_path = "mrdbourke/rt_detrv2_finetuned_trashify_box_detector_v1"
|
| 15 |
+
|
| 16 |
+
# Load image processor
|
| 17 |
+
image_processor = AutoImageProcessor.from_pretrained(model_save_path)
|
| 18 |
+
|
| 19 |
+
# Default size to 640x640 for simplicity, also handles strange shaped images
|
| 20 |
+
image_processor.size = {"height": 640,
|
| 21 |
+
"width": 640}
|
| 22 |
+
|
| 23 |
+
# Load the model
|
| 24 |
+
model = AutoModelForObjectDetection.from_pretrained(model_save_path)
|
| 25 |
+
|
| 26 |
+
# Setup the target device (use GPU if it's accessible)
|
| 27 |
+
# Note: if you want to use a GPU in your Space, you can use ZeroGPU: https://huggingface.co/docs/hub/en/spaces-zerogpu
|
| 28 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 29 |
+
model = model.to(device)
|
| 30 |
+
|
| 31 |
+
# Get the id2label dictionary from the model
|
| 32 |
+
id2label = model.config.id2label
|
| 33 |
+
|
| 34 |
+
# Setup a color dictionary for pretty drawings
|
| 35 |
+
color_dict = {
|
| 36 |
+
"bin": "green",
|
| 37 |
+
"trash": "blue",
|
| 38 |
+
"hand": "purple",
|
| 39 |
+
"trash_arm": "yellow",
|
| 40 |
+
"not_trash": "red",
|
| 41 |
+
"not_bin": "red",
|
| 42 |
+
"not_hand": "red"
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
# 3. Create a function predict_on_image
|
| 46 |
+
# Use a GPU on a target function
|
| 47 |
+
# @spaces.GPU
|
| 48 |
+
def predict_on_image(image, conf_threshold):
|
| 49 |
+
model.eval()
|
| 50 |
+
|
| 51 |
+
# Make a prediction on target image
|
| 52 |
+
with torch.no_grad():
|
| 53 |
+
inputs = image_processor(images=[image],
|
| 54 |
+
return_tensors="pt")
|
| 55 |
+
model_outputs = model(**inputs.to(device))
|
| 56 |
+
|
| 57 |
+
# Get original size of image
|
| 58 |
+
# PIL.Image.size = width, height
|
| 59 |
+
# But post_process_object_detection requires height, width
|
| 60 |
+
target_sizes = torch.tensor([[image.size[1], image.size[0]]]) # -> [batch_size, height, width]
|
| 61 |
+
|
| 62 |
+
print(target_sizes)
|
| 63 |
+
|
| 64 |
+
# Post process the raw outputs from the model
|
| 65 |
+
results = image_processor.post_process_object_detection(model_outputs,
|
| 66 |
+
threshold=conf_threshold,
|
| 67 |
+
target_sizes=target_sizes)[0]
|
| 68 |
+
|
| 69 |
+
# Return all data items/objects to the CPU if they aren't already there
|
| 70 |
+
for key, value in results.items():
|
| 71 |
+
try:
|
| 72 |
+
results[key] = value.item().cpu() # can't get scalars as .item() so add try/except block
|
| 73 |
+
except:
|
| 74 |
+
results[key] = value.cpu()
|
| 75 |
+
|
| 76 |
+
### 4. Draw the predictions on the target image ###
|
| 77 |
+
draw = ImageDraw.Draw(image)
|
| 78 |
+
|
| 79 |
+
# Get a font to write on our image
|
| 80 |
+
font = ImageFont.load_default(size=20)
|
| 81 |
+
|
| 82 |
+
# Get a list of the detect class names
|
| 83 |
+
detected_class_names_text_labels = []
|
| 84 |
+
|
| 85 |
+
# Iterate through the predictions of the model and draw them on the target image
|
| 86 |
+
for box, score, label in zip(results["boxes"], results["scores"], results["labels"]):
|
| 87 |
+
# Create the coordinates
|
| 88 |
+
x, y, x2, y2 = tuple(box.tolist()) # XYXY
|
| 89 |
+
|
| 90 |
+
# Get the text-based label
|
| 91 |
+
label_name = id2label[label.item()]
|
| 92 |
+
targ_color = color_dict[label_name]
|
| 93 |
+
detected_class_names_text_labels.append(label_name)
|
| 94 |
+
|
| 95 |
+
# Draw the rectangle
|
| 96 |
+
draw.rectangle(xy=(x, y, x2, y2),
|
| 97 |
+
outline=targ_color,
|
| 98 |
+
width=3)
|
| 99 |
+
|
| 100 |
+
# Create the text to display on the box
|
| 101 |
+
text_string_to_show = f"{label_name} ({round(score.item(), 4)})"
|
| 102 |
+
|
| 103 |
+
# Draw the text on the image
|
| 104 |
+
draw.text(xy=(x, y),
|
| 105 |
+
text=text_string_to_show,
|
| 106 |
+
fill="white",
|
| 107 |
+
font=font)
|
| 108 |
+
|
| 109 |
+
# Remove the draw each time to make sure it doesn't get caught in memory
|
| 110 |
+
del draw
|
| 111 |
+
|
| 112 |
+
### 5. Create logic for outputting information message
|
| 113 |
+
|
| 114 |
+
# Setup set of target items to discover
|
| 115 |
+
target_items = {"trash", "bin", "hand"}
|
| 116 |
+
detected_items = set(detected_class_names_text_labels)
|
| 117 |
+
|
| 118 |
+
# If no items detected or bin, trash, hand not in detected items, return notification
|
| 119 |
+
if not detected_items & target_items:
|
| 120 |
+
return_string = (
|
| 121 |
+
f"No trash, bin or hand detected at confidence threshold {conf_threshold}. "
|
| 122 |
+
"Try another image or lowering the confidence threshold."
|
| 123 |
+
)
|
| 124 |
+
print(return_string)
|
| 125 |
+
return image, return_string
|
| 126 |
+
|
| 127 |
+
# If there are items missing, output what's missing for +1 point
|
| 128 |
+
missing_items = target_items - detected_items
|
| 129 |
+
if missing_items:
|
| 130 |
+
return_string = (
|
| 131 |
+
f"Detected the following items: {sorted(detected_items & target_items)}. "
|
| 132 |
+
f"Missing the following: {missing_items}. "
|
| 133 |
+
"In order to get +1 point, all target items must be detected."
|
| 134 |
+
)
|
| 135 |
+
print(return_string)
|
| 136 |
+
return image, return_string
|
| 137 |
+
|
| 138 |
+
# Final case, all items are detected
|
| 139 |
+
return_string = f"+1! Found the following items: {sorted(detected_items)}, thank you for cleaning up your local area!"
|
| 140 |
+
print(return_string)
|
| 141 |
+
return image, return_string
|
| 142 |
+
|
| 143 |
+
### 6. Setup the demo application to take in image/conf threshold, pass it through our function, show the output image/text
|
| 144 |
+
|
| 145 |
+
description = """
|
| 146 |
+
Help clean up your local area! Upload an image and get +1 if there is all of the following items detected: trash, bin, hand.
|
| 147 |
+
|
| 148 |
+
Model is a fine-tuned version of [RT-DETRv2](https://huggingface.co/docs/transformers/main/en/model_doc/rt_detr_v2#transformers.RTDetrV2Config) on the [Trashify dataset](https://huggingface.co/datasets/mrdbourke/trashify_manual_labelled_images).
|
| 149 |
+
|
| 150 |
+
See the full data loading and training code on [learnhuggingface.com](https://www.learnhuggingface.com/notebooks/hugging_face_object_detection_tutorial).
|
| 151 |
+
|
| 152 |
+
This version is v4 because the first three versions were using a different model and did not perform as well, see the [README](https://huggingface.co/spaces/mrdbourke/trashify_demo_v4/blob/main/README.md) for more.
|
| 153 |
+
"""
|
| 154 |
+
|
| 155 |
+
# Create the Gradio interface
|
| 156 |
+
demo = gr.Interface(
|
| 157 |
+
fn=predict_on_image,
|
| 158 |
+
inputs=[
|
| 159 |
+
gr.Image(type="pil", label="Target Input Image"),
|
| 160 |
+
gr.Slider(minimum=0, maximum=1, value=0.3, label="Confidence Threshold (set higher for more confident boxes)")
|
| 161 |
+
],
|
| 162 |
+
outputs=[
|
| 163 |
+
gr.Image(type="pil", label="Target Image Output"),
|
| 164 |
+
gr.Text(label="Text Output")
|
| 165 |
+
],
|
| 166 |
+
description=description,
|
| 167 |
+
title="🚮 Trashify Object Detection Demo V4 - Video",
|
| 168 |
+
examples=[
|
| 169 |
+
["trashify_examples/trashify_example_1.jpeg", 0.3],
|
| 170 |
+
["trashify_examples/trashify_example_2.jpeg", 0.3],
|
| 171 |
+
["trashify_examples/trashify_example_3.jpeg", 0.3],
|
| 172 |
+
],
|
| 173 |
+
cache_examples=True
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
# Launch demo
|
| 177 |
+
# demo.launch(debug=True) # run with debug=True to see errors in Google Colab
|
| 178 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
timm
|
| 2 |
+
gradio
|
| 3 |
+
torch
|
| 4 |
+
transformers
|
trashify_examples/trashify_example_1.jpeg
ADDED
|
trashify_examples/trashify_example_2.jpeg
ADDED
|
Git LFS Details
|
trashify_examples/trashify_example_3.jpeg
ADDED
|
Git LFS Details
|