T.Masuda commited on
Commit ·
62dd301
1
Parent(s): 1f19da5
segment-anything-demo
Browse files- app.py +41 -0
- color.py +36 -0
- color_wheel.py +146 -0
- meta_segment_anything.py +44 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from meta_segment_anything import SegmentAnything
|
| 3 |
+
from color import Color
|
| 4 |
+
from color_wheel import ColorWheel
|
| 5 |
+
|
| 6 |
+
def process_image(image):
|
| 7 |
+
if image is None:
|
| 8 |
+
yield None
|
| 9 |
+
return
|
| 10 |
+
|
| 11 |
+
color = Color.fromRgb(0xff, 0x0, 0x0)
|
| 12 |
+
cwt = ColorWheel(color)
|
| 13 |
+
colors = []
|
| 14 |
+
for t in cwt.tone15:
|
| 15 |
+
cw = ColorWheel(Color.fromRgb(t.r, t.g, t.b))
|
| 16 |
+
for h in cw.hue:
|
| 17 |
+
colors.append((h.r, h.g, h.b, 0x80))
|
| 18 |
+
|
| 19 |
+
generator = SegmentAnything()
|
| 20 |
+
masks = generator.generate(image)
|
| 21 |
+
index = 0
|
| 22 |
+
for mask in masks:
|
| 23 |
+
if index >= len(colors):
|
| 24 |
+
break
|
| 25 |
+
maskimage = SegmentAnything.makeMaskImage(mask['segmentation'].T, colors[index])
|
| 26 |
+
image.paste(maskimage, (0, 0), maskimage)
|
| 27 |
+
index += 1
|
| 28 |
+
yield image
|
| 29 |
+
|
| 30 |
+
app = gr.Interface(
|
| 31 |
+
title='Segment Anything Demo',
|
| 32 |
+
description='generate masks for an entire image',
|
| 33 |
+
fn=process_image,
|
| 34 |
+
inputs=gr.Image(type='pil'),
|
| 35 |
+
outputs=gr.Image(label='segmentation', type='pil'),
|
| 36 |
+
allow_flagging='never',
|
| 37 |
+
examples=[['examples/example1.jpg'], ['examples/example2.jpg']],
|
| 38 |
+
#cache_examples=False
|
| 39 |
+
)
|
| 40 |
+
app.queue(concurrency_count=5)
|
| 41 |
+
app.launch()
|
color.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Color:
|
| 2 |
+
@property
|
| 3 |
+
def a(self):
|
| 4 |
+
return self._a
|
| 5 |
+
|
| 6 |
+
@property
|
| 7 |
+
def r(self):
|
| 8 |
+
return self._r
|
| 9 |
+
|
| 10 |
+
@property
|
| 11 |
+
def g(self):
|
| 12 |
+
return self._g
|
| 13 |
+
|
| 14 |
+
@property
|
| 15 |
+
def b(self):
|
| 16 |
+
return self._b
|
| 17 |
+
|
| 18 |
+
def __init__(self, a, r, g, b):
|
| 19 |
+
self._a = a
|
| 20 |
+
self._r = r
|
| 21 |
+
self._g = g
|
| 22 |
+
self._b = b
|
| 23 |
+
|
| 24 |
+
@staticmethod
|
| 25 |
+
def fromArgb(a, r, g, b):
|
| 26 |
+
return Color(a, r, g, b)
|
| 27 |
+
|
| 28 |
+
@staticmethod
|
| 29 |
+
def fromRgb(r, g, b):
|
| 30 |
+
return Color(0xff, r, g, b)
|
| 31 |
+
|
| 32 |
+
def __eq__(self, other):
|
| 33 |
+
return self.a == other.a and self.r == other.r and self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
+
from color import Color
|
| 3 |
+
|
| 4 |
+
class ColorWheel:
|
| 5 |
+
@property
|
| 6 |
+
def baseColor(self):
|
| 7 |
+
return self._baseColor
|
| 8 |
+
|
| 9 |
+
@property
|
| 10 |
+
def hue(self):
|
| 11 |
+
return [
|
| 12 |
+
self.baseColor, self.addH(1.0 / 12), self.addH(2.0 / 12), self.addH(3.0 / 12), self.addH(4.0 / 12), self.addH(5.0 / 12),
|
| 13 |
+
self.addH(-6.0 / 12), self.addH(-5.0 / 12), self.addH(-4.0 / 12), self.addH(-3.0 / 12), self.addH(-2.0 / 12), self.addH(-1.0 / 12)
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
@property
|
| 17 |
+
def tone(self):
|
| 18 |
+
return [self.addWhite(-2.0 / 16), self.addWhite(-1.0 / 16), self.baseColor, self.addWhite(1.0 / 16), self.addWhite(2.0 / 16)]
|
| 19 |
+
|
| 20 |
+
@property
|
| 21 |
+
def tone15(self):
|
| 22 |
+
return [self.addWhite(-7.0 / 16), self.addWhite(-6.0 / 16), self.addWhite(-5.0 / 16), self.addWhite(-4.0 / 16), self.addWhite(-3.0 / 16),
|
| 23 |
+
self.addWhite(-2.0 / 16), self.addWhite(-1.0 / 16), self.baseColor, self.addWhite(1.0 / 16), self.addWhite(2.0 / 16),
|
| 24 |
+
self.addWhite(3.0 / 16), self.addWhite(4.0 / 16), self.addWhite(5.0 / 16), self.addWhite(6.0 / 16), self.addWhite(7.0 / 16)]
|
| 25 |
+
|
| 26 |
+
@property
|
| 27 |
+
def complementaryColors(self):
|
| 28 |
+
return [self.baseColor, self.addH(0.5)]
|
| 29 |
+
|
| 30 |
+
@property
|
| 31 |
+
def triadicColors(self):
|
| 32 |
+
return [self.addH(-4.0 / 12), self.baseColor, self.addH(4.0 / 12)]
|
| 33 |
+
|
| 34 |
+
@property
|
| 35 |
+
def splitComplementaryColors(self):
|
| 36 |
+
return [self.addH(-5.0 / 12), self.baseColor, self.addH(5.0 / 12)]
|
| 37 |
+
|
| 38 |
+
@property
|
| 39 |
+
def analogousColors(self):
|
| 40 |
+
return [self.addH(-2.0 / 12), self.addH(-1.0 / 12), self.baseColor, self.addH(1.0 / 12), self.addH(2.0 / 12)]
|
| 41 |
+
|
| 42 |
+
def __init__(self, c):
|
| 43 |
+
self._baseColor = c
|
| 44 |
+
self._r = c.r / 255.0
|
| 45 |
+
self._g = c.g / 255.0
|
| 46 |
+
self._b = c.b / 255.0
|
| 47 |
+
|
| 48 |
+
@staticmethod
|
| 49 |
+
def fromHsv(h, s, v):
|
| 50 |
+
r, g, b = ColorWheel.hsvToRgb(h, s, v)
|
| 51 |
+
c = Color.fromRgb(round(r * 255), round(g * 255), round(b * 255))
|
| 52 |
+
return ColorWheel(c)
|
| 53 |
+
|
| 54 |
+
def addWhite(self, value):
|
| 55 |
+
r, g, b = (self._r + value, self._g + value, self._b + value)
|
| 56 |
+
r = min(max(r, 0.0), 1.0)
|
| 57 |
+
g = min(max(g, 0.0), 1.0)
|
| 58 |
+
b = min(max(b, 0.0), 1.0)
|
| 59 |
+
return self._fromRgb(r, g, b)
|
| 60 |
+
|
| 61 |
+
def addH(self, value):
|
| 62 |
+
h, s, v = ColorWheel.rgbToHsv(self._r, self._g, self._b)
|
| 63 |
+
h = (h + value) % 1.0
|
| 64 |
+
if h < 0.0:
|
| 65 |
+
h += 1.0
|
| 66 |
+
r, g, b = ColorWheel.hsvToRgb(h, s, v)
|
| 67 |
+
return self._fromRgb(r, g, b)
|
| 68 |
+
|
| 69 |
+
def addS(self, value):
|
| 70 |
+
h, s, v = ColorWheel.rgbToHsv(self._r, self._g, self._b)
|
| 71 |
+
s += value
|
| 72 |
+
s = min(max(s, 0.0), 1.0)
|
| 73 |
+
r, g, b = ColorWheel.hsvToRgb(h, s, v)
|
| 74 |
+
return self._fromRgb(r, g, b)
|
| 75 |
+
|
| 76 |
+
def addV(self, value):
|
| 77 |
+
h, s, v = ColorWheel.rgbToHsv(self._r, self._g, self._b)
|
| 78 |
+
v += value
|
| 79 |
+
v = min(max(v, 0.0), 1.0)
|
| 80 |
+
r, g, b = ColorWheel.hsvToRgb(h, s, v)
|
| 81 |
+
return self._fromRgb(r, g, b)
|
| 82 |
+
|
| 83 |
+
@staticmethod
|
| 84 |
+
def rgbToHsv(r, g, b):
|
| 85 |
+
if r < 0.0 or r > 1.0:
|
| 86 |
+
raise ValueError()
|
| 87 |
+
if g < 0.0 or g > 1.0:
|
| 88 |
+
raise ValueError()
|
| 89 |
+
if b < 0.0 or b > 1.0:
|
| 90 |
+
raise ValueError()
|
| 91 |
+
cmax = max(r, g, b)
|
| 92 |
+
cmin = min(r, g, b)
|
| 93 |
+
h = cmax - cmin
|
| 94 |
+
if h > 0.0:
|
| 95 |
+
if cmax == r:
|
| 96 |
+
h = (g - b) / h
|
| 97 |
+
if h < 0.0:
|
| 98 |
+
h += 6.0
|
| 99 |
+
elif cmax == g:
|
| 100 |
+
h = 2.0 + (b - r) / h
|
| 101 |
+
else:
|
| 102 |
+
h = 4.0 + (r - g) / h
|
| 103 |
+
h /= 6.0
|
| 104 |
+
s = cmax - cmin
|
| 105 |
+
if cmax != 0.0:
|
| 106 |
+
s /= cmax
|
| 107 |
+
v = cmax
|
| 108 |
+
return h, s, v
|
| 109 |
+
|
| 110 |
+
@staticmethod
|
| 111 |
+
def hsvToRgb(h, s, v):
|
| 112 |
+
if h < 0.0 or h > 1.0:
|
| 113 |
+
raise ValueError()
|
| 114 |
+
if s < 0.0 or s > 1.0:
|
| 115 |
+
raise ValueError()
|
| 116 |
+
if v < 0.0 or v > 1.0:
|
| 117 |
+
raise ValueError()
|
| 118 |
+
r = v
|
| 119 |
+
g = v
|
| 120 |
+
b = v
|
| 121 |
+
if s > 0.0:
|
| 122 |
+
h *= 6.0
|
| 123 |
+
i = math.floor(h)
|
| 124 |
+
f = h - i
|
| 125 |
+
if i == 1:
|
| 126 |
+
r *= 1 - s * f
|
| 127 |
+
b *= 1 - s
|
| 128 |
+
elif i == 2:
|
| 129 |
+
r *= 1 - s
|
| 130 |
+
b *= 1 - s * (1 - f)
|
| 131 |
+
elif i == 3:
|
| 132 |
+
r *= 1 - s
|
| 133 |
+
g *= 1 - s * f
|
| 134 |
+
elif i == 4:
|
| 135 |
+
r *= 1 - s * (1 - f)
|
| 136 |
+
g *= 1 - s
|
| 137 |
+
elif i == 5:
|
| 138 |
+
g *= 1 - s
|
| 139 |
+
b *= 1 - s * f
|
| 140 |
+
else:
|
| 141 |
+
g *= 1 - s * (1 - f)
|
| 142 |
+
b *= 1 - s
|
| 143 |
+
return r, g, b
|
| 144 |
+
|
| 145 |
+
def _fromRgb(self, r, g, b):
|
| 146 |
+
return Color.fromArgb(self.baseColor.a, round(r * 255), round(g * 255), round(b * 255))
|
meta_segment_anything.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from segment_anything import sam_model_registry, SamPredictor, SamAutomaticMaskGenerator
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
class SegmentAnything:
|
| 7 |
+
def __init__(self):
|
| 8 |
+
sam_checkpoint = 'checkpoint/sam_vit_h_4b8939.pth'
|
| 9 |
+
model_type = 'vit_h'
|
| 10 |
+
sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
|
| 11 |
+
if torch.cuda.is_available():
|
| 12 |
+
sam.to(device='cuda')
|
| 13 |
+
self.sam = sam
|
| 14 |
+
|
| 15 |
+
def predict(self, image, point_coords, point_labels, box=None):
|
| 16 |
+
predictor = SamPredictor(self.sam)
|
| 17 |
+
predictor.set_image(np.array(image, dtype=np.uint8))
|
| 18 |
+
return predictor.predict(point_coords=point_coords, point_labels=point_labels, box=box)
|
| 19 |
+
|
| 20 |
+
def generate(self, image):
|
| 21 |
+
mask_generator = SamAutomaticMaskGenerator(self.sam)
|
| 22 |
+
return mask_generator.generate(np.array(image, dtype=np.uint8))
|
| 23 |
+
|
| 24 |
+
@staticmethod
|
| 25 |
+
def makeMaskImage(mask, color):
|
| 26 |
+
image = Image.new('RGBA', mask.shape)
|
| 27 |
+
width, height = image.size
|
| 28 |
+
for x in range(width):
|
| 29 |
+
for y in range(height):
|
| 30 |
+
if mask[x, y]:
|
| 31 |
+
image.putpixel((x, y), color)
|
| 32 |
+
return image
|
| 33 |
+
|
| 34 |
+
@staticmethod
|
| 35 |
+
def makeNewImage(image, maskImage):
|
| 36 |
+
newImage = Image.new('RGBA', image.size)
|
| 37 |
+
timage = maskImage.copy()
|
| 38 |
+
width, height = timage.size
|
| 39 |
+
for x in range(width):
|
| 40 |
+
for y in range(height):
|
| 41 |
+
_, _, _, a = timage.getpixel((x, y))
|
| 42 |
+
timage.putpixel((x, y), (0, 0, 0, 255) if a > 0 else (0, 0, 0, 0))
|
| 43 |
+
newImage.paste(image, (0, 0), timage)
|
| 44 |
+
return newImage
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
torch
|
| 3 |
+
torchvision
|
| 4 |
+
torchaudio
|
| 5 |
+
git+https://github.com/facebookresearch/segment-anything.git
|