Spaces:
Runtime error
Runtime error
Merge pull request #1 from onuralpszr/gradio-sv-update
Browse filesWIP - feat: β¨ new annotators from supervision and title,description updates
- .github/workflows/hf-space-sync.yml +20 -0
- .gitignore +13 -0
- .pre-commit-config.yaml +54 -0
- app.py +339 -113
- aerodefence.jpg β assets/aerodefence.jpg +0 -0
- city.jpg β assets/city.jpg +0 -0
- household.jpg β assets/household.jpg +0 -0
- industry.jpg β assets/industry.jpg +0 -0
- retail.jpg β assets/retail.jpg +0 -0
- pyproject.toml +109 -0
- requirements.txt +1 -0
.github/workflows/hf-space-sync.yml
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: Sync to Hugging Face hub π€
|
| 2 |
+
on:
|
| 3 |
+
push:
|
| 4 |
+
branches: [main]
|
| 5 |
+
|
| 6 |
+
workflow_dispatch:
|
| 7 |
+
|
| 8 |
+
jobs:
|
| 9 |
+
sync-to-hub:
|
| 10 |
+
runs-on: ubuntu-latest
|
| 11 |
+
steps:
|
| 12 |
+
- uses: actions/checkout@v4
|
| 13 |
+
with:
|
| 14 |
+
fetch-depth: 0
|
| 15 |
+
lfs: true
|
| 16 |
+
- name: Push to HF Space π
|
| 17 |
+
env:
|
| 18 |
+
HF_TOKEN: ${{ secrets.HF_TOKEN }}
|
| 19 |
+
HF_USERNAME: ${{ secrets.HF_USERNAME }}
|
| 20 |
+
run: git push https://$HF_USERNAME:$HF_TOKEN@huggingface.co/spaces/Roboflow/Annotators main
|
.gitignore
CHANGED
|
@@ -158,3 +158,16 @@ cython_debug/
|
|
| 158 |
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 159 |
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 160 |
#.idea/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 159 |
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 160 |
#.idea/
|
| 161 |
+
|
| 162 |
+
# Mac
|
| 163 |
+
.DS_Store
|
| 164 |
+
.AppleDouble
|
| 165 |
+
.LSOverride
|
| 166 |
+
|
| 167 |
+
# YoloV8 files
|
| 168 |
+
yolov8n-seg.pt
|
| 169 |
+
yolov8s-seg.pt
|
| 170 |
+
yolov8m-seg.pt
|
| 171 |
+
yolov8l-seg.pt
|
| 172 |
+
yolov8x-seg.pt
|
| 173 |
+
yolov8s.pt
|
.pre-commit-config.yaml
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
ci:
|
| 3 |
+
autofix_prs: true
|
| 4 |
+
autoupdate_schedule: weekly
|
| 5 |
+
autofix_commit_msg: "fix(pre_commit): π¨ auto format pre-commit hooks"
|
| 6 |
+
autoupdate_commit_msg: "chore(pre_commit): β¬ pre_commit autoupdate"
|
| 7 |
+
|
| 8 |
+
repos:
|
| 9 |
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
| 10 |
+
rev: v4.5.0
|
| 11 |
+
hooks:
|
| 12 |
+
- id: end-of-file-fixer
|
| 13 |
+
- id: trailing-whitespace
|
| 14 |
+
exclude: test/.*\.py
|
| 15 |
+
- id: check-yaml
|
| 16 |
+
- id: check-docstring-first
|
| 17 |
+
- id: check-executables-have-shebangs
|
| 18 |
+
- id: check-toml
|
| 19 |
+
- id: check-case-conflict
|
| 20 |
+
- id: check-added-large-files
|
| 21 |
+
- id: detect-private-key
|
| 22 |
+
- id: forbid-new-submodules
|
| 23 |
+
- id: pretty-format-json
|
| 24 |
+
exclude: demo.ipynb
|
| 25 |
+
args: ['--autofix', '--no-sort-keys', '--indent=4']
|
| 26 |
+
- id: end-of-file-fixer
|
| 27 |
+
- id: mixed-line-ending
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
- repo: https://github.com/PyCQA/bandit
|
| 32 |
+
rev: '1.7.6'
|
| 33 |
+
hooks:
|
| 34 |
+
- id: bandit
|
| 35 |
+
args: ["-c", "pyproject.toml"]
|
| 36 |
+
additional_dependencies: ["bandit[toml]"]
|
| 37 |
+
|
| 38 |
+
- repo: https://github.com/pycqa/isort
|
| 39 |
+
rev: 5.13.0
|
| 40 |
+
hooks:
|
| 41 |
+
- id: isort
|
| 42 |
+
name: isort (python)
|
| 43 |
+
- id: isort
|
| 44 |
+
name: isort (pyi)
|
| 45 |
+
types: [pyi]
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
| 49 |
+
rev: v0.1.7
|
| 50 |
+
hooks:
|
| 51 |
+
- id: ruff
|
| 52 |
+
args: [--fix, --exit-non-zero-on-fix]
|
| 53 |
+
- id: ruff-format
|
| 54 |
+
types_or: [ python, pyi, jupyter ]
|
app.py
CHANGED
|
@@ -1,160 +1,386 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import supervision as sv
|
|
|
|
|
|
|
|
|
|
| 3 |
from ultralytics import YOLO
|
| 4 |
-
import os #added for cache_examples
|
| 5 |
-
from PIL import Image, ImageColor
|
| 6 |
-
import numpy as np
|
| 7 |
-
|
| 8 |
-
def load_model(img):
|
| 9 |
-
# Load model, get results and return detections/labels
|
| 10 |
-
model = YOLO('yolov8s-seg.pt')
|
| 11 |
-
result = model(img, verbose=False, imgsz=1280)[0]
|
| 12 |
-
detections = sv.Detections.from_ultralytics(result)
|
| 13 |
-
labels = [
|
| 14 |
-
f"{model.model.names[class_id]} {confidence:.2f}"
|
| 15 |
-
for class_id, confidence in zip(detections.class_id, detections.confidence)
|
| 16 |
-
]
|
| 17 |
-
|
| 18 |
-
return detections, labels
|
| 19 |
-
|
| 20 |
-
def calculate_crop_dim(a,b):
|
| 21 |
-
#Calculates the crop dimensions of the image resultant
|
| 22 |
-
if a>b:
|
| 23 |
-
width= a
|
| 24 |
-
height = a
|
| 25 |
-
else:
|
| 26 |
-
width = b
|
| 27 |
-
height = b
|
| 28 |
-
|
| 29 |
-
return width, height
|
| 30 |
-
|
| 31 |
-
def annotator(img,annotators,colorbb,colormask,colorellipse,colorbc,colorcir,colorlabel):
|
| 32 |
-
|
| 33 |
-
"""
|
| 34 |
-
Function that changes the color of annotators
|
| 35 |
-
Args:
|
| 36 |
-
annotators: Icon whose color needs to be changed.
|
| 37 |
-
color: Chosen color with which to edit the input icon in Hex.
|
| 38 |
-
img: Input image is numpy matrix in BGR.
|
| 39 |
-
Returns:
|
| 40 |
-
annotators: annotated image
|
| 41 |
-
"""
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# Apply Blur
|
| 53 |
blur_annotator = sv.BlurAnnotator()
|
| 54 |
-
img = blur_annotator.annotate(img, detections=
|
| 55 |
|
| 56 |
-
if "BoundingBox" in
|
| 57 |
-
# Draw
|
| 58 |
box_annotator = sv.BoundingBoxAnnotator(sv.Color.from_hex(str(colorbb)))
|
| 59 |
-
img = box_annotator.annotate(img, detections=
|
| 60 |
|
| 61 |
-
if "Mask" in
|
| 62 |
# Draw Mask
|
| 63 |
mask_annotator = sv.MaskAnnotator(sv.Color.from_hex(str(colormask)))
|
| 64 |
-
img = mask_annotator.annotate(img, detections=
|
| 65 |
|
| 66 |
-
if "Ellipse" in
|
| 67 |
-
# Draw
|
| 68 |
ellipse_annotator = sv.EllipseAnnotator(sv.Color.from_hex(str(colorellipse)))
|
| 69 |
-
img = ellipse_annotator.annotate(img, detections=
|
| 70 |
|
| 71 |
-
if "BoxCorner" in
|
| 72 |
# Draw Box corner
|
| 73 |
corner_annotator = sv.BoxCornerAnnotator(sv.Color.from_hex(str(colorbc)))
|
| 74 |
-
img = corner_annotator.annotate(img, detections=
|
| 75 |
-
|
| 76 |
|
| 77 |
-
if "Circle" in
|
| 78 |
-
# Draw
|
| 79 |
circle_annotator = sv.CircleAnnotator(sv.Color.from_hex(str(colorcir)))
|
| 80 |
-
img = circle_annotator.annotate(img, detections=
|
| 81 |
|
| 82 |
-
if "Label" in
|
| 83 |
# Draw Label
|
| 84 |
label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER)
|
| 85 |
label_annotator = sv.LabelAnnotator(sv.Color.from_hex(str(colorlabel)))
|
| 86 |
-
img = label_annotator.annotate(
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
res_img = Image.fromarray(img)
|
| 92 |
-
print(type(res_img))
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
y=0
|
| 96 |
-
|
| 97 |
|
| 98 |
-
print("size of the pil im=", res_img.size)
|
| 99 |
-
(v1,v2) = res_img.size
|
| 100 |
width, height = calculate_crop_dim(v1, v2)
|
| 101 |
-
print(width, height)
|
| 102 |
my_img = np.array(res_img)
|
| 103 |
|
| 104 |
-
crop_img = my_img[y:y+height, x:x+width]
|
| 105 |
-
print(type(crop_img))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
return crop_img[...,::-1].copy() # BGR to RGB using numpy
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
with gr.Column():
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
with gr.Column():
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
with gr.Column():
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
| 130 |
with gr.Column():
|
| 131 |
-
|
|
|
|
| 132 |
with gr.Column():
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
with gr.Row():
|
| 136 |
-
with gr.Column():
|
| 137 |
-
with gr.Tab("Input image"):
|
| 138 |
-
image_input = gr.Image(type="numpy", show_label=False)
|
| 139 |
-
with gr.Column():
|
| 140 |
-
with gr.Tab("Result image"):
|
| 141 |
-
image_output = gr.Image(type="numpy", show_label=False)
|
| 142 |
image_button = gr.Button(value="Annotate it!", variant="primary")
|
| 143 |
|
| 144 |
-
image_button.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
gr.Markdown("## Image Examples")
|
| 147 |
gr.Examples(
|
| 148 |
-
examples=[
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
| 153 |
inputs=image_input,
|
| 154 |
outputs=image_output,
|
| 155 |
fn=annotator,
|
| 156 |
cache_examples=False,
|
| 157 |
)
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os # added for cache_examples
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
+
import numpy as np
|
| 6 |
import supervision as sv
|
| 7 |
+
from gradio import ColorPicker
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from torch import cuda, device
|
| 10 |
from ultralytics import YOLO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Use GPU if available
|
| 13 |
+
if cuda.is_available():
|
| 14 |
+
device = device("cuda")
|
| 15 |
+
else:
|
| 16 |
+
device = device("cpu")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
TITLE = """<h1 align="center">Supervision Annotator Playground π</h1>"""
|
| 20 |
+
SUBTITLE = """<h2 align="center">Experiment with Supervision Annotators</h2>"""
|
| 21 |
+
BANNER = """
|
| 22 |
+
<div align="center">
|
| 23 |
+
<p>
|
| 24 |
+
<a align="center" href="https://supervision.roboflow.com/" target="_blank">
|
| 25 |
+
<img style="max-width: 50%; height: auto; margin: 0 auto; display: block; padding: 20"
|
| 26 |
+
src="https://media.roboflow.com/open-source/supervision/rf-supervision-banner.png?updatedAt=1678995927529">
|
| 27 |
+
</a>
|
| 28 |
+
</p>
|
| 29 |
+
</div>
|
| 30 |
+
""" # noqa: E501 title/docs
|
| 31 |
+
DESC = """
|
| 32 |
+
<div style="text-align: center; display: flex; justify-content: center; align-items: center;">
|
| 33 |
+
<a href="https://huggingface.co/spaces/Roboflow/Annotators?duplicate=true">
|
| 34 |
+
<img src="https://bit.ly/3gLdBN6" alt="Duplicate Space" style="margin-right: 10px;">
|
| 35 |
+
</a>
|
| 36 |
+
<a href="https://github.com/roboflow/supervision">
|
| 37 |
+
<img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/roboflow/supervision"
|
| 38 |
+
style="margin-right: 10px;">
|
| 39 |
+
</a>
|
| 40 |
+
<a href="https://colab.research.google.com/github/roboflow/supervision/blob/main/demo.ipynb">
|
| 41 |
+
<img alt="Open In Colab" src="https://colab.research.google.com/assets/colab-badge.svg"
|
| 42 |
+
style="margin-right: 10px;">
|
| 43 |
+
</a>
|
| 44 |
+
</div>
|
| 45 |
+
""" # noqa: E501 title/docs
|
| 46 |
+
|
| 47 |
+
last_detections = sv.Detections.empty()
|
| 48 |
+
last_labels: list[str] = []
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def load_model(img, model: str | Path = "yolov8s-seg.pt"):
|
| 52 |
+
# Load model, get results and return detections/labels
|
| 53 |
+
model = YOLO(model=model)
|
| 54 |
+
result = model(img, verbose=False, imgsz=1280)[0]
|
| 55 |
+
detections = sv.Detections.from_ultralytics(result)
|
| 56 |
+
labels = [
|
| 57 |
+
f"{model.model.names[class_id]} {confidence:.2f}"
|
| 58 |
+
for class_id, confidence in zip(detections.class_id, detections.confidence)
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
return detections, labels
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def calculate_crop_dim(a, b):
|
| 65 |
+
# Calculates the crop dimensions of the image resultant
|
| 66 |
+
if a > b:
|
| 67 |
+
width = a
|
| 68 |
+
height = a
|
| 69 |
+
else:
|
| 70 |
+
width = b
|
| 71 |
+
height = b
|
| 72 |
+
|
| 73 |
+
return width, height
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def annotators(
|
| 77 |
+
img,
|
| 78 |
+
last_detections,
|
| 79 |
+
annotators_list,
|
| 80 |
+
last_labels,
|
| 81 |
+
colorbb,
|
| 82 |
+
colormask,
|
| 83 |
+
colorellipse,
|
| 84 |
+
colorbc,
|
| 85 |
+
colorcir,
|
| 86 |
+
colorlabel,
|
| 87 |
+
colorhalo,
|
| 88 |
+
colortri,
|
| 89 |
+
colordot,
|
| 90 |
+
) -> np.ndarray:
|
| 91 |
+
if last_detections == sv.Detections.empty():
|
| 92 |
+
gr.Warning("Detection is empty please add image and annotate first")
|
| 93 |
+
return np.zeros()
|
| 94 |
+
|
| 95 |
+
if "Blur" in annotators_list:
|
| 96 |
# Apply Blur
|
| 97 |
blur_annotator = sv.BlurAnnotator()
|
| 98 |
+
img = blur_annotator.annotate(img, detections=last_detections)
|
| 99 |
|
| 100 |
+
if "BoundingBox" in annotators_list:
|
| 101 |
+
# Draw Boundingbox
|
| 102 |
box_annotator = sv.BoundingBoxAnnotator(sv.Color.from_hex(str(colorbb)))
|
| 103 |
+
img = box_annotator.annotate(img, detections=last_detections)
|
| 104 |
|
| 105 |
+
if "Mask" in annotators_list:
|
| 106 |
# Draw Mask
|
| 107 |
mask_annotator = sv.MaskAnnotator(sv.Color.from_hex(str(colormask)))
|
| 108 |
+
img = mask_annotator.annotate(img, detections=last_detections)
|
| 109 |
|
| 110 |
+
if "Ellipse" in annotators_list:
|
| 111 |
+
# Draw Ellipse
|
| 112 |
ellipse_annotator = sv.EllipseAnnotator(sv.Color.from_hex(str(colorellipse)))
|
| 113 |
+
img = ellipse_annotator.annotate(img, detections=last_detections)
|
| 114 |
|
| 115 |
+
if "BoxCorner" in annotators_list:
|
| 116 |
# Draw Box corner
|
| 117 |
corner_annotator = sv.BoxCornerAnnotator(sv.Color.from_hex(str(colorbc)))
|
| 118 |
+
img = corner_annotator.annotate(img, detections=last_detections)
|
|
|
|
| 119 |
|
| 120 |
+
if "Circle" in annotators_list:
|
| 121 |
+
# Draw Circle
|
| 122 |
circle_annotator = sv.CircleAnnotator(sv.Color.from_hex(str(colorcir)))
|
| 123 |
+
img = circle_annotator.annotate(img, detections=last_detections)
|
| 124 |
|
| 125 |
+
if "Label" in annotators_list:
|
| 126 |
# Draw Label
|
| 127 |
label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER)
|
| 128 |
label_annotator = sv.LabelAnnotator(sv.Color.from_hex(str(colorlabel)))
|
| 129 |
+
img = label_annotator.annotate(
|
| 130 |
+
img, detections=last_detections, labels=last_labels
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
if "Pixelate" in annotators_list:
|
| 134 |
+
# Apply PixelateAnnotator
|
| 135 |
+
pixelate_annotator = sv.PixelateAnnotator()
|
| 136 |
+
img = pixelate_annotator.annotate(img, detections=last_detections)
|
| 137 |
+
|
| 138 |
+
if "Halo" in annotators_list:
|
| 139 |
+
# Draw HaloAnnotator
|
| 140 |
+
halo_annotator = sv.HaloAnnotator(sv.Color.from_hex(str(colorhalo)))
|
| 141 |
+
img = halo_annotator.annotate(img, detections=last_detections)
|
| 142 |
+
|
| 143 |
+
if "HeatMap" in annotators_list:
|
| 144 |
+
# Draw HeatMapAnnotator
|
| 145 |
+
heatmap_annotator = sv.HeatMapAnnotator()
|
| 146 |
+
img = heatmap_annotator.annotate(img, detections=last_detections)
|
| 147 |
+
|
| 148 |
+
if "Dot" in annotators_list:
|
| 149 |
+
# Dot DotAnnotator
|
| 150 |
+
dot_annotator = sv.DotAnnotator(sv.Color.from_hex(str(colordot)))
|
| 151 |
+
img = dot_annotator.annotate(img, detections=last_detections)
|
| 152 |
+
|
| 153 |
+
if "Triangle" in annotators_list:
|
| 154 |
+
# Draw TriangleAnnotator
|
| 155 |
+
tri_annotator = sv.TriangleAnnotator(sv.Color.from_hex(str(colortri)))
|
| 156 |
+
img = tri_annotator.annotate(img, detections=last_detections)
|
| 157 |
+
|
| 158 |
+
# crop image for the largest possible square
|
| 159 |
res_img = Image.fromarray(img)
|
| 160 |
+
# print(type(res_img))
|
| 161 |
+
x = 0
|
| 162 |
+
y = 0
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
# print("size of the pil im=", res_img.size)
|
| 165 |
+
(v1, v2) = res_img.size
|
| 166 |
width, height = calculate_crop_dim(v1, v2)
|
| 167 |
+
# print(width, height)
|
| 168 |
my_img = np.array(res_img)
|
| 169 |
|
| 170 |
+
crop_img = my_img[y : y + height, x : x + width]
|
| 171 |
+
# print(type(crop_img))
|
| 172 |
+
|
| 173 |
+
return crop_img[..., ::-1].copy() # BGR to RGB using numpy
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def annotator(
|
| 177 |
+
img,
|
| 178 |
+
model,
|
| 179 |
+
annotators_list,
|
| 180 |
+
colorbb,
|
| 181 |
+
colormask,
|
| 182 |
+
colorellipse,
|
| 183 |
+
colorbc,
|
| 184 |
+
colorcir,
|
| 185 |
+
colorlabel,
|
| 186 |
+
colorhalo,
|
| 187 |
+
colortri,
|
| 188 |
+
colordot,
|
| 189 |
+
progress=gr.Progress(track_tqdm=True),
|
| 190 |
+
) -> np.ndarray:
|
| 191 |
+
"""
|
| 192 |
+
Function that changes the color of annotators
|
| 193 |
+
Args:
|
| 194 |
+
annotators: Icon whose color needs to be changed.
|
| 195 |
+
color: Chosen color with which to edit the input icon in Hex.
|
| 196 |
+
img: Input image is numpy matrix in BGR.
|
| 197 |
+
Returns:
|
| 198 |
+
annotators: annotated image
|
| 199 |
+
"""
|
| 200 |
+
|
| 201 |
+
img = img[..., ::-1].copy() # BGR to RGB using numpy
|
| 202 |
+
|
| 203 |
+
detections, labels = load_model(img, model)
|
| 204 |
+
last_detections = detections
|
| 205 |
+
last_labels = labels
|
| 206 |
+
|
| 207 |
+
return annotators(
|
| 208 |
+
img,
|
| 209 |
+
last_detections,
|
| 210 |
+
annotators_list,
|
| 211 |
+
last_labels,
|
| 212 |
+
colorbb,
|
| 213 |
+
colormask,
|
| 214 |
+
colorellipse,
|
| 215 |
+
colorbc,
|
| 216 |
+
colorcir,
|
| 217 |
+
colorlabel,
|
| 218 |
+
colorhalo,
|
| 219 |
+
colortri,
|
| 220 |
+
colordot,
|
| 221 |
+
)
|
| 222 |
|
|
|
|
| 223 |
|
| 224 |
+
purple_theme = theme = gr.themes.Soft(primary_hue=gr.themes.colors.purple).set(
|
| 225 |
+
button_primary_background_fill="*primary_600",
|
| 226 |
+
button_primary_background_fill_hover="*primary_700",
|
| 227 |
+
checkbox_label_background_fill_selected="*primary_600",
|
| 228 |
+
checkbox_background_color_selected="*primary_400",
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
with gr.Blocks(theme=purple_theme) as app:
|
| 232 |
+
gr.HTML(TITLE)
|
| 233 |
+
gr.HTML(SUBTITLE)
|
| 234 |
+
gr.HTML(BANNER)
|
| 235 |
+
gr.HTML(DESC)
|
| 236 |
+
|
| 237 |
+
models = gr.Dropdown(
|
| 238 |
+
[
|
| 239 |
+
"yolov8n-seg.pt",
|
| 240 |
+
"yolov8s-seg.pt",
|
| 241 |
+
"yolov8m-seg.pt",
|
| 242 |
+
"yolov8l-seg.pt",
|
| 243 |
+
"yolov8x-seg.pt",
|
| 244 |
+
],
|
| 245 |
+
type="value",
|
| 246 |
+
value="yolov8s-seg.pt",
|
| 247 |
+
label="Select Model:",
|
| 248 |
+
)
|
| 249 |
|
| 250 |
+
annotators_list = gr.CheckboxGroup(
|
| 251 |
+
choices=[
|
| 252 |
+
"BoundingBox",
|
| 253 |
+
"Mask",
|
| 254 |
+
"Halo",
|
| 255 |
+
"Ellipse",
|
| 256 |
+
"BoxCorner",
|
| 257 |
+
"Circle",
|
| 258 |
+
"Label",
|
| 259 |
+
"Blur",
|
| 260 |
+
"Pixelate",
|
| 261 |
+
"HeatMap",
|
| 262 |
+
"Dot",
|
| 263 |
+
"Triangle",
|
| 264 |
+
],
|
| 265 |
+
value=["BoundingBox", "Mask"],
|
| 266 |
+
label="Select Annotators:",
|
| 267 |
+
)
|
| 268 |
|
| 269 |
+
gr.Markdown("## Color Picker π¨")
|
| 270 |
+
with gr.Row(variant="panel"):
|
| 271 |
with gr.Column():
|
| 272 |
+
colorbb = gr.ColorPicker(value="#A351FB", label="BoundingBox")
|
| 273 |
+
colormask = gr.ColorPicker(value="#A351FB", label="Mask")
|
| 274 |
+
colorellipse = gr.ColorPicker(value="#A351FB", label="Ellipse")
|
| 275 |
with gr.Column():
|
| 276 |
+
colorbc = gr.ColorPicker(value="#A351FB", label="BoxCorner")
|
| 277 |
+
colorcir = gr.ColorPicker(value="#A351FB", label="Circle")
|
| 278 |
+
colorlabel = gr.ColorPicker(value="#A351FB", label="Label")
|
| 279 |
with gr.Column():
|
| 280 |
+
colorhalo = gr.ColorPicker(value="#A351FB", label="Halo")
|
| 281 |
+
colordot = gr.ColorPicker(value="#A351FB", label="Dot")
|
| 282 |
+
colortri = gr.ColorPicker(value="#A351FB", label="Triangle")
|
| 283 |
+
|
| 284 |
+
with gr.Row():
|
| 285 |
with gr.Column():
|
| 286 |
+
with gr.Tab("Input image"):
|
| 287 |
+
image_input = gr.Image(type="numpy", show_label=False)
|
| 288 |
with gr.Column():
|
| 289 |
+
with gr.Tab("Result image"):
|
| 290 |
+
image_output = gr.Image(type="numpy", show_label=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
image_button = gr.Button(value="Annotate it!", variant="primary")
|
| 292 |
|
| 293 |
+
image_button.click(
|
| 294 |
+
annotator,
|
| 295 |
+
inputs=[
|
| 296 |
+
image_input,
|
| 297 |
+
models,
|
| 298 |
+
annotators_list,
|
| 299 |
+
colorbb,
|
| 300 |
+
colormask,
|
| 301 |
+
colorellipse,
|
| 302 |
+
colorbc,
|
| 303 |
+
colorcir,
|
| 304 |
+
colorlabel,
|
| 305 |
+
colorhalo,
|
| 306 |
+
colortri,
|
| 307 |
+
colordot,
|
| 308 |
+
],
|
| 309 |
+
outputs=image_output,
|
| 310 |
+
)
|
| 311 |
|
| 312 |
+
gr.Markdown("## Image Examples πΌοΈ")
|
| 313 |
gr.Examples(
|
| 314 |
+
examples=[
|
| 315 |
+
os.path.join(os.path.abspath(""), "./assets/city.jpg"),
|
| 316 |
+
os.path.join(os.path.abspath(""), "./assets/household.jpg"),
|
| 317 |
+
os.path.join(os.path.abspath(""), "./assets/industry.jpg"),
|
| 318 |
+
os.path.join(os.path.abspath(""), "./assets/retail.jpg"),
|
| 319 |
+
os.path.join(os.path.abspath(""), "./assets/aerodefence.jpg"),
|
| 320 |
+
],
|
| 321 |
inputs=image_input,
|
| 322 |
outputs=image_output,
|
| 323 |
fn=annotator,
|
| 324 |
cache_examples=False,
|
| 325 |
)
|
| 326 |
|
| 327 |
+
annotators_list.change(
|
| 328 |
+
fn=annotator,
|
| 329 |
+
inputs=[
|
| 330 |
+
image_input,
|
| 331 |
+
models,
|
| 332 |
+
annotators_list,
|
| 333 |
+
colorbb,
|
| 334 |
+
colormask,
|
| 335 |
+
colorellipse,
|
| 336 |
+
colorbc,
|
| 337 |
+
colorcir,
|
| 338 |
+
colorlabel,
|
| 339 |
+
colorhalo,
|
| 340 |
+
colortri,
|
| 341 |
+
colordot,
|
| 342 |
+
],
|
| 343 |
+
outputs=image_output,
|
| 344 |
+
)
|
| 345 |
|
| 346 |
+
def change_color(color: ColorPicker):
|
| 347 |
+
color.change(
|
| 348 |
+
fn=annotator,
|
| 349 |
+
inputs=[
|
| 350 |
+
image_input,
|
| 351 |
+
models,
|
| 352 |
+
annotators_list,
|
| 353 |
+
colorbb,
|
| 354 |
+
colormask,
|
| 355 |
+
colorellipse,
|
| 356 |
+
colorbc,
|
| 357 |
+
colorcir,
|
| 358 |
+
colorlabel,
|
| 359 |
+
colorhalo,
|
| 360 |
+
colortri,
|
| 361 |
+
colordot,
|
| 362 |
+
],
|
| 363 |
+
outputs=image_output,
|
| 364 |
+
)
|
| 365 |
+
|
| 366 |
+
colors = [
|
| 367 |
+
colorbb,
|
| 368 |
+
colormask,
|
| 369 |
+
colorellipse,
|
| 370 |
+
colorbc,
|
| 371 |
+
colorcir,
|
| 372 |
+
colorlabel,
|
| 373 |
+
colorhalo,
|
| 374 |
+
colortri,
|
| 375 |
+
colordot,
|
| 376 |
+
]
|
| 377 |
+
|
| 378 |
+
for color in colors:
|
| 379 |
+
change_color(color)
|
| 380 |
+
|
| 381 |
+
|
| 382 |
+
if __name__ == "__main__":
|
| 383 |
+
print("Starting app...")
|
| 384 |
+
print("Dark theme is available at: http://localhost:7860/?__theme=dark")
|
| 385 |
+
# app.launch(debug=False, server_name="0.0.0.0") # for local network
|
| 386 |
+
app.launch(debug=False)
|
aerodefence.jpg β assets/aerodefence.jpg
RENAMED
|
File without changes
|
city.jpg β assets/city.jpg
RENAMED
|
File without changes
|
household.jpg β assets/household.jpg
RENAMED
|
File without changes
|
industry.jpg β assets/industry.jpg
RENAMED
|
File without changes
|
retail.jpg β assets/retail.jpg
RENAMED
|
File without changes
|
pyproject.toml
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[tool.isort]
|
| 2 |
+
line_length = 88
|
| 3 |
+
profile = "black"
|
| 4 |
+
|
| 5 |
+
[tool.bandit]
|
| 6 |
+
target = ["test", "supervision"]
|
| 7 |
+
tests = ["B201", "B301", "B318", "B314", "B303", "B413", "B412", "B410"]
|
| 8 |
+
|
| 9 |
+
[tool.autoflake]
|
| 10 |
+
check = true
|
| 11 |
+
imports = ["cv2", "supervision"]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
[tool.black]
|
| 15 |
+
target-version = ["py38"]
|
| 16 |
+
line-length = 88
|
| 17 |
+
include = '\.pyi?$'
|
| 18 |
+
exclude = '''
|
| 19 |
+
/(
|
| 20 |
+
\.git
|
| 21 |
+
| \.hg
|
| 22 |
+
| \.mypy_cache
|
| 23 |
+
| \.tox
|
| 24 |
+
| \.venv
|
| 25 |
+
| _build
|
| 26 |
+
| buck-out
|
| 27 |
+
| build
|
| 28 |
+
| dist
|
| 29 |
+
| docs
|
| 30 |
+
)/
|
| 31 |
+
'''
|
| 32 |
+
|
| 33 |
+
[tool.ruff]
|
| 34 |
+
target-version = "py38"
|
| 35 |
+
# Enable pycodestyle (`E`) and Pyflakes (`F`) codes by default.
|
| 36 |
+
select = ["E", "F"]
|
| 37 |
+
ignore = []
|
| 38 |
+
|
| 39 |
+
# Allow autofix for all enabled rules (when `--fix`) is provided.
|
| 40 |
+
fixable = ["A", "B", "C", "D", "E", "F", "G", "I", "N", "Q", "S", "T", "W", "ANN", "ARG", "BLE", "COM", "DJ", "DTZ", "EM", "ERA", "EXE", "FBT", "ICN", "INP", "ISC", "NPY", "PD", "PGH", "PIE", "PL", "PT", "PTH", "PYI", "RET", "RSE", "RUF", "SIM", "SLF", "TCH", "TID", "TRY", "UP", "YTT"]
|
| 41 |
+
unfixable = []
|
| 42 |
+
|
| 43 |
+
# Exclude a variety of commonly ignored directories.
|
| 44 |
+
exclude = [
|
| 45 |
+
".bzr",
|
| 46 |
+
".direnv",
|
| 47 |
+
".eggs",
|
| 48 |
+
".git",
|
| 49 |
+
".git-rewrite",
|
| 50 |
+
".hg",
|
| 51 |
+
".mypy_cache",
|
| 52 |
+
".nox",
|
| 53 |
+
".pants.d",
|
| 54 |
+
".pytype",
|
| 55 |
+
".ruff_cache",
|
| 56 |
+
".svn",
|
| 57 |
+
".tox",
|
| 58 |
+
".venv",
|
| 59 |
+
"__pypackages__",
|
| 60 |
+
"_build",
|
| 61 |
+
"buck-out",
|
| 62 |
+
"build",
|
| 63 |
+
"dist",
|
| 64 |
+
"node_modules",
|
| 65 |
+
"venv",
|
| 66 |
+
"yarn-error.log",
|
| 67 |
+
"yarn.lock",
|
| 68 |
+
"docs",
|
| 69 |
+
]
|
| 70 |
+
|
| 71 |
+
# Same as Black.
|
| 72 |
+
line-length = 88
|
| 73 |
+
indent-width = 4
|
| 74 |
+
|
| 75 |
+
# Allow unused variables when underscore-prefixed.
|
| 76 |
+
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
|
| 77 |
+
|
| 78 |
+
[tool.ruff.flake8-quotes]
|
| 79 |
+
inline-quotes = "double"
|
| 80 |
+
multiline-quotes = "double"
|
| 81 |
+
docstring-quotes = "double"
|
| 82 |
+
|
| 83 |
+
[tool.ruff.pydocstyle]
|
| 84 |
+
convention = "google"
|
| 85 |
+
|
| 86 |
+
[tool.ruff.per-file-ignores]
|
| 87 |
+
"__init__.py" = ["E402","F401"]
|
| 88 |
+
"supervision/assets/list.py" = ["E501"]
|
| 89 |
+
|
| 90 |
+
[tool.ruff.lint.mccabe]
|
| 91 |
+
# Flag errors (`C901`) whenever the complexity level exceeds 5.
|
| 92 |
+
max-complexity = 20
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
[tool.ruff.pylint]
|
| 96 |
+
max-args = 20
|
| 97 |
+
|
| 98 |
+
[tool.ruff.format]
|
| 99 |
+
# Like Black, use double quotes for strings.
|
| 100 |
+
quote-style = "double"
|
| 101 |
+
|
| 102 |
+
# Like Black, indent with spaces, rather than tabs.
|
| 103 |
+
indent-style = "space"
|
| 104 |
+
|
| 105 |
+
# Like Black, respect magic trailing commas.
|
| 106 |
+
skip-magic-trailing-comma = false
|
| 107 |
+
|
| 108 |
+
# Like Black, automatically detect the appropriate line ending.
|
| 109 |
+
line-ending = "auto"
|
requirements.txt
CHANGED
|
@@ -3,3 +3,4 @@ gradio
|
|
| 3 |
ultralytics
|
| 4 |
supervision
|
| 5 |
matplotlib
|
|
|
|
|
|
| 3 |
ultralytics
|
| 4 |
supervision
|
| 5 |
matplotlib
|
| 6 |
+
tqdm
|